Skip to main content

Easily build a BattleSnake

Project description

battlesnake-builder

Easily build a battlesnake in python

Installation

Not published yet

$ pip install battlesnake_builder

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.coord.y > me.coord.y:
        return "up"
    elif closest_food.coord.y < me.coord.y:
        return "down"

    if closest_food.coord.x > me.coord.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.1.tar.gz (5.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: battlesnake_builder-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0

File hashes

Hashes for battlesnake_builder-0.1.1.tar.gz
Algorithm Hash digest
SHA256 939c1372cbd171b2e2a7cc80a1c62f0d09114ca4cd19f00ec7fccc4cede40dcc
MD5 eb7574c42e82e262c4ba2cb002ba29fa
BLAKE2b-256 445199f1184dccc4f882f415c53d6d8b94aac010f5d5b1ec985bfc6249c8b218

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