Tic-Tac-Toe is a classic game that two people can enjoy together. It is played on a 3x3 grid where players take turns placing their marks, X or O, in empty spots. The main goal is to get three of the same marks in a row-horizontally, vertically, or diagonally.
In this article, we are going to build a Java program that lets two players play the Tic-Tac-Toe game right from the console. There will be a board with numbers from 1 to 9, and we have to select a spot. And when it's our turn, we have to choose a number to put the mark there, and the board will update after every move, and the game continues until someone wins or it ends in a tie.
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.
- He enters the cell number where he wishes 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
Java
// A simple Java program to demonstrate
// Tic-Tac-Toe Game
import java.util.*;
public class Geeks {
static String[] board;
static String turn;
// CheckWinner method will
// decide the combination
// of three box given below.
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";
}
}
// To enter the X Or O at the exact place on board
System.out.println(
turn + "'s turn; enter a slot number to place "
+ turn + " in:");
return null;
}
// To print out the board
/* |---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|*/
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;
// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
// This game has two player x and O.
// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;
if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}
printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}
// If no one win or lose from both player x and O.
// then here is the logic to print "draw".
if (winner.equalsIgnoreCase("draw")) {
System.out.println(
"It's a draw! Thanks for playing.");
}
// For winner -to display Congratulations! message.
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
- 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.
You can check below screenshot for your reference.
Similar Reads
Memory Game in Java The Memory Game is a fun and simple two-player game that tests your memory. In this game, a set of cards is laid face down. Players take turns flipping two cards to find matching pairs. If the two cards match, they remain face up. If not, they are flipped back down. The game continues until all pair
5 min read
Java Arrays Coding Practice Problems Arrays are a fundamental data structure in Java programming, enabling efficient storage, manipulation, and retrieval of elements. This collection of Java array practice problems covers essential operations, including array traversal, sorting, searching, matrix manipulations, and element-wise calcula
2 min read
How to Build a Tic Tac Toe Game in Android? In this article, we will be building a Tic Tac Toe Game Project using Java and XML in Android. The Tic Tac Toe Game is based on a two-player game. Each player chooses between X and O. The Player plays one move at a time simultaneously. In a move, a player can choose any position from a 3x3 grid. The
8 min read
How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m
2 min read
Java Fundamentals Coding Practice Problems Understanding Java fundamentals is the first step to becoming a proficient Java programmer. This collection of Java basic coding practice problems covers essential topics such as input/output operations, arithmetic and logical operators, type conversion, conditional statements, loops, and more. Thes
1 min read