A Python-based implementation of the classic Snake game, featuring AI agents powered by various search algorithms and interactive visualizations of their exploration process.
This project features two distinct modes for interacting with the AI. You can either watch the algorithm's "thought process" in the Visualization Mode or see it take control of the snake in the Autonomous Gameplay Mode.
This project is more than just a game; it's a comprehensive tool for exploring and understanding AI search algorithms.
| Feature | Description |
|---|---|
| π§ Multiple AI Agents | Switch between seven different AI algorithms, from simple heuristics like Greedy Best-First Search to complex pathfinding like A*. |
| π Real-Time Visualization | Watch the AI's "thought process" in real-time as it explores the game board, providing a clear visual understanding of how each algorithm works. |
| πΉοΈ Full Joystick Support | Play the game or navigate all menus using a standard joystick, in addition to keyboard controls. |
| π§βπ» Human Playable Mode | Take control yourself and try to beat the high scores in a classic, polished Snake experience. |
| βοΈ Customizable Difficulty | Adjust the game speed across four difficulty levels to provide a challenge for both human players and the AI agents. |
| π Persistent High Scores | Your best scores for each difficulty level are saved and displayed, encouraging replayability. |
| π¨ Polished UI/UX | A clean and intuitive user interface with a full main menu, level selection, and in-game pause functionality. |
| Component | Technologies |
|---|---|
| Core Language | Python 3.7+ |
| Libraries & Frameworks | Pygame (for game logic, rendering, controls), NumPy (numerical operations), Matplotlib (visualizations) |
| Development Tools | Pyinstrument (for optional code profiling), black (for code formatting) |
π§ Click to see Detailed Algorithm Explanations
- Online Search (Reinforcement Learning with Danger/Success Memory): This agent learns by experience, remembering dangerous locations and successful paths. It uses a custom heuristic incorporating learned danger to guide its decisions, dynamically adapting to the game environment.
- A Search (with Dynamic Difficulty Adjustment):* Employs the A* search algorithm with Manhattan distance as the heuristic. To manage search complexity and adapt to the game's dynamics, it incorporates a DFS component.
- Breadth-First Search (BFS): A systematic algorithm that guarantees finding the shortest path to the food but can be computationally expensive.
- Depth-First Search (DFS): Explores a single branch as deeply as possible before backtracking. While potentially faster than BFS, it doesn't guarantee finding the shortest path.
- Greedy Best-First Search: A heuristic-driven approach that prioritizes the move seemingly closest to the food, which can miss optimal solutions.
- Hamiltonian Cycle (Demonstration): A deterministic approach following a pre-calculated path that visits every grid cell exactly once.
- These algorithms focus on visualizing the search process itself, providing insights into how different strategies explore the game board to find a path to the food for A*, BFS, DFS, Greedy, and Online Search.
Installation Instructions
- Prerequisites: Ensure you have Python 3.7+ installed.
- Clone the repository:
git clone https://github.com/Ta-Pc/AI-Snake-Game.git cd AI-Snake-Game - Set up a virtual environment:
- On Linux/macOS:
python3 -m venv .venv source .venv/bin/activate - On Windows:
python -m venv .venv .venv\Scripts\activate
- On Linux/macOS:
- Install dependencies:
pip install -r requirements.txt
Running the Game & Controls
-
To run the main game:
python main.py
-
Controls:
Control Action Arrow KeysControl snake direction & navigate menus JoystickControl snake direction & navigate menus SPACEPause / Resume the game ESCReturn to the main menu or exit the visualiser
- Implementing more advanced AI techniques (e.g., Q-learning, Deep Reinforcement Learning).
- Adding more sophisticated game elements and levels.
- Improving the user interface and visualization tools.
- Incorporating sound effects and music.
###π€ Contributing Contributions are welcome! Feel free to submit pull requests for bug fixes, new features, or improved documentation.
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.