Beta

Gameplay

We think almost everyone knows how to play snake. But if you don’t, read about it here.

To prevent infinite loops, our version of snake implements a new mechanic called hunger. A snake must eat an apple before its hunger reaches 0, and it decreases every tick. When a snake eats an apple, the hunger gets reset.

Ranking

Algos first get ranked on how many apples they eat on average. If there is a tie, the algo who has the fastest average apple eating rate (ticks per apple) wins.

Details

Your algo’s on_update() function gets called every 50 milliseconds with the new snake gamestate. Call SnakeInterface.set_direction() with 0, 1, 2, or 3 to change your snake’s direction north, east, south, or west respectively. If the algo does not call SnakeInterface.set_direction() before the snake engine moves onto the next tick, your snake will continue going the direction it already was.

See SnakeGameState and SnakeInterface for API reference.

Sample Algo

from algogames.Snake import SnakeGameState, SnakeInterface

# Do algo setup here if needed. 
# No config is given as all starting states are the same 
def setup():
    pass

# This gets called with a new gamestate on each tick
def on_update(state: SnakeGameState):
    head = state.snake[0] 
    apple = state.apple

    # Head straight for the apple, one axis at a time
    if head[0] < apple[0]:
        SnakeInterface.log('want to go south')
        if state.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 state.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 state.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 state.direction == SnakeInterface.E:
            SnakeInterface.set_direction(SnakeInterface.N)
        else:
            SnakeInterface.set_direction(SnakeInterface.W)