Beispiel #1
0
    def make_guess(self, game, drawn_tile, is_optional=False, view=None):
        if view:
            view.canvas.draw_game(game, drawn_tile=drawn_tile)

        # Choose a player
        prompt = "\nOf which player you want to guess a tile?" + (
            "\nType -1 to not guess" if is_optional else "")
        player_name = int(
            self.save_prompt(
                prompt,
                list({str(p.name)
                      for p in game.players} - {str(self.name)}) + ['-1'],
                view.master))
        if is_optional and player_name == -1:
            return None, None, None
        chosen_player = [p for p in game.players if p.name == player_name][0]

        # Choose a tile idx
        prompt = "\nWhat is the idx of the tile? (Starting at 1)"
        tile_idx = int(
            self.save_prompt(
                prompt,
                [str(idx + 1)
                 for idx in range(len(chosen_player.tiles))], view.master)) - 1

        # Choose the tile
        prompt = "\nWhat tile do you think the player has. (Enter as b1 or w2)"
        guess = Tile.from_string(
            self.save_prompt(
                prompt,
                [str(t) for t in Tile.complete_set(game.max_tile_number)],
                view.master))

        return chosen_player, tile_idx, guess
def perms(unused_tiles, game_state, idx=0, previous_tile=None):
    # We reached the required length
    if idx == len(game_state.flat_player_tiles) - 1:
        if '*' in game_state.flat_player_tiles[idx]:
            return [[v] for v in unused_tiles
                    if permutation_adheres_to_game_rules(
                        v, previous_tile, game_state, idx)]
        else:
            already_known_tile = Tile.from_string(
                game_state.flat_player_tiles[idx])
            if permutation_adheres_to_game_rules(already_known_tile,
                                                 previous_tile, game_state,
                                                 idx):
                return [[already_known_tile]]
            else:
                return []

    # The next tile is already known
    if '*' not in game_state.flat_player_tiles[idx]:
        already_known_tile = Tile.from_string(
            game_state.flat_player_tiles[idx])
        if permutation_adheres_to_game_rules(already_known_tile, previous_tile,
                                             game_state, idx):
            return [[already_known_tile] + p
                    for p in perms(unused_tiles, game_state, idx +
                                   1, already_known_tile)]
        else:  # If the known tile does not adhere to the game rules, something must be wrong in this permutation
            return []

    # Normal permutation step, add all possible combinations
    result = []
    for i, v in enumerate(unused_tiles):
        if permutation_adheres_to_game_rules(v, previous_tile, game_state,
                                             idx):
            result += [[v] + p
                       for p in perms(unused_tiles[:i] +
                                      unused_tiles[i + 1:], game_state, idx +
                                      1, v)]
    return result
Beispiel #3
0
from image import Image

puzzle_input = sys.stdin.read()

tiles = []

tile_string = ''
tile_id = None
for line in puzzle_input.split('\n'):
    if 'Tile' in line:
        tile_id = int(line.strip()[5:-1])
    elif line:
        tile_string += line
        tile_string += '\n'
    else:
        tiles.append(Tile.from_string(tile_id, tile_string.strip()))
        tile_string = ''
        tile_id = None

corners = []
for tile in tiles:

    # See how many of the edges in t are in the rest of the data.
    for t in tiles:
        if tile == t:
            continue

        if len(set(tile.get_edges()) & set(t.get_edges())):
            tile.add_shared_edge(t)

    if len(tile._shared_edges) == 2:
Beispiel #4
0
    def update_bounding_box(self):
        '''
        '''

        # first, grab the meta data, then calculate the bounding box
        directory = self._directory
        metadata_file = os.path.join(directory, settings.METADATA_FILE)
        image_coordinates_file = os.path.join(directory,
                                              settings.IMAGE_COORDINATES_FILE)

        #
        # read meta data
        #
        metadata = {}

        with open(metadata_file) as f:
            for l in f.readlines():
                l = l.strip()
                values = l.split()
                metadata[values[0].strip(':')] = values[-1]

        #
        # we do want to parse some of the meta data
        #
        width = int(metadata['Width'].strip('px'))
        height = int(metadata['Height'].strip('px'))

        #
        # index tiles
        #
        tiles = {}

        with open(image_coordinates_file) as f:

            # we need to remove duplicate entries here and only grab the last
            # 61
            lines = FoV.filter_duplicate_lines(f.readlines())[-61:]

            for i, l in enumerate(lines):

                # if i>60:
                #   # only look at the first 61 entries since we do not use
                #   # other thumbnails
                #   break
                tile = Tile.from_string(l)
                # update width and height
                tile.width = width
                tile.height = height
                tiles[tile.id] = tile

        self._tiles = tiles
        self._metadata = metadata

        # now the bounding box
        width = -sys.maxsize
        height = -sys.maxsize

        minX = sys.maxsize
        minY = sys.maxsize
        maxX = -sys.maxsize
        maxY = -sys.maxsize

        for i in self._tiles:
            image = self._tiles[i]
            minX = min(minX, image._tx)
            minY = min(minY, image._ty)
            maxX = max(maxX, image._tx)
            maxY = max(maxY, image._ty)

        width = maxX - minX + image.width
        height = maxY - minY + image.height

        self._tx = minX
        self._ty = minY

        self._width = width
        self._height = height
 def known_tiles(self):
     return [
         Tile.from_string(t) for t in self.flat_player_tiles if '*' not in t
     ]
Beispiel #6
0
  def update_bounding_box(self):
    '''
    '''

    # first, grab the meta data, then calculate the bounding box
    directory = self._directory
    metadata_file = os.path.join(directory, Constants.METADATA_FILE)
    image_coordinates_file = os.path.join(directory, Constants.IMAGE_COORDINATES_FILE)


    #
    # read meta data
    #
    metadata = {}

    with open(metadata_file) as f:
      for l in f.readlines():
        l = l.strip()
        values = l.split()
        metadata[values[0].strip(':')] = values[-1]

    #
    # we do want to parse some of the meta data
    #
    width = int(metadata['Width'].strip('px'))
    height = int(metadata['Height'].strip('px'))
    
    #
    # index tiles
    #
    tiles = {}

    with open(image_coordinates_file) as f:
      
      # we need to remove duplicate entries here and only grab the last 61
      lines = FoV.filter_duplicate_lines(f.readlines())[-61:]
      
      for i,l in enumerate(lines):

        # if i>60:
        #   # only look at the first 61 entries since we do not use other thumbnails
        #   break
        tile = Tile.from_string(l)
        # update width and height
        tile.width = width
        tile.height = height
        tiles[tile.id] = tile


    self._tiles = tiles
    self._metadata = metadata

    # now the bounding box
    width = -sys.maxint
    height = -sys.maxint

    minX = sys.maxint
    minY = sys.maxint
    maxX = -sys.maxint
    maxY = -sys.maxint

    for i in self._tiles:
      image = self._tiles[i]
      minX = min(minX, image._tx)
      minY = min(minY, image._ty)
      maxX = max(maxX, image._tx)
      maxY = max(maxY, image._ty)

    width = maxX - minX + image.width
    height = maxY - minY + image.height

    self._tx = minX
    self._ty = minY

    self._width = width
    self._height = height

    print width, height