from positions import positions from tron.game import Game, PositionPlayer def run_battle(ai1, ai2, width, height): games = 50 ai1_victories = 0 ai2_victories = 0 for (initial_position_one, initial_position_two) in positions(): player_1 = None player_2 = None try: player_1 = ai1.constructor() except: pass try: player_2 = ai2.constructor() except: pass winner = None if player_1 is not None and player_2 is not None: game = Game(width, height, [ PositionPlayer(1, player_1, initial_position_one), PositionPlayer(2, player_2, initial_position_two), ]) game.main_loop() winner = game.winner elif player_1 is None and player_2 is not None: # Player 2 wins because he was able to build an ai... winner = 2 elif player_1 is not None and player_2 is None: winner = 1 else: pass if winner == 1: ai1_victories += 1 elif winner == 2: ai2_victories += 1 # Inverse positions and replay to be symmetrical try: player_2 = ai2.constructor() except: pass try: player_1 = ai1.constructor() except: pass winner = None if player_1 is not None and player_2 is not None: game = Game(width, height, [ PositionPlayer(1, player_2, initial_position_one), PositionPlayer(2, player_1, initial_position_two), ]) game.main_loop() winner = game.winner elif player_1 is None and player_2 is not None: winner = 1 elif player_1 is not None and player_2 is None: winner = 2 if winner == 1: ai2_victories += 1 elif winner == 2: ai1_victories += 1 return (ai1_victories, ai2_victories, 2 * games - ai1_victories - ai2_victories)