Ejemplo n.º 1
0
    def test_playable_locations(self):
        b = Board(self.conf)
        l = b.playable_locations()
        self.assertEquals(len(l), 4)

        # can only play cloister below starter
        l = b.playable_locations('2')
        self.assertEquals(len(l), 1)
        self.assertEquals(l.pop(), (0, -1))

        b.add_to_board('2', (0, -1), tile.ROTATIONS.deg0)
        l = b.playable_locations()
        # remove 1, add 3 => 6
        self.assertEquals(len(l), 6)
Ejemplo n.º 2
0
    def test_impossible_play(self):
        """From actual random output:
        INFO:root:Played tile 1 at location (0, 0), rotated 0
        INFO:root:Played tile 58 at location (-1, 0), rotated 2
        INFO:root:Played tile 17 at location (-1, -1), rotated 2
        INFO:root:Played tile 29 at location (-2, -1), rotated 0
        INFO:root:Played tile 9 at location (0, -1), rotated 2
        INFO:root:Played tile 28 at location (1, -1), rotated 0
        INFO:root:Played tile 43 at location (1, -2), rotated 1
        INFO:root:Played tile 61 at location (-2, -2), rotated 0
        DEBUG:root:Could not play tile 2, Played tiles: 8
        """

        b = Board(self.conf)
        b.add_to_board('58', (-1, 0), tile.ROTATIONS.deg180)
        b.add_to_board('17', (-1, -1), tile.ROTATIONS.deg180)
        b.add_to_board('29', (-2, -1), tile.ROTATIONS.deg0)
        b.add_to_board('9', (0, -1), tile.ROTATIONS.deg180)
        b.add_to_board('28', (1, -1), tile.ROTATIONS.deg0)
        b.add_to_board('43', (1, -2), tile.ROTATIONS.deg90)
        b.add_to_board('61', (-2, -2), tile.ROTATIONS.deg0)

        # no playable positions for the cloister now:
        self.assertEquals(b.playable_locations('2'), set())
Ejemplo n.º 3
0
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()