Tic-Tac-Toe
#include<iostream>
using namespace std;
void main()
{
int i,row=0,column=0,winner=0,player,put;
char dice[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
for (i=0;i<9 && winner==0;i++)
{
cout<<dice[0][0]<<"|"<<dice[0][1]<<"|"<<dice[0][2];
cout<<endl;
cout<<"-|-|-"<<endl;
cout<<dice[1][0]<<"|"<<dice[1][1]<<"|"<<dice[1][2];
cout<<endl;
cout<<"-|-|-"<<endl;
cout<<dice[2][0]<<"|"<<dice[2][1]<<"|"<<dice[2][2];
cout<<endl<<endl;
player=i%2+1;
if(player==2){cout<<"2nd player enter you position: ";}
else{cout<<"1st Player enter your position: ";}
cin>>put;
cout<<endl;
row=--put/3;
column=put%3;
if(player==1)dice[row][column]='X';
else{dice[row][column]='O';}
if((dice[0][0]==dice[1][1]&&dice[0][0]==dice[2][2])||(dice[0][2]==dice[1][1]&&dice[0][2]==dice[2][0]))
{
winner=player;
}
else
{
for(int line=0;line<=2;line++)
{
if((dice[line][0]==dice[line][1]&&dice[line][0]==dice[line][2])||(dice[0][line]==dice[1][line]&&dice[0][line]==dice[2][line]))
{
winner=player;
}
}
}}
if(winner==0)cout<<"bored"<<endl;
else{cout<<"!!!congrats!!!"<<endl;
cout<<" "<<winner<<" player win the match "<<endl;
}
cout<<"final result: "<<endl<<endl;
cout<<dice[0][0]<<"|"<<dice[0][1]<<"|"<<dice[0][2];
cout<<endl;
cout<<"-|-|-"<<endl;
cout<<dice[1][0]<<"|"<<dice[1][1]<<"|"<<dice[1][2];
cout<<endl;
cout<<"-|-|-"<<endl;
cout<<dice[2][0]<<"|"<<dice[2][1]<<"|"<<dice[2][2];
cout<<endl<<endl;
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment