Esempio n. 1
0
def import_hive(file: str) -> Hive:
    data = json.load(open(file, 'r'))

    current_player = data["player"]
    if current_player.lower() != "w" and current_player.lower() != "b":
        logging.error("Wrong format: cannot decode current player")
        raise RuntimeError()
    game = data["game"]
    if not game:
        logging.error("Could not open hive data")
        raise RuntimeError()
    if not isinstance(data, dict):
        logging.error("Wrong format, expected dict")
        raise RuntimeError()
    hive = Hive()
    hive.level.current_player = current_player.lower()
    for hex_str_tuple, v in game.items():
        hex_tuple = ast.literal_eval(hex_str_tuple)
        if not isinstance(hex_tuple, tuple):
            logging.error("Wrong format while deparsing hex tuple")
            raise RuntimeError()
        hexagon = Hex(*hex_tuple)
        if not isinstance(v, list):
            logging.error("Wrong format, expected list")
            raise RuntimeError()
        for piece_str in v:
            hive.level.append_to(piecefact.name_to_piece(piece_str), hexagon)
    return hive
Esempio n. 2
0
 def index_to_target_cell(self, hive: 'Hive', number: int,
                          pos: hexutil.Hex):
     aval_indexes = (
         i for i, v in enumerate(self.available_moves_vector(hive, pos))
         if v > 0)
     assert number in aval_indexes
     return pos.neighbours()[number]
Esempio n. 3
0
 def available_moves_vector(self, hive: 'Hive', pos: hexutil.Hex):
     super().available_moves(hive, pos)
     if self.check_blocked(hive, pos):
         return [0] * 6
     result = []
     aval_moves = self.available_moves(hive, pos)
     for nb in pos.neighbours():
         if nb in aval_moves:
             result.append(1)
         else:
             result.append(0)
     return result
Esempio n. 4
0
    def available_moves(self, hive: 'Hive', pos: hexutil.Hex):
        super().available_moves(hive, pos)
        if self.check_blocked(hive, pos):
            return []
        res = []
        # are we on top of the hive?
        # TODO this practically does the trick, but not precise
        if len(hive.level.get_tile_content(pos)) > 1:
            res += pos.neighbours()
        else:
            # remove piece temporary
            del hive.level.tiles[pos]
            res += (hive.bee_moves(pos) +
                    hive.level.occupied_surroundings(pos))
            hive.level.tiles[pos] = [self]

        return res
Esempio n. 5
0
    def bee_moves(self, cell: hexutil.Hex) -> List[hexutil.Hex]:
        """
        A bee can move to an adjacent target position only if:
        - target position is free
        - and there is a piece adjacent to both the bee and that position
        - and there is a free cell that is adjacent to both the bee and the
          target position.
        :param cell: The hexagon the queen is currently at.
        :return: A list of hexagons where the queen can possibly go
        """
        available_hexes = []
        surroundings = cell.neighbours()
        for nb in surroundings:
            if not self.level.is_cell_free(nb):
                continue
            # does it have an adjacent free and an adjacent occupied cell that
            # is also adjacent to the starting cell
            mutual_nbs = list(GameState.get_mutual_neighbors(cell, nb))
            assert len(mutual_nbs) == 2  # there are always two of them
            if self.level.is_cell_free(
                    mutual_nbs[0]) != self.level.is_cell_free(mutual_nbs[1]):
                available_hexes.append(nb)

        return available_hexes
Esempio n. 6
0
 def get_mutual_neighbors(hex1: hexutil.Hex,
                          hex2: hexutil.Hex) -> Set[hexutil.Hex]:
     return set(hex1.neighbours()).intersection(hex2.neighbours())
Esempio n. 7
0
 def occupied_surroundings(self, hexagon: hexutil.Hex):
     if not self.tiles:
         return []
     return [nb for nb in hexagon.neighbours() if nb in self.tiles.keys()]
Esempio n. 8
0
 def index_to_target_cell(self, hive: 'Hive', number: int,
                          pos: hexutil.Hex):
     nbs = pos.neighbours()
     if nbs[number] not in self.available_moves(hive, pos):
         raise HiveException("Invalid action index of queen", 10000)
     return nbs[number]