A simple console-based Tic-Tac-Toe game implemented in Java. Players take turns placing X and O on a 3x3 grid until someone wins or the game ends in a draw.
- Interactive Console Interface: Clear visual representation of the game board
- Input Validation: Prevents invalid moves and provides user feedback
- Win Detection: Automatically detects wins across rows, columns, and diagonals
- Draw Detection: Recognizes when the board is full with no winner
- The game displays a 3x3 grid with numbered positions (1-9)
- Player X goes first
- Enter a number (1-9) to place your mark in that position
- Players alternate turns until someone wins or the board is full
- The game announces the winner or declares a draw
To run this game, you'll need Java installed on your system.
-
Compile the program:
javac src/App.java -d bin
-
Run the game:
java -cp bin App
- Open the project in VS Code with Java extensions installed
- Press
F5or use the "Run" button to compile and execute
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: 1
|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in: 4
|---|---|---|
| X | 2 | 3 |
|-----------|
| O | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in: 10
Invalid input; re-enter another slot number: 9
|---|---|---|
| X | 2 | 3 |
|-----------|
| O | 5 | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|
O's turn; enter a slot number to place O in: 5
|---|---|---|
| X | 2 | 3 |
|-----------|
| O | O | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|
X's turn; enter a slot number to place X in: 5
Invalid input; re-enter another slot number: 3
|---|---|---|
| X | 2 | X |
|-----------|
| O | O | 6 |
|-----------|
| 7 | 8 | X |
|---|---|---|
O's turn; enter a slot number to place O in: 6
|---|---|---|
| X | 2 | X |
|-----------|
| O | O | O |
|-----------|
| 7 | 8 | X |
|---|---|---|
Congratulations! O's have won! Thanks for playing
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: 1
|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in: 5
|---|---|---|
| X | 2 | 3 |
|-----------|
| 4 | O | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in: 2
|---|---|---|
| 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: 7
|---|---|---|
| X | X | O |
|-----------|
| 4 | O | 6 |
|-----------|
| X | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in: 4
|---|---|---|
| X | X | O |
|-----------|
| O | O | 6 |
|-----------|
| X | 8 | 9 |
|---|---|---|
X's turn; enter a slot number to place X in: 6
|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | 8 | 9 |
|---|---|---|
O's turn; enter a slot number to place O in: 9
|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | 8 | O |
|---|---|---|
X's turn; enter a slot number to place X in: 8
|---|---|---|
| X | X | O |
|-----------|
| O | O | X |
|-----------|
| X | X | O |
|---|---|---|
It's a draw! Thanks for playing.