Skip to main content

battlesnake-builder

Easily build a battlesnake in python

Installation

$ pip install battlesnake_builder

Requirements

  • python >= 3.10
  • Flask >= 2.0.2

Examples

Basic snake that always moves up

from battlesnake_builder import BattleSnake

my_snake = BattleSnake()

@my_snake.on_move
def move(data, _store):
    return "up"

my_snake.run()

More advanced snake that looks for food

from battlesnake_builder import BattleSnake

my_snake = BattleSnake()

@my_snake.on_move
def move(data, _store):
    me = data.you
    closest_food = data.board.closest_food(me)

    if closest_food.y > me.head.y:
        return "up"
    elif closest_food.y < me.head.y:
        return "down"

    if closest_food.x > me.head.x:
        return "right"

    return "left"

my_snake.run()

Snake with static config

from battlesnake_builder import BattleSnake, Config

my_snake = BattleSnake(Config(apiversion="0.1"))

Or

from battlesnake_builder import BattleSnake

my_snake = BattleSnake({
    "apiversion": "0.1"
})

Snake with dynamic config

from battlesnake_builder import BattleSnake
import random

my_snake = BattleSnake()

@my_snake.on_config
def config():
    tails = ["default","curled"]
    return {
        "tail": random.choice(tails)
    }

my_snake.run()

You can also mix a static and dynamic config. If you overwrite an existing value, the newer one is getting adapted.

Shout something

@my_snake.on_move
def move(_data, _store):
    return {
        "move": "up",
        "shout": "WHY ARE WE SHOUTING"
    }

Snake that stores data across events

from battlesnake_builder import BattleSnake

my_snake = BattleSnake()

@my_snake.on_start
def start(data, store):
    store["snake_amount"] = len(data.board.snakes)

@my_snake.on_move
def end(data, store):
    shout = "Everyone is alive"
    if len(data.board.snakes) < store["snake_amount"]:
        shout = "Some snake died!"

    return {
        "move": "up",
        "shout": shout
    }

my_snake.run()

All features

from battlesnake_builder import BattleSnake, Config, Data

my_snake = BattleSnake(Config(tail="curly"))

@my_snake.on_config
def config():
    return {
        "head": "default"
    }

@my_snake.on_start
def start(data: Data, store: dict):
    store["start_snake_size"] = len(data.board.snakes)

@my_snake.on_move
def move(data: Data, store: dict):
    shout = "Everything is fine"
    if store["start_snake_size"] != len(data.board.snakes):
        shout = "Oh no! Someone lost :("

    for m in ["up", "down", "left", "right"]:
        if data.board.is_move_safe(data.you, m):
            return {
                "move": m,
                "shout": shout
            }
    return {
        "move": "up",
        "shout": shout
    }

@my_snake.on_end
def end(data: Data, store: dict):
    end_snake_size = len(data.board.snakes)
    print(f"Out of {store['start_snake_size']} Snakes, only {end_snake_size} survived")


my_snake.run()

Metadata

Release files for battlesnake-builder 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for battlesnake-builder 0.1.2
File Size Uploaded
battlesnake_builder-0.1.2.tar.gz 6.6 kB Details

Release files / battlesnake_builder-0.1.2.tar.gz

Download URL battlesnake_builder-0.1.2.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f6178ac6c51db1d6e159842034651e744076d96fcba5678bdfaf8acb1ee98e1b
BLAKE2b-256 checksum
How to use checksums
8e25b7382799d3ef55ac4e7800f2e3d67d04af721c86e522167ba9609b990a40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page