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

Write a program that allows two players to play a game of tic-tac-toe. Use a two

ID: 3594789 • Letter: W

Question

Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that: Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row number and then the column number. Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row number and then the column number. Validates the user's input: Do not let the user input a row outside the range [1,3] Do not let the user input a column outside the range [1,3] Do not let the user input into a cell that already has an 'X' or 'O' If the user enters an invalid input, just ask them for that input again. This is best done using nested do-while loops. Below is some pseudo-code that will help with this:do { do { output "Row: " input row; }while(row is outside range); do { output "Column: "; input col; }while(col is outside range); }while(cell at row, col contains 'X' or 'O'); Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should declare that and end. These are the following end game outputs: Player 1 wins! Player 2 wins! Tie! Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. The same logic applies to player 2, but with Os. A tie occurs when all of the locations on the board are full, but there is no winner. Below is a sample output where player 1 wins: 1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 1 Player 2, Column: 2 1 2 3 1 X O * 2 * * * 3 * * * Player 1, Row: 2 Player 1, Column: 2 1 2 3 1 X O * 2 * X * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X O * 2 * X * 3 O * * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X O * 2 * X * 3 O * X Player 1 wins! Next is a sample output where player 2 wins after ignoring some invalid input: 1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 3 Player 2, Column: 1 1 2 3 1 X * * 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 * * * 3 O * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 * * * 3 O * * Player 1, Row: 1 Player 1, Column: 3 Player 1, Row: 9 Player 1, Row: 9 Player 1, Row: 2 Player 1, Column: 1 1 2 3 1 X X O 2 X * * 3 O * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X O 2 X O * 3 O * * Player 2 wins! 1 2 3 1 * * * 2 * * * 3 * * * Player 1, Row: 1 Player 1, Column: 1 1 2 3 1 X * * 2 * * * 3 * * * Player 2, Row: 2 Player 2, Column: 1 1 2 3 1 X * * 2 O * * 3 * * * Player 1, Row: 1 Player 1, Column: 2 1 2 3 1 X X * 2 O * * 3 * * * Player 2, Row: 2 Player 2, Column: 2 1 2 3 1 X X * 2 O O * 3 * * * Player 1, Row: 2 Player 1, Column: 3 1 2 3 1 X X * 2 O O X 3 * * * Player 2, Row: 1 Player 2, Column: 3 1 2 3 1 X X O 2 O O X 3 * * * Player 1, Row: 3 Player 1, Column: 1 1 2 3 1 X X O 2 O O X 3 X * * Player 2, Row: 3 Player 2, Column: 2 1 2 3 1 X X O 2 O O X 3 X O * Player 1, Row: 3 Player 1, Column: 3 1 2 3 1 X X O 2 O O X 3 X O X Tie!

Explanation / Answer

Here is the C++ program for tic-toe:

#include<iostream>

#include<cstdlib>

using namespace std;

char square[11]={'0','1','2','3','4','5','6','7','8','9',''};

void board();

int win();

int main()

{

char playagain='y';

while(playagain=='y')

{

cout<<"Welcome in my Tic Tac Toe!!! "<<endl;

int player=1;

int i,choice;

char mark;

do

{

board();

player=(player%2)?1:2;

cout<<"Player "<<player<<" enter a number: ";

cin>>choice;

mark=(player==1)?'X':'O';

if(choice==1 && square[1]=='1')

square[1]=mark;

else if (choice == 2 && square[2] == '2')

square[2] = mark;

else if (choice == 3 && square[3] == '3')

square[3] = mark;

else if (choice == 4 && square[4] == '4')

square[4] = mark;

else if (choice == 5 && square[5] == '5')

square[5] = mark;

else if (choice == 6 && square[6] == '6')

square[6] = mark;

else if (choice == 7 && square[7] == '7')

square[7] = mark;

else if (choice == 8 && square[8] == '8')

square[8] = mark;

else if (choice == 9 && square[9] == '9')

square[9] = mark;

else

{

cout<<" INVALID MOVE ";

player--;

}

i=win();

player++;

}while(i==-1);

board();

if(i==1)

cout<<"Player"<<--player<<" win";

if(i==0)

cout<<"Game draw";

cout<<" Wanna play again(y/n)?:";

cin>>playagain;

cout<<endl;

if(playagain=='y')

{

cout<<"You choice to play again ";

}

else if(playagain=='n')

{

cout<<"You choice not to play again ";

}

else

{

cout<<"You didnt put an valid character so i will take it like(n),so the program will close. ";

}

for(int c=0;c<40;c++)

{

cout<<"****";

}

cout<<endl;

square[1] = '1';

square[2] = '2';

square[3] = '3';

square[4] = '4';

square[5] = '5';

square[6] = '6';

square[7] = '7';

square[8] = '8';

square[9] = '9';

}

}

int win()

{

if(square[1]==square[2]&&square[2]==square[3])

return 1;

else if(square[4]==square[5]&&square[5]==square[6])

return 1;

else if(square[7]==square[8]&&square[8]==square[9])

return 1;

else if(square[1]==square[4]&&square[4]==square[7])

return 1;

else if(square[2]==square[5]&&square[5]==square[8])

return 1;

else if(square[3]==square[6]&&square[6]==square[9])

return 1;

else if(square[1]==square[5]&&square[5]==square[9])

return 1;

else if(square[3]==square[5]&&square[5]==square[7])

return 1;

else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'&&square[5] != '5'

&& square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;

else

return -1;

}

void board()

{

cout<<" Tic Tac Toe ";

cout<<"PLAYER 1(X) , PLAYER 2(O) ";

cout<<square[1]<<"|"<<square[2]<<"|"<<square[3]<<endl;

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

cout<<square[4]<<"|"<<square[5]<<"|"<<square[6]<<endl;

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

cout<<square[7]<<"|"<<square[8]<<"|"<<square[9]<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote