apologies.engine

Game engine that coordinates character actions to play a game.

Module Contents

class apologies.engine.Character

A character that plays a game, which could be a person or could be computer-driven.

name

The name of this character

Type:

str

source

The character input source from which moves are taken

Type:

CharacterInputSource

name: str
source: apologies.source.CharacterInputSource
choose_move(mode: apologies.game.GameMode, view: apologies.game.PlayerView, legal_moves: List[apologies.rules.Move], evaluator: Callable[[apologies.game.PlayerView, apologies.rules.Move], apologies.game.PlayerView]) apologies.rules.Move

Choose the next move for a character via the user input source.

Parameters:
  • mode (GameMode) – Game mode

  • view (PlayerView) – Player-specific view of the game

  • legal_moves (Set[Move]) – The set of legal moves

  • evaluator (Callable[[PlayerView, Move], PlayerView]) – Function to evaluate a move, returning new state

Returns:

The character’s next as chosen by the configured source

Return type:

Move

class apologies.engine.Engine

Game engine that coordinates character actions in a game.

Normally, playing a game via an engine is as simple as:

engine.start_game()
while not engine.completed:
  state = engine.play_next()

This synchronously plays a turn for each player, one after another, until the game is complete. Other, more fine-grained methods exist if you need to structure game play differently for your purposes (for instance, to train a machine learning model or to play the game in an asynchronous event-driven environment).

mode

The game mode

Type:

GameMode

characters

Characters playing the game

Type:

List[Character]

first

The first player, chosen randomly by default

Type:

PlayerColor

property players: int

Number of players in the game.

property state: str

String describing the state of the game.

property game: apologies.game.Game

A reference to the underlying game.

property started: bool

Whether the game is started.

property completed: bool

Whether the game is completed.

property colors: Dict[apologies.game.PlayerColor, Character]
mode: apologies.game.GameMode
characters: List[Character]
first: apologies.game.PlayerColor
winner() Tuple[Character, apologies.game.Player] | None

Return the winner of the game, as a tuple of (Character, Player)

reset() apologies.game.Game

Reset game state.

start_game() apologies.game.Game

Start the game, returning game state.

Returns:

Current state of the game.

Return type:

Game

next_turn() Tuple[apologies.game.PlayerColor, Character]

Get the color and character for the next turn This will give you a different player each time you call it.

play_next() apologies.game.Game

Play the next turn of the game, returning game state as of the end of the turn.

Returns:

Current state of the game.

Return type:

Game

draw() apologies.game.Card

Draw a random card from the game’s draw pile.

discard(card: apologies.game.Card) None

Discard back to the game’s discard pile.

Construct the legal moves based on a player view, using the passed-in card if provided.

choose_next_move(character: Character, view: apologies.game.PlayerView) apologies.rules.Move

Choose the next move for a character based on a player view.

execute_move(color: apologies.game.PlayerColor, move: apologies.rules.Move) bool

Execute a move for a player, returning True if the player’s turn is done.