Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Tic-Tac-Toe) Write a program that allows two players to play the tic-tac-toe ga

ID: 3546964 • Letter: #

Question

(Tic-Tac-Toe)  Write a program that allows two players to play the tic-tac-toe game.  Your program must contain the class ticTacToe to implement a ticTacToe object.  Include a 3-by-3 dimensional array, as a private member variable, to create the board.  If needed, include additional member variables.  Some of the opertions on a ticTacToe object are printing the current board, getting a move, checking if a move is valid, and determining the winner after each move.  Add additional operations as needed.

Explanation / Answer

#include <iostream>


using namespace std;


class TicTacToe

{

private:

char arr[3][3];

bool playerTurn;

void init()

{

for(int i = 0;i<3;i++)

{

for(int j = 0;j<3;j++)

{

arr[i][j]='*';

}

}

}

bool getPlayerMove(int x,int y,bool turn)

{

if(arr[x][y]!='*')

{

cout << "the move was not valid please try again" << endl;

return false;

}

if(turn)

{

arr[x][y]='x';

}

else

{

arr[x][y]='o';

}


return true;


}

bool isThereADraw()

{

for(int i = 0;i<3;i++)

{

for(int j = 0;j<3;j++)

{

if(arr[i][j]=='*')

{

return false;

}

}

}

return true;

}

bool isDiagonalWin(char a)

{


if(arr[0][0]==a&&arr[1][1]==a&&arr[2][2]==a)

{

return true;

}

if(arr[0][2]==a&&arr[1][1]==a&&arr[2][0]==a)

{

return true;

}

return false;

}

bool isColumnWin(int i,char a)

{

if(arr[0][i]==a&&arr[1][i]==a&&arr[2][i]==a)

{

return true;

}

return false;

}

bool isLineWin(int i,char a)

{

if(arr[i][0]==a&&arr[i][1]==a&&arr[i][2]==a)

{

return true;

}

return false;

}

bool isThereAWinner(char a)

{


for(int i =0;i<3;i++)

{

if(isLineWin(i,a))

{

return true;

}

if(isColumnWin(i,a))

{

return true;

}

}

if(isDiagonalWin(a))

{

return true;

}

return false;


}

public:

TicTacToe()

{

init();

playerTurn = true;



}

void printBoard()

{

cout << endl;

cout << " "<< arr[0][0] << " | " << arr[0][1] << " | " << arr[0][2] << endl;

cout << "-------------" << endl;

cout << " "<< arr[1][0] << " | " << arr[1][1] << " | " << arr[1][2] << endl;

cout << "-------------" << endl;

cout << " "<< arr[2][0] << " | " << arr[2][1] << " | " << arr[2][2] << endl;

cout << endl;

}

bool promptUserStart()

{

init();

char reps;

cout << "do you want to play?y/n: ";

cin >>reps;

if(reps=='y'||reps=='Y')

{


playerTurn=true;

return true;

}

else

{

cout << "thank you for playing" << endl;

return false;

}

return false;

}

void startGame()

{

int x,y;


if(playerTurn)

{

printBoard();

cout << "player one" << endl;

cout << "what row will you choose? 1 to 3" << endl;

cin >> x;

cout << "what column will you choose? 1 to 3" << endl;

cin >> y;

if(getPlayerMove(x-1,y-1, playerTurn))

{

playerTurn = false;

if(isThereAWinner('x'))

{

cout << "congrats!!Player 1 is the Winner!!" << endl;

printBoard();

return;

}

else if(isThereADraw())

{

cout << "the game is a draw" << endl;

printBoard();

return;

}

startGame();

}

else

{

playerTurn = true;

startGame();

}


}

else

{


printBoard();

cout << "player two" << endl;

cout << "what row will you choose? 1 to 3" << endl;

cin >> x;

cout << "what column will you choose? 1 to 3" << endl;

cin >> y;

if(getPlayerMove(x-1,y-1, playerTurn))

{

playerTurn = true;

if(isThereAWinner('o'))

{

cout << "congrats!!Player 2 is the Winner!!" << endl;

printBoard();

return;

}

else if(isThereADraw())

{

cout << "the game is a draw" << endl;

printBoard();

return;

}

startGame();

}

else

{

playerTurn = false;

startGame();

}


}

}



};


int main()

{

TicTacToe game;


while(game.promptUserStart())

{

game.startGame();

}

return 0;

}