1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include<iostream>
using namespace std;
void shape();
int playertoggle();
int check();
char a[10]={'0','1','2','3','4','5','6','7','8','9'};
int n,player,i;
char mark='x';
int main()
{
do
{
shape();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin>>n;
mark=(player == 1) ? 'X' : 'O';
if(n==1&&a[1]=='1')
a[1]=mark;
else if(n==2&&a[2]=='2')
a[2]=mark;
else if(n==3&&a[3]=='3')
a[3]=mark;
else if(n==4&&a[4]=='4')
a[4]=mark;
else if(n==5&&a[5]=='5')
a[5]=mark;
else if(n==6&&a[6]=='6')
a[6]=mark;
else if(n==7&&a[7]=='7')
a[7]=mark;
else if(n==8&&a[8]=='8')
a[8]=mark;
else if(n==9&&a[9]=='9')
a[9]=mark;
else
{
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=check();
player++;
}while(i==-1);
shape();
if(i==1)
cout<<"\aPlayer "<<--player<<" win ";
else
cout<<"\aGame draw";
cin.ignore();
cin.get();
return 0;
}
void shape()
{
system("cls");
cout<<"enter a nubmer "<<endl;
cout<<" | " << " |"<<endl;
cout<<" "<<a[1]<<" |"<<" "<<a[2]<<" |"<<" "<<a[3]<<endl;
cout<<"___|"<<"_____|"<<"____"<<endl;
cout<< " |"<<" |"<<" "<<endl;
cout<<" "<<a[4]<<" |"<<" "<<a[5]<<" |"<<" "<<a[6]<<endl;
cout<<"___|"<<"_____|"<<"____"<<endl;
cout<< " |"<<" |"<<" "<<endl;
cout<<" "<<a[7]<<" |"<<" "<<a[8]<<" |"<<" "<<a[9]<<endl;
}
int check()
{
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;
}
int playertoggle()
{
player=(player%2)?1:2;
return 1;
}.
|