I have been doing some recreational programming at play.battlesnake.com. It is a series of online leagues where you enter a program to play competitive ‘snake’.
The rules are pretty simple:
- eat food to grow
- die of starvation if you go too long without eating
- die if you collide with a wall or the body of another snake
- die if you collide head-on with another snake that if of equal size or bigger
- last snake standing wins the match
There are also some variants, such as ‘royale’ where hazards move in from the walls.
You can program your snake in pretty much any language and host it where you like. When a match starts your program recieves JSON data with the board state and has 500 milliseconds to return either “left”, “right”, “up” or “down” for each move. You can write something super-simple (move to the nearest food, avoid other snakes) or you can get as complex as you like (machine learning or full game tree with alpha-pruning).
I have written my snake in Python (which seems appropriate) and host it on a free replit.com account. It uses a series of heuristics to decide it’s next action. It uses flood fill to assess how much space is available and A* for path finding.
You can see my snake (‘RhinoCrocoPede’) in action below, it is the purple one:
You can also see it more clearly here.
At the time of writing RhinoCrocoPede is 132nd in the global league (out of 450) and steadily rising.
The Battlesnake documentation is good and I was able to get the starter Python/Replit snake up and running in 15 minutes or so. I then just built on that. Replit is a nice online IDE. I did have issues with the free Replit account timing out. But I fixed this with a 90 day free upgrade code I found on the Battlesnake discord. This allowed me to set my REPL as ‘always’ on and ‘boosted’. I still have a fairly long ping time to the server (which is in California). This eats into my 500 milliseconds. But the time remaining is plenty for my current heuristic approach, even in Python.
If I wanted to get really serious I would rewrite my snake in C++, use a full game tree or Monte Carlo approach and host it on a fast server near the battlesnake server (to reduce ping time). But it is just a bit of fun and I don’t think I’ll get that serious.
My son has also written his own snake, which has been useful programming experience for him.
Battlesnake is really slick and well done. If you feel like doing some recreational programming, I recommend you give it a try.