Beta

Gameplay

Duel Snakes is Snake for two. Your snake and your opponent’s share one board, with five apples on it at all times instead of one.

Both snakes have hunger, just like in Snake. 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.

The snake left alive wins. If both die on the same tick, or the board fills up with both still alive, the snake that ate more apples wins, and the same number of apples is a draw.

Ranking

Todo: write a general elo based doc page and reference it here

Details

Your algo’s on_update() function gets called every 50 milliseconds with the new duel snakes gamestate. Call DuelSnakesInterface.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 DuelSnakesInterface.set_direction() before the duel snakes engine moves onto the next tick, your snake will continue going the direction it already was.

Your algo’s setup() function is given start_pos, the (row, column) your snake starts at, which is either [12, 2] or [12, 23].

See DuelSnakesGameState and DuelSnakesInterface for API reference.

Sample Algo

from algogames.DuelSnakes import DuelSnakesGameState, DuelSnakesInterface

# Do algo setup here if needed. 
# start_pos is where your snake starts: [12, 2] or [12, 23]
def setup(start_pos):
    pass

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

    # Head straight for the first apple, one axis at a time
    if head[0] < apple[0]:
        DuelSnakesInterface.log('want to go south')
        if state.directions[0] == DuelSnakesInterface.N:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.E)
        else:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.S)

    elif head[0] > apple[0]:
        DuelSnakesInterface.log('want to go north')
        if state.directions[0] == DuelSnakesInterface.S:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.E)
        else:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.N)

    elif head[1] < apple[1]:
        DuelSnakesInterface.log('want to go east')
        if state.directions[0] == DuelSnakesInterface.W:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.N)
        else:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.E)

    elif head[1] > apple[1]:
        DuelSnakesInterface.log('want to go west')
        if state.directions[0] == DuelSnakesInterface.E:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.N)
        else:
            DuelSnakesInterface.set_direction(DuelSnakesInterface.W)