Tic-Tac-Toe Game in C. Code: /* ** Tic-Tac-Toe game in C ** @author: Tanaz Kerawala ** @author: Pradeep ** @date: 11/12/06 */ #include<conio.h> #include<dos.h> #include<stdio.h> /* ** Creates the playing box */ void box() { clrscr(); textcolor(78); gotoxy(25,9); cprintf(" TIC TAC TOE "); textcolor(10); gotoxy(25,10); cprintf("ÉÍÍÍÍÍÍËÍÍÍÍÍÍËÍÍÍÍÍÍ»"); gotoxy(25,11); cprintf("º º º º"); gotoxy(25,12); cprintf("º º º º"); gotoxy(25,13); cprintf("ÌÍÍÍÍÍÍÎÍÍÍÍÍÍÎÍÍÍÍÍ͹"); gotoxy(25,14); cprintf("º º º º"); gotoxy(25,15); cprintf("º º º º"); gotoxy(25,16); cprintf("ÌÍÍÍÍÍÍÎÍÍÍÍÍÍÎÍÍÍÍÍ͹"); gotoxy(25,17); cprintf("º º º º"); gotoxy(25,18); cprintf("º º º º"); gotoxy(25,19); cprintf("ÈÍÍÍÍÍÍÊÍÍÍÍÍÍÊÍÍÍÍÍͼ"); gotoxy(28,12); } /* ** Handles the cursor and keys */ void cursor() { char ch=' '; int x=28; int y=12; int a[2]; char t[3][3]; char z='o'; char s; int j,k; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } while(ch!='q') { ch=getch(); if(ch==0) { ch=getch(); } gotoxy(x,y); a[0]=x; a[1]=y; switch(ch) { case'H': if(y>12) { y=y-3; } gotoxy(a[0],a[1]-3); break; case'P': if(y<18) { y=y+3; } gotoxy(a[0],a[1]+3); break; case'K': if(x>28) { x=x-7; } gotoxy(a[0]-7,a[1]); break; case'M': if(x<42) { x=x+7; } gotoxy(a[0]+7,a[1]); break; case'x': if(z!='x') { z=ch; if(a[0]==28&&a[1]==12) { if(t[0][0]==' ') { printf("x"); t[0][0]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==28&&a[1]==15) { if(t[1][0]==' ') { printf("x"); t[1][0]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==28&&a[1]==18) { if(t[2][0]==' ') { printf("x"); t[2][0]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==12) { if(t[0][1]==' ') { printf("x"); t[0][1]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==15) { if(t[1][1]==' ') { printf("x"); t[1][1]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==18) { if(t[2][1]==' ') { printf("x"); t[2][1]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==12) { if(t[0][2]==' ') { printf("x"); t[0][2]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==15) { if(t[1][2]==' ') { printf("x"); t[1][2]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==18) { if(t[2][2]==' ') { printf("x"); t[2][2]='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'x' "); z='o'; gotoxy(a[0],a[1]); } } if(t[0][0]=='x') { if(t[0][1]=='x') { if(t[0][2]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[0][0]=='x') { if(t[1][0]=='x') { if(t[2][0]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[0][0]=='x') { if(t[1][1]=='x') { if(t[2][2]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[0][2]=='x') { if(t[1][2]=='x') { if(t[2][2]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][2]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[2][0]=='x') { if(t[2][1]=='x') { if(t[2][2]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[2][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[0][2]=='x') { if(t[1][1]=='x') { if(t[2][0]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][2]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[0][1]=='x') { if(t[1][1]=='x') { if(t[2][1]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][1]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } if(t[1][0]=='x') { if(t[1][1]=='x') { if(t[1][2]=='x') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[1][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } } else { gotoxy(25,3); printf("this move is not allowed turn of player 'o'"); sound(500); delay(1000); nosound(); getch(); gotoxy(25,3); printf(" "); z='x'; gotoxy(25,3); printf("turn of player 'o'"); gotoxy(a[0],a[1]); } if(t[0][0]!=' ') { if(t[0][1]!=' ') { if(t[0][2]!=' ') { if(t[1][0]!=' ') { if(t[1][1]!=' ') { if(t[1][2]!=' ') { if(t[2][0]!=' ') { if(t[2][1]!=' ') { if(t[2][2]!=' ') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf("game over"); gotoxy(25,4); printf("to start new game click G"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } box(); z='o'; gotoxy(25,3); printf("turn of player 'x'"); } else { ch='q'; } } } } } } } } } } break; case 'o': if(z!='o') { z=ch; if(a[0]==28&&a[1]==12) { if(t[0][0]==' ') { printf("o"); t[0][0]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==12) { if(t[0][1]==' ') { printf("o"); t[0][1]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==12) { if(t[0][2]==' ') { printf("o"); t[0][2]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[0][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==28&&a[1]==15) { if(t[1][0]==' ') { printf("o"); t[1][0]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==15) { if(t[1][1]==' ') { printf("o"); t[1][1]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==15) { if(t[1][2]==' ') { printf("o"); t[1][2]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[1][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==28&&a[1]==18) { if(t[2][0]==' ') { printf("o"); t[2][0]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][0]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==35&&a[1]==18) { if(t[2][1]==' ') { printf("o"); t[2][1]='o'; gotoxy(25,3); printf("trun of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][1]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } else if(a[0]==42&&a[1]==18) { if(t[2][2]==' ') { printf("o"); t[2][2]='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } else { gotoxy(25,3); printf("this move is not allowed at t[2][2]"); sound(700); delay(1000); nosound(); getch(); gotoxy(25,3); printf("turn of player 'o' "); z='x'; gotoxy(a[0],a[1]); } } if(t[0][0]=='o') { if(t[0][1]=='o') { if(t[0][2]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[0][0]=='o') { if(t[1][0]=='o') { if(t[2][0]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[0][0]=='o') { if(t[1][1]=='o') { if(t[2][2]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[0][2]=='o') { if(t[1][2]=='o') { if(t[2][2]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][2]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[2][0]=='o') { if(t[2][1]=='o') { if(t[2][2]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[2][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[0][2]=='o') { if(t[1][1]=='o') { if(t[2][0]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][2]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[0][1]=='o') { if(t[1][1]=='o') { if(t[2][1]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[0][1]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } if(t[1][0]=='o') { if(t[1][1]=='o') { if(t[1][2]=='o') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf(" %c is the winner",t[1][0]); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } } else { gotoxy(25,3); printf("this move is not allowed turn of player 'x'"); sound(500); delay(1000); nosound(); getch(); gotoxy(25,3); printf(" "); z='o'; gotoxy(25,3); printf("turn of player 'x'"); gotoxy(a[0],a[1]); } if(t[0][0]!=' ') { if(t[0][1]!=' ') { if(t[0][2]!=' ') { if(t[1][0]!=' ') { if(t[1][1]!=' ') { if(t[1][2]!=' ') { if(t[2][0]!=' ') { if(t[2][1]!=' ') { if(t[2][2]!=' ') { sound(1000); delay(1500); nosound(); gotoxy(25,3); printf("game over"); gotoxy(25,4); printf("press any key to start new game"); gotoxy(25,5); printf("or click Q to exit"); scanf("%c",&s); if(s!='q') { for(j=0;j<3;j++) { for(k=0;k<3;k++) { t[j][k]=' '; } } z='o'; box(); gotoxy(25,3); printf("turn of player 'x'"); gotoxy(x,y); } else { ch='q'; } } } } } } } } } } break; case 't': gotoxy(3,3); for(j=0;j<3;j++) { for(k=0;k<3;k++) { printf("t[%d][%d]=%c\n",j,k,t[j][k]); } } break; default: if(ch!='q') { gotoxy(25,3); printf("press proper keys"); gotoxy(a[0],a[1]); sound(700); delay(1000); nosound(); getch(); } else { gotoxy(25,3); printf("press any key to quit"); sound(1000); delay(1000); nosound(); getch(); } break; } } } void main() { clrscr(); box(); cursor(); }