From 8e40c851ec4f327c3106155403af04b131bacdf8 Mon Sep 17 00:00:00 2001 From: Bryan Collazo Date: Fri, 1 Dec 2023 17:03:57 -0400 Subject: [PATCH] Fix adjacent-same-resource tile production bug (#264) --- catanatron_core/catanatron/models/map.py | 16 ++++++++-------- tests/models/test_map.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/catanatron_core/catanatron/models/map.py b/catanatron_core/catanatron/models/map.py index 8538c447..1c4dc0b3 100644 --- a/catanatron_core/catanatron/models/map.py +++ b/catanatron_core/catanatron/models/map.py @@ -290,15 +290,15 @@ def init_node_production( return node_production -def get_node_counter_production(adjacent_tiles, node_id): +def get_node_counter_production( + adjacent_tiles: Dict[int, List[LandTile]], node_id: NodeId +): tiles = adjacent_tiles[node_id] - return Counter( - { - t.resource: number_probability(t.number) - for t in tiles - if t.resource is not None - } - ) + production = defaultdict(float) + for tile in tiles: + if tile.resource is not None: + production[tile.resource] += number_probability(tile.number) + return Counter(production) def build_dice_probas(): diff --git a/tests/models/test_map.py b/tests/models/test_map.py index 57ab6c95..b6892381 100644 --- a/tests/models/test_map.py +++ b/tests/models/test_map.py @@ -5,9 +5,24 @@ CatanMap, LandTile, get_nodes_and_edges, + get_node_counter_production, + DICE_PROBAS, ) +def test_node_production_of_same_resource_adjacent_tile(): + # See https://github.com/bcollazo/catanatron/issues/263. + adjacent_tiles = { + 1: [ + LandTile(1, WOOD, 8, dict(), dict()), + LandTile(2, WOOD, 6, dict(), dict()), + LandTile(3, WOOD, 12, dict(), dict()), + ] + } + result = get_node_counter_production(adjacent_tiles, 1) + assert result["WOOD"] == DICE_PROBAS[12] + DICE_PROBAS[6] + DICE_PROBAS[8] + + def test_mini_map_can_be_created(): mini = CatanMap.from_template(MINI_MAP_TEMPLATE) assert len(mini.land_tiles) == 7