Game Tic tac in c + +
#include<iostream.h>
#include<conio.h>
char a[10]={'0','1','2','3','4','5','6','7','8','9'};
int rule();
void display();
int op;
int main()
{
int player=1,i,choice;
char mark;
do
{
display();
player=(player%2)?1:2;
cout<<"\n player"<<player<<" "<<"enter a number:";
cin>>choice;
mark=(player==1)?'x':'o';
if(choice==1&&a[1]=='1')
{
a[1]=mark;
}
else if(choice==2&&a[2]=='2')
{
a[2]=mark;
}
else if(choice==3&&a[3]=='3')
{
a[3]=mark;
}
else if(choice==4&&a[4]=='4')
{
a[4]=mark;
}
else if(choice==5&&a[5]=='5')
{
a[5]=mark;
}
else if(choice==6&&a[6]=='6')
{
a[6]=mark;
}
else if(choice==7&&a[7]=='7')
{
a[7]=mark;
}
else if(choice==8&&a[8]=='8')
{
a[8]=mark;
}
else if(choice==9&&a[9]=='9')
{
a[9]=mark;
}
else
{
cout<<"\n envalid choice";
player--;
//cin.ignore();
//cin.get();
getch();
}
i=rule();
player++;
}
while(i==-1);
display();
if(i==1)
{
cout<<"\n\n\n ==>player"<<--player<<"win";
}
else
{
cout<<"\n\n\n ==> game draw";
//cin.ignore();
//cin.get();
getch();
return 0;
}
getch();
}
int rule()
{
if(a[1]==a[2]&&a[2]==a[3])
{
return 1;
}
else if(a[4]==a[5]&&a[5]==a[6])
{
return 1;
}
else if(a[7]==a[8]&&a[8]==a[9])
{
return 1;
}
else if(a[1]==a[4]&&a[4]==a[7])
{
return 1;
}
else if(a[2]==a[5]&&a[5]==a[8])
{
return 1;
}
else if(a[3]==a[6]&&a[6]==a[9])
{
return 1;
}
else if(a[1]==a[5]&&a[5]==a[9])
{
return 1;
}
else if(a[3]==a[5]&&a[5]==a[7])
{
return 1;
}
else if(a[1]!='1'&&a[2]!='2'&&a[3]!='3'&&a[4]!='4'&&a[5]!='5'&&a[6]!='6'&&a[7]!='7'&&a[8]!='8'&&a[9]!='9')
{
return 0;
}
else
{
return -1;
}
}
void display()
{
clrscr();
cout<<"\n\n\n tic tac\n";
cout<<"\n player 1(x)-player 2(o)";
cout<<"\n\n\n";
cout<<" | | ";
cout<<"\n "<<a[1]<<" | "<<a[2]<<" | "<<a[3];
cout<<"\n";
cout<<"____|____|____";
cout<<"\n";
cout<<" | | ";
cout<<"\n";
cout<<" "<<a[4]<<" | "<< a[5]<<" | "<<a[6];
cout<<" \n";
cout<<"____|____|____";
cout<<"\n";
cout<<" | | ";
cout<<"\n";
cout<<" "<<a[7]<<" | "<<a[8]<<" | "<<a[9];
cout<<"\n";
cout<<" | | ";
}
#include<conio.h>
char a[10]={'0','1','2','3','4','5','6','7','8','9'};
int rule();
void display();
int op;
int main()
{
int player=1,i,choice;
char mark;
do
{
display();
player=(player%2)?1:2;
cout<<"\n player"<<player<<" "<<"enter a number:";
cin>>choice;
mark=(player==1)?'x':'o';
if(choice==1&&a[1]=='1')
{
a[1]=mark;
}
else if(choice==2&&a[2]=='2')
{
a[2]=mark;
}
else if(choice==3&&a[3]=='3')
{
a[3]=mark;
}
else if(choice==4&&a[4]=='4')
{
a[4]=mark;
}
else if(choice==5&&a[5]=='5')
{
a[5]=mark;
}
else if(choice==6&&a[6]=='6')
{
a[6]=mark;
}
else if(choice==7&&a[7]=='7')
{
a[7]=mark;
}
else if(choice==8&&a[8]=='8')
{
a[8]=mark;
}
else if(choice==9&&a[9]=='9')
{
a[9]=mark;
}
else
{
cout<<"\n envalid choice";
player--;
//cin.ignore();
//cin.get();
getch();
}
i=rule();
player++;
}
while(i==-1);
display();
if(i==1)
{
cout<<"\n\n\n ==>player"<<--player<<"win";
}
else
{
cout<<"\n\n\n ==> game draw";
//cin.ignore();
//cin.get();
getch();
return 0;
}
getch();
}
int rule()
{
if(a[1]==a[2]&&a[2]==a[3])
{
return 1;
}
else if(a[4]==a[5]&&a[5]==a[6])
{
return 1;
}
else if(a[7]==a[8]&&a[8]==a[9])
{
return 1;
}
else if(a[1]==a[4]&&a[4]==a[7])
{
return 1;
}
else if(a[2]==a[5]&&a[5]==a[8])
{
return 1;
}
else if(a[3]==a[6]&&a[6]==a[9])
{
return 1;
}
else if(a[1]==a[5]&&a[5]==a[9])
{
return 1;
}
else if(a[3]==a[5]&&a[5]==a[7])
{
return 1;
}
else if(a[1]!='1'&&a[2]!='2'&&a[3]!='3'&&a[4]!='4'&&a[5]!='5'&&a[6]!='6'&&a[7]!='7'&&a[8]!='8'&&a[9]!='9')
{
return 0;
}
else
{
return -1;
}
}
void display()
{
clrscr();
cout<<"\n\n\n tic tac\n";
cout<<"\n player 1(x)-player 2(o)";
cout<<"\n\n\n";
cout<<" | | ";
cout<<"\n "<<a[1]<<" | "<<a[2]<<" | "<<a[3];
cout<<"\n";
cout<<"____|____|____";
cout<<"\n";
cout<<" | | ";
cout<<"\n";
cout<<" "<<a[4]<<" | "<< a[5]<<" | "<<a[6];
cout<<" \n";
cout<<"____|____|____";
cout<<"\n";
cout<<" | | ";
cout<<"\n";
cout<<" "<<a[7]<<" | "<<a[8]<<" | "<<a[9];
cout<<"\n";
cout<<" | | ";
}
Thanks..
ReplyDelete