Skip to content

ahmedyar7/Snake-Game-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🐍 Snake Game AI (Deep Q-Learning)

This project implements a self-learning Snake Game AI using Deep Q-Learning, a reinforcement learning technique. The AI learns to play the game through trial and error by interacting with the environment, receiving rewards, and improving over time.


📌 Features

  • Train an agent using Deep Q-Learning (DQN)
  • Real-time game environment using Pygame
  • Neural network powered by PyTorch
  • Save/load models (model.pth)
  • Fully customizable for further research or improvements
  • Tracks performance metrics (score, mean score, record)

🧠 How It Works

  1. State Representation: 11-dimensional vector representing:

    • Danger in straight/right/left direction
    • Current direction
    • Food location relative to snake
  2. Action Space: [straight, right, left] turns from current direction

  3. Reward Mechanism:

    • +10 for eating food
    • -10 for dying
    • Small penalty each frame to encourage faster solutions
  4. Neural Network:

    • Input: 11-dim state
    • Output: Q-values for each possible action
    • Loss: Mean squared error
    • Optimizer: Adam

📂 Project Structure

.
├── main.py               # Training loop
├── model.py              # Neural network and saving logic
├── agent.py              # Reinforcement learning agent
├── snake_game.py         # Pygame-based snake game environment
├── model/                # Saved models (auto-created)
│   └── model.pth
└── README.md

🚀 Getting Started

1. Clone the Repository

git clone https://github.com/ahmedyar7/Snake-Game-AI.git
cd Snake-Game-AI

2. Install Dependencies

pip install pygame torch numpy matplotlib

3. Run Training

python main.py

4. Visualize Game (Optional)

Modify main.py to render the game in real time (turn off plot if needed).


📈 Training Progress

The training loop uses matplotlib to plot the score over time:

  • 📊 Score per game
  • 📉 Mean score
  • 🏅 High score record

Tip: You can stop training early and it will save the best model automatically.


💾 Model Saving

Trained models are saved to:

./model/model.pth

To load a saved model, add logic in agent.py or main.py to load the state dict.


LICENSE

MIT-LICENSE

About

Snake Learns how to play the game and learns how to find the reward based upon Q-learning

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages