Snake Overview
We all know snake: eat as many apples as possible without bumping into walls.
To prevent infinite games we implemented a mechanic called hunger. Every tick, your snake’s hunger decreases by one, and if you reach zero before getting an apple, you lose the game. An apple reached on the tick your hunger reaches zero is still in time. Players start the game with 50 hunger, and each time the snake eats an apple the hunger gets replenished to 50 plus the number of apples eaten before it.
Our board has 13 rows and 14 columns. The origin is in the top left corner and the coordinates are 0 indexed taking the form of (row, column). See the game state page for more info.
Details
| Time per move | 50 milliseconds: the engine plays a tick every 50 milliseconds, with the last move your algo sent, and state.time_left says how much of the tick is left. (We are still figuring out the best amount of time to provide for our games. This is subject to change.) |
| Ranking system | The algos that get the most apples rise to the top. If two algos average the same number of apples per game, as two that fill the board every time do, the one that takes fewer ticks per apple ranks higher. The leaderboard shows each of those two numbers with a ± after it, which is how far off it may still be; it shrinks as the algo plays more ranked matches. A newly ranked version is in trials until its average is known to within 5 apples: its ranked matches are played and you can see its numbers under My Algos, but it has no place on the leaderboard yet. That takes two matches at least, a handful for a steady algo, and more for an erratic one. |
setup() |
Snake's setup takes no arguments, since there is nothing to tell you. The snake always starts halfway down the board, at [6, 2]. |
| Valid moves | 0, 1, 2, or 3 which correspond to north, east, south, and west respectively. |
This is the starter algo the local app fills a seat with, so it is the same code you can run and edit.
from algogames import SnakeInterface
# No setup: Snake is handed no config, so there is nothing to set up.
def on_update(state):
snake = state.snake
head = snake[0]
direction = state.direction
apple = state.apple
# Head straight for the apple, one axis at a time. A snake cannot turn back
# on itself, so where that is the only way towards the apple it goes east
# and tries again next tick.
if head[0] < apple[0]:
SnakeInterface.log('want to go south')
if direction == SnakeInterface.N:
SnakeInterface.set_direction(SnakeInterface.E)
else:
SnakeInterface.set_direction(SnakeInterface.S)
elif head[0] > apple[0]:
SnakeInterface.log('want to go north')
if direction == SnakeInterface.S:
SnakeInterface.set_direction(SnakeInterface.E)
else:
SnakeInterface.set_direction(SnakeInterface.N)
elif head[1] < apple[1]:
SnakeInterface.log('want to go east')
if direction == SnakeInterface.W:
SnakeInterface.set_direction(SnakeInterface.N)
else:
SnakeInterface.set_direction(SnakeInterface.E)
elif head[1] > apple[1]:
SnakeInterface.log('want to go west')
if direction == SnakeInterface.E:
SnakeInterface.set_direction(SnakeInterface.N)
else:
SnakeInterface.set_direction(SnakeInterface.W)