Tic Tac Toe Game in Java

Tic tac toe game is very funny game . In this program we will make tic tac toe game in java. we write tic tac toe java program using multiple java classes in this example. This program for java tic tac-toe 2 player .

Tic Tac Toe Game in Java

In this example we make tic tac toe game in java using eclipse. We write our tic tac toe game logic in TicTacToe.java class . tic tac toe 2 player In this example of tic tac toe java program we will use 2d array  to manage the data. we will create a 2d array _board[] [] . because this is a tic tac toe 2 player game, So board will manage sign for both users. isGameOver() method is use for checking game over or not . You can check draw tic tac toe java code in this method .

Design Tic Tac Toe game In java

Now We will design tic tac toe game in java. We will write logic for tic tac toe game in tictactoe.java class and take input in from user in MainTicTacToe.java class .

TicTacToe.java

public class TicTacTOe {
int _row=3;
char _board[][]=new char[_row][_row];
public void makeBoard(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
_board[i][j]='o';
}

}
}
boolean isValid(int r,int c){
return((r>=0&&r<_row)&&(c>=0&&c<_row));
}
boolean isFill(int r,int c){
return(_board[r][c]!='o');
}
public boolean gamePlay(int r ,int c,int playerNo){
if (!(isValid(r,c))) {
System.out.println("Enter Correct index");
return true ;
}
if (isFill(r,c)) {
System.out.println("This index already fill");
return true;
}
if (playerNo==1) {
_board[r][c]='*';
} else {
_board[r][c]='+';
}
return false;
}

boolean horizontalAndVerticalMatch(char ch,int n){
// for row and col math
boolean flag=true;
for (int i = 0; i < _row; i++) {
flag=true;
for (int j = 0; j < _row; j++) {
System.out.println("i="+i+"j="+j);
char ch1=(n==1)?_board[i][j]:_board[j][i];
if (ch1!=ch) {
System.out.println("y");
flag=false;
break;
}
}
if (flag) {
return flag;
}
}
return flag;
}
boolean majorDiagonal(char ch){
return((_board[0][0]==ch)&&(_board[1][1]==ch)&&(_board[2][2]==ch));
}
boolean minorDiagonal(char ch){
return((_board[2][0]==ch)&&(_board[1][1]==ch)&&(_board[0][2]==ch));
}
public boolean isGameOver(int PlayerNo){
char ch;
if (PlayerNo==1) {
ch='*';
} else {
ch='+';
}
return(horizontalAndVerticalMatch(ch,1)||horizontalAndVerticalMatch(ch,2)
||majorDiagonal(ch)||minorDiagonal(ch));
}
public void display(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
System.out.print("     "+_board[i][j]);
}
System.out.println("");
}
}

}

MainTicTacToe.java class uses to take input from user for row and column of board . It is tic tac toe 2 player , so take input fro manager and manage to tic tac toe board game _board[][]_board[][] as per players input . We will check draw tic tac toe java code in isGameOver() method as per user input.

MainTicTacToe.java

import java.util.Scanner;


public class MainTicTacToe extends TicTacTOe {
 public static void main(String[] args) {
TicTacTOe obj=new TicTacTOe();
obj.makeBoard();
obj.display();
int r,c;
Scanner input=new Scanner(System.in);
while(true){
boolean flag=true;
while(flag){
System.out.println("Player 1");
System.out.println("enter row");
r=input.nextInt();
System.out.println("enter col");
c=input.nextInt();
flag=obj.gamePlay(r,c,1);
}
if (obj.isGameOver(1)) {
obj.display();
System.out.println("Player 1 Win Game");
break;
}
System.out.println("============");
obj.display();
flag=true;
while(flag){
System.out.println("Player 2");
System.out.println("enter row");
r=input.nextInt();
System.out.println("enter col");
c=input.nextInt();
flag=obj.gamePlay(r,c,2);
}
if (obj.isGameOver(2)) {
obj.display();
System.out.println("Player 2 Win Game");
break;
}
System.out.println("============");
obj.display();
}
}
}
In this program we learned how to design tic tac toe . We take input from users for building java tic tac toe 2d array . This game user can play with computer(we can say tic tac toe java code against computer). This is tic tac toe game in java for 3×3 java tic tac toe . This is tic tac toe game in java or xo game or xox game and you can play tic tac toe with the help of eclipse using this tic tac toe java program . You can see also Minesweeper game in java.

Leave a Reply

Your email address will not be published. Required fields are marked *

− 4 = 6