GFG Day 7: Mini Project: Tic-Tac-Toe Game in Java

6 min read

- Tic-Tac-Toe is a simple, classic two-player game, often used as an introductory project in Java programming. The game is played on a 3x3 grid where two players take turns marking the cells with X and O. The objective is to get three of your symbols in a horizontal, vertical, or diagonal row.
Rules of the Tic-Tac-Toe Game
Rule | Description |
Number of Players | 2 |
Symbols | First player: X, Second player: O |
Board | 3x3 grid |
How to Play | Players alternate turns, placing their symbol in empty cells |
Objective | First to place three marks in a row (column, row, or diagonal) wins |
Draw Condition | If the board is filled with no winner, the game is a draw |
Invalid Move Handling | A player cannot mark a cell that is already taken |
Basic Gameplay Sequence
The game starts with an empty board.
Player 1 (X) chooses a cell to place their symbol.
Player 2 (O) chooses a cell.
Players alternate turns.
After each move, check if the player has completed a row, column, or diagonal.
If there is a winner, declare them; if the grid is full without a winner, declare a draw.
The game ends.
Game Board Layout
- The game board layout will look something like this and game will begin.
|---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
Project Implementation
// Tic-Tac-Toe Game
// File name: TicTacToeGame.java
import java.util.*;
public class TicTacToeGame {
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
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:
2
|---|---|---|
| 1 | X | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
5
|---|---|---|
| 1 | X | 3 |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
1
|---|---|---|
| X | X | 3 |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
3
|---|---|---|
| X | X | O |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
8
|---|---|---|
| X | X | O |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | X | 9 |
|---|---|---|
O's turn; enter a slot number to place O in:
6
|---|---|---|
| X | X | O |
|-----------|
| 4 | O | O |
|-----------|
| 7 | X | 9 |
|---|---|---|
X's turn; enter a slot number to place X in:
9
|---|---|---|
| X | X | O |
|-----------|
| 4 | O | O |
|-----------|
| 7 | X | X |
|---|---|---|
O's turn; enter a slot number to place O in:
4
|---|---|---|
| X | X | O |
|-----------|
| O | O | O |
|-----------|
| 7 | X | X |
|---|---|---|
Congratulations! O's have won! Thanks for playing.
Happy Learning
Thanks For Reading! :)
SriParthu ๐๐ฅ
0
Subscribe to my newsletter
Read articles from sri parthu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
JavaCore Javajava full stack developmentJava Full Stack Developer TrainingJava Full Stack Development, Java Backend Development ,Java Frontend Development, Java Web Development, HTML/CSS/JavaScript#skillupwithgfg#nationskillup.projectstic tac toe
Written by

sri parthu
sri parthu
Hello! I'm Sri Parthu! ๐ I'm aiming to be a DevOps & Cloud enthusiast ๐ Currently, I'm studying for a BA at Dr. Ambedkar Open University ๐ I really love changing how IT works to make it better ๐ก Let's connect and learn together! ๐๐