This is a simple implementation of the Tic Tac Toe game in Python using the Pygame library.
-
Make sure you have Python 3.9 installed on your system.
-
Clone this repository to your local machine using the following command:
git clone <repository-url>
-
Navigate to the project directory:
cd TicTacToe
-
(Optional) Create and activate a virtual environment to isolate the project dependencies:
python3 -m venv venv
source venv/bin/activate
-
Install the required dependencies using pip:
pip install -r requirements.txt
- Open a terminal or command prompt and navigate to the project directory.
- Run the following command to start the game:
python main.py
- The game window will open, and you can start playing.
- Click on an empty cell to make a move. The game alternates between the human player (O) and the AI player (X).
- The AI player's move will be automatically calculated based on the selected game mode (Easy, Medium, or Hard).
- The game will continue until one player wins or the game ends in a draw.
- To restart the game, you can press the "R" key.
- The game starts with default mode which is 'Easy' mode.
- You can switch the game modes by pressing 'E' for Easy, 'M' for Medium and 'H' for Hard.
- You can restart the game anywhere in the middle of the game or when the game ends.
- Likewise, you can switch the game modes when the game finishes or in the middle of the game.
- The game modes will be displayed on the Window's caption. When you switch the game mode, The caption will change after you have done your move.
The game offers three difficulty modes for the AI player:
- Easy Mode: The AI player makes random moves.
- Medium Mode: The AI player uses a combination of random moves and optimal move.
- Hard Mode: The AI player uses the Minimax algorithm to make optimal moves.
- There are different classes to handle different things.
- Player class is responsible for handling AI moves and Player moves
- Board class is responsible for handling console board. (Inner Functionality)
- Renderer class is responsible for the GUI of the game and GUI Board.
- Game class is responsible for defining the game rules, for instance, when the game is over.
- Main is used to initialize the classes and start the game.
- I have used minimax algorithm for AI to choose the optimal move.
- In order to optimize the algorithm I have used Alpha-Beta pruning.
- To further optimize the algorithm we can also use memoization to store the winning moves. In my opinion, it increases space complexity and for a game like tic tac toe where moves are not as numerous as chess, increasing space complexity is not optimal.