Tic-Tac-Toe Game by using java.

Hey! everyone here we are going to explain and making  one of the most exiting and popular game i.e Tic-Tac-Toe   (criss-cross game ) in the language of java or we can say we are going to make it by using java program.

   =>  Rules for playing Tic-Tac-Toe game:-
         1.  At a time only two players are allowed to play this game.
2. Both the players must have to use the different sign with each other(i.e ‘X’,’O’).
3. A player wins if the sign given by him\her occupied the three consecutive space on the board.
4. If the sign of both player is present on the same line either horizontally or vertically then that line will blocked.
5. If the number of line blocked is equal to eight then the game end with no result or it became draw .

Code for this Game by using java code:-

import java.util.Scanner;
public class TicTacToe {
int [][]board=new  int [3][3];
String p1,p2;
int current x,current y,current_playerno;
String currentplayername;
public TicTacToe()
{
current_playerno=2;
Scanner s=new Scanner(System.in);
System.out.println(“Enter the first player name=”);
p1=s.nextLine();
System.out.println(“Enter the Second player name=”);
p2=s.nextLine();  

nextPlayer();
}

public void play()
{ print_board();
while(true)
{

read_move();
print_board();
int status=gameState();
if(status==1)
{
System.out.println(p1 + ” wins”);
return; 
}
if(status==2)
{
System.out.println(p2 + ” wins”);
return;
}
if(status==3) 
{
System.out.println(“Game draw”);
return;
}
nextPlayer(); 
}
}
public void nextPlayer()
{
current_playerno=3-current_playerno;
if(current_playerno==1)
currentplayername=p1;
else
currentplayername=p2;
}
public void print_board()
{

for(int i=0;i<=board.length-1;i++)
{
for(int j=0;j<=board.length-1;j++)
if(board[i][j]==0)
System.out.print(“_”);
else
if(board[i][j]==1)
System.out.print(“O”);
else
if(board[i][j]==2)
System.out.print(“X”);
System.out.println();
}

}
public void read_move()
{
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println(“Enter the x and y position ” + currentplayername);
current x=s.nextInt();
current y=s.nextInt();
if(current x<0 || current x>2||current y>2 || current y<0)
{
System.out.println(“Sorry!! out of Range please enter the right position”);
continue;
}
if(board[current y][current x]!=0)
{
System.out.println(“Sorry!! already occupied. Try again”);
continue;
}

board[current y][current x]=current_playerno;
break;
}

}
public int gameState()
{
//0 for continue
// 1 for p1 wins
//2 for p2 wins
//3 for blocked
int blockedlines=0,p1,p2;
//********************************Top Line*************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[0][i]==1)
p1++;
if(board[0][i]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;
//*************************Second line*************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[1][i]==1)
p1++;
if(board[1][i]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;

//************************third line*************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[2][i]==1)
p1++;
if(board [2][i]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;
//************************first column********************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[i][0]==1)
p1++;
if(board[i][0]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;
//**************************************Second column**********************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[i][1]==1)
p1++;
if(board[i][1]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;
//**************************************Third column**************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[i][2]==1)
p1++;
if(board[i][2]==2)
p2++;
}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;

//*************************************left to right diagonal*******************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[i][i]==1)
p1++;
if(board[i][i]==2)
p2++;

}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;

//***************************************Right to left diagonal**********************************************************
p1=p2=0;
for(int i=0;i<=2;i++)
{
if(board[i][2-i]==1)
p1++;
if(board[i][2-i]==2)
p2++;

}
if(p1==3)
return 1;
if(p2==3)
return 2;
if(p1>0 && p2>0)
blockedlines++;
//***************************************************************************************************************
if(blockedlines==8)
return 3;
return 0;

}
public static void main(String[] args) {
TicTacToe t=new TicTacToe();
t.play();
}

}

   Output :-

 

To be continued . . .

 

 

 

 

 

Leave a Reply