Welcome to Escape the Castle! This project features an AI agent, trained from scratch using Q-learning, that has one goal: navigate a dangerous, guard-filled castle and escape to freedom.
Our hero starts in a random location within a 7x7 castle grid. The exit is always at the far corner (6,6). But it's not that simple! The castle is a dynamic environment, patrolled by four unique guards, each with their own strengths and weaknesses. The agent must learn to deal with them, avoid hidden traps, and use healing fountains wisely to survive.
The catch? The agent has partial observability, meaning it can only see its immediate 3x3 surroundings. It must make life-or-death decisions based on limited information, just like a real adventurer!
This agent's intelligence isn't hard-coded. It's built on the principles of Temporal Difference learning, specifically Q-learning. Through thousands of simulated runs, it learns the optimal strategy for any situation—when to fight, when to hide, when to heal, and when to just wait for a guard to pass. It learns from its mistakes, associating rewards and penalties with its actions until it masters the art of escape.
Want to see it in action?
First, you need to let the agent learn. Run the following command to start the training process. This will generate a Q_table.pickle file containing the agent's "brain."
python3 Q_learning.py train(Note: Training can take a long time! The more episodes, the smarter the agent gets.)
Once training is complete, run the agent in evaluation mode with a GUI to see what it has learned.
python3 Q_learning.py guiThis project was built with Python, PyGame, NumPy, and Matplotlib. Dive into Q_learning.py to see how the magic happens!