Skip to content

An AI-driven Snake game implementation that uses neural networks and genetic algorithms to create an autonomous snake that learns and improves its gameplay over time.

Notifications You must be signed in to change notification settings

aindong/learning-snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Autonomous Snake Game

An AI-driven Snake game implementation that uses neural networks and genetic algorithms to create an autonomous snake that learns and improves its gameplay over time.

Features

  • Neural Network Control: Snake movements are controlled by a neural network that learns optimal paths
  • Genetic Evolution: Implements genetic algorithms to evolve better snake behaviors across generations
  • Real-time Visualization:
    • Neural network architecture visualization
    • Movement heatmap display
    • Game state and performance metrics
  • Adaptive Learning: Snake learns to avoid collisions and find efficient paths to food
  • Persistence: Saves learned behaviors using IndexedDB for continuous improvement

Technical Stack

  • TypeScript
  • D3.js for network visualization
  • Vite as build tool
  • Pako for data compression

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn package manager

Setup

  1. Clone the repository
  2. Install dependencies:
    npm install
  3. Start the development server:
    npm run dev
  4. Open your browser and navigate to the local server address (typically http://localhost:5173)

How It Works

Neural Network Architecture

The snake's brain is implemented as a neural network with:

  • Input layer: Processes environmental data (distance to food, walls, self)
  • Hidden layer: Processes complex patterns
  • Output layer: Determines movement direction

Genetic Algorithm

The game implements a genetic algorithm that:

  1. Maintains a population of neural networks
  2. Evaluates performance based on score and survival time
  3. Selects best performers for breeding
  4. Applies mutations to explore new strategies
  5. Creates new generations with improved behaviors

Path Finding

Implements A* pathfinding algorithm with:

  • Dynamic path calculation to food
  • Collision avoidance with walls and self
  • Efficient space utilization

Development

Build

npm run build

Preview Production Build

npm run preview

License

MIT

About

An AI-driven Snake game implementation that uses neural networks and genetic algorithms to create an autonomous snake that learns and improves its gameplay over time.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published