Skip to main content

Python SDK for Grid — Tron-style autonomous bot battles

Project description

grid-sdk-py

Python SDK for Grid — a Tron-style competitive bot battle platform.

Installation

pip install grid-sdk-py

Quick start

import os
from gridbot import Client, Config, StrategyFunc, GameState, Direction
from gridbot.helpers import safe_moves, flood_fill

def my_strategy(state: GameState) -> Direction:
    moves = safe_moves(state)
    if not moves:
        return Direction(state.you.direction)
    # pick the direction with the most open space
    best, best_space = moves[0], -1
    for d in moves:
        nx, ny = d.apply(state.you.x, state.you.y)
        space = flood_fill(nx, ny, state)
        if space > best_space:
            best_space = space
            best = d
    return best

client = Client(Config(
    server_url=os.environ.get("BOT_SERVER", "ws://localhost:8083"),
    token=os.environ["BOT_TOKEN"],
    strategy=StrategyFunc(my_strategy),
))
client.run()
BOT_TOKEN=your-token python bot.py

Architecture

The client uses two threads:

  • WebSocket thread — receives frames, replies to server PINGs immediately (never blocked).
  • Strategy thread — reads the latest game state, computes your move, sends the command.

This prevents disconnection when strategy computation takes longer than the server's 30 s pong timeout.

Key helpers (gridbot.helpers)

Function Description
safe_moves(state) Directions that don't hit a wall or trail
safe_moves_detailed(state) Same, with target position + head-on risk
flood_fill(x, y, state) Reachable empty cells from (x, y)
voronoi_bfs(mx, my, ox, oy, state) Territorial split — (my_cells, opp_cells)
head_on_risk(x, y, state) True if an opponent could also move here
find_closest_opponent(state) Nearest alive opponent bot
manhattan_distance(x1, y1, x2, y2)
wall_count(x, y, state) Adjacent non-empty cells (0–4)

Strategy interface

from gridbot import Strategy, MatchAware, GameState, Direction

class MyBot(Strategy, MatchAware):
    def on_match_start(self, state: GameState) -> None:
        ...  # called at the start of every match

    def on_death(self, state: GameState) -> None:
        ...  # called when your bot dies

    def move(self, state: GameState) -> Direction:
        ...  # called every turn — must return within 500 ms

Requirements

  • Python 3.10+
  • websocket-client >= 1.7.0

License

MIT

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

grid_sdk_py-0.3.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grid_sdk_py-0.3.4-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file grid_sdk_py-0.3.4.tar.gz.

File metadata

  • Download URL: grid_sdk_py-0.3.4.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for grid_sdk_py-0.3.4.tar.gz
Algorithm Hash digest
SHA256 108c1abc6727c7ad92dff88e887f9435c68be38fc2c4fca6b7e50a479fa90dfc
MD5 a6a4d78229c5a3e94c2d39c239862c95
BLAKE2b-256 bd614441e6d9fa04540b28006e7a7f6391d94f3ec3d9961675374c5a93a2d5c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for grid_sdk_py-0.3.4.tar.gz:

Publisher: publish.yml on tot0p/grid-sdk-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grid_sdk_py-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: grid_sdk_py-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for grid_sdk_py-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ec18c30822248491e3d06cb33ff50e2ed2d7d04c9adc514831f5cca1798a3f
MD5 eaf1096d70c738af10022f39a266f0ed
BLAKE2b-256 9a337c4b918fe5a0101e991a45a5668cd5440953ea5d8fe879d1cd124af9e91f

See more details on using hashes here.

Provenance

The following attestation bundles were made for grid_sdk_py-0.3.4-py3-none-any.whl:

Publisher: publish.yml on tot0p/grid-sdk-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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