VOOZH about

URL: https://www.geeksforgeeks.org/java/tic-tac-toe-game-in-java/

⇱ Tic-Tac-Toe Game in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tic-Tac-Toe Game in Java

Last Updated : 24 Apr, 2026

Tic-Tac-Toe is a simple two-player game played on a 3Γ—3 grid. Players take turns placing X and O, aiming to get three marks in a row horizontally, vertically, or diagonally. In this project, we will build a console-based Java program that allows two players to play the game. continues until someone wins or it ends in a tie.

  • 3Γ—3 board numbered from 1 to 9 for easy position selection
  • Players take turns entering a number to place their mark
  • Board updates after every move until someone wins or the game ends 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.

Before you begin coding the project, it’s important to understand two key concepts used throughout:

Arrays: Arrays are used to store multiple values in a single variable. In this Tic-Tac-Toe game, we use a 1D array of size 9 to represent the 3x3 board, with each slot initialized to numbers 1–9.

Example:

// Creating a board with 9 positions
String[] board = new String[9];

// Filling the board with slot numbers (1 to 9)
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}

Conditionals (if, switch): Conditional statements help the program decide what to do next.

For Example:

  • if is used to validate input, check for a winner, or alternate turns.
  • switch is used in the checkWinner() method to determine all possible winning combinations.

Examples:

if statement:

// Example of an if condition to validate move

if (board[numInput - 1].equals(String.valueOf(numInput))) {

board[numInput - 1] = turn;

} else {

System.out.println("Slot already taken; re-enter slot number:");

}

switch case:

// Example of a switch statement used to check winning lines

switch (a) {

case 0:

line = board[0] + board[1] + board[2];

break;

case 1:

line = board[3] + board[4] + board[5];

break;

// ... other cases to check rows, columns, and diagonals

}

Project Implementation

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

  • Open IntelliJ IDE.
  • Create a New Java project.
  • Right-click on the src folder and create a new class like a class named Geeks.
  • Now, write your source code and ctrl+s to save it.
  • Now, to execute the program right-click the src folder and click on run as Java application.

We can check below screenshot for your reference.

πŸ‘ output
Running the code
Comment
Article Tags:
Article Tags: