DEV Community

Cover image for Battlesnake Challenge
Rob van der Leek
Rob van der Leek

Posted on • Edited on

Battlesnake Challenge

In this series I'll share my progress with my self-imposed programming challenge: build a Battlesnake in as many different programming languages as possible.

I'll try to post an update frequently, although there's no strict pace I'm going to keep. This series will end whenever there are no programming languages left, or I lose interest. Whichever comes first.

You can also follow my progress on GitHub.

What is a Battlesnake?

Battlesnake is a bit like Robot Wars for programmers: by controlling your snake with code, battles with other snakes can be won or lost.

To compete in Battlesnake you need to implement and host a simple web API. The API will be called by the Battlesnake servers for each move on the board.

Your code will need to make the decision if your snake goes left, right, up, or down that move. If your API takes too long to respond, your snake will continue heading in its current direction.

The Battlesnake API

The API that needs to be implemented consists of just 4 endpoints:

GET  /
POST /start
POST /move
POST /end
Enter fullscreen mode Exit fullscreen mode

The first endpoint (/) is used to register your snake with the battlesnake server. It should return information about the author and styling of the snake, such as its color.

The second endpoint (/start) will be called at the start of a new game.

The third endpoint (/move) will be called at every turn and receives an overview of the complete board in the body. This endpoint needs to return the direction of the snake for the next turn.

Finally, the last endpoint (/end) will be called at the end of a game.

The animated GIF below shows two code controlled snakes roaming around on the game board.

Battlesnake example

Anatomy of a Battlesnake program

Implementing a basic Battlesnake program consists of 3 parts:

  1. Start a web server
  2. Handle the four GET and POST requests listed above
  3. Decide what direction to take for your next move

Each part can be as advanced, or as simple as you want it to be. For example, part 3, the game logic, can contain sophisticated strategies, or it can be a straightforward straight-ahead-while-possible-otherwise-turn solution.

The implementation I'll write for this challenge will be very simplistic. I won't be using any 3rd-party libraries, and keep all the code in a single file. The game logic will have the snake take the shortest path to food without bumping into other snakes or the end of the board.

On average, the above functionality can be implemented in most programming languages in about 100 lines of code, and is easy to understand even if you're new to the language. The code can of course be the beginning of your own, much more advanced Battlesnake.

Structure of posts in this series

Every post presents a Battlesnake implementation in a new programming language.

The post starts with a small Hello, World! program, and a Dockerfile that implements the build and runtime environment.

Next, a basic web server is added that implements the GET / endpoint.

The last part shows how the game logic is implemented.

Feedback appreciated!

I hope you like reading along with my coding adventures.

Let me know in the comments below what programming languages you are looking forward to.

The first language I've picked is Python, see what a Python Battlesnake looks like in the next post.

Top comments (0)