This project implements an AI player for the game of Nim using the Q-learning algorithm. Nim is a two-player game where players take turns removing amounts from distinct piles. The person that moves right before all piles are empty loses. The goal of the AI player is to learn optimal strategies for playing Nim through reinforcement learning.
You'll need to have Python installed. You can download it from the official Python website.
- Clone the repository:
git clone https://github.com/ColinDao/nim.git- Navigate to the project directory:
cd nimTo play against the AI, run the following command:
python play.pyFollow the prompts to make your moves. The AI player will respond with its moves based on the optimal strategy. Good luck!
Q-learning: The AI agent uses the Q-learning algorithm, a type of reinforcement learning, to associate playable moves with rewards such as if it results in a winning position.
State Representation: Design an effective state representation scheme to represent the current state of the game, including the number of piles and their amounts.
Action Selection: Implement a policy for selecting actions (i.e., moves) based on the current state and the learned Q-values.
Training and Evaluation: Train the AI player through repeated gameplay sessions against itself, adjusting Q-values based on observed rewards and penalties.
Language: Python
Libraries: Random, Time
This project was completed as a part of CS50's Introduction to Artificial Intelligence with Python. Go check them out!
MIT License
Copyright (c) 2024 Colin Dao
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.