We are suppose to create a program that will let us play tic tac toe. We are required to have 4 functions including main and use arrays as parameters to functions. Is my code correct?? Also there's something wrong with my code and can't figure it out.
#include<iostream>
using namespace std;
bool player1Won();
bool player1WonInARow();
bool player1WonInAColumn();
bool player1WonInDia();
bool player2Won();
bool player2WonInARow();
bool player2WonInAColumn();
bool player2WonInDia();
char tic_tac_toe[3][3]={'*','*','*','*','*','*','*','*','*'};
int player1R[5];
int player1C[5];
int player2R[4];
int player2C[4];
int countForP1R=0;
int countForP2R=0;
int main()
{
int i=0;
for(;i<9;i++)
{
int x,y,z,k;
//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<endl;
}
//ask player one to select a location
cout<<"Player 1 select a location by entering two number while the first number, in the range of 0 to 2, stands for the row of the loation and the second number in the range of 0 to 2, stands for the columns of the location. Sperate two number by space. ONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE"<<endl;
cin>>x>>y;
tic_tac_toe[x][y]='x';
player1R[i]=x;
player1C[i]=y;
//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<endl;
}
//determinate if player1 wins
if(player1Won()==true)
{
cout<<"Player 1 wins the game!";
return 0;
}
//ask player two to select a location
cout<<"PLAYER2 Select a location by entering two number while"
<<"\nthe first number, in the range of 0 to 2, stands"
<<"\nfor the row of the loation and the second num, "
<<"\n in the range of 0 to 2, stands for the columns"
<<"\nof the location. Sperate two number by space."
<<"\n ONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE"<<endl;
cin>>z>>k;
tic_tac_toe[z][k]='O';
player2R[i]=z;
player2C[i]=k;
//determinate if player2 wins
if(player2Won()==true)
{
cout<<"Player 2 wins the game!";
return 0;
}
}
return 0;
}
//Determinate if player1 wins
bool player1Won();
{
bool player1Won=false;
if(player1WonInARow()==true)
{
cout<<"player1WonInARow"<<endl;
player1Won=true;
}
if(player1WonInAColumn()==true)
{
cout<<"player1WonInAColumn";
player1Won=true;
}
if(player1WonInDia()==true)
{
cout<<"player1WonInDia";
player1Won=true;
}
return player1Won;
}
bool player1WonInARow();
{
bool player1WonInARow=false;
if(tic_tac_toe[0][0]=='X'&&tic_tac_toe[1][0]=='X'&&tic_tac_toe[2][0]=='X') player1WonInARow=true;
else if(tic_tac_toe[0][1]=='X'&&tic_tac_toe[1][1]=='X'&&tic_tac_toe[2][1]=='X') player1WonInARow=true;
else if(tic_tac_toe[0][2]=='X'&&tic_tac_toe[1][2]=='X'&&tic_tac_toe[2][2]=='X') player1WonInARow=true;
return player1WonInARow;
}
bool player1WonInAColumn();
{
bool player1WonInAColumn=false;
if(tic_tac_toe[0][0]=='X'&&tic_tac_toe[0][1]=='X'&&tic_tac_toe[0][2]=='X') player1WonInAColumn=true;
else if(tic_tac_toe[1][0]=='X'&&tic_tac_toe[1][1]=='X'&&tic_tac_toe[1][2]=='X') player1WonInAColumn=true;
else if(tic_tac_toe[2][0]=='X'&&tic_tac_toe[2][1]=='X'&&tic_tac_toe[2][2]=='X') player1WonInAColumn=true;
return player1WonInAColumn;
}
}