Hello, I'm trying to teach myself C++ based on a little bit of Java experience, and because I'm bored.]
I made a function called move, which takes in two arguments: the board, which is a 2D array of numbers, and the player, which is also a number.
The way I made the game is as such:
The array, gameBoard, is a SIDE x SIDE array as defined - #define SIDE 3
gameBoard is filled with values of -1, 0 or 1. -1 is the default value. 0 and 1 are "players", aka player 1 marking the spot will put a 'O' on that spot in the array, and player 2 will mark an 'X' on the gameBoard.
The int player is either 0 or 1... therefore if player 2 marks the spot gameBoard[0,1], then gameBoard[0,1] should equal 1.
I use another function displayBoard(int gameBoard[][]) which works fine.
My 2 questions are as such:
1) Should I reference the array (gameBoard[][]) with the move() function, or should I have it return a new array? Don't worry about memory or hogging resources or efficiency or whatnot -- that'll fly right over my head.
2) I keep getting errors. Right now, the error I get is "error: expected unqualified-id before '[' token", and the error points to the line of the function move (in my code, line 2). What's going on?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// Places a player's move onto the board
int[][] move(int gameBoard[SIDE][SIDE], int player)
{
int spot; // Spot that they choose
char mark;
if(player == 0)
mark = 'O';
else
mark = 'X';
bool validMove = true;
do
{
// Ask where to move
cout << "Player " << player + 1 << ": Where would you like to place an ";
cout << mark << "?" << endl;
// Check the spot
cin >> spot;
if (spot < 1 || spot > 9)
{
cout << "Invalid move!\n";
validMove = false;
}
}
while(validMove);
// Place the mark
int m, n; // m is the row number, n is the column number
m = spot / 3;
n = spot % 3;
gameBoard[m][n] == player;
displayBoard(gameBoard[SIDE][SIDE]);
return gameBoard[SIDE][SIDE];
}
|
Thanks!