Skip to content
/ snek Public

Battlesnake-compatible rulesets and board positions in Elixir.

License

Notifications You must be signed in to change notification settings

xtagon/snek

Repository files navigation

Snek

A framework for defining Battlesnake-compatible rulesets and board positions in Elixir.

Battlesnake is an online competitive take on the classic game of Snake. Snek provides some structure and tooling that developers can use toward creating Battlesnake AIs in Elixir.

This project is not affiliated with or sponsored by Battlesnake.

Status

This is an early work in progress, and should be considered experimental, incomplete, and unstable until v1.0.0, following Semantic Versioning.

All notable changes will be recorded in the changelog.

Installation

Add snek to your list of dependencies in mix.exs:

def deps do
  [
    {:snek, "~> 0.4.0"}
  ]
end

Documentation

Documentation can be found here at Hex.pm or generated from the source code using mix docs.

Examples

The following example simulates a couple of game turns using the Standard ruleset, for a two-player game, on a standard medium (11x11) board size.

alias Snek.Ruleset.Standard
alias Snek.Board

# Two-player game
snake_ids = MapSet.new(["snek1", "snek2"])

# Initialize the game
{:ok, turn0} = Standard.init(Board.Size.medium, snake_ids)

# The ruleset emits `Snek.Board` states that you can analyze in various ways:
Board.empty?(turn0) # => false
Board.alive_snakes_remaining(turn0) # => 2

# Apply moves for two turns
turn1 = Standard.next(turn0, %{"snek1" => :left, "snek2" => :up})
turn2 = Standard.next(turn1, %{"snek1" => :down, "snek2" => :right})

# Is the game over? (See if you can figure out *why* the game isn't over!)
Standard.done?(turn2) # => false

All rulesets implement the same callbacks, so you can perform dynamic dispatch. In the following example, the ruleset variable could be set to any ruleset module name:

ruleset = Snek.Ruleset.Solo

{:ok, turn0} = ruleset.init(Board.Size.medium, snake_ids)

Open Invite

If you have any questions, or just wish to geek out and chat about Battlesnake or Elixir or programming in general, feel free to reach out! I love talking with people and sharing tips and tricks.

You can reach me at xtagon@gmail.com, or catch me in Battlesnake Discord (username: @xtagon).

Development

The following Mix tasks are available to assist in development:

  • mix docs
  • mix test
  • mix coveralls
  • mix credo --strict
  • mix bench
  • mix profile

License

This project is released under the terms of the MIT License.