Ejemplo n.º 1
0
def main(*argv):
    if not argv: 
        argv = None
    parser = argparse.ArgumentParser()
    
    parser.add_argument('--replay', type=argparse.FileType('w'),
                        default=None)
    parser.add_argument('--maxturns', type=int, default=rules.MAX_TURNS)
    parser.add_argument('bot1')
    parser.add_argument('bot2')
    parser.add_argument('number', nargs='?', type=int, default=None, 
                        help='if number is provided, script interacts '
                        'in official way, and another bot is ignored')
    
    args = parser.parse_args(argv)
    replay = args.replay
    bot_number = args.number
    
    game = Game()
    game.start_timer()
    
    bots = [eval(args.bot1), eval(args.bot2)]
    if bot_number in (0, None):
        bots[0].set_game(game)
    if bot_number in (1, None):
        bots[1].set_game(game)

    while not game.is_finished():
        #if game.half_moves % 10000 == 0:
            #print>>sys.stderr, 'half turn', game.half_moves
        if game.half_moves >= args.maxturns*2:
            break
        if game.has_zombie_phase():
            game.zombie_phase()
        bot = bots[game.half_moves%2]
        
        if bot_number == 1-game.half_moves%2:
            game.stop_timer() # playing vs opponent -- pause timer only for IO
            move = receive_move()
            game.start_timer()
        else:
            move = bot.choose_move()

            if bot_number is None: 
                # pitting two bots together -- next bot's time slot starts right here.
                game.stop_timer()
                game.start_timer()
        if bot_number == game.half_moves%2:
            send_move(*move)
        game.make_half_move(*move)
        if replay is not None:
            if move[0] == 'l':
                d = 1
            else:
                d = 2
            print>>replay, d, move[1], move[2]
            replay.flush()
        
    if replay is not None:
        replay.close()
Ejemplo n.º 2
0
        if game.half_moves == len(replay):
            print>>log, 'replay prematurely ended'
            break
        player = game.half_moves%2
        if player == 0:
            print>>log, '###### turn {0}'.format(game.half_moves//2+1)
        print>>log, "*** player {0}'s turn, with slots:".format(player)
        prop = game.proponent
        for i in range(SLOTS):
            vit, value = prop.vitalities[i], prop.values[i]
            if (vit, value) != (INITIAL_VITALITY, cards.I):
                print>>log, '{0}={{{1},{2}}}'.format(i, vit, value)
        print>>log, '(slots {10000,I} are omitted)'
        
        if game.has_zombie_phase():
            game.zombie_phase()
        move = bots[game.half_moves%2].choose_move()
        
        print>>log, '(1) apply card to slot, or (2) apply slot to card?'
        if move[0] == LEFT_APP:
            print>>log, 'card name?'
            print>>log, 'slot no?'
            print>>log, 'player {0} applied card {2} to slot {1}'.\
                format(player, move[1], move[2])
        else:
            print>>log, 'slot no?'
            print>>log, 'card name?'
            print>>log, 'player {0} applied slot {1} to card {2}'.\
                format(player, move[1], move[2])
        game.make_half_move(*move)
Ejemplo n.º 3
0
def main(replay_name):
    with open(replay_name) as fin:
        replay = list(fin)
        
    html_name = os.path.splitext(replay_name)[0]+'.html'
    html = open(html_name, 'w')
    
    
    #log = sys.stdout
    print>>html, '<html><body><div style="background-color:#FFFFFF">'
    print>>html, 'Lambda: The Gathering log emulator'
    
    game = Game(output_level=2)
    original_stdout = sys.stdout
    sys.stdout = html # because game outputs to stdout
    # it's dirty, but i don't care    
    bots = [PlaybackBot(replay[::2]).set_game(game), PlaybackBot(replay[1::2]).set_game(game)]

    skip_begin = -1
    skip_end = -1
    if len(replay) > 210:
        skip_begin = 100
        skip_end = len(replay)-100

    while not game.is_finished():
        if game.half_moves == len(replay):
            print>>html, '<hr/>replay prematurely ended'
            break
        
        show = game.half_moves < HEAD or game.half_moves >= len(replay)-TAIL
        
        game.output_level = 2 if show else 0
        
        if game.half_moves == HEAD and game.half_moves < len(replay)-TAIL:
            print>>html, '<h1>...</h1>'*3
        
        
        if show:
            player = game.half_moves%2
            if player == 0:
                print>>html, '<h4>###### turn {0}</h4>'.format(game.half_moves//2+1)
            print>>html, '<div style="background-color:{0}">'.format('#FFFFE0' if player else '#E0FFFF') 
            print>>html, "<h5>*** player {0}'s move</h5>".format(player)
            print>>html, '<table border="1"><tr>'
            for p in game.players:
                print>>html, '<td style="vertical-align:top">'
                print>>html, 'Slots: <table>'
                for i in range(SLOTS):
                    vit, value = p.vitalities[i], p.values[i]
                    try:
                        value = str(value)
                    except RuntimeError as e:
                        value = str(e)
                    color = '#B0FFB0'
                    if vit == 0:
                        color = '#FFB0B0'
                    if vit == -1:
                        color = '#00A060'
                    if (vit, value) != (INITIAL_VITALITY, 'I'):
                        print>>html, '<tr style="background-color:{3}"><td>{0}</td><td>{1}</td><td>{2}</td>'.format(i, vit, value, color)
                print>>html, '</table>'
                print>>html, '</td>'
            print>>html, '</tr></table>'

            print>>html, '<pre>'
            
        if game.has_zombie_phase():
            game.zombie_phase()
        move = bots[game.half_moves%2].choose_move()
        
        if show:
            if move[0] == LEFT_APP:
                print>>html, ('player {0} applied card {2} to slot {1}'.
                    format(player, move[1], move[2]))
            else:
                print>>html, ('player {0} applied slot {1} to card {2}'.
                    format(player, move[1], move[2]))
            print>>html, '</div>'
        game.make_half_move(*move)
        if show:
            print>>html, '</pre>'

    print>>html, '<h1>{0}:{1}</h1>'.format(game.players[0].num_alive_slots(), game.players[1].num_alive_slots())
    print>>html, '</div></body></html>'
    html.close()
    sys.stdout = original_stdout