Skip to content

Game state

From a given RuleSet you can create GameState instances, which track the state of the tile rack and played tiles for a player:

from rummikub_solver import RuleSet

ruleset = RuleSet()  # standard settings for a Sabra-rules Rummikub game
game = ruleset.new_game()

# add tiles to the rack
all_tiles = ruleset.tiles
black = all_tiles[:ruleset.numbers]
blue = all_tiles[ruleset.numbers:ruleset.numbers * 2]
orange = all_tiles[ruleset.numbers * 2:ruleset.numbers * 3]
red = all_tiles[ruleset.numbers * 3:ruleset.numbers * 4]
# a typical run of 14 random starter tiles
game.add_rack(
    black[9], black[11], black[12],    # black 10, 12 and 13 tiles
    red[2], red[3], red[10], red[12],  # red 3, 4, 11 and 12 tiles
    orange[1], red[1], blue[1],        # orange, red and blue 2 tiles
    blue[7], blue[7]                   # both the blue 8 tiles
    orange[8], blue[8]                 # orange and blue 9 tiles.
)
# player can't make a move, so picks a black 11 tile
game.add_rack(black[10])
# another player can make moves and placed tiles on the table
game.add_table(
    blue[0], blue[1], blue[2],         # blue 1, 2 and 3 tiles
    black[10], orange[10], red[10]     # black, orange and red 11 tiles
)

GameState instances are pickleable, to facilitate saving a player's game state between sessions.

rummikub_solver.GameState

State of a single game for one player.

Tracks the tiles placed on the table and the rack, and if the player has managed to place the initial set of tiles from their rack.

This is normally created by calling RuleSet.new_game().

Methods:

Name Description
reset

Remove all tiles from the rack and table, and set the initial flag.

with_move

Create new state with specific tiles moved from rack to table.

with_table

Create a new state with all table tiles replaced.

table_only

Create new state with just the table tiles.

add_rack

Add tiles to the rack.

remove_rack

Remove tiles from the rack.

add_table

Add tiles to the table.

remove_table

Remove tiles from the table.

Attributes:

Name Type Description
initial bool

Does this player still have to make their opening move.

rack Counter[Tile]

The unique tiles on this rack, with their counts.

sorted_rack list[Tile]

The rack tiles as a sorted list.

table Counter[Tile]

The unique tiles on the table, with their counts.

sorted_table list[Tile]

The table tiles as a sorted list.

initial property writable

initial: bool

Does this player still have to make their opening move.

rack property

rack: Counter[Tile]

The unique tiles on this rack, with their counts.

sorted_rack property

sorted_rack: list[Tile]

The rack tiles as a sorted list.

table property

table: Counter[Tile]

The unique tiles on the table, with their counts.

sorted_table property

sorted_table: list[Tile]

The table tiles as a sorted list.

reset

reset() -> None

Remove all tiles from the rack and table, and set the initial flag.

with_move

with_move(*tiles: Tile | int) -> GameState

Create new state with specific tiles moved from rack to table.

The tiles are verified to be on the rack and are moved to the table. This doesn't mutate this state, and instead creates a new state with the new tile locations.

The new state will have the initial flag cleared.

Parameters:

Name Type Description Default
tiles Tile | int

The tiles to remove from the rack and add to the table.

()

Returns:

Type Description
GameState

The new state with the tiles moved.

with_table

with_table(*tiles: Tile) -> GameState

Create a new state with all table tiles replaced.

This makes it simpler to pass the set of tiles on the table from player to player in a multi-player setup.

Parameters:

Name Type Description Default
tiles Tile

the tiles to add to the table.

()

Returns:

Type Description
GameState

The new state with the additional tiles on the table.

table_only

table_only() -> GameState

Create new state with just the table tiles.

Returns:

Type Description
GameState

The new game state with the rack empty.

add_rack

add_rack(*tiles: Tile | int) -> None

Add tiles to the rack.

Tiles added are not validated against a ruleset.

Parameters:

Name Type Description Default
tiles Tile | int

The tiles to add to the rack.

()

remove_rack

remove_rack(*tiles: Tile | int) -> None

Remove tiles from the rack.

If any of the tiles passed in were not on the rack to begin with, they are ignored.

Parameters:

Name Type Description Default
tiles Tile | int

The tiles to remove from the rack.

()

add_table

add_table(*tiles: Tile | int) -> None

Add tiles to the table.

Tiles added are not validated against a ruleset.

Parameters:

Name Type Description Default
tiles Tile | int

The tiles to remove from the rack.

()

remove_table

remove_table(*tiles: Tile | int) -> None

Remove tiles from the table.

If any of the tiles passed in were not on the table to begin with, they are ignored.

Parameters:

Name Type Description Default
tiles Tile | int

The tiles to remove from the table.

()