splendor.splendor package

Subpackages

Submodules

splendor.splendor.constants module

Useful constants regarding the game.

splendor.splendor.features module

Features extraction from SplendorState

splendor.splendor.features.agent_buying_power(agent: AgentState) Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int][source]

Find the buying power of a specific agent.

Parameters:

agent – an agent.

Returns:

the agent buying power.

splendor.splendor.features.agent_won(agent: AgentState) bool[source]

Check if a given agent has won.

Parameters:

agent – an agent.

Returns:

whether or not the agent has won.

splendor.splendor.features.build_array(base_array: ndarray[Any, dtype[_ScalarType_co]], instruction: Tuple[int, ...]) ndarray[Any, dtype[_ScalarType_co]][source]

construct an np.array from given instructions.

splendor.splendor.features.diminish_return(value: int | float) float[source]

calculate the diminished return of a value.

Parameters:

value – a value.

Returns:

the calculated diminished value of the given value.

splendor.splendor.features.extract_cards(game_state: SplendorState, agent_index: int) ndarray[Any, dtype[_ScalarType_co]][source]

Extract all the vector representations of the cards (both dealt & reserved).

Parameters:
  • game_state – the state of the game - the dealt cards of this state would be encoded.

  • agent_index – which agent’s reserved cards should be extracted.

Returns:

a vector of all the cards (both dealt & reserved - 15 in total), shape would be (15 * 13,). Each 13 entries slice corresponds to a card. The order of the cards is as follows: 1) the first 12 cards. 2) the 3 reserved cards.

Note:

the shape of the resulting vector is constant, rather than dependent upon the state. if for example there aren’t any reserved cards then the 3 reserved cards slots would be filled with zeros.

splendor.splendor.features.extract_metrics(game_state: SplendorState, agent_index: int) ndarray[Any, dtype[_ScalarType_co]][source]

Extract metrics/features from a given state.

Parameters:
  • game_state – a game state.

  • agent_index – the index an agent.

Returns:

the extracted feature vector of that state.

splendor.splendor.features.extract_metrics_with_cards(game_state: SplendorState, agent_index: int) ndarray[Any, dtype[_ScalarType_co]][source]

Extract metrics from state & encoded the cards as vectors.

Returns:

the long vector representing the full state, both it’s features and it’s cards.

splendor.splendor.features.extract_reserved_cards(game_state: SplendorState, agent_index: int) List[ndarray[Any, dtype[_ScalarType_co]]][source]

Extract all the vector representations of the cards (only reserved).

Parameters:
  • game_state – the state of the game.

  • agent_index – which agent’s reserved cards should be extracted.

Returns:

a vector of all the reserved cards, shape would be (3 * 13,). Each 13 entries slice corresponds to a card.

Note:

the shape of the resulting vector is constant, rather than dependent upon the state. if for example there aren’t any reserved cards then the 3 reserved cards slots would be filled with zeros.

splendor.splendor.features.get_agent(game_state: SplendorState, agent_index: int) AgentState[source]

Extract the AgentState of a specific agent from the game state.

splendor.splendor.features.get_color_encoder() OneHotEncoder[source]

Return an encoder of all the colors (including yellow)

Note:

this function is cached in order to avoid re-creation of this encoder. however this means that we uses the same encoder. use with caution.

splendor.splendor.features.get_indices_access_by_color(color_name: str) ndarray[Any, dtype[_ScalarType_co]][source]

convert gem’s color name to an indexing array.

splendor.splendor.features.get_yellow_gem_index() int[source]

Return the index of the yellow color within the one-hot representation of the colors.

splendor.splendor.features.missing_gems_to_card(card: Card, buying_power: Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]) Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int][source]

find how many gems are required in order to buy a specific card.

Parameters:
  • card – a card up for purchase.

  • buying_power – the current buying power.

Returns:

the required gems for purchasing that card.

splendor.splendor.features.normalize_metrics(metrics: ndarray[Any, dtype[_ScalarType_co]]) ndarray[Any, dtype[_ScalarType_co]][source]

normalize the feature vector.

Parameters:

metrics – un-normalized features vector.

Returns:

normalized features vector.

splendor.splendor.features.turns_made_by_agent(agent: AgentState) int[source]

Extract the number of turns made by a given agent.

splendor.splendor.features.turns_to_buy_card(missing_gems: ValuesView[int]) int[source]

calculate an optimistic estimate for the amount of turns required in order to buy a card by collecting all the missing gems without any interference from the opponents.

Parameters:

missing_gems – the missing gems required to buy a card.

Returns:

an estimate on how many turns are required in order to collect all those gems.

splendor.splendor.features.vectorize_card(card: Card | None) ndarray[Any, dtype[_ScalarType_co]][source]

Return the vector form a given card. This is required when supplying an agent such as the DQN or PPO with a representation of a state via features vector, that vector must also describe which cards are present.

Parameters:

card – a card to be vectorized.

Returns:

the vector representation of the given card.

Note:

this function can’t be cached since Card isn’t hashable…

splendor.splendor.splendor_displayer module

class splendor.splendor.splendor_displayer.AgentArea(root, agent_id, agent_title)[source]

Bases: object

set_agent_title(score)[source]
update(agent, resources)[source]
class splendor.splendor.splendor_displayer.BoardArea(root)[source]

Bases: object

update(state, resources, no_highlighting)[source]
class splendor.splendor.splendor_displayer.GUIDisplayer(half_scale, delay=0.1, no_highlighting=False)[source]

Bases: Displayer

EndGame(game_state, scores)[source]
ExcuteAction(player_id, action, game_state)[source]
InitDisplayer(runner)[source]
TimeOutWarning(runner, id)[source]
close_window()[source]
end_fullscreen(event=None)[source]
get_selection(event)[source]
prime_action_box()[source]
toggle_fullscreen(event=None)[source]
user_input(actions)[source]
class splendor.splendor.splendor_displayer.TextDisplayer[source]

Bases: Displayer

EndGame(game_state, scores)[source]
ExcuteAction(i, move, game_state)[source]
InitDisplayer(runner)[source]
StartRound(game_state)[source]
TimeOutWarning(runner, id)[source]
user_input(actions)[source]
splendor.splendor.splendor_displayer.can_buy(agent, card)[source]
splendor.splendor.splendor_displayer.make_label(master, x, y, h, w, *args, **kwargs)[source]

splendor.splendor.splendor_model module

class splendor.splendor.splendor_model.Card(colour, code, cost, deck_id, points)[source]

Bases: object

class splendor.splendor.splendor_model.SplendorGameRule(num_of_agent)[source]

Bases: GameRule

calScore(game_state, agent_id)[source]
gameEnds()[source]
generatePredecessor(state, action, agent_id)[source]
generateSuccessor(state, action, agent_id)[source]
generate_return_combos(current_gems, collected_gems)[source]
getLegalActions(game_state, agent_id)[source]
initialGameState()[source]
noble_visit(agent, noble)[source]
resources_sufficient(agent, costs)[source]
class splendor.splendor.splendor_model.SplendorState(num_agents)[source]

Bases: GameState

class AgentState(_id)[source]

Bases: object

class BoardState(num_agents)[source]

Bases: object

deal(deck_id)[source]
dealt_list()[source]

splendor.splendor.splendor_utils module

splendor.splendor.splendor_utils.ActionToString(agent_id, action)[source]
splendor.splendor.splendor_utils.AgentToString(agent_id, ps)[source]
class splendor.splendor.splendor_utils.AgentTrace(pid)[source]

Bases: object

splendor.splendor.splendor_utils.BoardToString(game_state)[source]
splendor.splendor.splendor_utils.GemsToString(gem_dict)[source]
splendor.splendor.splendor_utils.convert_filename(filename)[source]

splendor.splendor.types module

define useful type hints.

class splendor.splendor.types.BuyAction[source]

Bases: TypedDict

TypedDict for buying action.

card: Required[Card]
noble: Required[Tuple[str, Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]]
returned_gems: Required[Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]
type: Required[Literal['buy_available', 'buy_reserve']]
class splendor.splendor.types.CollectAction[source]

Bases: TypedDict

TypedDict for collecting gems action.

collected_gems: Required[Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]
noble: Required[Tuple[str, Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]]
returned_gems: Required[Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]
type: Required[Literal['collect_diff', 'collect_same']]
class splendor.splendor.types.ReserveAction[source]

Bases: TypedDict

TypedDict for reserving a card action.

collected_gems: Required[YellowGemCount]
noble: Required[Tuple[str, Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]]
returned_gems: Required[Dict[Literal['black', 'red', 'yellow', 'green', 'blue', 'white'], int]]
type: Required[Literal['reserve']]
class splendor.splendor.types.YellowGemCount[source]

Bases: TypedDict

TypedDict for the yellow gems count.

yellow: Required[Literal[1] | None]

splendor.splendor.utils module

Usefule utilities.

class splendor.splendor.utils.LimitRoundsGameRule(num_of_agent)[source]

Bases: SplendorGameRule

Wraps SplendorGameRule.

gameEnds()[source]

Limits the game to ROUNDS_LIMIT rounds, so random initial agents won’t get stuck by accident.

Module contents