AI plays snake game

Neural Network Trained using Genetic Algorithm which acts as the brain for the snake.

The snake looks in the 8 direction for food, body part and the boundary which acts as the 24 input for the Neural Network.


Getting Started


To install the dependencies, run on terminal :

python3 -m pip -r requirements.txt

Project Structure

├──            # class that helps in setting the boundary and parameters of the arena
├──            # class that deals with the neural network
├──           # consists of colors used in the whole project
├──             # lets the saved snakes to run in 
├── samples
│   ├── generation23.gif    
│   └── generation6.gif
├──            # parametes to apply genetic algorithm on your own
├── requirements.txt    # python dependencies required
├── saved
│   └── top_snakes.pickle   # saved list of objects of snake class for each generation
└──            # class snake that handles all properties of snake


To test the Genetic Algorithm, alter the parameters inside the, then run the following command specifying the path to save the optimised result as a pickle file (a list is stored, containing the best snake from each generation):

python3 --output saved/test.pickle 


To run the snakes saved previously, run the following commands specifying the path to the saved file :

python3 --input saved/test.pickle