This project implements a self-learning Snake AI game using HTML, Sketch.js (or p5.js), and TensorFlow.js for the neural network. The AI-controlled snake learns to navigate the game environment by avoiding walls and optimizing its movements to achieve higher scores over time.
- HTML5 Canvas & Sketch.js: Renders the game in the browser for smooth animations.
- TensorFlow.js Neural Network: Implements a neural network using TensorFlow.js for real-time learning.
- Self-Learning AI: The snake learns to avoid walls and optimize its path through reinforcement learning.
- Interactive Controls: Pause, step through, and observe the learning process.
- Visualization of Neural Network: Real-time rendering of the neural network's decision-making process.
- Snake Movement: The snake moves on a grid, consuming food to grow longer.
- Collision Detection: The snake must avoid colliding with walls or its own body.
- Scoring System: Points are awarded for each piece of food consumed.
- Inputs: The neural network takes a state vector of four elements representing:
- Wall Up: Whether there's a wall above the snake.
- Wall Down: Whether there's a wall below the snake.
- Wall Right: Whether there's a wall to the right of the snake.
- Wall Left: Whether there's a wall to the left of the snake.
- Hidden Layer: Consists of 4 neurons with ReLU activation.
- Outputs: A vector of four probabilities corresponding to the actions:
- Move Up
- Move Down
- Move Left
- Move Right
- State Evaluation: The snake assesses its environment using the state vector.
- Action Selection: The neural network predicts the best action based on the current state.
- Reward System: The snake receives rewards or penalties:
- Positive Reward: For moving without hitting a wall.
- Negative Reward: For colliding with a wall (-1).
- Q-Learning Algorithm: Updates the neural network using temporal difference learning.
- Web Browser: A modern browser like Google Chrome or Firefox.
- Local Server (Optional): For improved performance, especially with TensorFlow.js.
- Clone the Repository
git clone https://github.com/sharik709/snake-game.git
- Navigate to the Project Directory
cd snake-game
- View Game
- open index.html in moderm browser.
- Without a Server: Open
index.html
in your browser. - With a Server: Navigate to
http://localhost:8000
(or the port you specified).
- Frame Rate (
FR
): Controls the speed of the game.var FR = 100; // Higher value speeds up the game
- Exploration Rate (
EXPLORATION
): Balances exploration and exploitation.var EXPLORATION = 0.1; // Lower value reduces random actions
- Learning Rate (
LEARNING_RATE
): Affects how quickly the AI learns.const LEARNING_RATE = 0.01; // Increase for faster learning
- Gamma (
GAMMA
): The discount factor for future rewards.const GAMMA = 0.9; // Closer to 1 considers future rewards more
- Change Input Size: Modify if you add more environmental factors.
this.inputs = 4; // Number of input neurons
- Adjust Hidden Layers: Increase or decrease complexity.
this.hidden = 4; // Number of neurons in the hidden layer
- Alter Activation Functions: Experiment with different activation functions.
activation: 'relu', // Options: 'sigmoid', 'tanh', etc.
Unavailable. Please clone this repository and open index.html in modern browser.
Contributions are welcome! Please follow these steps:
- Fork the Repository
- Create a Feature Branch
git checkout -b feature/YourFeature
- Commit Your Changes
git commit -m "Add Your Feature"
- Push to the Branch
git push origin feature/YourFeature
- Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
Note: Make sure to replace placeholders like your-username
and demo links with your actual information.