What I learned from Battlesnake this summer

This is a guest post from my nephew Daniel Lapinski, a high school junior at the Shipley School in Pennsylvania.

Battlesnake is a multiplayer programming game where developers use a live webserver to implement the Battlesnake API in their own way. My uncle and I used the Battlesnake API on the webserver Replit, so we could compete in the summer tournament. During this process, I learned how to work with a web server and how to code something that can change depending on what our opponents do.

For starters, I learned how to use a scoring system to determine what move the snake will choose out of its four options. The snake considers hunting, food, hazards, and space when it makes a decision. It will rate each decision on a scale of 0-1 and it will add up all of the scores to decide which move it will choose. This was something that helped our snake do well and it gave me a new understanding of how to make something change in different scenarios. However, we needed to make sure the next move wouldn’t kill our snake and that it was the best move, so we wrote code to evaluate which areas of the board were crowded, and we wrote code to hunt snakes that were smaller than us. These functions we wrote were useful because we were able to stay in places with a ton of space, while still having our snake think about hunting.

I also learned about the basics of the minimax algorithm, and I learned how to utilize it in a tic tac toe match. This was an interesting experience because the minimax algorithm is able to calculate future situations which is something very useful in a competition.

Nevertheless, one of the most important outcomes from all of this was that my programming skills strengthened tremendously. I was able to practice writing code, and I learned about a ton of things like classes, cherrypy, lists, JSON, unit tests, distance calculation, debugging, and the use of print statements to find issues. For example, we used a class object to represent the snake’s brain which was used in a stateless environment to make a new brain for each move. This was needed because your snake wasn’t able to retain information from previous moves. In this snake brain, we created many functions to give the snake tools to survive as long as possible. One of the crucial things we implemented was the distance calculation function. This function allowed us to find everything that was near our snake, and we did this by iterating through a list.

However, we also needed to make sure all of these functions worked, so we made unit tests to see if the functions worked as intended. We used print statements so we could see the board and the scores for each move. This made debugging way easier because we could see the thought process of the brain.

Overall, this was a great experience because I was able to learn how to create stuff with a web server, which helped me become a better programmer.

Related: Adam’s Battlesnake Experience, Why I Hate Python