This project is a reinforcement learning experiment where an AI agent learns to play the classic Snake game using Q-Learning.
Make sure you have Python 3.7+ installed.
Install the required dependencies:
pip install pygame plotext numpyTo run the program, go into the home directory where qlearning.py is and run:
python qlearning.pyor
python3 qlearning.pyYou can go to qlearning.py and adjust the following quality of life parameters:
render_episode-- Visualize the snake game and learning progress in the terminal when training reaches episode:render_episodeframe_rate-- Frame rate of snake game visualization
The Q-learning agent observes the environment using a compact feature set that enables fast learning:
- Danger indicators relative to snake (Booleans):
danger_aheaddanger_leftdanger_right
- Snake’s current direction (4 values):
up,down,left,right - Relative food location (Booleans):
food_aheadfood_behindfood_leftfood_right
This results in a total of 1536 hypothetical states (2³ danger × 4 directions × 2⁴ food positions). In practice, many of these states aren't reachable, e.g. the food can't be both to the left and right.
The agent chooses from 3 discrete actions:
- Turn left
- Turn right
- Move straight