Write a program that allows two players to play a game of tic-tac-toe. Use a dyn
ID: 3581668 • Letter: W
Question
Write a program that allows two players to play a game of tic-tac-toe. Use a dynamic two-dimensional char array with n rows and n columns where the program reads n from the standard input device. Make sure that n is an integer greater than 1. Initialize each element of the array with an asterisk (*). The program should run a loop that
Display the contents of the board array
Allows player 1 to l select a location on the board for an X. The program should ask the user to enter the row and 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 and column number.
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 say so and end.
Player 1 wins when there are n Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board.
Player 2 wins when there are n Os in a row on the game board. The Os can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
c++
Explanation / Answer
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.