Ad Code

Responsive Advertisement

Tic Tac Toe Game program in C.

Tic Tac Toe Game program in C






How To implement the Tic Tac Toe Game program in C Language.

Here is the program of Tic Tac Toe Game you can easy to create this Game in C language.



please refresh again for internet problems




#include<stdio.h>
#include<stdlib.h>
void dis(){
 int k=1;
 for(int i=0; i<3; i++){
  for(int j=0; j<3; j++,k++){
   printf(" %d ",k);
  }printf("\n");
 }
}
void main(){
 int val,i,j,k=0,z=0,x=1,m=0,d=0,x1,x2,d2=0;
 char c,b;
 char a[9][9];
 int n2,n3,n4,n5;
 system("clear");
 dis();
 x2:
 for(i=0; i<3; i++){
  for(j=0; j<3; j++){
   a[i][j]='-';         /* Assign the - into the Array */
  }
 }
 do{  d2=0;
  if(z==0){
   printf("\n\nFIRST USER( o ):-Enter position: ");       /* First User */
   scanf("%d",&val);
   b='x';z=1;c='o';
  }else{
   printf("\n\nSECOND USER( x ):-Enter position: ");     /* Second User */
   scanf("%d",&val);
   b='o';z=0;c='x';
  }
  for(i=0; i<3; i++){
   for(j=0; j<3; j++){
    m=0;
    if(i*3+j==val-1){
     if(a[i][j]=='-' && a[i][j]!=b){
      a[i][j]=c;
      for(k=j; k>=0; k--){
       if(a[i][k]==c){m++;}else{k=-1;}
      }if(m==3){d=1;goto x1;break;}m=0;  
      for(k=j; k<3; k++){
       if(a[i][k]==c){m++;}else{k=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=j-1, k=j+1; n>=0,k<3; n--,k++ ){
       if(a[i][k]==c && a[i][n]==c){m=3;}else{n=-1;k=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i-1,n2=j-1,n3=i+1,n4=j+1; n>=0,n3<3; n--,n2--,n3++,n4++){
       if(a[n][n2]==c && a[n3][n4]==c){m=3;}else{n=-1;n3=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i-1,n2=j+1,n3=i+1,n4=j-1; n>=0,n2<3; n--,n2++,n3++,n4--){
       if((a[n][n2]==c)&&a[n3][n4]==c){m=3;}else{n=-1;n2=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(n2=i; n2<3; n2++){
       if(a[n2][j]==c){m++;}else{n2=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(n2=i; n2>=0; n2--){
       if(a[n2][j]==c){m++;}else{n2=-1;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i,n2=j; n<3, n2<3; n++,n2++){
       if(a[n][n2]==c){m++;}else{n2=3;n=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i,n2=j; n>=0, n2>=0; n--,n2--){
       if(a[n][n2]==c){m++;}else{n2=-1;n=-1;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i,n2=j; n<3,n2>=0; n++,n2--){
       if(a[n][n2]==c){m++;}else{n=3;n2=-1;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i,n2=j; n>=0,n2<3; n--,n2++){
       if(a[n][n2]==c){m++;}else{n=-1;n2=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
      for(int n=i-1,n2=j,n3=i+1; n>=0,n3<3; n--,n3++){
       if((a[n][n2]==c)&&(a[n3][n2])==c){m=3;}else{n=-1;n3=3;}
      }if(m==3){d=1;goto x1;break;}m=0; 
     }else{printf("\tThe value already exists \t");if(z==0){z=1;}else{z=0;}}
    }
   printf(" %c ",a[i][j]);   /* Display the Array of value. */
   }
  printf("\n");
  }
 }while(x==1);
 x1:
 if(d==1){printf("\n\n\t You win \t\n\n\t-----START AGAIN -----\n\n");d=0;z=0; goto x2;}
}
    


out put :
 
 1  2  3 
 4  5  6 
 7  8  9 


FIRST USER( o ):-Enter position: 3
 -  -  o 
 -  -  - 
 -  -  - 


SECOND USER( x ):-Enter position: 4
 -  -  o 
 x  -  - 
 -  -  - 


FIRST USER( o ):-Enter position: 1
 o  -  o 
 x  -  - 
 -  -  - 


SECOND USER( x ):-Enter position: 9
 o  -  o 
 x  -  - 
 -  -  x 


FIRST USER( o ):-Enter position: 5
 o  -  o 
 x  o  - 
 -  -  x 


SECOND USER( x ):-Enter position: 7
 o  -  o 
 x  o  - 
 x  -  x 


FIRST USER( o ):-Enter position: 2

            You win 

 ----- AGAIN -----





If you like code with vibhu  and would like to contribute, you can also write an article using   This link :

https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1


OR  mail your article to codewithvibhu@gmail.com.  See your article appearing on the code with vibhu main page and help other students. 

And also Like and comment our program👇.

Post a Comment

14 Comments

  1. too short Tic Tac toe game program nice..
    ☺️

    ReplyDelete
  2. it is better than the other websites

    Thank you...

    ReplyDelete
  3. please upload this type of games...
    👍👍👍

    ReplyDelete
  4. Nice coding.. ✌️

    ReplyDelete
  5. Oppo nice bro..

    ReplyDelete
  6. it is better for the geeksforgeeks.org and other websites..

    ReplyDelete
  7. please upload more games programs


    And thank for this...

    ReplyDelete
  8. ������������ too small program ��������

    ReplyDelete
  9. So dangerous program too crazy...

    ReplyDelete
  10. Algorithm expert bro... ������

    ReplyDelete