MCPcopy Index your code
hub / github.com/bones-ai/rust-snake-ai-ratatui

github.com/bones-ai/rust-snake-ai-ratatui @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
96 symbols 154 edges 10 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

AI learns to play Snake!

A neural network learns to play snakes in the terminal

It was built from scratch using Rust and Ratatui

Check out this for an older version of the AI that uses macroquad for GUI

screenshot

Usage

  • Clone the repo
git clone git@github.com:bones-ai/rust-snake-ai-ratatui.git
cd rust-snake-ai-ratatui
  • Run the simulation
cargo run --release

Configs

  • The project configs file is located at src/configs.rs
  • Update IS_LOW_DETAIL_MODE for a minimal UI, it runs faster
  • Set IS_SAVE_BEST_NET to train your own network. Networks are saved in data/net.json, once saved you can use the trained network by setting IS_LOAD_SAVED_NET
  • Use data/net-100.json to watch the AI complete the game for 15x15 board size
  • Set USE_GAME_CANVAS to true and update VIZ_GAME_SCALE to scale the game if needed.

Resources

Here are some excellent resources to learn more about genetic algorithms: - Video Series: Genetic Algorithm by The Coding Train. - Book: Nature of Code by Daniel Shiffman for those who prefer reading. - I highly recommend checking out Joshka's fork for more idiomatic Rust code.

Algorithm

  1. Initialization:
  2. The simulation begins at Generation 0.
  3. A new population of snakes is created, each with a neural network initialized with random weights and biases.
  4. Game Update:
  5. Each step, every game is updated by passing vision inputs to the neural network to decide the snake's action.
  6. A game is flagged as complete if:
    • The snake collides with walls or itself.
    • The snake fails to eat food within a certain number of steps, preventing indefinite looping.
  7. Generation Completion:
  8. The generation continues updating each game until all games are complete.
  9. Fitness Evaluation:
  10. At the end of each generation, snakes are ranked based on their performance.
  11. Parent Selection:
  12. Parents for the next generation are chosen based on rankings. Higher-ranked snakes have a higher probability of being selected as parents.
  13. Reproduction:
  14. Techniques such as roulette wheel selection, elitism, and other methods are used to generate children for the next generation.
  15. New Generation:
  16. A new population is created, and the process repeats from step 2 until the simulation is manually stopped.

This iterative process leads to snakes fine-tuning their strategies, resulting in longer snakes over time.

Extras

Core symbols most depended-on inside this repo

fitness
called by 7
src/agent.rs
value
called by 6
src/utils.rs
score
called by 4
src/game.rs
is_wall
called by 4
src/game.rs
is_snake_body
called by 4
src/game.rs
mutate
called by 4
src/nn.rs
draw_rect
called by 4
src/viz.rs
get_one_hot_dir
called by 2
src/utils.rs

Shape

Method 79
Class 14
Function 2
Enum 1

Languages

Rust100%

Modules by API surface

src/viz.rs35 symbols
src/agent.rs13 symbols
src/utils.rs10 symbols
src/pop.rs10 symbols
src/nn.rs10 symbols
src/game.rs9 symbols
src/sim.rs8 symbols
src/main.rs1 symbols

For agents

$ claude mcp add rust-snake-ai-ratatui \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact