Tic Tac Toe Java With Source Code

The Tic Tac Toe In Java is a game board with two-person paper-and-pencil game in which each player play the game alternates marking squares in a three-by-three grid with an X or an O.

The winner is the player who successfully places three of their markers in a horizontal, vertical, or diagonal row. It’s a solved game with a forced draw if both players play their best.

What is Tic Tac Toe in Java?

A Tic Tac Toe is a game in which two players alternately place the marks X and O in one of the nine places on a three-by-three grid.

In this game, two players will be played and you have one print board on the screen where from 1 to 9 number will be displayed or you can say it box number.

Now, you have to choose X or O for the specific box number. For example, if you have to select any number then for X or O will be shown on the print board, and turn for next will be there. The task is to create a Java program to implement a 3×3 Tic-Tac-Toe game for two players.

How Can You Tell If Someone Has Won Java Tic Tac Toe?

In Java, Tic Tac Toe, a player wins if they have 3 of their symbols in one row, column, or diagonal. Let’s start with rows with empty cells. We can use a for loop to iterate through each row i . Inside the for loop, we can use a conditional to check if board[i][0] equals board[i][1] and if board[i][1] equals board[i][2] .

Importance of Tic Tac Toe Game In Java

To help children enhance their attentiveness, play systems have included alphabet and tic-tac-toe panels. By encouraging greater social engagement through collaborative play, these tic-tac-toe panels improve hand-eye coordination and stimulate improved social interaction. Also, kids shouldn’t merely play with other kids.

Tic Tac Toe Game Project Details and Technology

Project Name:Tic Tac Toe Project In Java With Source Code
AbstractTic Tac Toe is game in which two players seek in alternate turns to complete a row, a column, or a diagonal with either three O’s or three X’s drawn in the spaces of a grid of nine squares.
Language/s Used:JAVA
JAVA version (Recommended):8
Database:None
Type:Desktop Application
Developer:Source Code Hero
Updates:0
Tic Tac Toe Project In Java With Source Code – Project Information

About Tic Tac Toe Game Project

This Tic Tac Toe In Java is a Game Application created with the Java Programming Language’s Graphical User Interface (GUI). This Tic Tac Toe is a simple Project that anybody can enjoy because it is simple to comprehend and play.

A Java Code Tic Tac Toe board you will see the approach of the game is implemented. In this Tic Tac Toe Code For Beginners, two players will be played and you have one print board on the screen where 9 boxes will be displayed. Now, you have to choose X or O for the specific boxes.

For example, if you have to select any boxes then for X or O will be shown on the print public class board row col, and turn for next will be there. The task is to create a program to implement a 3×3 Tic Tac Toe 2 Player GUI.

How To Code Tic Tac Toe In Java

Here’s the complete source code on how to create a tic tac toe in java using console based.

import java.util.Scanner;
#import the scanner


public class emp
{
    private int counter;
    private   char posn[]=new char[10];
    private   char player;
    
    
    public static void main(String args[])
    {
        String ch;
        emp Toe=new emp();
        do{
            Toe.newBoard();
            Toe.play();
            System.out.println ("Would you like to play again (Enter 'yes')? ");
            Scanner in =new Scanner(System.in);
            ch=in.nextLine();
            System.out.println("ch value is  "+ch);
        }while (ch.equals("yes"));
        
        
    }
    public  void newBoard()
    {
        
        char posndef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
        int i;
        counter = 0;
        player = 'X';
        for (i=1; i<10; i++) posn[i]=posndef[i];
        currentBoard();
        
        
    }
    public  String currentBoard()
    {
        System.out.println( "\n\n" );
        System.out.println(  "\n\n" );
        System.out.println(  "\n\n\t\t" + posn [1] + "   | " +posn [2]+ "  | " +posn [3]);
        System.out.println(  " \t\t    |    |   " );
        System.out.println(  " \t\t ___|____|___ " );
        System.out.println(  "\n\n\t\t" +posn [4]+ "   | " +posn [5]+ "  | " +posn [6]);
        System.out.println(  " \t\t    |    |   " );
        System.out.println(  " \t\t ___|____|___ " );
        System.out.println(  "\n\n\t\t" +posn [7]+ "   | " +posn [8]+ "  | " +posn [9]);
        System.out.println(  " \t\t    |    |   " );
        System.out.println(  " \t\t    |    |   " );
        System.out.println(  "\n\n" );
        return "currentBoard";
    }
    
    public  void play()
    {
        int spot;
        char blank = ' ';
        
        System.out.println(  "Player " + getPlayer() +" will go first and be the letter 'X'" );
        
        do {
            currentBoard();              // display current board
            
            System.out.println(  "\n\n Player " + getPlayer() +" choose a posn." );
            
            boolean posTaken = true;
            while (posTaken) {
                // System.out.println( "position is taken, please enter a valid space");
                Scanner in =new Scanner (System.in);
                spot=in.nextInt();
                posTaken = checkPosn(spot);
                if(posTaken==false)
                posn[spot]=getPlayer();
            }
            
            System.out.println(  "Nice move." );
            
            currentBoard();              // display current board
            
            nextPlayer();
        }while ( checkWinner() == blank );
        
    }
    
    public  char checkWinner()
    {
        char Winner = ' ';
        
        // Check if X wins
        if (posn[1] == 'X' && posn[2] == 'X' && posn[3] == 'X') Winner = 'X';
        if (posn[4] == 'X' && posn[5] == 'X' && posn[6] == 'X') Winner = 'X';
        if (posn[7] == 'X' && posn[8] == 'X' && posn[9] == 'X') Winner = 'X';
        if (posn[1] == 'X' && posn[4] == 'X' && posn[7] == 'X') Winner = 'X';
        if (posn[2] == 'X' && posn[5] == 'X' && posn[8] == 'X') Winner = 'X';
        if (posn[3] == 'X' && posn[6] == 'X' && posn[9] == 'X') Winner = 'X';
        if (posn[1] == 'X' && posn[5] == 'X' && posn[9] == 'X') Winner = 'X';
        if (posn[3] == 'X' && posn[5] == 'X' && posn[7] == 'X') Winner = 'X';
        if (Winner == 'X' )
        {System.out.println("Player1 wins the game." );
            return Winner;
        }
        
        // Check if O wins
        if (posn[1] == 'O' && posn[2] == 'O' && posn[3] == 'O') Winner = 'O';
        if (posn[4] == 'O' && posn[5] == 'O' && posn[6] == 'O') Winner = 'O';
        if (posn[7] == 'O' && posn[8] == 'O' && posn[9] == 'O') Winner = 'O';
        if (posn[1] == 'O' && posn[4] == 'O' && posn[7] == 'O') Winner = 'O';
        if (posn[2] == 'O' && posn[5] == 'O' && posn[8] == 'O') Winner = 'O';
        if (posn[3] == 'O' && posn[6] == 'O' && posn[9] == 'O') Winner = 'O';
        if (posn[1] == 'O' && posn[5] == 'O' && posn[9] == 'O') Winner = 'O';
        if (posn[3] == 'O' && posn[5] == 'O' && posn[7] == 'O') Winner = 'O';
        if (Winner == 'O' )
        {
            System.out.println( "Player2 wins the game." );
        return Winner; }
        
        // check for Tie
        for(int i=1;i<10;i++)
        {
            if(posn[i]=='X' || posn[i]=='O')
            {
                if(i==9)
                {
                    char Draw='D';
                    System.out.println(" Game is stalemate ");
                    return Draw;
                }
                continue;
            }
            else
            break;
            
        }
        
        return Winner;
    }
    
    public  boolean checkPosn(int spot)
    {
        
        
        if (posn[spot] == 'X' || posn[spot] == 'O')
        {
            System.out.println("That posn is already taken, please choose another");
            return true;
        }
        else {
            return false;
        }
        
        //  counter++;
        //    return false;
    }
    
    
    
    public  void nextPlayer()
    {
        if (player == 'X')
        player = 'O';
        else player = 'X';
        
    }
    
    public String getTitle()
    {
        return "Tic Tac Toe" ;
    }
    
    public  char getPlayer()
    {
        return player;
    }
    
}

To start executing a Java Project, makes sure that you have NetBeans IDE or any platform of Java installed in your computer.

Steps On How To Run The Tic Tac Toe Java With Source Code

Time needed: 5 minutes.

These are the step on how to run Tic Tac Toe Java With Source Code.

  • Step 1: Download source code.

    First, download the source code given below.
    tic tac toe download source code

  • Step 2: Extract file.

    Next, after you finished download the source code, extract the zip file.
    tic tac toe extract file

  • Step 3: Click open project.

    Next, open netbeans IDE and click open project and choose your download source code.
    tic tac toe open project

  • Step 4: Run the project.

    Next, right click the project folder and click run.
    tic tac toe run project

Downloadable Source Code Here!

Summary

The Project With Source Code is built fully in Java. It has full-featured Graphical User Interface (GUI) with all the functionalities

This Article is the way to enhance and develop our skills and logic ideas which is important in practicing the Java programming language which is most well known and most usable programming language in many company.

As a result, this System is a basic project for all beginning and intermediate JAVA users who want to broaden their understanding of JAVA Desktop applications. Finally, the entire JAVA project with open source code is an absolute project and a valuable way for users to understand and explore more about it.

Inquiries

If you have any questions or suggestions about Tic Tac Toe Java With Source Code, please feel free to leave a comment below.

Leave a Comment