Exemplo n.º 1
0
def play_batch_core(num_games, players, game_config, accumulators=[]):
    for accumulator in accumulators:
        if isinstance(accumulator, SimulationAccumulator):
            accumulator.before_all()

    for _ in range(num_games):
        for player in players:
            player.reset_state()
        catan_map = (CatanMap(MINI_MAP_TEMPLATE) if game_config.catan_map
                     == "MINI" else CatanMap(BASE_MAP_TEMPLATE))
        game = Game(
            players,
            discard_limit=game_config.discard_limit,
            vps_to_win=game_config.vps_to_win,
            catan_map=catan_map,
        )
        game.play(accumulators)
        yield game

    for accumulator in accumulators:
        if isinstance(accumulator, SimulationAccumulator):
            accumulator.after_all()
Exemplo n.º 2
0
def test_mini_map_can_be_created():
    mini = CatanMap(MINI_MAP_TEMPLATE)
    assert len(mini.land_tiles) == 7
    assert len(mini.land_nodes) == 24
    assert len(mini.tiles_by_id) == 7
    assert len(mini.ports_by_id) == 0
    assert len(mini.port_nodes) == 0
    assert len(mini.adjacent_tiles) == 24
    assert len(mini.node_production) == 24

    resources = [i.resource for i in mini.land_tiles.values()]
    assert any(isinstance(i, str) for i in resources)
    assert any(i is None for i in resources)  # theres one desert
Exemplo n.º 3
0
    def __init__(self, catan_map=None, initialize=True):
        if initialize:
            self.map: CatanMap = catan_map or CatanMap(
                BASE_MAP_TEMPLATE)  # Static State (no need to copy)

            self.buildings = dict()  # node_id => (color, building_type)
            self.roads = dict()  # (node_id, node_id) => color

            # color => int{}[] (list of node_id sets) one per component
            #   nodes in sets are incidental (might not be owned by player)
            self.connected_components: Any = defaultdict(list)
            self.board_buildable_ids = set(self.map.land_nodes)
            self.road_lengths = defaultdict(int)
            self.road_color = None
            self.road_length = 0

            # assumes there is at least one desert:
            self.robber_coordinate = filter(
                lambda coordinate: self.map.land_tiles[coordinate].resource is
                None,
                self.map.land_tiles.keys(),
            ).__next__()
Exemplo n.º 4
0
def test_buildable_nodes_in_mini_map():
    board = Board(catan_map=CatanMap(MINI_MAP_TEMPLATE))
    nodes = board.buildable_node_ids(Color.RED)
    assert len(nodes) == 0
    nodes = board.buildable_node_ids(Color.RED, initial_build_phase=True)
    assert len(nodes) == 24
Exemplo n.º 5
0
def test_buildable_edges_in_mini():
    board = Board(catan_map=CatanMap(MINI_MAP_TEMPLATE))
    board.build_settlement(Color.RED, 19, initial_build_phase=True)
    buildable = board.buildable_edges(Color.RED)
    assert len(buildable) == 2
Exemplo n.º 6
0
from typing import Any, Set
import functools

import networkx as nx

from catanatron.models.player import Color
from catanatron.models.map import (
    BASE_MAP_TEMPLATE,
    MINI_MAP_TEMPLATE,
    NUM_NODES,
    CatanMap,
)
from catanatron.models.enums import BuildingType

# Used to find relationships between nodes and edges
base_map = CatanMap(BASE_MAP_TEMPLATE)
mini_map = CatanMap(MINI_MAP_TEMPLATE)
STATIC_GRAPH = nx.Graph()
for tile in base_map.tiles.values():
    STATIC_GRAPH.add_nodes_from(tile.nodes.values())
    STATIC_GRAPH.add_edges_from(tile.edges.values())


@functools.lru_cache(1)
def get_node_distances():
    global STATIC_GRAPH
    return nx.floyd_warshall(STATIC_GRAPH)


@functools.lru_cache(3)  # None, range(54), range(24)
def get_edges(land_nodes=None):
Exemplo n.º 7
0
def test_base_map_can_be_created():
    catan_map = CatanMap(BASE_MAP_TEMPLATE)
    assert len(catan_map.land_tiles) == 19
    assert len(catan_map.node_production) == 54