![]() |
VOOZH | about |
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.
- Java Development Kit (JDK 8 or higher) installed
- Any IDE like IntelliJ IDEA, Eclipse IDE, or terminal
- Basic knowledge of Java (arrays, loops, conditionals, Scanner)
The game board layout will look something like this:
|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
The steps to play the game are listed below:
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:
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
}
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.
We can check below screenshot for your reference.