My problem is when my board displays it shows 2's. My enum Player is not working or I just don't understand enum. It is displaying 0,1,2 instead of X, O, and empty. It is also not changing players, again tied to the enum problem I'm sure. Any help would be great!

#define TTT_H
 
class Board
{
private:
int board[3][3];
int moves;

public:
//Constructor
Board();
void Place(enum Player, int, int);
const bool Win();
const bool Stalemate();
const void displayBoard();
int player;
};
#include "TTT.h"
#include <iostream>
using std::endl;
using std::cout;
 
enum Player {X, O, empty}; 
Player player;
//Blank tic-tac-toe board
Board::Board()
{
player = empty;
moves = 0;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
board[i][j] = player;
}
}
};
void Board::Place(enum Player, int col, int row)
{
board[col][row] = player;

};
const bool Board::Stalemate()
{
if(Board::Win()== false)
return true;
else
return false;
};
const bool Board::Win()
{
if(moves > 5)
{
if(board[0][0]!= empty)
{
if (board[0][0]== X && board[0][1]== X && board[0][2]== X)
cout << "Player X wins." << endl;
return true; 
if (board[0][0]== O && board[0][1]== O && board[0][2]== O)
cout << "Player O wins." << endl;
return true; 
if (board[0][0]== X && board[1][0]== X && board[2][0]== X)
cout << "Player X wins." << endl;
return true;
if (board[0][0]== O && board[1][0]== O && board[2][0]== O)
cout << "Player O wins." << endl;
return true; 
if (board[0][0]== X && board[1][1]== X && board[2][2]== X)
cout << "Player X wins." << endl;
return true;
if (board[0][0]== O && board[1][1]== O && board[2][2]== O)
cout << "Player O wins." << endl;
return true; 
}
if(board[2][2]!= empty)
{
if (board[2][0]== X && board[2][1]== X && board[2][2]== X)
cout << "Player X wins." << endl;
return true;
if (board[2][0]== O && board[2][1]== O && board[2][2]== O)
cout << "Player O wins." << endl;
return true; 
if (board[0][2]== X && board[1][2]== X && board[2][2]== X)
cout << "Player X wins." << endl;
return true;
if (board[0][2]== O && board[1][2]== O && board[2][2]== O)
cout << "Player O wins." << endl;
return true; 
}
if(board[2][0]!= empty)
{
if (board[2][0]== X && board[1][1] == X && board[0][2]== X)
cout << "Player X wins." << endl;
return true;
if (board[2][0]== O && board[1][1] == O && board[0][2]== O)
cout << "Player O wins." << endl;
return true; 
}
if(board[0][1]!= empty)
{
if (board[0][1]== X && board[1][1]== X && board[2][1]== X)
cout << "Player X wins." << endl;
return true;
if (board[0][1]== O && board[1][1]== O && board[2][1]== O)
cout << "Player O wins." << endl;
return true; 
}
if(board[1][0]!= empty)
{
if (board[1][0]== X && board[1][1]== X && board[1][2]== X)
cout << "Player X wins." << endl;
return true;
if (board[1][0]== O && board[1][1]== O && board[1][2]== O)
cout << "Player O wins." << endl;
return true; 
}
return false;
}
return false;
};
const void Board::displayBoard()
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}; 
#include "ttt.h" //header file
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
enum Player {X, O, empty}; 
 
void main()
{
Board myBoard;
int col,row;
Player player;
player = X;

switch(player)
{
case X:
cout << "X";
break;
case O:
cout << "O";
break;
case empty:
cout << "empty";
break;
};

while(!myBoard.Win())
{
myBoard.displayBoard();
cout << endl << "Enter your column number:";
cin >> col;
cout << endl << "Enter your row number:";
cin >> row;
myBoard.Place(player,col,row);
//switch players
if (player)
player = X;
else
player = O;
}
};

Enums are internally integers, they are normally used for the convinience of the programmer as well as for clean design. They help in avoiding magic numbers in your program ( integer literals in the program which are not self explanatory ).

So if you do:

enum { ZERO, ONE, TWO } ;
// here ZERO stands for 0, ONE stands for 1 and so on

So what you need to do is to keep a character array of 3 X 3 instead of integer array and do what you are doing now.

Maybe that should do away with the 0 and 1's.

Hope it helped, bye.

I already have: enum{x, o, empty}; and it prints 0, 1, 2.

*sigh*
That I know.

The point I was trying to prove is "don't use integer array and replace it by a character array".

Then do something like:

const char EX = 'X' ;
const char ZERO = 'O' ; // letter O
const char BLANK = ' ' ; // a single whitespace
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.