Tic Tac Toe in Java With Source Code

The Tic Tac Toe In Java is a Game Application developed using Graphical User Interface (GUI) in Java Programming Language.

This Tic Tac Toe In Java Code is a simple Java Project that anyone can like this Game Application because this Game Application is easy to understand and easy to play.

A Tic Tac Toe For Java you will see the approach of the game is implemented.

In this Tic Tac Toe Java Code For Beginners, two players will be playing and you have one print board on the screen where 9 boxes will be displayed on the board.

Project Name InformationProject Description
Project Name:Tic Tac Toe
Language/s Used:JAVA
Type:Desktop Application
Tic Tac Toe In Java– Project Information

Now, you have to choose X or O for the specific boxes. For example, if you have to select any boxes then X or O will be shown on the print board, and the turn for next will be there.

The task is to create a Java program to implement a 3×3 Java Tic Tac Toe 2 Player GUI.

This Game Application also includes a downloadable Tic Tac Toe In Java Source Code for free, just find the downloadable source code below and click to start downloading.

To start creating a Tic Tac Toe In Java, make sure that you have  Net Beans IDE or any platform of Java installed on your computer.

About Java code tic tac toe

You can observe how the game’s strategy is used in the game of tic tac toe.

There will be two players participating in this game, and the print board on the screen will display numbers from 1 to 9, or box numbers. You must now select either X or O for the particular box number.

For instance, if you need to choose any number, the letters X or O will appear on the print board along with the words “turn for next.” A 3×3 Tic-Tac-Toe game in Java for two players needs to be implemented in Java software.

How to make a tic tac toe board in Java?

For the sake of simplicity, this entire tutorial will be composed of a single class with multiple functions.

The creation of the board is the initial step in developing our game. To build our board, we’ll utilize a 2D array filled with dashes [-], vertical bars [|], and spaces [“”].

Let’s make and populate the game board array. In order to see the board, we will also develop a function named print Board().

How to code tic tac toe in Java?

Dashes should be placed on a Tic Tac Toe board. Create a function that squares up the board. The player’s turn and the symbol they are using should be noted.

Ask for a row and a column repeatedly until they are correct.

The general order of steps to implement:

  • Dashes should be placed on a Tic Tac Toe board.
  • Create a function that squares up the board.
  • It is the player’s turn, and the symbol they are using should be noted.
  • Ask for a row and a column repeatedly until they are correct.
  • Set the appropriate symbol in the relevant place on the board.
  • Add a function that determines who won the game.
  • Verify to see if there was a tie in the game.
  • To keep the game rolling, use a loop.

How do we do each of these steps?

  • We can make our board nxn and store its dimensions in a variable called n in our main procedure.
  • Board length and board[0] are required.
  • length rather than 3 in the nested loops of our routines.
  • Instead of testing if the row and col are more than 2, we must determine whether they are greater than or equal to n when determining whether the row and col are out of bounds.
  • The function that determines whether a player has won must also be updated so that it can accommodate boards of any size.

The general construction for the final stage is as follows:

  • Make a boolean that is true if the n characters in the row, column, or diagonal you are testing are present.
  • Make a value variable that has one of the rows, columns, or diagonals you are checking.
  • Since there is no space after the dash, you can set the boolean to false if the value is equivalent to a dash.
  • Alternatively, iterate through all the additional locations in the row, column, or diagonal that you are verifying.
  • You can set the boolean to false and exit the loop if a position does not equal the value.
  • If the boolean is still true outside of the loop, return the value.

How to play Java tic tac toe?

  • Both players can mark their cells with X or O.
  • Each of the nine cells will have a number assigned to it in a 33 grid.
  • The person who selected X gets to start playing first.
  • The cell number he wants to place X in is entered.
  • Now, O and X play alternately until one of the two of them triumphs.
  • winning standards The game is over when either of the two players has completely filled one row, column, or diagonal with his sign (X/O).
  • In the event that neither of the two players prevails, the match is deemed a draw.

How to create tic tac toe in Java?

Time needed: 5 minutes

Here are the steps for making Java tic tac toe code.

  • Step 1: Create a project.

    First, open the file and then click “project” to create.
    tic tac toe create project

  • Step 2: Create a project name.

    Second, name your project.
    tic tac toe project name

  • Step 3: Create JFrame.

    Third, create a “JFrame” form.
    tic tac toe create jframe

  • Step 4: Create a JFrame name

    Fourth, name your JFrame form.
    jframe name

  • Step 5: Project Design.

    Fifth, The actual design of the project.tic tac toe output

Downloadable Source Code Below

Anyway, if you want to level up your programming knowledge, especially Java, try this new article I’ve made for you Best Java Projects With Source Code For Beginners Free Download.


This Game Application is built fully in Java. It has a full-featured Graphical User Interface (GUI) with all the functionalities.

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

Related article below


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

Leave a Comment