def _draw_piece_shadows(self, piece_type, board, terrain_centers): logging.debug('Drawing piece shadows of type={}'.format(piece_type.value)) piece = Piece(piece_type, self.game.get_cur_player()) if piece_type == PieceType.road: edges = hexgrid.legal_edge_coords() count = 0 for edge in edges: if (hexgrid.EDGE, edge) in board.pieces: logging.debug('Not drawing shadow road at coord={}'.format(edge)) continue count += 1 self._draw_piece(edge, piece, terrain_centers, ghost=True) logging.debug('Road shadows drawn: {}'.format(count)) elif piece_type == PieceType.settlement: nodes = hexgrid.legal_node_coords() for node in nodes: if (hexgrid.NODE, node) in board.pieces: continue self._draw_piece(node, piece, terrain_centers, ghost=True) elif piece_type == PieceType.city: for (_, node), p in board.pieces.items(): if p.type == PieceType.settlement and p.owner.color == piece.owner.color: self._draw_piece(node, piece, terrain_centers, ghost=True) elif piece_type == PieceType.robber: for coord in hexgrid.legal_tile_coords(): if hexgrid.tile_id_from_coord(coord) != self.game.robber_tile: self._draw_piece(coord, piece, terrain_centers, ghost=True) else: logging.warning('Attempted to draw piece shadows for nonexistent type={}'.format(piece_type))
def _draw_piece_shadows(self, piece_type, board, terrain_centers): logging.debug('Drawing piece shadows of type={}'.format( piece_type.value)) piece = Piece(piece_type, self.game.get_cur_player()) if piece_type == PieceType.road: edges = hexgrid.legal_edge_coords() count = 0 for edge in edges: if (hexgrid.EDGE, edge) in board.pieces: logging.debug( 'Not drawing shadow road at coord={}'.format(edge)) continue count += 1 self._draw_piece(edge, piece, terrain_centers, ghost=True) logging.debug('Road shadows drawn: {}'.format(count)) elif piece_type == PieceType.settlement: nodes = hexgrid.legal_node_coords() for node in nodes: if (hexgrid.NODE, node) in board.pieces: continue self._draw_piece(node, piece, terrain_centers, ghost=True) elif piece_type == PieceType.city: for (_, node), p in board.pieces.items(): if p.type == PieceType.settlement and p.owner.color == piece.owner.color: self._draw_piece(node, piece, terrain_centers, ghost=True) elif piece_type == PieceType.robber: for coord in hexgrid.legal_tile_coords(): if hexgrid.tile_id_from_coord(coord) != self.game.robber_tile: self._draw_piece(coord, piece, terrain_centers, ghost=True) else: logging.warning( 'Attempted to draw piece shadows for nonexistent type={}'. format(piece_type))
def get_board_vec(board, players): """ Makes a numpy vector based on the board. """ edges = list(hexgrid.legal_edge_coords()) # So we can get the indices nodes = list(hexgrid.legal_node_coords()) # same # Tiles tile_data = np.zeros(114) for i, tile_type in enumerate([ResourceType.FOREST, ResourceType.ORE, ResourceType.BRICK, ResourceType.SHEEP, ResourceType.WHEAT, ResourceType.DESERT]): tile_data[i*19:(i+1)*19] = [i.resource() == tile_type for i in board.hexes()] # Cities and settlements city_sett_data = np.zeros(216, np.uint8) for i, p in enumerate(players): for j in p.settlement_nodes(): index = nodes.index(j) city_sett_data[(54 * i) + index] = 1 for j in p.city_nodes(): index = nodes.index(j) city_sett_data[(54 * i) + index] = 2 # Boolean roads (72x4): 288 roads_data = np.zeros(288, np.uint8) for i, p in enumerate(players): for j in p.road_edges(): index = edges.index(j) roads_data[(72 * i) + index] = 1 # Boolean Robber robber_data = [i == board.robber_hex().id() - 1 for i in range(19)] return np.hstack([tile_data, city_sett_data, roads_data, robber_data])
def get_random_assignment(self, d): assigned_nodes = {piece[1] for piece in d if piece[0] == 1} free_nodes = hexgrid.legal_node_coords() - assigned_nodes assigned_edges = {piece[1] for piece in d if piece[0] == 0} free_edges = hexgrid.legal_edge_coords() - assigned_edges # random placement of both road and settlement return (random.sample(free_nodes, 1)[0], random.sample(free_edges, 1)[0])
def nodes_map(self) -> str: def player_color(player): return self.__player_colors[self.__players.index(player)] def get_color(edge, is_edge=True): coords = self.edges() if is_edge else self.nodes() if coords.get(edge) is not None: return self.__player_colors[self.__players.index(coords.get(edge).player())] else: return Board.COLORS['END'] dh = {f'h{i}': str(h) for i, h in enumerate(self.hexes())} dht = {f'h{i}t': h.token() for i, h in enumerate(self.hexes())} x = 'x' dr = {f'r{hex(edge).split(x)[1]}': get_color(edge) for edge in hexgrid.legal_edge_coords()} legend = ' '.join('{}{}{}'.format(player_color(player), player, Board.COLORS['END']) for player in self.__players) detc = {'e': Board.COLORS['END'], 'legend': legend} dn = {f'n{hex(node).split(x)[1]}': '{}{}{}'.format(get_color(node, is_edge=False), hex(node).split(x)[1], Board.COLORS['END']) for node in hexgrid.legal_node_coords()} dy = {f'y{i}': 'R' if h.has_robber() else ' ' for i, h in enumerate(self.hexes())} d = dict() for other_dict in (dh, dht, dr, dn, dy, detc): for key, value in other_dict.items(): d[key] = value return """ 3:1 / \\ {n27}{r27}_____{e}{n38} {r26}/{e} {y0} {r38}\\{e} ORE __ {n25}{r25}_____{e}{n36}{r26}/{e} {h0:^15} {r38}\\{e}{n49}{r49}_____{e}{n5a} __ SHEEP \\ {r24}/{e} {y1} {r36}\\{e} {h0t:^2} {r48}/{e} {y11} {r5a}\\{e} / {n23}{r23}_____{e}{n34}{r24}/{e} {h1:^15} {r36}\\{e}{n47}{r47}_____{e}{n58}{r48}/{e} {h11:^15} {r5a}\\{e}{n6b}{r6b}_____{e}{n7c} {r22}/{e} {y2} {r34}\\{e} {h1t:^2} {r46}/{e} {y12} {r58}\\{e} {h11t:^2} {r6a}/{e} {y10} {r7c}\\{e} {n32}{r22}/{e} {h2:^15} {r34}\\{e}{n45}{r45}_____{e}{n56}{r46}/{e} {h12:^15} {r58}\\{e}{n69}{r69}_____{e}{n7a}{r6a}/{e} {h10:^15} {r7c}\\{e}{n8d} {r32}\\{e} {h2t:^2} {r44}/{e} {y13} {r56}\\{e} {h12t:^2} {r68}/{e} {y17} {r7a}\\{e} {h10t:^2} {r8c}/{e} WHEAT __ {r32}\\{e}{n43}{r43}_____{e}{n54}{r44}/{e} {h13:^15} {r56}\\{e}{n67}{r67}_____{e}{n78}{r68}/{e} {h17:^15} {r7a}\\{e}{n8b}{r8b}_____{e}{n9c}{r8c}/{e} __ 3:1 \\ {r42}/{e} {y3} {r54}\\{e} {h13t:^2} {r66}/{e} {y18} {r78}\\{e} {h17t:^2} {r8a}/{e} {y9} {r9c}\\{e} / {n52}{r42}/{e} {h3:^15} {r54}\\{e}{n65}{r65}_____{e}{n76}{r66}/{e} {h18:^15} {r78}\\{e}{n89}{r89}_____{e}{n9a}{r8a}/{e} {h9:^15} {r9c}\\{e}{nad} {r52}\\{e} {h3t:^2} {r64}/{e} {y14} {r76}\\{e} {h18t:^2} {r88}/{e} {y16} {r9a}\\{e} {h9t:^2} {rac}/{e} {r52}\\{e}{n63}{r63}_____{e}{n74}{r64}/{e} {h14:^15} {r76}\\{e}{n87}{r87}_____{e}{n98}{r88}/{e} {h16:^15} {r9a}\\{e}{nab}{rab}_____{e}{nbc}{rac}/{e} {r62}/{e} {y4} {r74}\\{e} {h14t:^2} {r86}/{e} {y15} {r98}\\{e} {h16t:^2} {raa}/{e} {y8} {rbc}\\{e} {n72}{r62}/{e} {h4:^15} {r74}\\{e}{n85}{r85}_____{e}{n96}{r86}/{e} {h15:^15} {r98}\\{e}{na9}{ra9}_____{e}{nba}{raa}/{e} {h8:^15} {rbc}\\{e}{ncd} / {r72}\\{e} {h4t:^2} {r84}/{e} {y5} {r96}\\{e} {h15t:^2} {ra8}/{e} {y7} {rba}\\{e} {h8t:^2} {rcc}/{e} \\ 3:1 __ {r72}\\{e}{n83}{r83}_____{e}{n94}{r84}/{e} {h5:^15} {r96}\\{e}{na7}{ra7}_____{e}{nb8}{ra8}/{e} {h7:^15} {rba}\\{e}{ncb}{rcb}_____{e}{ndc}{rcc}/{e} __ 3:1 {r94}\\{e} {h5t:^2} {ra6}/{e} {y6} {rb8}\\{e} {h7t:^2} {rca}/{e} {r94}\\{e}{na5}{ra5}_____{e}{nb6}{ra6}/{e} {h6:^15} {rb8}\\{e}{nc9}{rc9}_____{e}{nda}{rca}/{e} | / {rb6}\\{e} {h6t:^2} {rc8}/{e} \\ | FOREST {rb6}\\{e}{nc7}{rc7}_____{e}{nd8}{rc8}/{e} BRICK {legend}""".format(**d)
def __buildable_edges(self, player: Player.Player) -> List[int]: player_nodes = set() for road_edge in player.road_edges(): for node in hexgrid.nodes_touching_edge(road_edge): player_nodes.add(node) adj_edges = set() for node in player_nodes: for edge_id in self.board().get_adj_edges_to_node(node): adj_edges.add(edge_id) for existing_edge in player.road_edges(): if existing_edge in adj_edges: adj_edges.remove(existing_edge) to_remove = [] for edge in adj_edges: if edge not in hexgrid.legal_edge_coords() or self.board().edges().get(edge) is not None: to_remove.append(edge) for edge in to_remove: adj_edges.remove(edge) return list(adj_edges)
def get_adj_edges_to_node(location: int) -> List[int]: if location % 2 == 1: locs = [location, location - 0x11, location - 0x1] else: locs = [location - 0x10, location - 0x11, location] return [loc for loc in locs if loc in hexgrid.legal_edge_coords()]