Ad Code

Responsive Advertisement

Tic Tac Toe Game Program In Java.

Tic Tac Toe Game program in Java.






How To implement the Tic Tac Toe Game program in Java.

it is easy to implement the Tic Tac Game in Java Here you can follow the program and you can also copy that program.



please refresh again for internet problems




/* If You Wanna Take Credits For This Code, Please Look Yourself Again ! */
import java.util.*;      /* Import All The Package*/
class tic{
 public static void main(String[] argc){  /* Main Function */
  tic obj=new tic();      /* Create the class object*/ 
  System.out.print("\tInstration to entered the Possition\n\n");
  for(int i=0,k=1; i<3; i++){
   for(int j=0; j<3; j++,k++){
    System.out.print("\t "+k+" ");
   }System.out.print("\n");
  }
  int a=obj.game();
  if(a==1){System.out.print("\n\n\t----- You winn ----- \n\n");}  /* If the Function Return 1 then The Condition will True.*/
 }
 public static int game(){           /* Create the Function */
  Scanner input =new Scanner(System.in);
  int val,i,j,k=0,z=0,x=1,m=0,d=0,x1,x2,d2=0,n,n2,n3,n4,n5;
  char c,b;
  char[][] a= new char[9][9];
  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){
    System.out.print("\n\nFIRST USER( o ):-Enter position: ");
    val=input.nextInt();
    b='x';z=1;c='o';
   }else{
    System.out.print("\n\nSECOND USER( x ):-Enter position: ");
    val=input.nextInt();
    b='o';z=0;c='x';
   }
   for(i=0; i<3; i++){               // Here we check all the condtion
    for(j=0; j<3; j++){              //  if the condition will true
     m=0;                // then the funciton Return 1 
     if(i*3+j==val-1){             // And the Funciton will Stop.
      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;return 1;}m=0;  
       for(k=j; k<3; k++){
        if(a[i][k]==c){m++;}else{k=3;}
       }if(m==3){d=1;return 1;}m=0; 
       for( 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;return 1;}m=0; 
       for( n=i-1,n2=j-1,n3=i+1,n4=j+1; (n>=0 && n2>=0) && (n3<3 && n4<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;return 1;}m=0; 
       for( n=i-1,n2=j+1,n3=i+1,n4=j-1; (n>=0 && n2<3) && (n3<3 && n4>=0); 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;return 1;}m=0; 
       for(n2=i; n2<3; n2++){
        if(a[n2][j]==c){m++;}else{n2=3;}
       }if(m==3){d=1;return 1;}m=0; 
       for(n2=i; n2>=0; n2--){
        if(a[n2][j]==c){m++;}else{n2=-1;}
       }if(m==3){d=1;return 1;}m=0; 
       for(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;return 1;}m=0; 
       for( 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;return 1;}m=0; 
       for( 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;return 1;}m=0; 
       for( 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;return 1;}m=0; 
       for( 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;return 1;}m=0; 
      }else{System.out.print("\t\nThe value already exists \t\n\n--");if(z==0){z=1;}else{z=0;}}     /* If User Intered Any Wrong Value .*/
     }
    System.out.print("\t "+a[i][j]+" ");   
    }
   System.out.print("\n");
   }
  }while(x==1);
 return 1;
 }
}



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 -----


Tic Tac Toe Game program in Java.

Download the programs file:

https://drive.google.com/file/d/1cYGuJPE5SvWVEV3eR-zFKb3T1ONvAwQL/view?usp=drivesdk




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

7 Comments

  1. Really it is a too small program ��

    ReplyDelete
  2. https://www.google.com/search?q=image&client=ubuntu&tbm=isch&tbs=rimg:CSY6uWuaORvNImDp4Naqr7sEtkKu4Zmw3YJuBrXIyKK93MoCELL9zB4zqZAqRwcNpiOR9pbDBS7I2tWN7uW-GEn3BKrAtv4ksDrX-9bzJWaNjiHGuNFAzdxO-nT_1WmQ7QMAJxSIZFRoIE-UqEgnp4Naqr7sEthEyiBD2_1UtnOioSCUKu4Zmw3YJuEQbKB9FSNwXEKhIJBrXIyKK93MoREdgqNlg5yP0qEgkCELL9zB4zqRHJhdSkQhPL_1ioSCZAqRwcNpiOREVxCWS5SIsCfKhIJ9pbDBS7I2tURfHQXWG_1qsFsqEgmN7uW-GEn3BBHlEFCwB8wu7SoSCarAtv4ksDrXEc8LEZkXEj7rKhIJ-9bzJWaNjiERRxxcTRb4-0IqEgnGuNFAzdxO-hGji5XpjWZQOCoSCXT_1WmQ7QMAJEfX8dO8dw9znKhIJxSIZFRoIE-URjBhdTGvjLfI&tbo=u&sa=X&ved=2ahUKEwjYkaah4Z3mAhWQ8XMBHc-IAKoQuIIBegQIARAs&biw=1299&bih=639&dpr=1

    ReplyDelete