Tic Tac Toe Project
Tic Tac Toe Project
Tic Tac Toe Project
void board();
void player1();
void player2();
void win();
int choice;
int player=1;
char mark;
int main()
{
do{
board();
player1();
board();
win();
}while(player!=3);
}
//PLAYER 1 FUNCTION
void player1()
{
if(player==1)
{
cout<<"Player 1 plz enter the box no"<<endl;
cin>>choice;
win();
mark='X';
if (choice == 1 && square[1] == '1')
{
square[1] = mark;
player++;
}
else if (choice == 2 && square[2] == '2')
{
square[2] = mark;
player++;
}
else if (choice == 3 && square[3] == '3')
{
square[3] = mark;
player++;
}
else if (choice == 4 && square[4] == '4')
{
square[4] = mark;
player++;
}
else if (choice == 5 && square[5] == '5')
{
square[5] = mark;
player++;
}
else if (choice == 6 && square[6] == '6')
{
square[6] = mark;
player++;
}
else if (choice == 7 && square[7] == '7')
{
square[7] = mark;
player++;
}
else if (choice ==
{
square[8] =
player++;
}
else if (choice ==
{
square[9] =
player++;
}
else
{
cout<<"invalid choice"<<endl;
}
}
else if(player==2)
{
player2();
}
}
//PLAYER 2 FUNCTION
void player2()
{
cout<<"Player 2 plz enter the box no"<<endl;
cin>>choice;
win();
mark='O';
if (choice == 1 && square[1] == '1')
{
square[1] = mark;
player--;
}
else if (choice == 2 && square[2] == '2')
{
square[2] = mark;
player--;
}
else if (choice == 3 && square[3] == '3')
{
square[3] = mark;
player--;
}
else if (choice == 4 && square[4] == '4')
{
square[4] = mark;
player--;
}
else if (choice ==
{
square[5] =
player--;
}
else if (choice ==
{
square[6] =
player--;
}
else if (choice ==
{
square[7] = mark;
player--;
}
else if (choice ==
{
square[8] =
player--;
}
else if (choice ==
{
square[9] =
player--;
}
else
{
cout<<"invalid
}
choice"<<endl;
}
//WIN FUNCTION
void win()
{
if (square[1] == square[2] && square[2] == square[3])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[4] == square[5] && square[5] == square[6])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[7] == square[8] && square[8] == square[9])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[1] == square[4] && square[4] == square[7])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[2] == square[5] && square[5] == square[8])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[3] == square[6] && square[6] == square[9])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[1] == square[5] && square[5] == square[9])
{
cout<<"The player won"<<endl;
player=3;
}
else if (square[3] == square[5] && square[5] == square[7])
{
cout<<"The player won"<<endl;
player=3;
}
}
//BOARD FUNCTION
void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X)
cout << endl;
cout << "
cout << "
|
|
" << endl;
" << square[1] << " | " << square[2] << "