Example #1
0
def black_box_run(args):
    """Run the game in a black box"""
    kw = dict(ai0_filename=args.ai_0, ai1_filename=args.ai_1)
    if args.timeout:
        kw['timeout'] = args.timeout

    try:
        if args.battle:  # Load from pervious battle
            b = load(args.map, **kw)
        else:  # Construct a new battle
            m = map_info.load(args.map)
            b = AIBattle(m, **kw)

        if args.time_per_round:  # override time_per_round of the map
            b.gamebody.map_info.time_per_round = args.time_per_round

        if args.debug:
            while b.gamebody.state == gamebody.STATE_CONTINUE:
                raw_input('Round %d (press Enter to advance)' % b.round())
                b.feed_ai_commands()
                b.next_round()
        else:
            b.run_until_end()
    except KeyboardInterrupt:
        logger.error("KeyboardInterrupt received, aborting")
        os.abort()

    # Print result
    print b.round(), 'round(s) passed'
    result = b.state()
    if result == 0 or result == 1:
        print 'Team %d wins' % result
    elif result == gamebody.STATE_TIE:
        print 'Tie (%d : %d)' % (b.score(0), b.score(1))
    else:
        raise RuntimeError('Invalid game state: %d', result)

    # Save into files
    if args.save:
        b.save(args.save, compact=args.compact, compress=args.compress)
    if args.out:
        b.save_event_strings(args.out)

    del b  # Destroy the battle
    return result
Example #2
0
def run_test_match(args):
    """Run test match"""
    # check level & load match_info
    level = int(args.level)
    if level < 1 or level > len(TestGame):
        print 'Level %d does not exist' % level
        return

    judge, map_file, opponent_ai = TestGame[level - 1]  # level begins from 1

    # Try to figure out where built AIs are
    # TODO: likely to failed mow
    last_slash_pos = args.ai.rfind('/')
    # Try back slash if failed
    if last_slash_pos == -1:
        last_slash_pos = args.ai.rfind('\\')

    base_dir_end_pos = 0 if last_slash_pos == -1 else last_slash_pos + 1
    opponent_ai = args.ai[0:base_dir_end_pos] + opponent_ai
    logger.debug('opponent_ai: %s', opponent_ai)

    kw = dict(ai0_filename=args.ai, ai1_filename=opponent_ai, judge=judge)
    if args.timeout:
        kw['timeout'] = args.timeout

    if map_file.endswith('.battle'):  # Load from pervious battle
        b = ai_battle.load(map_file, **kw)
    else:  # Construct a new battle
        m = map_info.load(map_file)
        b = ai_battle.AIBattle(m, **kw)

    if args.time_per_round:  # override time_per_round of the map
        b.gamebody.map_info.time_per_round = args.time_per_round

    if args.debug:
        while b.gamebody.state == gamebody.STATE_CONTINUE:
            raw_input('Round %d (press Enter to advance)' % g.round())
            b.feed_ai_commands()
            b.next_round()
    else:
        b.run_until_end()

    # Print result
    print b.round(), 'round(s) passed'
    print 'Your AI (%s)' % b.team_name(0),
    result = b.state()
    if result == 0:
        print 'won'
    elif result == 1:
        print 'lost'
    elif result == gamebody.STATE_TIE:
        print 'tied'
    else:
        raise RuntimeError('Invalid game state: %d', result)

    print 'Score (Your AI : opponent_ai):'
    print '%d : %d' % (b.score(0), b.score(1))

    # Save into files
    if args.save:
        b.save(args.save, compact=args.compact, compress=args.compress)
    if args.out:
        b.save_event_strings(args.out)

    del b  # Destroy the battle
Example #3
0
def main(m, ai0, ai1):
    b = AIBattle(map_info.load(m), ai0_filename=ai0, ai1_filename=ai1)
    b.run_until_end()