Write Your Own Tic Tac Toe Game in Java

Introduction: Write Your Own Tic Tac Toe Game in Java

About: I'm a high school senior who is very interested in coding, robotics, drawing, dancing, video making, and photography.

I am sure all of you know about the classic game of Tic Tic Toe. Since my elementary school years, Tic Tac Toe was a popular game I used to play with my friends. I have always been fascinated with the simplicity of the game. In my freshman year, my Java class required me to create a game, an interactive one. Tic Tac Toe was the first one that came to my mind. It took me a few weeks to develop this program and I wanted to share this with all of you. Hope you all get to create this program and share with me your journey.

Happy Tic Tac Toeing :)

Here are simple steps to write your own tic tac toe program in java.

Step 1: Understanding the High Level Flow

Here is the setup:

Tic tac toe board represented by a two dimensional character array. There are 3 rows and 3 columns, numbered 0 through 2 each way.

There are 2 players. The user and computer. The player has one action. Pick the next best cell on the board.

There is a gaming module which controls the next steps and the decision making.

Here is how the Tic Tac Toe program works:

1. Initialize board

2. Repeat steps below in a loop:

a. Get move from user (Player must enter move in array index format [row][column])

b. Check if the move is valid

c. Mark the chosen move the board

d. Check game status. Break from the loop if the game has ended.

e. Get move from Computer

f. Check game status. Break from the loop if the game has ended.

3. Declare game result (User wins/Computer Wins/ Draw)

Attached image shows these high level steps. Please see attached java project for the full code.

Step 2: Downloading the IDE

Download and Install the IDE (Interactive Development Environment). Eclipse is the IDE used in this tutorial and can be using this link below:


Choose the version that best suits your operating system and configuration.

Save eclipse to desire location and choose work-space.

Step 3: Setting Up Eclipse

1) Once Eclipse is open, a blank screen should appear.

2) Create a New Java Project: Go to File > New > Java Project.

3) Name File ("Tic_Tac_Toe").

4) Create a New Java Class Inside created Java Project: Go to Tic_Tac_Toe > src > New > Class.

5) An empty class should open up.

Step 4: Create Your Tic_Tac_Toe Class As Shown Below

Start adding code to your Tic_Tac_Toe class as shown in attached file.

High is the main logic of the program and how the class is organized:

Class TicTacToe

Board board = new char[2][2];




String userMove = getUserMove();

markMoveOnBoard('X', userMove);

gameStatus = getGameStatus('X', board);

if (!gameStatus.equals("InProgress"))




String computerMove = getComputerMove();

markMoveOnBoard('O', computerMove);

gameStatus = getGameStatus('O', board);

if (!gameStatus.equals("InProgress"))






Step 5: Run the Program

Run the program by clicking Run -> Run menu (or by Ctrl + F11) and start playing the Tic Tac Toe game.

You will be playing against the computer. When prompted for user move, provide your move in the format [row][column]

For example: [2][1] represents the third row and second column.

Keep playing until the game ends (User Wins, Computer Wins or Draw).

Step 6: Output

See attached image how the output looks while you play.

You have successfully learnt how to write a tic tac toe program in java!

Be the First to Share


    • Baking Contest

      Baking Contest
    • Clocks Contest

      Clocks Contest
    • Make it Glow Contest

      Make it Glow Contest



    2 years ago

    I wonder if you will see this comment .....what if you wanted the 2 players to use numbers instead of X and O . like player 1 uses odd numbers only and player 2 uses even numbers

    DIY Hacks and How Tos

    Nice project. We don't get a lot of java projects on the site. Thanks for sharing.