Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

How to Train your Snake - Teach a Neural Network to play Snake!

A topic by bewelge created Apr 05, 2017 Views: 2,740 Replies: 3
Viewing posts 1 to 3

Hey everyone,

I was just playing around with Neural Networks for the first time when I read about this Jam and immediately had the idea to make a snake game that teaches itself. I used a javascript library ( https://github.com/zefman/Brainwave/blob/master/bu... ) for the Network but the rest was done in the last 4 days.

The current version is already playable at https://bewelge.github.io/HowToTrainYourSnake/ .

I think it turned out alright. The game field is 3x3 and for each tile an input is given to the neural network (either food/ snake-body/ snake-head/ empty Field). The network has 1 hidden Layer with 10 Neurons and gives 3 Outputs (Turn Left, Turn Right or Go Straight).

In the beginning there are 4 Snake-Games. When all of the snakes have died, the two best performing snakes will continue into the new generation. The two other new snakes will be a crossing/offspring of the best performing snakes. Finally theres a chance for mutation, meaning something in the snakes brain (the neural network) will be randomly changed.

Due to this randomness some games may be significantly shorter than others but on the 3x3 grid my snakes always eventually learn to play a "perfect game" (fill the entire board).

Still planning to add:

- More options so you can configure the neural network yourself after you've unlocked everything.

- Possibility to play on larger Maps than 3x3.

- Some chiptunes for when the snakes eat :)

Let me know what you think. Especially if the upgrade-progress is too slow.

Also, I'm unsure if publishing my game here is final. That's why I put it on github first because I'm not quite done yet. Anyone know if you can edit your game after publishing/submitting it here?

I quite like that. And no the progression wasn't too slow really, I managed to play into the options and lag it to death without getting bored. :)

And it's a good example of a learning system.

Thanks for trying it out, I might need to add that option later in the game or with a bigger warning :) Do you remember at what board size you lagged out?

Did one of your snakes "win" the game? (Got to length 9?)

(1 edit)

I managed to get a snake of 9!

I had to brutally punish looping to do it.