An interactive maze game that uses reinforcement learning (Q-learning) to find optimal paths through procedurally generated mazes.
- 🎮 Interactive maze generation
- 🧠 Machine learning pathfinding
- 📊 Real-time statistics display
- 🎯 Goal-oriented learning
- 🔄 Continuous training capability
- 🐍 Built with Python and PyTorch
- Clone the repository:
git clone https://github.com/rajaminyak/maze-game-ml.git
cd maze-game-ml
- Install required packages:
pip install pygame torch numpy
Run the game:
python main.py
SPACE
: Let ML agent make a moveR
: Reset/generate new mazeT
: Train ML agent (500 episodes)D
: Toggle debug mode
-
State Space (What the AI "sees"):
- Distances to walls in 4 directions
- Distance to goal
- Angle to goal
-
Action Space (What the AI can do):
- Move Up
- Move Down
- Move Left
- Move Right
-
Reward System:
- +100 for reaching goal
- -1 for hitting walls
- -0.1 step cost
- Distance-based penalties
QNetwork Structure:
Input Layer (6 neurons)
→ Hidden Layer (64 neurons)
→ Hidden Layer (64 neurons)
→ Output Layer (4 neurons)
- Uses epsilon-greedy strategy for exploration
- Experience replay for better learning
- Adaptive learning rate
- Continuous improvement through training
- 🟢 Green Circle: Player
- 🔴 Red Square: Goal
- ⬛ Dark Gray: Walls
- ⬜ Light Gray: Paths
- 🔷 Blue: Visited cells
- 🟡 Orange: Training indicator
The game displays real-time statistics:
- Steps taken
- Goals reached
- Current epsilon value (exploration rate)
- Training episode (during training)
- Current reward
Debug mode (D
key) provides additional information:
- Episode details
- Reward calculations
- Path analysis
- Training progress
maze-game-ml/
│
├── main.py # Main game file
├── requirements.txt # Dependencies
└── README.md # Documentation
QNetwork
: Neural network for decision makingAdvancedMazeGame
: Main game logic and ML integration
learning_rate = 0.001
gamma = 0.99 # Discount factor
epsilon_start = 1.0 # Initial exploration rate
epsilon_min = 0.01 # Minimum exploration rate
memory_size = 10000 # Experience replay buffer size
batch_size = 32 # Training batch size
To contribute:
- Fork the repository
- Create your feature branch
- Make your changes
- Submit a pull request
Common issues and solutions:
-
Game freezes during training:
- Reduce maze size
- Lower episode count
- Check system resources
-
Agent not learning:
- Increase training episodes
- Adjust reward values
- Check maze connectivity
- Add different maze generation algorithms
- Implement A* comparison
- Add visualization of neural network decisions
- Support for larger mazes
- Save/load trained models
This project is licensed under the MIT License - see the LICENSE file for details.
- PyTorch documentation
- Pygame community
- Reinforcement learning resources
- Your Name
- Contributors welcome!