game_logic.py
+ board snippet
+ logic snippet
+ field check
+ choice snippet
Terminal Output
0 lines
Gambit Python API GameBoard class

Every Gambit game script gets a pre-built GameBoard class. Instantiate it once, place your pieces, attach a click handler, and the engine takes care of rendering. Handlers that use await must be declared async def.

Setup
GameBoard(variant_id) constructor
Creates and renders a board. variant_id must match an id from the Boards registry (e.g. '8x8_chess').
board = GameBoard('8x8_chess')
board.set_turn(player) → None
Sets whose turn it is. player is 'player1' or 'player2'.
board.get_turn() → str
Returns 'player1' or 'player2'.
board.set_logic(piece, move_id, capture_id) → None
Assigns movement and capture rules to a piece type. IDs come from the Logics registry.
board.set_logic('P', 'step_1', 'displace') board.set_logic('N', 'knight', 'displace')
Placing & Moving Pieces
board.add_piece(x, y, player, code) → None
Places a piece on the board. code is the piece identifier, e.g. 'wK' (white King) or 'bP' (black Pawn). Prefix w = player1, b = player2.
board.add_piece(3, 7, 'player1', 'wK') board.add_piece(4, 0, 'player2', 'bQ')
board.remove_piece(x, y) → None
Removes whatever piece is on square (x, y). Does nothing if the square is already empty.
board.move_piece(fx, fy, tx, ty) → None
Moves the piece at (fx, fy) to (tx, ty), replacing any piece already there.
Field Inspection
board.has_piece(x, y) → bool
Returns True if a piece occupies (x, y), otherwise False. The lightest check — use this when you only need a yes/no.
board.get_piece(x, y) → str
Returns the raw piece code, e.g. 'wK', or an empty string '' if the square is empty.
board.get_piece_info(x, y) → dict
Returns a dict with full details about a square. Keys:
'occupied'bool — whether any piece is present
'piece'str — raw code e.g. 'wK', or None
'player''player1' | 'player2' | None
'type'piece letter e.g. 'K', 'P', or None
info = board.get_piece_info(x, y) if info['occupied'] and info['type'] == 'K': print("Found a King!")
board.get_all_pieces() → dict
Returns a dict of every occupied square. Keys are 'x,y' strings; values are dicts with piece, player, and type.
for coord, p in board.get_all_pieces().items(): print(coord, p['piece'], p['player'])
Click Handler
board.on_click(callback) → None
Registers a function that is called whenever a player clicks a square. The callback receives x and y as integers. Returning True blocks the engine's default move logic. Use async def if your handler calls await board.choice().
async def on_click(x, y): info = board.get_piece_info(x, y) # ... your logic here ... return True board.on_click(on_click)
Choice Modal
await board.choice(question, options) → any
Shows a modal dialog with a question and a list of buttons. Pauses execution until the player picks one, then returns the chosen value. options can be strings, numbers, or a mix.
⚠ The click handler must be async def to use await.
# Promotion example async def on_click(x, y): piece = await board.choice( "Promote pawn to:", ['Q', 'R', 'B', 'N'] ) n = await board.choice("Pick a number:", [1,2,3,4]) return True
Board Properties
board.widthint — number of columns
board.heightint — number of rows
board.categorystr — e.g. 'chess', 'go', 'reversi'
board.variant_idstr — the id passed to GameBoard()
Use + field check and + choice snippet tabs to insert ready-made code.