This is a console-based Tic-Tac-Toe game implemented in Java. Two players take turns marking cells on a 3x3 grid with their symbols: X and O. The first player to align three marks in a row, column, or diagonal wins. If the grid is full and no player wins, the game ends in a tie.
This project is ideal for beginners learning:
- Java basics (
arrays,loops,conditionals) - Methods and modular programming
- Input validation and console interaction
- Interactive 3x3 console grid display
- Row and column numbers for easy move selection
- Input validation to prevent invalid moves or overwriting
- Automatic detection of winner or tie
- Clear turn switching between Player X and Player O
- Optional console clearing for a cleaner board display
- Run the program in a Java IDE or terminal.
- Player X goes first.
- Players take turns entering their move in the format:
row column
rowandcolumnare numbers between0and2.- Example:
1 2places a mark at row 1, column 2.
-
The program validates the input:
- Checks if the coordinates are valid (0–2)
- Ensures the chosen cell is empty
-
The game continues until:
- A player gets three in a row → winner is announced
- All cells are filled → game ends in a tie
-
The board is displayed after each move.
0 1 2
0 X | | O
---+---+---
1 | X |
---+---+---
2 O | | X
- Column and row numbers help players select cells easily.
-
TicTacToeclass: main class containing the game logic. -
Variables:
grid→ 3x3 char array representing the boardPLAYER_XandPLAYER_O→ symbols for playerscurrentPlayer→ tracks whose turn it is
-
Methods:
| Method | Description |
|---|---|
main(String[] args) |
Starts the game, initializes the grid, and handles game loop |
printGrid() |
Displays the current grid with row and column numbers |
isGameOver() |
Checks if a player has won or the grid is full |
hasWinner() |
Returns true if a player has a winning combination |
isRowWin(int row) |
Checks if a specific row has the same marks |
isColWin(int col) |
Checks if a specific column has the same marks |
isDiag1Win() |
Checks top-left to bottom-right diagonal for a win |
isDiag2Win() |
Checks top-right to bottom-left diagonal for a win |
isFull() |
Checks if all cells in the grid are filled |
- Understand how 2D arrays are used to represent the board.
- Learn how loops iterate through rows and columns to check for wins.
- Notice the use of methods to modularize the code and avoid repetition.
- Input validation is key to prevent crashes or illegal moves.
- The
currentPlayervariable helps alternate turns easily.
- Save the code as
TicTacToe.java. - Open a terminal or IDE (e.g., IntelliJ, Eclipse, VS Code).
- Compile the program:
javac TicTacToe.java
- Run the program:
java TicTacToe
- Follow on-screen instructions to play.
0 1 2
0 | |
---+---+---
1 | |
---+---+---
2 | |
Player X, enter your move (row and column): 0 0
0 1 2
0 X | |
---+---+---
1 | |
---+---+---
2 | |
Player O, enter your move (row and column): 1 1
...
Player X wins! 🎉
This project is free to use and modify. Ideal for learning and practice.