#include<iostream.h>
#include<conio.h>
using namespace std;
main()
{
char a[9]={'0','1','2','3','4','5','6','7','8'};
int n=1,m=0,num;
do
{
system("cls");
cout<<(char)4<<" Welcome into tic tac toe game "<<(char)4<<"\n"<<endl;
cout<<" "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<" "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<" "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";
if(n==m%2)
{
cout<<"\n2nd Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
for(int k=0;k<9;k++)
{
if(num==k)
{
a[k]='X';
}
}
}
}
if(m%2==0){
cout<<"\n1st Player turn"<<endl;
cin>>num;
if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7||num==8)
{
for(int k=0;k<9;k++)
{
if(num==k)
{
a[k]='Y';
}
}
}
}
if((a[0]==a[1]&&a[1]==a[2]&&a[3]==a[0])||(a[0]==a[3]&&a[3]==a[6]&&a[0]==a[6])||(a[0]==a[4]&&a[4]==a[8]&&a[0]==a[8])||(a[1]==a[4]&&a[4]==a[7]&&a[1]==a[7])||(a[2]==a[5]&&a[5]==a[8]&&a[2]==a[8])||(a[2]==a[4]&&a[4]==a[6]&&a[2]==a[6])||(a[3]==a[4]&&a[4]==a[5]&&a[3]==a[5])||(a[6]==a[7]&&a[7]==a[8]&&a[6]==a[8]))
{
system("cls");
cout<<" "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<"\n";
cout<<"____________\n";
cout<<" "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<"\n";
cout<<"____________\n";
cout<<" "<<a[6]<<" | "<<a[7]<<" | "<<a[8]<<"\n";
cout<<"\nYOU Won!!\n"<<(char)2<<(char)2<<endl;
break;
}
else{cout<<"you lose\n"; }
m++;
}
while((a[0]!=a[1]&&a[1]!=a[2]&&a[3]!=a[0])||(a[0]!=a[3]&&a[3]!=a[6]&&a[0]!=a[6])||(a[0]!=a[4]&&a[4]!=a[8]&&a[0]!=a[8])||(a[1]!=a[4]&&a[4]!=a[7]&&a[1]!=a[7])||(a[2]!=a[5]&&a[5]!=a[8]&&a[2]!=a[8])||(a[2]!=a[4]&&a[4]!=a[6]&&a[2]!=a[6])||(a[3]!=a[4]&&a[4]!=a[5]&&a[3]!=a[5])||(a[6]!=a[7]&&a[7]!=a[8]&&a[6]!=a[8]));
cout<<"\nbye";
getch();
}
No comments:
Post a Comment