Battlesnake Challenge
Tags: programming, python, challenge, javascript
Published: 2024-05-15 10:00:00

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](https://en.wikipedia.org/wiki/List_of_programming_languages), or I lose interest. Whichever comes first.

You can also follow my progress [on GitHub](https://github.com/getcodelimit/battlesnake-challenge).

## What is a Battlesnake?

[Battlesnake](https://play.battlesnake.com/) is a bit like [Robot Wars](https://en.wikipedia.org/wiki/Robot_Wars_(TV_series)) 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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot3rduemad65vhyio2i3.gif)

## 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](https://dev.to/robvanderleek/battlesnake-challenge-1-python-1pon).