games.core.game.containers.base¶

Container models for game pieces in the game framework.

This module defines containers for game pieces like decks of cards, bags of tiles, and player hands.

Classes¶

Deck

A deck of cards.

GamePieceContainer

Base container for game pieces.

PlayerHand

A player's hand of pieces.

Module Contents¶

class games.core.game.containers.base.Deck¶

Bases: GamePieceContainer[C]

A deck of cards.

This represents a collection of cards that can be drawn, shuffled, and dealt.

deal(num_players, cards_per_player)¶

Deal cards to multiple players.

Parameters:
  • num_players (int) – Number of players to deal to

  • cards_per_player (int) – Number of cards per player

Returns:

List of lists, where each inner list contains a player’s cards

Return type:

list[list[C]]

discard(card)¶

Add a card to the discard pile.

Parameters:

card (C) – Card to discard

Return type:

None

draw()¶

Draw the top card and set its face up/down based on deck configuration.

Returns:

The drawn card, or None if deck is empty

Return type:

C | None

draw_bottom()¶

Draw the bottom card.

Returns:

The bottom card, or None if deck is empty

Return type:

C | None

peek_bottom(count=1)¶

Look at bottom cards without drawing.

Parameters:

count (int) – Number of cards to peek at

Returns:

List of cards from the bottom

Return type:

list[C]

peek_top(count=1)¶

Look at top cards without drawing.

Parameters:

count (int) – Number of cards to peek at

Returns:

List of cards from the top

Return type:

list[C]

recycle_discards(shuffle=True)¶

Move all cards from discard pile back into the deck.

Parameters:

shuffle (bool) – Whether to shuffle the deck after recycling

Return type:

None

class games.core.game.containers.base.GamePieceContainer(/, **data)¶

Bases: pydantic.BaseModel, Generic[T]

Base container for game pieces.

This represents a collection of game pieces like a deck of cards, a bag of tiles, or a player’s hand.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

add(piece, position='top')¶

Add a piece to this container.

Parameters:
  • piece (T) – The piece to add

  • position (str) – Where to add the piece (“top”, “bottom”, or “random”)

Raises:

ValueError – If position is not valid

Return type:

None

clear()¶

Remove all pieces from the container.

Return type:

None

count()¶

Count pieces in the container.

Returns:

Number of pieces in the container

Return type:

int

draw()¶

Draw the top piece.

Returns:

The top piece, or None if container is empty

Return type:

T | None

draw_many(count)¶

Draw multiple pieces from the top.

Parameters:

count (int) – Number of pieces to draw

Returns:

List of drawn pieces

Return type:

list[T]

filter(predicate)¶

Filter pieces by predicate.

Parameters:

predicate (collections.abc.Callable[[T], bool]) – Function that returns True for pieces to include

Returns:

List of pieces matching the predicate

Return type:

list[T]

find(predicate)¶

Find a piece matching the predicate.

Parameters:

predicate (collections.abc.Callable[[T], bool]) – Function that returns True for the desired piece

Returns:

The first matching piece, or None if not found

Return type:

T | None

get_property(key, default=None)¶

Get a container property.

Parameters:
  • key (str) – Property name

  • default (Any) – Default value if property doesn’t exist

Returns:

Property value or default

Return type:

Any

is_empty()¶

Check if container is empty.

Returns:

True if the container is empty, False otherwise

Return type:

bool

peek(count=1)¶

Look at the top pieces without removing them.

Parameters:

count (int) – Number of pieces to peek at

Returns:

List of pieces from the top

Return type:

list[T]

remove(piece_id)¶

Remove a piece by ID.

Parameters:

piece_id (str) – ID of the piece to remove

Returns:

The removed piece, or None if not found

Return type:

T | None

set_property(key, value)¶

Set a container property.

Parameters:
  • key (str) – Property name

  • value (Any) – Property value

Return type:

None

shuffle()¶

Shuffle the pieces in the container.

Return type:

None

class games.core.game.containers.base.PlayerHand¶

Bases: GamePieceContainer[T]

A player’s hand of pieces.

This represents the collection of pieces a player holds, such as cards in a card game or tiles in Scrabble.

add_piece(piece)¶

Add a piece to the hand and assign ownership to the player.

Parameters:

piece (T) – The piece to add

Return type:

None

play_piece(piece_id)¶

Play a piece (remove from hand).

Parameters:

piece_id (str) – ID of the piece to play

Returns:

The played piece, or None if not found

Return type:

T | None

play_pieces(piece_ids)¶

Play multiple pieces.

Parameters:

piece_ids (list[str]) – List of piece IDs to play

Returns:

List of played pieces

Return type:

list[T]