Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio>
- #include <process>
- #define getch __getch
- using namespace std;
- char a[3][3];
- void draw(){
- cout<<" "<<a[0][0]<<"__|__"<<a[0][1]<<"__|__"<<a[0][2]<<endl;
- cout<<" "<<a[1][0]<<"__|__"<<a[1][1]<<"__|__"<<a[1][2]<<endl;
- cout<<" "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
- }
- int check(){
- for(int q=0; q<3; q++){
- if(a[q][0]==a[q][1] && a[q][1]== a[q][2] && (a[q][2]=='O' || a[q][2]=='X'))
- return 1;
- if(a[0][q]== a[1][q] && a[1][q]==a[2][q] && (a[2][q]=='O' || a[2][q]=='X'))
- return 1;
- }
- if(a[0][0]==a[1][1] && a[1][1]== a[2][2] && (a[2][2]=='X' || a[2][2]=='O'))
- return 1;
- if(a[0][2]==a[1][1] && a[1][1]==a[2][0] && (a[2][0]=='X'|| a[2][0]=='O'))
- return 1;
- else
- return -1;
- }
- void initialise(){
- for(int i=0; i<3; i++){
- for(int j=0; j<3; j++){
- a[i][j]='*';
- }
- }
- }
- int main(){
- clrscr();
- int player;
- int w,i;
- char mark;
- int r, c;
- initialise();
- while(w!=1){
- if(i % 2==0) { player=1; mark='X'; }
- else { player=2; mark='O'; }
- cout<<"PLAYER: "<<player;
- cout<<"\nrow: ";
- cin>>r;
- cout<<"col: " ;
- cin>>c;
- getch();
- clrscr();
- //this if statement i tried to write in the check function block but then wo while loop me implement nai ho ra..how to do that?
- if(a[0][0]!='*'&& a[0][1]!='*'&&a[0][2]!='*'&&a[1][0]!='*'&&a[1][1]!='*'&&a[1][2]!='*'&&a[2][0]!='*'&&a[2][1]!='*'&&a[2][2]!='*'){
- w=0;
- break;
- }
- if(a[r][c]=='*'){
- a[r][c]=mark;
- draw();
- w=check();
- i++;
- }else{
- draw();
- i=i+2;
- }
- }
- if(w==1){
- clrscr();
- cout<<"\n\n\n PLAYER "<<player<<" WON" ;
- getch();
- exit(0);
- }
- if(w==0){
- clrscr();
- cout<<"GAME draw ";
- getch();
- exit(0);
- }
- // Something went wrong
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement