示例#1
0
    def _render_head(board):
        meta = '<meta http-equiv="content-type" content="text/html; charset=UTF-8">'
        title = "<title>carcasonne board construction sample</title>"
        jquery = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>'

        images = []
        for tile in board.grid.values():
            if tile.rotation != 0:
                images.append(
                    '$("#%s").rotate(%s);'
                    % (
                        HtmlRenderer.tile_to_id(tile),
                        HtmlRenderer._rotation_to_deg(ROTATIONS.by_ordinal(tile.rotation)),
                    )
                )

        rotation = """<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
%s
});//]]>  
</script>""" % (
            "\n".join(images)
        )

        return "<head>\n%s\n%s\n%s\n%s\n</head>" % (meta, title, jquery, rotation)
示例#2
0
    def pos_to_tile_xy(pos, rotation):
        x = int(float(pos["x"]) / 100.0 * float(Renderer.tile_width))
        y = int(float(pos["y"]) / 100.0 * float(Renderer.tile_height))

        rot = ROTATIONS.by_ordinal(rotation)
        angle = int(rot[3:])
        x, y = Renderer.rotate(x, y, angle)

        return x, y
示例#3
0
 def playable_locations(self, for_tile=None):
     # if not for a specific tile, return the whole set
     if not for_tile:
         return self.edges
     else:
         # else match
         locations = set()
         for l in self.edges:
             for r in ROTATIONS.values():
                 if self.is_legal_on_location(for_tile, l, ROTATIONS[r]):
                     locations.add(l)
         return locations
def main():
    if len(sys.argv) < 2:
        sys.stderr.write("Usage: construct_random_board <tileset>\n")
        return 1

    logging.getLogger().setLevel(logging.DEBUG)

    print "Loading ..."
    json = load_config(sys.argv[1])

    validate_tileset_config(json, set(['city', 'field', 'road']))


    seed = int(time.time())
    #seed = 1333213878
    print "Constructing with seed %s ..." % (seed)

    random.seed(seed)

    b = Board(json)
    failed_count = 0
    while len(b.tilesleft) > 0:
        tile = random.sample(b.tilesleft, 1)[0]
        locations = b.playable_locations(tile)

        location = None
        playable_locations = []
        location_neighbour_count = 0
        for l in locations:
            count = count_neighbours(b, l)

            if count > location_neighbour_count:
                location_neighbour_count = count
                playable_locations = []
                playable_locations.append(l)
                location = l
            elif count == location_neighbour_count:
                playable_locations.append(l)

        #location = random.sample(locations, 1)[0]
        if not playable_locations:
            logging.debug('Could not play tile %s\n%s' % (tile, b))
            failed_count += 1
            continue

        most_playable = 0
        location = None
        for l in playable_locations:
            count = 0
            for r in ROTATIONS.values():
                if b.is_legal_on_location(tile, l, ROTATIONS[r]):
                    count += 1

            if count >= most_playable:
                most_playable = count
                location = l

        #location = random.sample(playable_locations, 1)[0]

        for r in ROTATIONS.values():
            if b.is_legal_on_location(tile, location, ROTATIONS[r]):
                b.add_to_board(tile, location, ROTATIONS[r])
                break

    print 'Done, had to retry %d tiles' % failed_count

    print b
    d = b.dimensions()
    print 'Board size: %dx%d' % (d[0], d[1])

    filename = 'board.html'
    print "Rendering to %s" % (filename)
    html = HtmlRenderer.render(b)
    file = open(filename, 'w')
    file.write(html)
    file.close()
示例#5
0
    def _play_tile(self, tid, location, rotation):
        # remove location from playable locations
        if location in self.edges:
            self.edges.remove(location)

        # set actual values in the tile
        tile = self.boardtiles[tid]
        tile.location = location
        tile.rotation = rotation
        self.grid[location] = tile
        self.tilesleft.remove(tid)

        # keep track of playable locations
        for n in self.neighbours_for(location):
            # add all neighbours that aren't played tiles == empty locations
            if type(n) is not PlayedTile:
                self.edges.add(n)

        self._update_graphs(tid, location)

        logging.info("Played tile %s at location %s, rotated %s" % (tid, location, rotation))
        logging.debug("b.add_to_board('%s', %s, tile.ROTATIONS.%s))" % (tid, location, ROTATIONS.by_ordinal(rotation)))