Tic Tac Toe Program - Odd Initialization Behavior

closed account (ivbjNwbp)
Hello, all! This is my first post here, so please excuse me if I've made any flubs. I've written a Tic Tac Toe program for a class assignment--it's been submitted already with known issues, so answering this question won't impact my score on the assignment. I'm just really at a loss for how to solve this problem, and I'd like to learn more. I apologize in advance if this is text-heavy--I did my best to pare it down, but please let me know if there is any additional information that you need about the code.

My issue is this: the program should be constructing an empty 3x3 array and accepting user-input parameters for coordinates to place either an 'X' or 'O'. Most of the functions work well, but for some reason the board is initializing with characters in the [0][0] and [0][1] positions with whichever character the first player is. For example, if the first player is 'O', the array looks like this at game start:

1
2
3
O O .
. . .
. . .


This is occurring in the main function sometime between prompting the user to enter which player is playing first and just BEFORE the user is prompted to make their first move, meaning the makeMove function hasn't executed yet at this point (if I understand correctly). Below are details about the program as is:

I have two .hpp files, one for the Board class and one for the TicTacToe class. The Board class constructs an empty 3x3 array and contains three additional functions:

- makeMove: for making a move (placing an 'O' or 'X' into the array cells)
- gameState: for checking and returning the state of the game (enumerated variable with values X_WON, O_WON, DRAW, UNFINISHED)
- print: for displaying the current state of the board

The TicTacToe class contains a constructor function to determine who takes the first turn and a function to begin and play the game (play), which calls the makeBoard, print, and gameState functions. TicTacToe.cpp also contains the main function.

Here is the main function:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
   char playerOne;
   std::cout << "Which player will go first? Please enter X or O." << std::endl;
   std::cin >> playerOne;

   TicTacToe TTTMain(playerOne);
   TTTMain.play();

   return 0;
}


Here is the default Board constructor:

1
2
3
4
Board::Board()
{
   char gameBoard[3][3] = {};
}


Here is the TicTacToe constructor:

1
2
3
4
TicTacToe::TicTacToe(char firstMove)
{
   currentPlayer = firstMove;
}


Finally, here are the class files for Board and TicTacToe, respectively:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BOARD_HPP
#define BOARD_HPP

enum GameState {X_WON, O_WON, DRAW, UNFINISHED}

class Board {
private:
   char gameBoard[][3];
public:
   Board();
   bool makeMove(int, int, char);
   GameState gameState();
   void print();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef TICTACTOE_HPP
#define TICTACTOE_HPP

#include "Board.hpp"

class TicTacToe {
private:
   board TTTBoard;
   char currentPlayer;
public:
   TicTacToe(char);
   void play();
};

#endif 


Thank you very much for your help!
Last edited on
board.hpp line 8
 
    char gameBoard[][3];
is not valid, the first dimension is missing = it should be [3][3].

The board constructor does nothing:
1
2
3
4
Board::Board()
{
   char gameBoard[3][3] = {};
}

well, it defines a local array named gameBoard and initialises it. But when the function ends, that local variable goes out of scope and no longer exists.
Instead you might try this:
1
2
3
4
5
6
Board::Board()
{
    for (int i=0; i<3; ++i)
        for (int j=0; j<3; ++j)
            gameBoard[i][j] = ' ';
}


above - assign whichever character you wish, rather than ' '.
Last edited on
closed account (ivbjNwbp)
Thank you SO much! It works perfectly now, and this has been bugging me for days.
Topic archived. No new replies allowed.