Esempio n. 1
0
def replay(seed, moveseq):
    from threes import play_game, to_val, to_score
    from collections import Counter
    import random

    random.seed(seed)

    initial_deck = Counter([1,2,3]*4)
    deck = None
    game = play_game()
    moveseq = [None] + ['UDLR'.find(x) for x in moveseq]

    for moveno, move in enumerate(moveseq):
        m, tileset, valid = game.send(move)
        print to_val(m)
        if deck is None:
            deck = initial_deck.copy() - Counter(m.flatten())

        if not valid:
            print "Game over."
            break

        print 'next tile:', list(to_val(tileset))

        if tileset[0] <= 3:
            deck[tileset[0]] -= 1
        if all(deck[i] == 0 for i in (1,2,3)):
            deck = initial_deck.copy()

    print "Your score is", to_score(m).sum()
    return to_score(m).sum()
Esempio n. 2
0
def replay(seed, moveseq):
    from threes import play_game, to_val, to_score
    from collections import Counter
    import random

    random.seed(seed)

    initial_deck = Counter([1, 2, 3] * 4)
    deck = None
    game = play_game()
    moveseq = [None] + ['UDLR'.find(x) for x in moveseq]

    for moveno, move in enumerate(moveseq):
        m, tileset, valid = game.send(move)
        print to_val(m)
        if deck is None:
            deck = initial_deck.copy() - Counter(m.flatten())

        if not valid:
            print "Game over."
            break

        print 'next tile:', list(to_val(tileset))

        if tileset[0] <= 3:
            deck[tileset[0]] -= 1
        if all(deck[i] == 0 for i in (1, 2, 3)):
            deck = initial_deck.copy()

    print "Your score is", to_score(m).sum()
    return to_score(m).sum()
Esempio n. 3
0
def play_with_search(verbose=True):
    from threes import play_game, to_val, to_score
    from collections import Counter

    import random
    import time
    seed = hash(str(time.time()))
    print "seed=%d" % seed
    random.seed(seed)

    initial_deck = Counter([1, 2, 3] * 4)
    deck = None
    game = play_game()
    move = None

    moveno = 0
    while True:
        m, tileset, valid = game.send(move)
        if verbose:
            print to_val(m)
        if deck is None:
            deck = initial_deck.copy() - Counter(m.flatten())

        if not valid:
            break

        if verbose:
            print 'next tile:', list(to_val(tileset))

        move = find_best_move(m, deck, tileset)
        moveno += 1
        if verbose:
            print "Move %d: %s" % (moveno, ['up', 'down', 'left', 'right'
                                            ][move])
        else:
            sys.stdout.write('UDLR'[move])
            sys.stdout.flush()

        if tileset[0] <= 3:
            deck[tileset[0]] -= 1
        if all(deck[i] == 0 for i in (1, 2, 3)):
            deck = initial_deck.copy()

    print
    print "Game over. Your score is", to_score(m).sum()
    return to_score(m).sum()
Esempio n. 4
0
def play_with_search(verbose=True):
    from threes import play_game, to_val, to_score
    from collections import Counter

    import random
    import time

    seed = hash(str(time.time()))
    print "seed=%d" % seed
    random.seed(seed)

    initial_deck = Counter([1, 2, 3] * 4)
    deck = None
    game = play_game()
    move = None

    moveno = 0
    while True:
        m, tileset, valid = game.send(move)
        if verbose:
            print to_val(m)
        if deck is None:
            deck = initial_deck.copy() - Counter(m.flatten())

        if not valid:
            break

        if verbose:
            print "next tile:", list(to_val(tileset))

        move = find_best_move(m, deck, tileset)
        moveno += 1
        if verbose:
            print "Move %d: %s" % (moveno, ["up", "down", "left", "right"][move])
        else:
            sys.stdout.write("UDLR"[move])
            sys.stdout.flush()

        if tileset[0] <= 3:
            deck[tileset[0]] -= 1
        if all(deck[i] == 0 for i in (1, 2, 3)):
            deck = initial_deck.copy()

    print
    print "Game over. Your score is", to_score(m).sum()
    return to_score(m).sum()