Esempio n. 1
0
def determine_outcome(bot_1,
                      bot_2,
                      game_name,
                      num_games=10,
                      path="maps/empty_room.txt"):
    botlist = [bot_1, bot_2]
    wins = 0
    # vis_game = random.randrange(num_games)
    vis_game = -1
    for n in range(num_games):
        game = TronProblem(path, 0 if n < num_games / 2 else 1)
        if n == vis_game:
            outcome, metadata = run_game(game, botlist, game_name)
        else:
            outcome, metadata = run_game(game,
                                         botlist,
                                         game_name,
                                         visualizer=None)
        winner = outcome.index(1)
        if winner == 0:
            wins += 1
        for bot in botlist:
            bot.cleanup()
    if wins > 6:
        return 1.0, metadata
    elif wins > 3:
        return 0.5, metadata
    else:
        return 0.0, metadata
Esempio n. 2
0
def main():
    parser = argparse.ArgumentParser(prog="gamerunner", usage="%(prog)s [options]")
    parser.add_argument('-map', type=str, help='''the filename of the map to use for this game. 
Defaults to "./maps/empty_room.txt"''', default = "./maps/empty_room.txt")
    parser.add_argument('-max_wait', type=float, help='''The amount of time (in seconds) the game engine will wait
for a player to decide what move they want to make. If the player takes too long, they go north. 
Defaults to 0.3 (this will be reset during grading)''', default = 0.3)
    parser.add_argument('-bots', type=str, nargs='+', help='''which bot each player will use. Valid bot
types include "student", "wall", "random", "TABot1". This argument takes in a 
sequnce of bot types, where the first bot is used for the first player, the second bot is for the second player,
and so on. Defaults to "random random". Note that errors will occur if there are not enough AIs for 
the number of players on the board.''', default=['random','random'])
    parser.add_argument('-image_delay', type=float, help='''The amount of time (in seconds) to wait after
printing the current state of the game. This is just to give users more time to watch the game progress. 
Defaults to 0.2 seconds.''', default=0.2)
    parser.add_argument('-no_image', dest='show_image', help='''include this flag (with no arguments) to supress the
output of all board states and just get final results''', action='store_false')
    parser.set_defaults(show_image=True)
    parser.add_argument('-multi_test', type=int, help='''Test this map (multi_test) times in a row. Useful if you
want to see how randomized algorithms do on average. It's recommended that you turn off verbose for this. Tracks 
how many times each player won across all games.''')
    
    args = parser.parse_args()

    wait = args.max_wait
    bots = support.determine_bot_functions(args.bots)
    delay = args.image_delay
    verbose = args.show_image
    multi = args.multi_test
    
    game = TronProblem(args.map,0)
    
    visualizer = None
    if verbose:
        visualizer = TronProblem.visualize_state
    
    if multi is not None:
        winners = defaultdict(int)
        bots = support.determine_bot_functions(args.bots)
        for i in range(multi):
            outcome = run_game(copy.deepcopy(game),bots,visualizer,delay,wait)
            winner = outcome.index(1)
            winners[winner] += 1
            for bot in bots:
                bot.cleanup()
        for winner, wins in winners.items():
            print "Player %s won %d out of %d times" % (winner+1, wins, multi)
                
    else:
        outcome = run_game(copy.deepcopy(game),bots,visualizer,delay,wait)
        winner = outcome.index(1) + 1
        print "Player %s won!" % winner
Esempio n. 3
0
def main():
    empty_game = TronProblem("maps/empty_room.txt", 0)
    joust_game = TronProblem("maps/joust.txt", 0)
    hunger_game = TronProblem("maps/hunger_games.txt", 0)
    divider_game = TronProblem("maps/divider.txt", 0)

    bots = support.determine_bot_functions(["ta2", "student"])

    e_total = 0
    j_total = 0
    h_total = 0
    d_total = 0

    for trials in range(100):
        e_total = e_total + run_game(copy.deepcopy(empty_game), bots).index(1)
        j_total = j_total + run_game(copy.deepcopy(joust_game), bots).index(1)
        h_total = h_total + run_game(copy.deepcopy(hunger_game), bots).index(1)
        d_total = d_total + run_game(copy.deepcopy(divider_game),
                                     bots).index(1)

    print("Empty: {} Joust: {} HG: {} Divider: {}\n".format(
        e_total, j_total, h_total, d_total))
def main():

    tm = TronModel.TM(2901, 4)
    mapList = [
        "center_block.txt", "diagonal_blocks.txt", "divider.txt",
        "empty_room.txt", "hunger_games.txt", "joust.txt", "small_room.txt"
    ]

    for i in range(20000):

        # mapChoice = random.choice(mapList)
        # loss, lengths = trainOneGame(TronProblem("maps/" + mapChoice, 0), tm)
        loss, lengths = trainOneGame(
            TronProblem("maps/" + "center_block.txt", 0), tm)
        print(i)
        if (i % 10 == 0):
            print(loss)
            print(lengths)
        print('\n')
Esempio n. 5
0
def main():
    random.seed(1)
    np.random.seed(1)

    parser = argparse.ArgumentParser(prog="gamerunner",
                                     usage="%(prog)s [options]")
    parser.add_argument("-map",
                        type=str,
                        help=HelpMessage.MAP,
                        default=Argument_Defaults.MAP)
    parser.add_argument(
        "-max_wait",
        type=float,
        help=HelpMessage.MAX_WAIT,
        default=Argument_Defaults.MAX_WAIT,
    )
    parser.add_argument(
        "-bots",
        type=str,
        nargs="+",
        help=HelpMessage.BOTS,
        default=Argument_Defaults.BOTS,
    )
    parser.add_argument(
        "-image_delay",
        type=float,
        help=HelpMessage.IMAGE_DELAY,
        default=Argument_Defaults.IMAGE_DELAY,
    )
    parser.add_argument("-no_image",
                        dest="show_image",
                        help=HelpMessage.NO_IMAGE,
                        action="store_false")
    parser.set_defaults(show_image=True)
    parser.add_argument("-multi_test", type=int, help=HelpMessage.MULTI_TEST)
    parser.add_argument("-no_color",
                        dest="colored",
                        help=HelpMessage.COLOR,
                        action="store_false")
    parser.set_defaults(colored=True)

    args = parser.parse_args()

    wait = args.max_wait
    bots = support.determine_bot_functions(args.bots)
    delay = args.image_delay
    verbose = args.show_image
    multi = args.multi_test
    colored = args.colored

    visualizer = None
    if verbose:
        visualizer = TronProblem.visualize_state

    if multi is not None:
        winners = defaultdict(int)
        bots = support.determine_bot_functions(args.bots)
        for i in range(multi):
            game = TronProblem(args.map, 0)
            outcome = run_game(game, bots, visualizer, delay, wait, colored)
            winner = outcome.index(1)
            winners[winner] += 1
            for bot in bots:
                bot.cleanup()
        for winner, wins in list(winners.items()):
            print("Player %s won %d out of %d times" %
                  (winner + 1, wins, multi))

    else:
        game = TronProblem(args.map, 0)
        outcome = run_game(game, bots, visualizer, delay, wait, colored)
        winner = outcome.index(1) + 1
        print("Player %s won!" % winner)