This project is a simple console-based implementation of the classic Tic-Tac-Toe game. Two players take turns to mark spaces in a 3x3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. If all nine squares are filled and neither player has three in a row, the game is a draw.
-
Starting the Game:
- The game starts with an empty 3x3 grid.
- Player X always goes first.
-
Making a Move:
- Players are prompted to enter their move in the format
row column
. - For example, entering
0 0
places a mark in the top-left corner of the grid.
- Players are prompted to enter their move in the format
-
Invalid Moves:
- If a player tries to mark a cell that is already occupied, they will be prompted to try again.
-
Winning the Game:
- The first player to get three of their marks in a horizontal, vertical, or diagonal row wins the game.
- A winning message is displayed, and the option to play again is provided.
-
Drawing the Game:
- If all nine cells are filled and no player has three in a row, the game is declared a draw.
- A draw message is displayed, and the option to play again is provided.
To run this project, you will need to have a Java environment set up. Simply follow the steps below:
-
Copy the Code:
- Copy the entire code block from the provided example game.
-
Create a New Java File:
- Open your preferred text editor or IDE.
- Create a new file and name it
tictactoe.java
.
-
Paste the Code:
- Paste the copied code into the
tictactoe.java
file.
- Paste the copied code into the
-
Save the File:
- Save the
tictactoe.java
file.
- Save the
-
Run the Script:
- Open your terminal or command prompt.
- Navigate to the directory where the
tictactoe.java
file is saved. - Run the script by typing
java tictactoe.java
and press Enter.
Enjoy the game!