#!/usr/bin/env python3 import sys import json sys.path.append('../pytron') from tron.map import Map from tron.game import Game, PositionPlayer from tron.player import Direction, ConstantPlayer from importlib import import_module import positions import ai_manager # Find all the AIs class AiClass: def __init__(self, name, builder): self.name = name self.builder = builder ais = [] for real_ai in ai_manager.__all__: ai_name = real_ai.split('/')[-2] ai_module = import_module('.'.join(real_ai.split('/')[-3:])) ai_class = getattr(ai_module, "Ai") ais.append(AiClass(ai_name, ai_class)) # This script shows how to create a game with AI, that will run automatically. # It is made to be fast and not to be used by humans. It especially doesn't # display and window and doesn't listen to any keystrokes. width = 10 height = 10 def run_battle(ai1, ai2): games = 50 ai1_victories = 0 ai2_victories = 0 for (initial_position_one, initial_position_two) in positions.positions(): game = Game(width, height, [ PositionPlayer(1, ai1.builder(), initial_position_one), PositionPlayer(2, ai2.builder(), initial_position_two), ]) game.main_loop() if game.winner == 1: ai1_victories += 1 elif game.winner == 2: ai2_victories += 1 # Inverse positions and replay to be symmetrical game = Game(width, height, [ PositionPlayer(1, ai2.builder(), initial_position_one), PositionPlayer(2, ai1.builder(), initial_position_two), ]) game.main_loop() if game.winner == 1: ai2_victories += 1 elif game.winner == 2: ai1_victories += 1 return (ai1_victories, ai2_victories, 2 * games - ai1_victories - ai2_victories) def main(): print('Welcome to pytron run!') dictionnary = {"ais": list(map(lambda x: x.name, ais)), "battles": {}} for (id1, ai1) in enumerate(ais): for (id2, ai2) in enumerate(ais): if id1 >= id2: continue print("Battling {} vs {}".format(ai1.name, ai2.name)) (score1, score2, nulls) = run_battle(ai1, ai2) dictionnary["battles"][ai1.name + "/" + ai2.name] = [score1, score2, nulls] with open("assets/data.json", "w") as f: f.write(json.dumps(dictionnary)) print('Pytron run has finished') if __name__ == '__main__': main()