I want to create a simple version of the game Tic-Tac-Toe.In my edition there wi
ID: 3624665 • Letter: I
Question
I want to create a simple version of the game Tic-Tac-Toe.In my edition there will be a 3x3 matrix.The 2 players that will play the game will start to fill the matrix with the symbols "X" and "O".When a player completes a column or a row or a arriswise the program ends with showing this message "Congratulation Player X. You Won!", where x is 1 or 2 depending on which player won.This is an example of execution:*******************
Turn 1: Player 1 ('X')
*******************
Enter Row: 1
Enter Column: 1
0 1 2
0
1 X
2
*******************
Turn 2: Player 2 ('O')
*******************
Enter Row: 1
Enter Column: 0
0 1 2
0
1 O X
2
*******************
Turn 3: Player 1 ('X')
*******************
Enter Row: 0
Enter Column: 0
0 1 2
0 X
1 O X
2
*******************
Turn 4: Player 2 ('O')
*******************
Enter Row: 2
Enter Column: 2
0 1 2
0 X
1 O X
2 O
*******************
Turn 5: Player 1 ('X')
*******************
Enter Row: 0
Enter Column: 2
0 1 2
0 X X
1 O X
2 O
*******************
Turn 6: Player 2 ('O')
*******************
Enter Row: 2
Enter Column: 0
0 1 2
0 X X
1 O X
2 O O
*******************
Turn 7: Player 1 ('X')
*******************
Enter Row: 0
Enter Column: 1
0 1 2
0 X X X
1 O X
2 O O
Congratulation Player 1. You Won!
I will appreciate any answers! :)
Explanation / Answer
#include <process.h>
#include <string>
using namespace std;
bool playerTurn(int,char,string);
void display();
bool isRowFull(int);
bool isColumnFull(int);
char t[3][3]={(' ',' ',' '),(' ',' ',' '),(' ',' ',' ')};
int main()
{
int i=1;
while(true)
{
if(playerTurn(i,'X',"Player 1"))
{
cout<<" Congratulations Player 1, You Won!"<<endl;
system("pause");
exit(-1);
}
if(playerTurn(++i,'O',"Player 2"))
{
cout<<" Congratulations Player 2, You Won!"<<endl;
system("pause");
exit(-1);
}
}
return 1;
}
bool playerTurn(int turnnum,char ch,string s)
{
int row,column;
bool flag=false;
cout<<" ********************"<<endl;
cout<<"Turn "<<(turnnum)<<":"<<s<<" ("<<ch<<")"<<endl;
cout<<"******************** "<<endl;
cout<<"Enter Row: ";
cin>>row;
cout<<"Enter Column: ";
cin>>column;
t[row][column]=ch;
display();
if(isRowFull(row)||isColumnFull(column))
flag=true;
return flag;
}
bool isRowFull(int row)
{
bool flag=true;
for(int i=0;i<3;i++)
{
if((t[row][i]!='X')&&(t[row][i]!='O'))
{
flag=false;
break;
}
}
return flag;
}
bool isColumnFull(int column)
{
bool flag=true;
for(int i=0;i<3;i++)
{
if((t[i][column]!='X')&&(t[i][column]!='O'))
{
flag=false;
break;
}
}
return flag;
}
void display()
{
cout<<" "<<"0"<<" "<<"1"<<" "<<"2"<<endl;
for(int i=0;i<3;i++)
{
cout<<i;
for(int j=0;j<3;j++)
cout<<" "<<t[i][j]<<" ";
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.