diff --git a/pytron_run/utils.py b/pytron_run/utils.py index c8ae7af..f0888d3 100644 --- a/pytron_run/utils.py +++ b/pytron_run/utils.py @@ -7,27 +7,69 @@ def run_battle(ai1, ai2, width, height): ai2_victories = 0 for (initial_position_one, initial_position_two) in positions(): - game = Game(width, height, [ - PositionPlayer(1, ai1.constructor(), initial_position_one), - PositionPlayer(2, ai2.constructor(), initial_position_two), - ]) - game.main_loop() - if game.winner == 1: + 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 game.winner == 2: + elif winner == 2: ai2_victories += 1 # Inverse positions and replay to be symmetrical - game = Game(width, height, [ - PositionPlayer(1, ai2.constructor(), initial_position_one), - PositionPlayer(2, ai1.constructor(), initial_position_two), - ]) - game.main_loop() + try: + player_2 = ai2.constructor() + except: + pass - if game.winner == 1: + 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 game.winner == 2: + elif winner == 2: ai1_victories += 1 return (ai1_victories, ai2_victories, 2 * games - ai1_victories - ai2_victories)