battlesnake

Types

A representation of the color of a Battlesnake

pub opaque type Color

The direction for your Battlesnake to move in.

pub type Direction {
  Up
  Down
  Left
  Right
}

Constructors

  • Up
  • Down
  • Left
  • Right

The response to move requests.

pub type Move {
  Move(direction: Direction, shout: Option(String))
}

Constructors

  • Move(direction: Direction, shout: Option(String))

Configures how your snake looks.

pub type SnakeConfig {
  SnakeConfig(
    apiversion: String,
    author: Option(String),
    color: Option(Color),
    head: Option(String),
    tail: Option(String),
    version: Option(String),
  )
}

Constructors

  • SnakeConfig(
      apiversion: String,
      author: Option(String),
      color: Option(Color),
      head: Option(String),
      tail: Option(String),
      version: Option(String),
    )

    Arguments

    • apiversion

      Version of the Battlesnake API implemented by this Battlesnake. Currently only API version 1 is valid. Example: “1”

    • author

      Username of the author of this Battlesnake. If provided, this will be used to verify ownership. Example: “BattlesnakeOfficial”

    • color

      Color of the Battlesnake.

    • head

      Head customization. Example: “default”

    • tail

      Tail customization. Example: “default”

    • version

      Optional version string for your Battlesnake. This value is not used in gameplay, but can be useful for tracking deployments on your end.

Functions

pub fn color_from_hex(color: String) -> Result(Color, Nil)

Convert a hex string to a Battlesnake color.

Accepts the formats ‘#000000’ or ‘000000’.

pub fn color_from_rgb(
  r red: Int,
  g green: Int,
  b blue: Int,
) -> Result(Color, Nil)

Create a Battlesnake color from red, green and blue components.

Each value must be in the range 0..255.

pub fn config() -> SnakeConfig

Create a base Battlesnake config.

pub fn move(direction: Direction) -> Move

Create a move response with the given direction

pub fn shout(move: Move, shout: String) -> Move

Add an optional message to the move response that the other snakes can see

pub fn simple(
  config config: SnakeConfig,
  on_move callback: fn(GameState) -> Move,
) -> fn(Request(Connection)) -> Response(Body)

Use this function if your algorithm doesn’t need to persist any state between moves. The returned handler ignores /start and /end requests and call your on_move function on every turn.

pub fn stateful(
  config config: SnakeConfig,
  on_start start: fn(GameState) -> a,
  on_move move: fn(GameState, a) -> #(Move, a),
  on_end end: fn(GameState, a) -> Nil,
) -> fn(Request(Connection)) -> Response(Body)

This function lets you persist state between turns per game.

pub fn with_author(
  battlesnake: SnakeConfig,
  author: String,
) -> SnakeConfig

Add your Battlesnake username. If provided, this will be used to verify ownership.

pub fn with_color(
  battlesnake: SnakeConfig,
  color: Color,
) -> SnakeConfig

Add a color to your Battlesnake.

pub fn with_head(
  battlesnake: SnakeConfig,
  head: String,
) -> SnakeConfig

Add a head customization.

pub fn with_tail(
  battlesnake: SnakeConfig,
  tail: String,
) -> SnakeConfig

Add a head customization.

pub fn with_version(
  battlesnake: SnakeConfig,
  version: String,
) -> SnakeConfig

Add an optional version string to your Battlesnake. This value is not used in gameplay, but can be useful for tracking deployments on your end.

Search Document