Trying to make Tic Tac Toe

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!
Should I reference the array (gameBoard[][]) with the move() function, or should I have it return a new array?
By reference.

It's easier: void move(int (&gameBoard)[SIDE][SIDE], int player)

Edit
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?
C/C++ does not allow to return a 2D array like that
Last edited on
closed account (3pj6b7Xj)
Since Tic-Tac-Toe has 3 rows, I would think in terms of a vector using strings to punch in X and O

vector<string> tictactoe(3);


tictactoe[0] = "#O#"
tictactoe[1] = "X#O"
tictactoe[2] = "#OX"

The # represents empty space or availale space, the above game board would
represent

| O |
--------------------
X | | O
--------------------
| O | X

There are other ways to do it of course using 3 dimensional arrays. I would use a class to represent the gameboard, as a matter of fact, a class that represents the gameboard could simulate the entire game, because you could pick a location to put an X or a O, it would check to see if an X or O is there and if not put which ever X or O at that location, it could also check to see if anyone has one, all from a single class...nice.

Topic archived. No new replies allowed.