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;
}
};