Tic-Tac-Toe Game in Java

Last Updated : 30 Sep, 2026

Tic-Tac-Toe is a simple two-player game played on a 3×3 grid. Players take turns placing X and O on the board, with the goal of getting three identical marks in a row, column, or diagonal. In this project, we will build a console-based Tic-Tac-Toe game in Java for two players.

  • Uses a 3×3 board with positions numbered from 1 to 9.
  • Players take turns selecting a position to place their mark.
  • The game continues until a player wins or all positions are filled, resulting in a tie.

Prerequisites

Game Board Layout

The game board layout will look something like this:

      |---|---|---|        
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

How to Play the Game

The steps to play the game are listed below:

  • Both players choose either X or O to mark their cells.
  • There will be a 3x3 grid with numbers assigned to each of the 9 cells.
  • The player who chooses X begins to play first.
  • The player enters the cell number where they wish to place X.
  • Now, both O and X play alternatively until one of the two wins.
  • Winning criteria: Whenever any of the two players has fully filled one row/ column/ diagonal with their symbol (X/ O), they win and the game ends.
  • If neither of the two players wins, the game is said to have ended in a draw.

Sample Input: Enter a slot number to place X in: 3 

Sample Output:

      |---|---|---|        
| 1 | 2 | X |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

Sample Input:

Now, O's turn, Enter a slot number to place O in: 5

Sample Output:

      |---|---|---|        
| 1 | 2 | X |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|

So, like this game will be continued.

Project Implementation

Java
import java.util.*;

public class Geeks {

    static String[] board;
    static String turn;

    // CheckWinner method will decide the winner
    static String checkWinner() {
        for (int a = 0; a < 8; a++) {
            String line = null;

            switch (a) {
                case 0:
                    line = board[0] + board[1] + board[2];
                    break;
                case 1:
                    line = board[3] + board[4] + board[5];
                    break;
                case 2:
                    line = board[6] + board[7] + board[8];
                    break;
                case 3:
                    line = board[0] + board[3] + board[6];
                    break;
                case 4:
                    line = board[1] + board[4] + board[7];
                    break;
                case 5:
                    line = board[2] + board[5] + board[8];
                    break;
                case 6:
                    line = board[0] + board[4] + board[8];
                    break;
                case 7:
                    line = board[2] + board[4] + board[6];
                    break;
            }

            // For X winner
            if (line.equals("XXX")) {
                return "X";
            }

            // For O winner
            else if (line.equals("OOO")) {
                return "O";
            }
        }

        for (int a = 0; a < 9; a++) {
            if (Arrays.asList(board).contains(String.valueOf(a + 1))) {
                break;
            } else if (a == 8) {
                return "draw";
            }
        }

        System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");
        return null;
    }

    // To print the board
    static void printBoard() {
        System.out.println("|---|---|---|");
        System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |");
        System.out.println("|---|---|---|");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        board = new String[9];
        turn = "X";
        String winner = null;

        for (int a = 0; a < 9; a++) {
            board[a] = String.valueOf(a + 1);
        }

        System.out.println("Welcome to 3x3 Tic Tac Toe.");
        printBoard();
        System.out.println("X will play first. Enter a slot number to place X in:");

        while (winner == null) {
            int numInput;

            try {
                numInput = in.nextInt();

                // Check range
                if (!(numInput > 0 && numInput <= 9)) {
                    System.out.println("Invalid input; re-enter slot number:");
                    continue;
                }

                // Check if slot is available
                if (board[numInput - 1].equals(String.valueOf(numInput))) {
                    board[numInput - 1] = turn;

                    // Toggle turn
                    turn = turn.equals("X") ? "O" : "X";

                    printBoard();
                    winner = checkWinner();
                } else {
                    System.out.println("Slot already taken; re-enter slot number:");
                }

            } catch (InputMismatchException e) {
                System.out.println("Invalid input; re-enter slot number:");
                in.nextLine(); // Consume invalid input to prevent infinite loop
            }
        }

        // Final result
        if (winner.equalsIgnoreCase("draw")) {
            System.out.println("It's a draw! Thanks for playing.");
        } else {
            System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
        }

        in.close();
    }
}

Output:

Below is the output of the above program :
Welcome to 3x3 Tic Tac Toe.
|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X will play first. Enter a slot number to place X in:
3
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
5
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
6
|---|---|---|
| 1 | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
1
|---|---|---|
| O | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
9
|---|---|---|
| O | 2 | X |
|-----------|
| 4 | O | X |
|-----------|
| 7 | 8 | X |
|---|---|---|
Congratulations! X's have won! Thanks for playing.

Steps to Run on IntelliJ IDE

  1. Open the project in IntelliJ IDEA.
  2. Open the Java class containing the main() method.
  3. Right-click inside the class or on the class file.
  4. Select Run 'Geeks.main()'.
  5. The game will start in the Run console.

IntelliJ IDEA automatically saves changes when required.

We can check below screenshot for your reference.

output
Running the code
Comment