Skip to content

Instantly share code, notes, and snippets.

@nitink133
Created October 17, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitink133/b4f8e7e62d8af19144739644f9a3b3d2 to your computer and use it in GitHub Desktop.
Save nitink133/b4f8e7e62d8af19144739644f9a3b3d2 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
char a[10]={'0','1','2','3','4','5','6','7','8','9'};
int flag=0;
class ZeroCross{
int pos;
public:void input();
void display();
void game();
void win();
};
void ZeroCross:: input(){
if(flag==0){
cout<<setw(10)<<"Its Your's turn (Player 1:(x)):"<<setw(3);
cin>>pos;
flag=1;
return;
}else{
cout<<setw(10)<<"Its Your's turn (Player 2:(o)):"<<setw(3);
cin>>pos;
flag=0;
}
}
void ZeroCross:: game(){
input();
char playr=(flag==0)?'o':'x';
switch(pos){
case 1:a[1]=playr;display();
break;
case 2:a[2]=playr;
display();break;
case 3:a[3]=playr;
display();break;
case 4:a[4]=playr;
display();break;
case 5:a[5]=playr;
display();break;
case 6:a[6]=playr;
display();break;
case 7:a[7]=playr;
display();break;
case 8:a[8]=playr;
display();break;
case 9:a[9]=playr;
display();break;
default:cout<<"Wrong Input!"<<endl<<"Dont Worry! Try Again"<<endl;flag=!flag;
}
}
void ZeroCross :: win(){
if(a[1]==a[2]&&a[2]==a[3]||a[1]==a[4]&&a[4]==a[7]||a[1]==a[5]&&a[5]==a[9]||a[2]==a[5]&&a[5]==a[8]||a[3]==a[6]&&a[6]==a[9]||a[3]==a[5]&&a[5]==a[7]){
if(flag==0){cout<<"Player 2(o) win the match!"<<endl;
exit(0);}
else{cout<<"Player 1(x) win the match!"<<endl;
exit(0);}
}
}
void ZeroCross :: display(){
cout<<setw(40)<<" |"<<" |"<<endl;
cout<<setw(38)<<a[1]<<" | "<<a[2]<<" | "<<a[3]<<endl;
cout<<setw(40)<<"____|"<<"____|____"<<endl;
cout<<setw(40)<<" |"<<" |"<<endl;
cout<<setw(38)<<a[4]<<" | "<<a[5]<<" | "<<a[6]<<endl;
cout<<setw(40)<<"____|"<<"____|____"<<endl;
cout<<setw(40)<<" |"<<" |"<<endl;
cout<<setw(38)<<a[7]<<" | "<<a[8]<<" | "<<a[9]<<endl;
cout<<setw(40)<<" |"<<" |"<<endl;}
int main(){
ZeroCross z1;
int i=1;
cout<<setw(50)<<"Body of The Game is"<<"\n\n";
z1.display();
cout<<"\n\n"<<setw(40)<<"Lets Start The Game"<<"\n\n\n";
for(int j=1;j<=9;j++){z1.game();
z1.win();
} return 0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment