Reference
pycatan
A python module for holding the game state and performing game logic for games of The Settlers of Catan.
pycatan.Game
- class pycatan.Game(board, num_players=4)
A game of Catan. Holds all the game state and game logic for interacting with the board, players and decks.
- Parameters
board (
Board) – The board to use in the Catan gamenum_players (
Optional[int]) – The number of players to start the game with. Defaults to 4
- longest_road_owner
The player who has the longest road token, or None if no players have a road of at least 5 length
- Type
- largest_army_owner
The player how has the largest army, or None if no players have played at least 3 knight cards
- Type
- development_card_deck
The deck of development cards
- Type
List[DevelopmentCard]
- build_settlement(player, coords, cost_resources=True, ensure_connected=True)
Build a settlement by the player given in the coords given, or raises an error if the input is invalid.
- Parameters
player (
Player) – The player who is building the settlementcoords (
Coords) – The coordinates to build the settlement atcost_resources (
Optional[bool]) – Whether to remove the resources required to build a settlement from the player’s hands, and raise an error if they don’t have them. Defaults to Trueensure_connection – Whether to raise an error if the settlement would not be connected to a road owned by the same player. Defaults to True
- Raises
NotEnoughResourcesError – If check_resources is True and the player does not have enough resources
NotConnectedError – If check_connection is True and the settlement would not be connected to any roads owned by the player
- build_road(player, path_coords, cost_resources=True, ensure_connected=True)
Build a road.
- Parameters
player (
Player) – The player who is building the roadpath_coords (
Set[Coords]) – The coordinates of the path to build a road on. Should be two valid connected intersection coordinates (i.e. {(1, 0), (1, -1)})cost_resources (
Optional[bool]) – Whether to remove resources from the player’s hand to build the road, and raise an error if they don’t have enoughensure_connected (
Optional[bool]) – Whether to ensure that the road is connected to another road, settlement or city
- Raises
NotEnoughResourcesError – If check_resources is True and the player doesn’t have the cards to build the road
NotConnectedError – If check_connection is True and the road is not connected to anything
ValueError – If path_coords is not a set of two valid intersection coordinates
CoordsBlockedError – If the position is already blocked by another road/other path building
- upgrade_settlement_to_city(player, coords, cost_resources=True)
Build a city from a settlement.
- Parameters
- Raises
NotEnoughResourcesError – If cost_resources is true and the player doesn’t have enough resources
ValueError – If coords is not a valid intersection
RequiresSettlementError – If there is not a valid settlement at the intersection to upgrade
- add_yield_for_roll(roll)
Add the resources to the player’s hands for the dice roll given.
- Parameters
roll (
int) – The number that was rolled
- add_yield(roll_yield)
Add the yield provided to the player’s hands.
- move_robber(coords)
Move the robber to the coords specified.
- Parameters
coords (
Coords) – The coordinates of the hex to move the robber to- Raises
ValueError – If the coordinates are not a valid hex
- build_development_card(player)
Build a development card and place it in the player’s hand.
- Parameters
player (
Player) – The player building the development card- Raises
NotEnoughResourcesError – If the player cannot afford to build a development card
- Return type
- Returns
The card that the player built and has been added to their hand
- play_development_card(player, card)
Play a development card.
Do not actually change the game state to play the card. Mainly just keep track of how many knight cards each player has played and may change who has the largest army
- Parameters
player (
Player) – The player playing a development cardcard (
DevelopmentCard) – The development card thay are playing
- Raises
ValueError – If the player does not have the card
pycatan.Player
- class pycatan.Player
A Player in a Catan game.
- development_cards
How many of each development card this player has
- Type
Dict[DevelopmentCard, int]
- connected_harbors
The harbors this player is connected to. Used to determine the valid trades
- Type
Set[Harbor]
- has_resources(resources)
Check if the player has the resources given.
- Parameters
resources (
Dict[Resource,int]) – The resources to check that the player has- Returns
True if the player has the resources, false otherwise
- Return type
bool
- remove_resources(resources)
Remove the given resources from the player’s hand.
- Parameters
resources (
Dict[Resource,int]) – The resources to remove- Raises
NotEnoughResourcesError – If the player does not have the resources
- add_resources(resources)
Add some resources to this player’s hand.
- Parameters
resources (
Dict[Resource,int]) – The resources to add
- get_possible_trades()
Get a list of the possible trades for this player.
- Return type
List[Dict[Resource,int]]- Returns
The possible trades for this player. Negative numbers mean the player would give away those resources, positive numbers mean the player would receive those resources
- play_development_card(card)
Mark a development card as played.
i.e. remove it from the player’s hand
- Parameters
card (
DevelopmentCard) – The card to play- Raises
ValueError – If the player does not have the card
- get_random_resource()
Get a random resource from this player.
Weighs the different resources depending on how many the player has in their hand. This is equavalent to randomly picking a resource out of the player’s hand, i.e. when stealing a card via a knight card or rolling a 7.
- Return type
Optional[Resource]- Returns
The resource, or None if the player has no resources
pycatan.Resource
pycatan.DevelopmentCard
- class pycatan.DevelopmentCard(value)
A development card in a game of Catan.
- KNIGHT = 0
The knight card
- YEAR_OF_PLENTY = 1
The year of plenty card
- ROAD_BUILDING = 2
The road building card
- MONOPOLY = 3
The monopoly card
- VICTORY_POINT = 4
Generic type to represent the victory point cards (i.e. library)
pycatan.RollYield
- class pycatan.RollYield
A utility class to represent what each player gets from a roll of the dice.
Contains information about where the resources came from as well.
- all_yields
The sources where the resources came from
- Type
Set[RollYieldSource]
- add_yield(resource, amount, source)
Add a yield to the RollYield.
Also updates total_yield. Use this method instead of directly changing all_yields.
- Parameters
resource (
Resource) – The resource the player has receivedamount (
int) – The amount of the resource the player has received
- Return type
None
pycatan.board
Submodule that is used to hold the board state.
pycatan.board.Board
- class pycatan.board.Board(hexes, harbors={}, robber=None)
An interface for holding the state of Catan boards.
Uses a triangular grid to hold the tiles, intersections and paths. The Board constructor will automatically generate the intersections and paths from a dict of hexes, assuming all the hexes tile correctly.
- Parameters
hexes (
Set[Hex]) – The hexes on the board, keyed by their coordinatesharbors (
Set[Harbor]) – The harbors on the boardrobber (
Optional[Coords]) – The inital coordinates of the robber. If None, then will automatically place the robber on the first desert hex it can find, and raise an error if there are non
- intersections
(Dict[Coords, Intersection]): The intersections on the board, keyed by their coordinates
- paths
The paths on the board, keyed by the coordinates of the two intersections they connect
- harbors
The harbors on the board, keyed by the coords of the path they are attached to
- Type
Dict[frozenset[Coords], Harbor]
- add_path_building(player, building_type, path_coords, ensure_connected=True)
Add an path building to the board.
Do not check if the player has enough resources, or any other checks other than the building’s location being valid.
- Parameters
player (
Player) – The player adding the buildingbuilding_type (
BuildingType) – The building_type of the building being addedpath_coords (
Set[Coords]) – The coordinates the path to build the building on (i.e. the coordinates of the two intersections the path connects)ensure_connected (
Optional[bool]) – Whetehr to ensure that the path building is connected to another building. Defaults to True
- Raises
ValueError – If the path_coords are not valid
CoordsBlockedError – If there is already a building on the path
NotConnectedError – If check_connection is true and the building is not connected to anything
- assert_valid_road_coords(player, path_coords, ensure_connected=True)
Assert that a given edge is a valid place for the player to build a road.
- add_intersection_building(player, coords, building_type, ensure_connected=True)
Add an intersection building to the board.
- Parameters
- Raises
InvalidCoordsError – If coords is not a valid intersection
TooCloseToBuildingError – If the building is too close to another building
PositionAlreadyTakenError – If the position is already taken
- assert_valid_settlement_coords(coords, player, ensure_connected)
Check whether the coordinates given are a valid place to build a settlement.
Does not return anything, but raises an error if the coordinates are not valid.
- Parameters
- Raises
TooCloseToBuildingError – If the building is too close to another building
PositionAlreadyTakenError – If the position is already taken
NotConnectedError – If check_connection is True and the settlement is not connected
- Return type
None
- assert_valid_city_coords(player, coords)
Check whether the coordinates given are a valid place to build a city by the player given.
- is_valid_settlement_coords(player, coords, ensure_connected)
Check whether the given coordinates are a valid place for the player to build a settlement.
- is_valid_city_coords(player, coords)
Check whether the coordinates given are valid city coordinates.
- is_valid_road_coords(player, path_coords, ensure_connected=True)
Check whether the path coordinates given are valid road coordinate for the player given.
- Parameters
- Return type
bool- Returns
Whether the player can build a road on this path
- get_valid_settlement_coords(player, ensure_connected=True)
Get all the valid settlement coordinates for the player to build a settlement.
- get_valid_city_coords(player)
Get all the valid city coordinates for the player to build a city.
- Parameters
player (Player) – The player building the city
- Returns
The coordinates of all the valid city locations
- Return type
Set[Coords]
- get_valid_road_coords(player, ensure_connected=True, connected_intersection=None)
Get all the valid coordinates for the player to build a road.
- Parameters
player (
Player) – The player building the roadensure_connected (
Optional[bool]) – Whether to only return the path coordinates that are connected to the player’s existing roads/settlements. Defaults to Trueconnected_intersection (
Optional[Coords]) – The coords of an intersection that the potential road must be attached to. Defaults to None
- Return type
Set[FrozenSet[Coords]]- Returns
The coordinates of all the paths where the player can build a road.
- get_intersection_connected_intersections(intersection)
Get all the intersections connected to the intersection given by an path.
- Parameters
intersection (
Intersection) – The intersection to get the connected intersections for- Return type
Set[Intersection]- Returns
The intersections that are connected to the intersection given
- get_connected_hex_intersections(hex)
Get all of the intersections that are connected to the hex.
- Parameters
hex (
Hex) – The hex- Return type
Set[Intersection]- Returns
All 6 intersections that are around this hex
- get_hexes_connected_to_intersection(intersection_coords)
Get all the hexes’ coordinates that are connected to the intersection with the coordinates provided.
- get_yield_for_roll(roll)
Calculate the resources given out for a particular roll.
- is_valid_hex_coords(coords)
Check whether the coordinates given are valid hex coordinates.
- Parameters
coords (
Coords) – The coordinates- Return type
bool- Returns
Whether there is a hex at those coordinates
- calculate_player_longest_road(player)
Calculate the length of the longest road segment for the player given.
- Parameters
player (
Player) – The player to calculate the longest road for- Return type
int- Returns
The length of the ongest road segment
- get_paths_for_intersection_coords(coords)
Get all the paths who that connected to the intersection given.
- get_hex_resources_for_intersection(coords)
Get the associated resources for the hexes around the intersection at the coords given.
pycatan.board.Hex
pycatan.board.HexType
pycatan.board.Intersection
- class pycatan.board.Intersection(coords, building=None)
A intersection on the Catan board.
- Parameters
coords (
Coords) – The coordinates of the intersection.building (
Optional[IntersectionBuilding]) – The building on the intersection.
- CONNECTED_CORNER_OFFSETS
The offsets of the intersections that are connected by an path. i.e. to get the connected intersections, add a intersection’s coords to these values, and then filter for which coords are valid intersection coords.
- Type
Set[Coords]
- building
The building on the intersection.
- Type
IntersectionBuilding, optional
pycatan.board.Path
- class pycatan.board.Path(path_coords, building=None)
A path on a Catan board.
- Parameters
path_coords (
Set[Coords]) – The coordinates of the two intersections that the path connects.building (
Optional[PathBuilding]) – The building on this path.
- path_coords
The coordinates of the two intersections that the path connects.
- building
The building on this path.
- Type
PathBuilding, optional
pycatan.board.Coords
- class pycatan.board.Coords(q, r)
A class used to represent coordinates on the Catan board.
Stores a coordinate on a triangular grid, so that each hex and point both has a unique coord.
- Parameters
q (int) – The q coordinate
r (int) – The r coordinate
pycatan.board.Building
- class pycatan.board.Building(owner, building_type)
A building on the Catan board.
- building_type
The type of building this is
- Type
- Parameters
owner (
Player) – The player who owns this buildingbuilding_type (
BuildingType) – The type of building this is
pycatan.board.BuildingType
pycatan.board.PathBuilding
- class pycatan.board.PathBuilding(owner, building_type, path_coords)
A building that is built on an path. In the base game, only roads.
- building_type
The type of building this is
- Type
- Parameters
owner (
Player) – The player who owns this buildingbuilding_type (
BuildingType) – The type of building this ispath_coords (
Set[Coords]) – The coordinates of the two intersections the building is connecting
pycatan.board.IntersectionBuilding
- class pycatan.board.IntersectionBuilding(owner, building_type, coords)
A building that is built on a intersection. In the base game, a settlement or a city.
- building_type
The type of building this is
- Type
- Parameters
owner (
Player) – The player who owns this buildingbuilding_type (
BuildingType) – The type of building this iscoords (
Coords) – The coords the building is at
pycatan.board.BeginnerBoard
- class pycatan.board.BeginnerBoard
The beginner board, as outlined in the Catan rules.
pycatan.board.RandomBoard
- class pycatan.board.RandomBoard
A board where the hexes, numbered tokens and harbors are all shuffled randomly.
pycatan.board.BoardRenderer
- class pycatan.board.BoardRenderer(board, player_color_map={}, hex_color_map={<HexType.FIELDS: 3>: '#ffea29', <HexType.FOREST: 0>: '#005e09', <HexType.PASTURE: 2>: '#52ff62', <HexType.HILLS: 1>: '#cc1f0c', <HexType.MOUNTAINS: 4>: '#7a7a7a', <HexType.DESERT: 5>: '#ffe5a3'}, resource_color_map={<Resource.GRAIN: 3>: '#ffea29', <Resource.LUMBER: 0>: '#005e09', <Resource.WOOL: 2>: '#52ff62', <Resource.BRICK: 1>: '#cc1f0c', <Resource.ORE: 4>: '#7a7a7a'})
Class for rendering a board in the terminal and configuring its appearance.
- Parameters
board (
Board) – The board to renderplayer_color_map (
Optional[Dict[Player,str]]) – A map of which colors to use for which players. Colors are string hex codes (i.e. ‘#FF0000’)hex_color_map (
Optional[Dict[HexType,str]]) – A map of which colors to use for the different types of hexes. Colors are string hex codes (i.e. ‘#FF00000’)resource_color_map (
Optional[Dict[Resource,str]]) – A map of which colors to use for the different resource harbors. Colors are string hex codes (i.e. ‘#FF00000’)
- get_coords_as_xy(coords)
Get the coordinates given as x, y position.
- Parameters
coords (
Coords) – The coordinates- Return type
Tuple- Returns
The (x, y) position
- get_board_as_string(hex_labels={}, intersection_labels={}, path_labels={})
Get the board as a large, multiline string that includes colors.
- Parameters
hex_labels (
Optional[Dict[Hex,str]]) – A dictionary of labels to put on the hexes instead of the numbered tokensintersection_labels (
Optional[Dict[Intersection,str]]) – A dictionary of labels to put on the pointspath_labels (
Optional[Dict[Path,str]]) – A dictionary of labels to put on the paths
- Returns
The board as a string
- Return type
str
- render_board(hex_labels={}, intersection_labels={}, path_labels={})
Render the board into the terminal.
- Parameters
hex_labels (
Optional[Dict[Hex,str]]) – A dictionary of labels to put on the hexes instead of the numbered tokensintersection_labels (
Optional[Dict[Intersection,str]]) – A dictionary of labels to put on the pointspath_labels (
Optional[Dict[Path,str]]) – A dictionary of labels to put on the paths