Snake Playing AI Agent
Reinforcement learning agent that teaches itself to play Snake. Trained using Q-Learning, the agent learns to navigate, avoid collisions, and maximize its score with no prior knowledge of the game — reaching a score of 100+ after roughly 75 training iterations.
View RepositoryA practical exploration of reinforcement learning fundamentals — how an agent learns optimal behavior purely from reward signals. The agent's state is represented as a compact vector encoding nearby danger, its current direction, and the food's location relative to its head. A neural network approximates Q-values for each possible action, trained on experience replayed from a memory buffer, gradually shifting the agent from random movement to consistently strong play.
Learns from scratch
No prior knowledge of the game — behavior is learned purely from reward signals.
Compact state design
Danger directions, food location, and current heading encoded as a simple state vector.
Epsilon-greedy exploration
Decaying exploration rate balances trying new moves against using what it has learned.
100+ score in ~75 games
Went from random movement to consistently scoring 100+ in about 75 training iterations.
Live training visualization
Watch the agent play and improve in real time through a Pygame window.
Designing an effective reward structure
Naive rewards made the agent chase food recklessly and die early. Added penalty shaping for moves that did not make real progress toward food.
Exploration vs. exploitation
Too-fast epsilon decay locked in bad habits; too slow never converged. Tuned the decay schedule to balance the two.
Training stability
Q-values swung wildly between updates. A replay buffer with random mini-batches and a Bellman update smoothed learning and cut score variance.
The agent achieved a score of 100+ autonomously, demonstrating effective Q-Learning convergence from a completely cold start — with no hand-crafted strategy, just a reward signal and enough training iterations to learn one.