This project implements a Tic-Tac-Toe game using reinforcement learning (Q-learning) to train an AI agent to play against a human player. The AI learns through interactions with the environment and gradually improves its gameplay strategy.
- TicTacToe Class: Manages the game environment, tracks the game state, and checks for wins, draws, available moves, etc.
- QLearningAgent Class: Implements the Q-learning algorithm for the AI agent. Manages Q-values, chooses actions based on exploration-exploitation strategy, and updates Q-values based on rewards received.
- play_game Function: Runs a single game between the AI agent and the environment, facilitating learning and updating Q-values accordingly.
- Main Function (main): Orchestrates the training process for the AI agent by running multiple episodes of the game. After training, it allows a user to play against the trained AI.
tictactoe.py
: Contains the main code implementing the game logic, Q-learning agent, and gameplay functionalities.README.md
: Provides an overview, instructions, and details about the project.
- Run the
main()
function intictactoe.py
to initiate the training process for the AI agent. - Adjust the
num_episodes
variable to control the number of training episodes.
- After training, the user can play against the trained AI by following the prompts displayed in the console.
- Run the
main()
function and interact with the command-line interface to make moves against the AI.
- Python 3.x
- NumPy library
- Clone this repository to your local machine.
- Ensure Python 3.x is installed.
- Create a virtual environment:
python3 -m venv venv_name
- Activate the virtual environment:
- For Windows:
venv_name\Scripts\activate
- For macOS and Linux:
source venv_name/bin/activate
- For Windows:
- Install the required dependencies (numpy):
pip install numpy
- Run
python tictactoe.py
to start the program.
- Feel free to explore and modify the code as needed. Contributions, suggestions, and improvements are welcome.
- If you encounter any issues or have questions, please create an issue in the repository.
Youssef Baghrous
This project is licensed under the MIT License.