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
|
#include <iostream>
using namespace std;
int playagain();
int main()
{
int board[3][3],x,y,loopcount=0;
char dispboard[3][3];
int PlayerA=79, PlayerB=88;
int i=0;
int winchecker=0, checkx=0, checky=0, leftdiag=0, rightdiag=0;
bool win=0;
cout<<"Player A=X, Player B=O"<<endl;
cout<<"[X,Y]"<<endl;
cout<<"[0,0]\t[1,0]\t[2,0]\n";
cout<<"[0,1]\t[1,1]\t[2,1]\n";
cout<<"[0,2]\t[1,2]\t[2,2]\n\n";
while(loopcount<9 & win!=1)
{
i++;
//to set board
cout<<"CURRENT BOARD";
for (y=0;y<3; y++)
{
cout<<endl<<"----------------------"<<endl;;
for (x=0;x<3; x++)
{
if (loopcount==0) {board[x][y]=0;}
dispboard[x][y]=board[x][y];
cout<<dispboard[x][y]<<"\t";
}
}
cout<<endl<<"----------------------"<<endl;
//game play
{
do{
if ((i % 2)!=0) {cout<<"Player A\n";loopcount++;}
else {cout<<"Player B\n";loopcount++;}
cin>>x; cin>>y;
cout<<endl;
if ((x>2 or x<0) or (y>2 or y<0) or (board[x][y]!=0)){cout<<"Error\n"; loopcount--;}
if (board[x][y]==0)
{
if ((i % 2)!=0) {board[x][y]=PlayerA;}
else {board[x][y]=PlayerB;}
}
else {x=5;}
// to determine winner-vertical
for (checkx=0;checkx<3;checkx++)
{
for (checky=0;checky<3;checky++)
{
winchecker+=board[checkx][checky];
if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
}
winchecker=0;
}
// to determine winner-horizontal
for (checky=0;checky<3;checky++)
{
for (checkx=0;checkx<3;checkx++)
{
winchecker+=board[checkx][checky];
if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
}
winchecker=0;
}
//to determine winner-left diagnal (left top start) & right diagnal
{
leftdiag=board[0][0]+board[1][1]+board[2][2];
rightdiag=board[2][0]+board[1][1]+board[0][2];
if (((leftdiag==(3*PlayerA))) or ((rightdiag==(3*PlayerA)))){cout<<"winner-player A"; winchecker=0; win=1; break;}
if (((leftdiag==(3*PlayerB))) or ((rightdiag==(3*PlayerB)))){cout<<"winner-player B"; winchecker=0; win=1; break;}
}
if ((win!=1) && loopcount==9){cout<<"No Winners Idiots";}
}while((x>2 or x<0) or (y>2 or y<0));
}
}
cout<<endl;
playagain();
return 0;
}
int playagain()
{
int playagain;
cout<<"Press 1 to play again, anything else to exit\n";
cin>>playagain;
while(playagain==1)
{
main();
}
return 0;
}
|