-1

I'm trying to create a classic tic tac toe game using classes. I established my array thru a default constructor, when I try to display the game board from another public function in the class I get random symbols like these '&♥'. (I know I'm missing functions and all but I can't figure this step out!) Here is my program output: OUTPUT and program:

class TicTacToe
{
 private:
  char board[3][3];
  char player;
  
 public:
  void getBoard();
  char changePlayer(char);
  void setPosition(int, int);
  TicTacToe();
};

int main ()
{
 bool endOfGame = false;
 int rows;
 int columns;
 int test;
 TicTacToe ttt;

 while (!endOfGame)
 {
  cout << "Please enter the row number: ";
  cin >> rows;
  cout << "Please enter the column number: ";
  cin >> columns;
  
  ttt.setPosition(rows, columns);
  ttt.getBoard();
 }



 return 0;
}


TicTacToe::TicTacToe()
{
 char board[3][3] = {'*','*','*',
      '*','*','*',
      '*','*','*',};
 player = 'x';
}

void TicTacToe::getBoard()
{        
 for (int i = 0; i < 3; i++)
 {
  for (int c = 0; c < 3; c++)
  {
   cout << board[i][c];
  }
 cout << endl;
 }
}

char TicTacToe::changePlayer(char choice)
{
}

void TicTacToe::setPosition(int row, int column)
{
 if (player == 'x')
 { board[row][column] = 'x'; }
 
 else
 { board[row][column] = 'o'; }

}
  • Unless I'm missing something, you're never initializing the array, so it will be pointing to garbage data that you're telling to interpret as a char. – Carcigenicate Mar 26 '16 at 19:46
  • in main when I declare ttt as tictactoe, wouldnt that declare a game board, making the default constructor make it the 3x3 array? – Nathan Schaefer Mar 26 '16 at 19:55

2 Answers2

0

You never initialize your class member array. The array in your constructor is a local object. A simple solution would be using in-class initializers and getting rid of the constructor:

class TicTacToe
{
    private:
        char board[3][3] {'*','*','*','*','*','*','*','*','*'};
        char player {'x'};

    public:
        void getBoard();
        char changePlayer(char);
        void setPosition(int, int);
};

Edit, when you need a constructor use the initializer list (since c++11):

TicTacToe::TicTacToe()
    : board {'*', '*', '*', '*', '*', '*', '*', '*', '*'}
{}
robsn
  • 734
  • 5
  • 18
  • only issue is my instructor wants an constructor.. If I don't have one she'll heavily dock my points.. – Nathan Schaefer Mar 26 '16 at 20:11
  • unfortunately we're not using c++11 :/ dev-c++ 5.11 – Nathan Schaefer Mar 26 '16 at 20:32
  • Look it up. This is a common problem and can easily be found on the web. Initially I was pointing out an error that could have been difficult to spot but now I feel like solving your homework. – robsn Mar 26 '16 at 20:37
  • That's not true.. and I only came here after hours of googling and trying to solve. Your edits are insignificant to my program as our class uses dev-c++. (your code only works with with c++11) – Nathan Schaefer Mar 26 '16 at 20:51
  • @NathanSchaefer Use this as a starting point: http://stackoverflow.com/questions/2409819/c-constructor-initializer-for-arrays/2409882#2409882 Or this: http://stackoverflow.com/a/22975356/279610 – robsn Mar 26 '16 at 20:58
  • It's okay. Thank you for trying to help, solved it now and posting solution in case anyone has same issue with this problem. – Nathan Schaefer Mar 26 '16 at 21:08
0

Solution:

TicTacToe::TicTacToe()
{
 for (int i = 0; i < 3; i++)
 {
  for (int c = 0; c < 3; c++)
  {
   board[i][c] = '*';
  }
 cout << endl;
 }
 
 player = 'x';
}

void TicTacToe::getBoard()
{ 
 cout << "\n" << board[0][0] << " | " << board[0][1] << " | " << board[0][2];
 cout << "\n" << "---------";
 cout << "\n" << board[1][0] << " | " << board[1][1] << " | " << board[1][2];
 cout << "\n" << "---------";
 cout << "\n" << board[2][0] << " | " << board[2][1] << " | " << board[2][2];
 cout << endl << endl;

}