/* This is a simple version of tic tac toe game. ####################################################################### # (c) 2007 by Krishna Khanna # krishnakhanna@gmail.com # http://kkonline.org ####################################################################### */ #include #include #include #include int check(int,int,int,int,int,int); float move(int b) { switch(b) { case 1:gotoxy(50,12); return 0; case 2:gotoxy(56,12); return 1; case 3:gotoxy(60,12); return 2; case 4:gotoxy(50,14); return 3; case 5:gotoxy(56,14); return 4; case 6:gotoxy(60,14); return 5; case 7:gotoxy(50,16); return 6; case 8:gotoxy(56,16); return 7; case 9:gotoxy(60,16); return 8; default:cout<<"Wrong key"; return 9; } } void main() { clrscr(); char r; int p,i,u,y,t,e,w,q,l,c,v; gotoxy(30,2); cout<<"INSTRUCTIONS"; gotoxy(1,4); cout<<"Enter 1 for entering 'X' or 'O' in the 1st box.Similarly,enter 2 for entering in the 2nd box.Bottom extreme right box=9,middle extreme left box=4 etc."; gotoxy(1,6); cout<<"AS YOU ARE ABLE TO MAKE A STRAIGHT OR DIAGONAL LINE WITH 'X' OR 'O' YOU WIN"; getch(); clrscr(); gotoxy(50,12); cout<<" | | "; gotoxy(50,13); cout<<"___|___|___"; gotoxy(50,14); cout<<" | | "; gotoxy(50,15); cout<<"___|___|___"; gotoxy(50,16); cout<<" | | "; gotoxy(50,17); cout<<" | | "; gotoxy(1,1); cout<<" Player 1= X AND Player 2= O (capital)]"; back: cout<<"\nFirst move(1st player):"; cin>>c; p=move(c); if(p==9) goto back; cout<<'X'; again: gotoxy(1,3); cout<<"First move(2nd player):"; cin>>c; i=move(c); if(i==p||i==9) goto again; else cout<<'O'; gain:gotoxy(1,4); cout<<"Second move(1st player):"; cin>>c; u=move(c); if(u==p||u==i||u==9) goto gain; else cout<<'X'; ain: gotoxy(1,5); cout<<"Second move(2nd player):"; cin>>c; y=move(c); if(y==p||y==u||y==p||y==9) goto ain; else cout<<'O'; in: gotoxy(1,6); cout<<"Third move(1st player):"; cin>>c;int s=3; t=move(c); if(t==p||t==i||t==u||t==y||t==9) goto in; else cout<<'X'; v=check(s,p,u,t,0,0); if(v==0) goto exit; bad: gotoxy(1,7); cout<<"Third move(2nd player):"; cin>>c;s=4; e=move(c); if(e==p||e==i||e==u||e==y||e==t||e==9) goto bad; else cout<<'O'; v=check(s,i,y,e,0,0); if(v==0) goto exit; ad:gotoxy(1,8); cout<<"Fourth move(1st player):"; cin>>c;s=5; w=move(c); if(w==p||w==i||w==u||w==y||w==e||w==t||w==9) goto ad; else cout<<'X'; v=check(s,p,u,t,w,0); if(v==0) goto exit; add:gotoxy(1,9); cout<<"Fourth move(2nd player):"; cin>>c;s=6; q=move(c); if(q==p||q==i||q==u||q==y||q==t||q==e||q==w||q==9) goto add; else cout<<'O'; v=check(s,i,y,e,q,0); if(v==0) goto exit; dd: gotoxy(1,10); cout<<"Fifth move(1st player):"; cin>>c;s=7; l=move(c); if(l==p||l==i||l==u||l==y||l==t||l==e||l==w||l==q||l==9) goto dd; else cout<<'X'; v=check(s,p,u,t,w,l); if(v==0) goto exit; exit: if(v==1) { clrscr(); gotoxy(35,12); cout<<"GAME DRAWN"; } getch(); } int check(int s,int p,int u,int t,int w,int l) { if(s==3||s==4) { if((p==0||p==1||p==2)&&(u==0||u==1||u==2)&&(t==0||t==1||t==2)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==0||p==3||p==6)&&(u==0||u==3||u==6)&&(t==0||t==3||t==6)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==0||p==4||p==8)&&(u==0||u==4||u==8)&&(t==0||t==4||t==8)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==2||p==4||p==6)&&(u==2||u==4||u==6)&&(t==2||t==4||t==6)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==1||p==4||p==7)&&(u==1||u==4||u==7)&&(t==1||t==4||t==7)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==2||p==5||p==8)&&(u==2||u==5||u==8)&&(t==2||t==5||t==8)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==3||p==4||p==5)&&(u==3||u==4||u==5)&&(t==3||t==4||t==5)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } if((p==6||p==7||p==8)&&(u==6||u==7||u==8)&&(t==6||t==7||t==8)) {delay(800); clrscr(); if(s==3) {cout<<"Congrats!Player1 has won";return 0;} if(s==4) {cout<<"Congrats!Player2 has won";return 0;} } } if(s==5||s==6 ) { if((w==0||w==1||w==2)&&(u==0||u==1||u==2)&&(t==0||t==1||t==2)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==3||w==6)&&(u==0||u==3||u==6)&&(t==0||t==3||t==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==4||w==8)&&(u==0||u==4||u==8)&&(t==0||t==4||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==4||w==6)&&(u==2||u==4||u==6)&&(t==2||t==4||t==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==1||w==4||w==7)&&(u==1||u==4||u==7)&&(t==1||t==4||t==7)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==5||w==8)&&(u==2||u==5||u==8)&&(t==2||t==5||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==3||w==4||w==5)&&(u==3||u==4||u==5)&&(t==3||t==4||t==5)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==6||w==7||w==8)&&(u==6||u==7||u==8)&&(t==6||t==7||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } } if(s==5||s==6 ) { if((w==0||w==1||w==2)&&(p==0||p==1||p==2)&&(t==0||t==1||t==2)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==3||w==6)&&(p==0||p==3||p==6)&&(t==0||t==3||t==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==4||w==8)&&(p==0||p==4||p==8)&&(t==0||t==4||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==4||w==6)&&(p==2||p==4||p==6)&&(t==2||t==4||t==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==1||w==4||w==7)&&(p==1||p==4||p==7)&&(t==1||t==4||t==7)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==5||w==8)&&(p==2||p==5||p==8)&&(t==2||t==5||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==3||w==4||w==5)&&(p==3||p==4||p==5)&&(t==3||t==4||t==5)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==6||w==7||w==8)&&(p==6||p==7||p==8)&&(t==6||t==7||t==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } } if(s==5||s==6 ) { if((w==0||w==1||w==2)&&(p==0||p==1||p==2)&&(u==0||u==1||u==2)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==3||w==6)&&(p==0||p==3||p==6)&&(u==0||u==3||u==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==0||w==4||w==8)&&(p==0||p==4||p==8)&&(u==0||u==4||u==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==4||w==6)&&(p==2||p==4||p==6)&&(u==2||u==4||u==6)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==1||w==4||w==7)&&(p==1||p==4||p==7)&&(u==1||u==4||u==7)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==2||w==5||w==8)&&(p==2||p==5||p==8)&&(u==2||u==5||u==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==3||w==4||w==5)&&(p==3||p==4||p==5)&&(u==3||u==4||u==5)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } if((w==6||w==7||w==8)&&(p==6||p==7||p==8)&&(u==6||u==7||u==8)) {delay(800); clrscr(); if(s==5) {cout<<"Congrats!Player1 has won";return 0;} if(s==6) {cout<<"Congrats!Player2 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(p==0||p==1||p==2)&&(u==0||u==1||u==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(p==0||p==3||p==6)&&(u==0||u==3||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(p==0||p==4||p==8)&&(u==0||u==4||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(p==2||p==4||p==6)&&(u==2||u==4||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(p==1||p==4||p==7)&&(u==1||u==4||u==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(p==2||p==5||p==8)&&(u==2||u==5||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(p==3||p==4||p==5)&&(u==3||u==4||u==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(p==6||p==7||p==8)&&(u==6||u==7||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(p==0||p==1||p==2)&&(t==0||t==1||t==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(p==0||p==3||p==6)&&(t==0||t==3||t==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(p==0||p==4||p==8)&&(t==0||t==4||t==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(p==2||p==4||p==6)&&(t==2||t==4||t==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(p==1||p==4||p==7)&&(t==1||t==4||t==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(p==2||p==5||p==8)&&(t==2||t==5||t==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(p==3||p==4||p==5)&&(t==3||t==4||t==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(p==6||p==7||p==8)&&(t==6||t==7||t==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(p==0||p==1||p==2)&&(w==0||w==1||w==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(p==0||p==3||p==6)&&(w==0||w==3||w==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(p==0||p==4||p==8)&&(w==0||w==4||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(p==2||p==4||p==6)&&(w==2||w==4||w==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(p==1||p==4||p==7)&&(w==1||w==4||w==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(p==2||p==5||p==8)&&(w==2||w==5||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(p==3||p==4||p==5)&&(w==3||w==4||w==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(p==6||p==7||p==8)&&(w==6||w==7||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(t==0||t==1||t==2)&&(u==0||u==1||u==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(t==0||t==3||t==6)&&(u==0||u==3||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(t==0||t==4||t==8)&&(u==0||u==4||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(t==2||t==4||t==6)&&(u==2||u==4||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(t==1||t==4||t==7)&&(u==1||u==4||u==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(t==2||t==5||t==8)&&(u==2||u==5||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(t==3||t==4||t==5)&&(u==3||u==4||u==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(t==6||t==7||t==8)&&(u==6||u==7||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(w==0||w==1||w==2)&&(u==0||u==1||u==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(w==0||w==3||w==6)&&(u==0||u==3||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(w==0||w==4||w==8)&&(u==0||u==4||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(w==2||w==4||w==6)&&(u==2||u==4||u==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(w==1||w==4||w==7)&&(u==1||u==4||u==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(w==2||w==5||w==8)&&(u==2||u==5||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(w==3||w==4||w==5)&&(u==3||u==4||u==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(w==6||w==7||w==8)&&(u==6||u==7||u==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } if(s==7) { if((l==0||l==1||l==2)&&(t==0||t==1||t==2)&&(w==0||w==1||w==2)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==3||l==6)&&(t==0||t==3||t==6)&&(w==0||w==3||w==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==0||l==4||l==8)&&(t==0||t==4||t==8)&&(w==0||w==4||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==4||l==6)&&(t==2||t==4||t==6)&&(w==2||w==4||w==6)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==1||l==4||l==7)&&(t==1||t==4||t==7)&&(w==1||w==4||w==7)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==2||l==5||l==8)&&(t==2||t==5||t==8)&&(w==2||w==5||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==3||l==4||l==5)&&(t==3||t==4||t==5)&&(w==3||w==4||w==5)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } if((l==6||l==7||l==8)&&(t==6||t==7||t==8)&&(w==6||w==7||w==8)) {delay(800); clrscr(); {cout<<"Congrats!Player1 has won";return 0;} } } return 1; }