You are on page 1of 4

#include<iostream>

using namespace std;


char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

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++;
}

8 && square[8] == '8')


mark;

9 && square[9] == '9')


mark;

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 ==

5 && square[5] == '5')


mark;

6 && square[6] == '6')


mark;

7 && square[7] == '7')

{
square[7] = mark;
player--;
}
else if (choice ==
{
square[8] =
player--;
}
else if (choice ==
{
square[9] =
player--;
}
else
{
cout<<"invalid
}

8 && square[8] == '8')


mark;

9 && square[9] == '9')


mark;

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 << "

Player 2 (O)" << endl << endl;

|
|
" << endl;
" << square[1] << " | " << square[2] << "

" << square[3] << endl;

" << square[6] << endl;

" << square[9] << endl;

cout << "_____|_____|_____" << endl;


cout << "
|
|
" << endl;
cout << "

" << square[4] << "

" << square[5] << "

cout << "_____|_____|_____" << endl;


cout << "
|
|
" << endl;
cout << "
cout << "

" << square[7] << "


|

" << square[8] << "

" << endl << endl;