sneks.engine.interface.snek module

class sneks.engine.interface.snek.Snek

Bases: ABC

Base snek from which submission sneks derive. This snek has no behavior, but derivations should implement get_next_action() to provide it.

head: Cell = Cell(x=0, y=0)

Head snek represented as Cell

bearing: Bearing = Bearing(x=0, y=0)

Bearing of the snek

occupied: frozenset[Cell] = frozenset({})

Set of currently occupied cells on the game board that the snek can see

get_next_action() Action

Method that determines which action the snek should take next.

Returns:

the next action for the snek to take

get_head() Cell

Helper method to return the head of the snek.

Returns:

the cell representing the head of the snek

get_bearing() Bearing

Helper method to return the bearing of the snek.

Returns:

the bearing representing the directional speeds of the snek

get_occupied() frozenset[Cell]

Helper method to return all occupied cells that the snek can see. This includes both your snek’s position, all other sneks’ positions, and space occupied by asteroids.

This can be used in your get_next_action() to check if a cell you are planning on moving to is already taken. Example:

potential_next_cell = self.get_head().get_up()
if potential_next_cell in self.get_occupied():
    # potential_next_cell is already taken
else:
    # potential_next_cell is free
Returns:

the set of occupied cells on the game board

look(direction: Direction) int

Look in a direction from the snek’s head and get the distance to the closest obstacle. An obstacle could either be an occupied cell or the game board’s border.

>>> self.get_head()
Cell(0, 0)
>>> self.look(Direction.LEFT)
0
Parameters:

direction – the direction to look

Returns:

the distance until the closest obstacle in the specified direction

get_direction_to_destination(destination: Cell, directions: Sequence[Direction] = (Direction.UP, Direction.DOWN, Direction.LEFT, Direction.RIGHT)) Direction

Get the next direction to travel in order to reach the destination from a set of specified directions (default: all directions).

When multiple directions have the same resulting distance, the chosen direction is determined by the order provided, with directions coming first having precedence.

For example, to get the direction the snek should travel to close the most distance between itself and a cell 5 columns and 9 rows away, this method could be used like:

self.get_direction_to_destination(Cell(5, 9))
Parameters:
  • destination – the cell to travel towards

  • directions – the directions to evaluate in order

Returns:

the direction to travel that will close the most distance