BlokusEnvironment¶
-
class
colosseumrl.envs.blokus.BlokusEnvironment.
BlokusEnvironment
(config: str = '')[source]¶ Bases:
colosseumrl.BaseEnvironment.BaseEnvironment
Full Blokus environment class with access to the actual game state.
-
static
all_orientations
() → List[str][source]¶ Get the names every possible piece orientation in a game of Blokus.
- Returns
orientations
- Return type
List[str]
-
static
all_piece_types
() → List[str][source]¶ Get the names every possible piece type in a game of Blokus.
- Returns
piece_types
- Return type
List[str]
-
convert_player_perspective_action_to_real_action
(player_action: str, player: int) → str[source]¶ Converts a player-perspective action in coordinance with the player’s rotated observation of the board to the corresponding real action consumable by the actual environment.
- Parameters
player_action (str) – The player-perspective action.
player (int) – The player to view this action from.
- Returns
action – The real action corresponding to the player-perspective action
- Return type
str
See also
blokus.blokus_env.convert_player_perspective_action_to_real_action()
blokus.blokus_env.BlokusEnv.next_state()
You can pass the result of this method to this.
-
convert_real_action_to_player_perspective_action
(action: str, player: int) → str[source]¶ Converts a real action consumable by the actual environment to the corresponding player-perspective action that is in coordinance with the player’s rotated observation of the board
- Parameters
action (str) – The real action.
player (int) – The player to view this action from.
- Returns
player_action – The player-perspective action.
- Return type
str
See also
blokus.blokus_env.convert_player_perspective_action_to_real_action()
You have to convert a player-perspective action to a real action before passing it to the environment
-
current_rewards
(state: object) → List[float][source]¶ Returns current reward for each player (in absolute order, not reltive to any specific player
- Parameters
state (object) – The current state to calculate rewards from
- Returns
rewards – A vector containing the current rewards for each player
- Return type
List[float]
-
static
deserialize_state
(serialized_state: bytearray) → object[source]¶ Convert a serialized bytearray back into a game state.
- Parameters
serialized_state (bytearray) – state bytearray to be deserialized
- Returns
deserialized_state – deserialized state
- Return type
object
-
is_valid_action
(state: object, player: int, action: str) → bool[source]¶ Returns True if an action is valid for a specific player and state.
(Does not validate rotated player-perspective actions)
- Parameters
state (object) – The current state to execute a game step from.
player (int) – The player that would be executing the action.
action (str) – The action in question
- Returns
is_action_valid – whether this action is valid
- Return type
bool
See also
blokus.blokus_env.action_to_string()
,blokus.blokus_env.string_to_action()
,blokus.blokus_env.valid_actions()
,blokus.blokus_env.is_valid_action()
blokus.blokus_env.BlokusEnv.next_state()
If an action is valid, you can pass it to this method.
Notes
This method does not keep track of who’s turn it is. That is up to the user. If a piece may be physically placed at the location suggest by the action, this method returns true, regardless of who just executed their turn or who should be going now.
-
property
max_players
¶ Property holding the max number of players present for a game.
(Always 4 for Blokus)
-
property
min_players
¶ Property holding the number of players present required to play the game.
(Always 4 for Blokus)
-
new_state
(self) → object[source]¶ Create a fresh Blokus board state for a new game.
- Returns
new_state (object) – A state for the new game.
new_players (List[int]) – List of players who’s turn it is in this new state.
See also
blokus.blokus_env.BlokusEnv.state_to_observation()
Convert states to player specific observations with this method.
blokus.blokus_env.BlokusEnv.next_state()
Pass states to this method to perform a game step.
Notes
States are arbitrary internal Blokus logic types. In a normal use case, there is no need to access or modifying individual data in a state.
States are not in a format intended to be consumable for a reinforcement learning agent. Reinforcement leaning agents are intended to take observations as input, and
blokus.blokus_env.BlokusEnv.state_to_observation()
can be used to convert states into observations.
-
next_state
(state: object, players: int, actions: str) → Tuple[object, List[int], List[float], bool, Optional[List[int]]][source]¶ Perform a game step from a given state.
- Parameters
state (object) – The current state to execute a game step from.
players (List[int]) – The players who’s turn it is and are executing actions. For Blokus, only one player should ever be passed in this list at a time.
actions (List[str],) – The actions to be executed by the players who’s turn it is. For Blokus, only one action should ever be passed in this list at a time.
- Returns
next_state (object) – The new state resulting after the game step.
next_players (List[int]) – The new players who’s turn it is after the game step. For Blokus, this will always only be one player.
rewards (List[float]) – Rewards for the players who’s turn it was. For Blokus, this will always only be one reward for the single player that execute the action.
terminal (bool) – Whether the game is now over.
winners (Union[List[int], None]) – The players that won the game if it is over, else None.
See also
blokus.blokus_env.BlokusEnv.state_to_observation()
Convert states to player specific observations with this method.
blokus.blokus_env.BlokusEnv.new_state()
Create new game states with this method.
Notes
States are arbitrary internal Blokus logic types. In a normal use case, there is no need to access or modifying individual data in a state.
States are not in a format intended to be consumable for a reinforcement learning agent. Reinforcement leaning agents are intended to take observations as input, and blokus.blokus_env.state_to_observation can be used to convert states into observations.
-
static
observation_names
()[source]¶ Get the names for each key in an observation dictionary.
- Returns
observation_names
- Return type
List[int]
-
property
observation_shape
¶ Property holding the numpy array shapes for each value in an observation dictionary.
-
player_perspective_valid_actions
(state: object, player: int) → List[str][source]¶ Valid actions for a specific state and player from the player’s perspective in coordinance with the player’s rotated observation of the board. If there are no valid actions, empty string is given to represent a no-op
- Parameters
state (object) – The current state to execute a game step from.
player (int) – The player for which valid actions will be returned.
- Returns
valid_actions – A list of valid action strings which the player may execute. These actions are rotated to match this player’s perspective of the board,
- Return type
list[int]
See also
blokus.blokus_env.action_to_string()
,blokus.blokus_env.string_to_action()
blokus.blokus_env.convert_player_perspective_action_to_real_action()
You have to convert a player-perspective action to a real action before passing it to the environment
Notes
This method does not keep track of who’s turn it is. That is up to the user. If the specified player can physically place a piece at a location (from the player’s perspective), it will be returned as a valid action.
-
static
serializable
() → bool[source]¶ Whether or not this class supports state serialization.
(This always returns True for Blokus)
- Returns
is_serializable – True
- Return type
bool
-
static
serialize_state
(state: object) → bytearray[source]¶ Serialize a game state and convert it to a bytearray to be saved or sent over a network.
- Parameters
state (object) – state to be serialized
- Returns
serialized_state – serialized state
- Return type
bytearray
-
state_to_observation
(state: object, player: int) → Dict[str, numpy.ndarray][source]¶ Convert the raw game state to a consumable observation for a specific player agent.
- Parameters
state (object) – The state to create an observation for
player (int) – The player who is intended to view the observation
- Returns
observation – The observation for the player RL agent to view
- Return type
Dict[str, np.ndarray]
See also
blokus.blokus_client_env.BlokusClientEnv.wait_for_turn()
also returns observations
blokus.blokus_client_env.BlokusClientEnv.step()
also returns observations
Notes
Observations returned here are of the same format as those given by
blokus.blokus_client_env.BlokusClientEnv.step()
.Observations are specific to individual players. Every observation is presented as if the player intended to receive it were actually player 0. This is done so that an RL agent only has to learn to perform moves that make player 0 win and other players lose.
-
valid_actions
(state: object, player: int) → List[str][source]¶ Valid actions for a specific state and player. If there are no valid actions, empty string is given to represent a no-op
- Parameters
state (object) – The current state to execute a game step from.
player (int) – The player for which valid actions will be returned.
- Returns
valid_actions – A list of valid action strings which the player may execute.
- Return type
list[int]
See also
blokus.blokus_env.action_to_string()
,blokus.blokus_env.string_to_action()
,blokus.blokus_env.is_valid_action()
blokus.blokus_env.BlokusEnv.next_state()
If an action is valid, you can pass it to this method.
Notes
Players must always choose actions included in this list. If no actions are valid for a player, this function returns an empty string. When it is a player’s turn, if the player has no valid actions, it must pass an empty string as its action for
blokus.blokus_env.BlokusEnv.next_state()
for the game to continue.This method does not keep track of who’s turn it is. That is up to the user. If the specified player can physically place a piece at a location, it will be returned as a valid action.
-
valid_actions_dict
(state: object, player: int) → Dict[str, Dict[Tuple[int, int], List[str]]][source]¶ Valid actions for a specific state and player in the dictionary form {piece_type: {index: [orientation,]}}
- Parameters
state (object) – The current state to execute a game step from.
player (int) – The player for which valid actions will be returned.
- Returns
valid_actions_dict – A dictionary of valid actions broken down into piece_type, index, and orientations.
- Return type
Dict[str, Dict[Tuple[int], List[str]]]
See also
blokus.blokus_env.action_to_string()
,blokus.blokus_env.string_to_action()
,blokus.blokus_env.valid_actions()
,blokus.blokus_env.is_valid_action()
blokus.blokus_env.BlokusEnv.next_state()
If an action is valid, you can pass it to this method.
Notes
This method does not keep track of who’s turn it is. That is up to the user. If the specified player can physically place a piece at a location, it will be returned as a valid action.
-
static
-
colosseumrl.envs.blokus.BlokusEnvironment.
COLOR_TO_PLAYER
= {0: -1, 1: 0, 2: 1, 3: 2, 4: 3}¶
-
colosseumrl.envs.blokus.BlokusEnvironment.
PIECE_NAME_TO_INDEX
= {'domino1': 1, 'monomino1': 0, 'pentominoe1': 9, 'pentominoe10': 18, 'pentominoe11': 19, 'pentominoe12': 20, 'pentominoe2': 10, 'pentominoe3': 11, 'pentominoe4': 12, 'pentominoe5': 13, 'pentominoe6': 14, 'pentominoe7': 15, 'pentominoe8': 16, 'pentominoe9': 17, 'tetrominoes1': 4, 'tetrominoes2': 5, 'tetrominoes3': 6, 'tetrominoes4': 7, 'tetrominoes5': 8, 'trominoe1': 2, 'trominoe2': 3}¶
-
colosseumrl.envs.blokus.BlokusEnvironment.
PLAYER_TO_COLOR
= {0: 1, 1: 2, 2: 3, 3: 4}¶
-
colosseumrl.envs.blokus.BlokusEnvironment.
State
¶ alias of
builtins.object
-
colosseumrl.envs.blokus.BlokusEnvironment.
action_to_string
(piece_type: str, index: Tuple[int, int], orientation: str) → str[source]¶ Convert a piece_type, index, and orientation into a formatted action string.
- Parameters
piece_type (str) – The type of blokus piece to be placed in the action.
index (Tuple[int, int]) – The location where the piece will be placed in the action.
orientation (str) – The orientation in which the piece will be placed in the action.
- Returns
action_string
- Return type
str
See also
blokus.blokus_env.BlokusEnv.all_piece_types()
Get list of all possible piece types
blokus.blokus_env.BlokusEnv.all_orientations()
Get list of all possible orientations
blokus.blokus_env.BlokusEnv.is_valid_action()
Check if action string is valid
-
colosseumrl.envs.blokus.BlokusEnvironment.
display_board
(state: object, player_num: int, winners: Optional[List[int]] = None)[source]¶ Render Board with graphical interface
- Parameters
state (object) – The state to render
player_num (int) – The current player whose turn it is.
winners (List[int] (optional)) – The winners of the game
Notes
blokus.blokus_env.start_gui()
must first be called once before calling this function.See also
blokus.blokus_env.start_gui()
,blokus.blokus_env.terminate_gui()
,blokus.blokus_env.print_board()
-
colosseumrl.envs.blokus.BlokusEnvironment.
print_board
(state: object)[source]¶ Print board to console
- Parameters
state (object) – The state to render
Notes
Pieces are marked by the player number that placed them. -1 marks empty spaces.
See also
blokus.blokus_env.start_gui()
,blokus.blokus_env.terminate_gui()
,blokus.blokus_env.display_board()
-
colosseumrl.envs.blokus.BlokusEnvironment.
start_gui
()[source]¶ Initialize graphical interface in order to render board.
You must first call this function once before making calls to
blokus.blokus_env.display_board()
.See also
blokus.blokus_env.display_board()
,blokus.blokus_env.terminate_gui()
-
colosseumrl.envs.blokus.BlokusEnvironment.
string_to_action
(action_str: str) → Optional[Tuple[str, Tuple[int, int], str]][source]¶ Convert a formatted action string into piece_type, index, and orientation.
- Parameters
action_str (str) – The action in string format
- Returns
piece_type (str) – The type of blokus piece to be placed in the action.
index (Tuple[int, int]) – The location where the piece will be placed in the action.
orientation (str) – The orientation in which the piece will be placed in the action.