In c++ Write a program that allows two players to play a game of tic tac toe. Us
ID: 3710742 • Letter: I
Question
In c++
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 after each move Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column numbers. 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 numbers 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 three X’s in a row on the game board. The X’s can appear in a row, column, or diagonally across the board. Player 2 wins when there are three O’s in a row on the game board. The O’s can appear in a row, column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner. Make sure to not allow a user to enter an X or O in a location that already has an entry.
Explanation / Answer
#include<iostream> //Header files
#include<fstream>
using namespace std;
void Display(char a[3][3]) //Function to display the char array
{
int i, j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<" ";
}
}
void InputSlot(char a[3][3], int i) //Function to input the row and column number
{
int m, n;
do
{
m = n = -1;
cout<<"Choose a row number (0 to 2): ";
cin>>m;
if(m!=0 && m!=1 && m!=2) //Row number is none from 0, 1 or 2
cout<<"It is not a valid row. ";
else
{
cout<<"Choose a column number (0 to 2): ";
cin>>n;
if(n!=0 && n!=1 && n!=2) //Column number is none from 0, 1 or 2
cout<<"It is not a valid column. ";
}
if(m>=0 && m<=2 && n>=0 && n<=2 && a[m][n]!='*') //If input slot was filled earlier
cout<<"Slot already filled. ";
}while((m!=0 && m!=1 && m!=2) || (n!=0 && n!=1 && n!=2) || (a[m][n]!='*')); //Repeat if no change occured in the char array
a[m][n] = (i%2==0) ? 'X' : 'O'; //Update the char array
}
char check(char a[3][3]) //Function to check if any player won, returns 'X' if Player1 won, 'O' if Player2 won and 'N' if none won
{
int i;
for(i=0;i<3;i++)
{
if(a[i][0]==a[i][1] && a[i][1]==a[i][2] && a[i][2]!='*') //Checks the rows
return(a[i][0]);
else if(a[0][i]==a[1][i] && a[1][i]==a[2][i] && a[2][i]!='*') //Check the columns
return(a[0][i]);
}
if(((a[0][0]==a[1][1] && a[1][1]==a[2][2]) || (a[0][2]==a[1][1] && a[1][1]==a[2][0])) && a[1][1]!='*') //Checks the diagonals
return(a[1][1]);
return('N'); //If nobody won
}
int main()
{
char a[3][3], win = 'N';
int i, j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j] = '*'; //Initialize the char array by '*'
Display(a); //Displays the char array
for(i=0;i<9;i++) //Runs 9 times as only 9 moves are possible at max
{
if(i%2==0) //Even value of i represents Player1's turn
cout<<"Player 1 turn(X): ";
else //Odd value of i represents Player2's turn
cout<<"Player 2 turn(O): ";
InputSlot(a, i); //Input the slot
Display(a); //Displays the char array
win = check(a); //Checks if anybody won
if(win=='X') //Player1 won
{
cout<<"Game is over: Player 1 (X) wins! ";
break;
}
else if(win=='O') //Player2 won
{
cout<<"Game is over: Player 2 (O) wins! ";
break;
}
}
if(win=='N') //After 9 turns, if check() returns 'N', then it is draw
cout<<"Game is over: It is a tie! ";
return(0);
}
The following are two outputs which show the different cases. The bold and underlined characters are the input.
Output 1:
* * *
* * *
* * *
Player 1 turn(X):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
0
X * *
* * *
* * *
Player 2 turn(O):
Choose a row number (0 to 2):
1
Choose a column number (0 to 2):
1
X * *
* O *
* * *
Player 1 turn(X):
Choose a row number (0 to 2):
2
Choose a column number (0 to 2):
2
X * *
* O *
* * X
Player 2 turn(O):
Choose a row number (0 to 2):
2
Choose a column number (0 to 2):
0
X * *
* O *
O * X
Player 1 turn(X):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
2
X * X
* O *
O * X
Player 2 turn(O):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
1
X O X
* O *
O * X
Player 1 turn(X):
Choose a row number (0 to 2):
1
Choose a column number (0 to 2):
2
X O X
* O X
O * X
Game is over:
Player 1 (X) wins!
Output 2:
* * *
* * *
* * *
Player 1 turn(X):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
0
X * *
* * *
* * *
Player 2 turn(O):
Choose a row number (0 to 2):
1
Choose a column number (0 to 2):
1
X * *
* O *
* * *
Player 1 turn(X):
Choose a row number (0 to 2):
2
Choose a column number (0 to 2):
2
X * *
* O *
* * X
Player 2 turn(O):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
2
X * O
* O *
* * X
Player 1 turn(X):
Choose a row number (0 to 2):
2
Choose a column number (0 to 2):
0
X * O
* O *
X * X
Player 2 turn(O):
Choose a row number (0 to 2):
1
Choose a column number (0 to 2):
0
X * O
O O *
X * X
Player 1 turn(X):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
1
X X O
O O *
X * X
Player 2 turn(O):
Choose a row number (0 to 2):
2
Choose a column number (0 to 2):
1
X X O
O O *
X O X
Player 1 turn(X):
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
0
Slot already filled.
Choose a row number (0 to 2):
3
It is not a valid row.
Choose a row number (0 to 2):
0
Choose a column number (0 to 2):
3
It is not a valid column.
Choose a row number (0 to 2):
1
Choose a column number (0 to 2):
2
X X O
O O X
X O X
Game is over:
It is a tie!
Kindly give a thumbs up, if found useful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.