pytron-web/pytron_run/run.py

51 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import json
import pathlib
sys.path.append('../pytron')
from tron.map import Map
from tron.game import Game, PositionPlayer
from tron.player import Direction, ConstantPlayer
import ai_manager
from utils import run_battle
ASSETS_PATH = "assets/data.json"
LAST_REFRESH_PATH = "assets/refresh.dat"
width = 10
height = 10
def main():
print('Welcome to pytron run!')
dictionnary = {}
# Set last update time
pathlib.Path(LAST_REFRESH_PATH).touch()
for (id1, ai1) in enumerate(ai_manager.__all__):
for (id2, ai2) in enumerate(ai_manager.__all__):
if id1 >= id2:
continue
# Sort ais by name just to be sure
if ai1.name > ai2.name:
(sai2, sai1) = (ai1, ai2)
else:
(sai1, sai2) = (ai1, ai2)
print("Battling {} vs {}".format(sai1.name, sai2.name))
(score1, score2, nulls) = run_battle(sai1, sai2, width, height)
dictionnary[sai1.name + "/" + sai2.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()