- #1
FallArk
- 127
- 0
What I should follow:
Template:
what I have:
As of now, I have a few questions:
1. What is this move variable? (I thought it was the moves left for the player to win but then realized that is wrong)
2. How do I use this TAILLENGTH constant? I understand the snake should be limited to this constant but how should I make it so the snake stop growing after reaching this number?
3. what is the _cell board[NUMROWS][NUMCOLS], it doesn't look like how a struct should be used.
Thanks for any input in advance!
• "U" is you, the player; you always start in the upper left corner.
• "X" is the opponent, which always starts in the lower right corner.
• NUMROWS and NUMCOLS delimits the size and shape of the game board.
• TAILLENGTH gives the length of the "tail" -- if one player hits the other's tail (or the head), the player loses.
• DO NOT Try to make a version which interacts with the arrow keys; see class discussion regarding why.
• DO NOT USE GLOBAL VARIABLES!
• You HAVE TO use the template given below; your job is to write the missing functions.
Template:
Code:
const int NUMROWS = 7;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
const int NUMCOLS = 10;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
const int TAILLENGTH = 15;// CAN BE CHANGED FOR DIFFERENT GAME PLAY
struct _cell {
char marker; int moveNumber;
};
*** OTHER FUNCTIONS THAT YOU WRITE ***
int main() {
int uRow = 0, uCol = 0;
int oRow = NUMROWS - 1, oCol = NUMCOLS - 1;
bool win = true; int move = 0;
_cell board[NUMROWS][NUMCOLS]; initBoard(board);
placePiece(board, uRow, uCol, 'U', move); placePiece(board, oRow, oCol, 'X', move); showBoard(board, move);
while (true) {
move++;
if (!movePlayer(board, uRow, uCol, move)) { win = false;
break;
}
if (!moveOpponent(board, oRow, oCol, move)) break; // player wins
showBoard(board, move);
}
showBoard(board, move); if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
string tmp; getline(cin, tmp);
}
what I have:
Code:
[FONT=Century Gothic]void initBoard (char board[NUMROWS][NUMCOLS]) {
for (int i = 0; i < NUMROWS; ++i) {
for (int j = 0; j < NUMCOLS; ++j) {
board[i][j] = '-';
}
}
}
void showBoard (char board[NUMROWS][NUMCOLS], int &move) {
for (int i = 0; i < NUMROWS; ++i) {
for (int j = 0; j < NUMCOLS; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
cout << "Enter direction (N/S/E/W): ";
}
void placePiece (char board[NUMROWS][NUMCOLS], int &row, int &col, char player, int &move) {
if (player == 'U') {
row = 0, col = 0;
board[row][col] = 'U';
}
else {
row = NUMROWS - 1, col = NUMCOLS - 1;
board[row][col] = 'X';
}
}[/FONT]
1. What is this move variable? (I thought it was the moves left for the player to win but then realized that is wrong)
2. How do I use this TAILLENGTH constant? I understand the snake should be limited to this constant but how should I make it so the snake stop growing after reaching this number?
3. what is the _cell board[NUMROWS][NUMCOLS], it doesn't look like how a struct should be used.
Thanks for any input in advance!