This project is a simple implementation of the classic Tic-Tac-Toe game where the user plays against the computer. The computer makes random moves, and the game continues until there is a winner or a draw.
- The computer plays using 'X's.
- The user plays using 'O's.
- The computer always makes the first move by placing an 'X' in the middle of the board.
- The board squares are numbered from 1 to 9, starting from the top-left corner and moving row by row.
- The user inputs their move by entering the number of the square they choose.
- The game ends when there is a winner or a draw.
-
Ensure you have Python 3.x or higher installed on your machine.
-
Clone this repository.
-
Navigate to the project directory.
-
Run the game using the following command:
python main.py
-
Initialization:
- The board is initialized as a 3x3 grid with numbers 1 to 9.
- Flags and dictionaries are set up to track the game state, including whether the game is a draw and whether a player has won.
-
Main Game Loop:
- The game continues while there are free fields and no player has won.
- The computer makes a move by placing 'X' on the board.
- The game checks if the computer has won.
- If there are still free fields and the computer hasn't won, the player makes a move by placing 'O' on the board.
- The game checks if the player has won.
- If either player has won, the game ends and the result is displayed.
-
Functions:
display_board(board)
: Displays the current state of the board.enter_move(board)
: Allows the player to enter their move and updates the board accordingly.make_list_of_free_fields(board)
: Returns a list of coordinates of the free cells on the board.victory_for(board, sign)
: Checks if the specified player has won the game.draw_move(board)
: Makes a move for the computer and updates the board accordingly.
Clik to expand
...
+-------+-------+-------+
| | | |
| 1 | 2 | 3 |
| | | |
+-------+-------+-------+
| | | |
| 4 | X | 6 |
| | | |
+-------+-------+-------+
| | | |
| 7 | 8 | 9 |
| | | |
+-------+-------+-------+
Enter your move: 1
+-------+-------+-------+
| | | |
| O | 2 | 3 |
| | | |
+-------+-------+-------+
| | | |
| 4 | X | 6 |
| | | |
+-------+-------+-------+
| | | |
| 7 | 8 | 9 |
| | | |
+-------+-------+-------+
+-------+-------+-------+
| | | |
| O | X | 3 |
| | | |
+-------+-------+-------+
| | | |
| 4 | X | 6 |
| | | |
+-------+-------+-------+
| | | |
| 7 | 8 | 9 |
| | | |
+-------+-------+-------+
...
Enter your move: 7
+-------+-------+-------+
| | | |
| O | X | X |
| | | |
+-------+-------+-------+
| | | |
| O | X | X |
| | | |
+-------+-------+-------+
| | | |
| O | O | 9 |
| | | |
+-------+-------+-------+
You won!
Below is a UML sequence diagram that illustrates the interaction between the main components of the game.
main.py
: The main Python script that implements the game logic.README.md
: This file, providing an overview of the project.assesment.md
: Contains the project description and instructions.
This project is licensed under the MIT License.