pretty much where i have many of my comments are where i need to call the functions from the player and cell class in the tictactoe class? anyone have any ideas on how to do this?
#include <iostream>
#include <cstdlib>
using namespace std;
class TicTacToe
{
public:
TicTacToe()//contructor initializes the board
{
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
void playTicTacToe()//function to play the game
{
srand(time(0));
int playerTurn = 1 + rand() % 2;//randomly choose who goes first and then pass it to the player class? yes/ no???
bool gameEnded = true;
TicTacToe myTicTacToe;
// do...while continue the game play until there is a winner or draw
do{
myTicTacToe.Player(displayTurn());// here i want to call the display turn from the player class
myTicTacToe.displayBoard();// call displaybooard from ttt class
// call setinfo() here from cell class
//cal trackcell() here from cell class
myTicTacToe.displayBoard( playerLetter );// displayBoard from ttt class
gameEnded = myTicTacToe.checkBoard();// Checks game status
if (gameEnded)
{
myTicTacToe.displayBoard();// problem need to change something in the displayboard function
} else
{
//here i want to call the display turn from the player class
}
}while (!gameEnded);
}
bool checkBoard()//function checks the board for a winner or tells if it is a draw
{
bool gameEnded = false;
//Check for winner
if ( board[0][0] != '1')//Checks top row and first column
{
if ( board[0][0] == board[0][1] && board[0][0] == board[0][2] )
{
gameEnded = true;
cout << "The winner is Player" << board[0][0] << "!" << endl;
}
if ( board[0][0] == board[1][0] && board[0][0] == board[2][0] )
{
gameEnded = true;
cout << "The winner is Player" << board[0][0] << "!" << endl;
}
}
if ( board[1][1] != '5')//Checks diagonally, middle row, and middle column
{
if ( board[1][1] == board[0][0] && board[1][1] == board[2][2] )
{
gameEnded = true;
cout << "The winner is Player" << board[1][1] << "!" << endl;
}
if ( board[1][1] == board[0][1] && board[1][1] == board[2][1] )
{
gameEnded = true;
cout << "The winner is Player" << board[1][1] << "!" << endl;
}
if ( board[1][1] == board[1][0] && board[1][1] == board[1][2] )
{
gameEnded = true;
cout << "The winner is Player" << board[1][1] << "!" << endl;
}
if ( board[1][1] == board[0][2] && board[1][1] == board[2][0] )
{
gameEnded = true;
cout << "The winner is Player" << board[1][1] << "!" << endl;
}
}
if ( board[2][2] != '9')//Checks bottom row and last column
{
if ( board[2][2] == board[0][2] && board[2][2] == board[1][2] )
{
gameEnded = true;
cout << "The winner is Player" << board[2][2] << "!" << endl;
}
if ( board[2][2] == board[2][0] && board[2][2] == board[2][1] )
{
gameEnded = true;
cout << "The winner is Player" << board[2][2] << "!" << endl;
}
}
//Checks to see if there is a draw
if ( board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' &&
board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' &&
board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9' &&
!gameEnded )
{
gameEnded = true;
cout << "It is a draw!" << endl;
}
return gameEnded;
}
void displayBoard(char pLetter) // fucntion displays the board**********feel like i need to change something here?
{
cout << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << endl;
cout << "-+-+-" << endl;
cout << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << endl;
cout << "-+-+-" << endl;
cout << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl;
}
private:
Cell board[3][3];//NEW board as a data member and type Cell
Player players[2];// NEW players declared as type Player
};
class Cell
{
public:
void setCellInfo()
{
cout << "Please choose where you would like to enter your letter:" << endl;
cin >> numOfCell;
}
int getCellInfo()
{
return numOfCell;
}
void trackCell(char pName)
{
numOfCell = getCellInfo();
if ( numOfCell == '1')
numOfCell = pName;
else if ( numOfCell == '2')
numOfCell = pName;
else if ( numOfCell == '3')
numOfCell = pName;
else if ( numOfCell == '4')
numOfCell = pName;
else if ( numOfCell == '5')
numOfCell = pName;
else if ( numOfCell == '6')
numOfCell = pName;
else if ( numOfCell == '7')
numOfCell = pName;
else if ( numOfCell == '8')
numOfCell = pName;
else if ( numOfCell == '9')
numOfCell = pName;
else
cout << "Invalid Move. Try Again.";
}
private:
int numOfCell;
};
class Player
{
public:
void setLetter()// change which letter will be displayed
{
if (playerTurn == 1)
{
pName = 'X';
playerTurn++;
} else
{
pName = 'O';
playerTurn--;
}
}
char getLetter()
{
return pName;
}
void displayTurn()
{
setLetter();
getLetter();
cout << "It is player " << pName << "'s turn. ";
}
private:
int playerTurn;
char pName;
};
int main()//main function
{
char gameboard;
TicTacToe myTicTacToe;
bool play = true;
char d;// players decision whether to continue or not
// Continue to play the game until the user does not enter y
while ( play )
{
cout << " Would you like to play Tic Tac Toe?(y/n) ";
cin >> d;
if ( d == 'Y' || d == 'y' )
{
cout << "Let the games begin!" << endl;
cout << "Player X will go first and Player O will go second." << endl;
myTicTacToe.playTicTacToe();//function that begins game
} else
{
cout << endl << "See you next time!" << endl;
play = false;
}
}
return 0;
}// end main