pytron-web/pytron_run/utils.py

77 lines
2.0 KiB
Python
Raw Normal View History

2019-03-28 10:56:22 +01:00
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():
2019-04-04 11:59:37 +02:00
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:
2019-03-28 10:56:22 +01:00
ai1_victories += 1
2019-04-04 11:59:37 +02:00
elif winner == 2:
2019-03-28 10:56:22 +01:00
ai2_victories += 1
# Inverse positions and replay to be symmetrical
2019-04-04 11:59:37 +02:00
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
2019-03-28 10:56:22 +01:00
2019-04-04 11:59:37 +02:00
if winner == 1:
2019-03-28 10:56:22 +01:00
ai2_victories += 1
2019-04-04 11:59:37 +02:00
elif winner == 2:
2019-03-28 10:56:22 +01:00
ai1_victories += 1
return (ai1_victories, ai2_victories, 2 * games - ai1_victories - ai2_victories)