Skip to main content

Easily build a BattleSnake

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

battlesnake_builder-0.1.2.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file battlesnake_builder-0.1.2.tar.gz.

File metadata

  • Download URL: battlesnake_builder-0.1.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for battlesnake_builder-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f6178ac6c51db1d6e159842034651e744076d96fcba5678bdfaf8acb1ee98e1b
MD5 396edea48a955be8cc0043e826d3786c
BLAKE2b-256 8e25b7382799d3ef55ac4e7800f2e3d67d04af721c86e522167ba9609b990a40

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page