-
Notifications
You must be signed in to change notification settings - Fork 0
/
tournament.py
82 lines (59 loc) · 2.45 KB
/
tournament.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!usr/bin/env python
"""
A command line program for multiple games between several bots.
For all the options run
python play.py -h
"""
from argparse import ArgumentParser
from api import State, util, engine
import random
def run_tournament(options):
botnames = options.players.split(",")
bots = []
for botname in botnames:
bots.append(util.load_player(botname))
n = len(bots)
wins = [0] * len(bots)
matches = [(p1, p2) for p1 in range(n) for p2 in range(n) if p1 < p2]
totalgames = (n*n - n)/2 * options.repeats
playedgames = 0
print('Playing {} games:'.format(int(totalgames)))
for a, b in matches:
for r in range(options.repeats):
if random.choice([True, False]):
p = [a, b]
else:
p = [b, a]
# Generate a state with a random seed
start = State.generate(phase=int(options.phase))
winner = engine.play(bots[p[0]], bots[p[1]], start, options.max_time*1000, verbose=False)
#TODO: ALSO IMPLEMENT POINTS FOR WINNING
if winner is not None:
winner = p[winner[0] - 1]
wins[winner] += 1
playedgames += 1
print('Played {} out of {:.0f} games ({:.0f}%): {} \r'.format(playedgames, totalgames, playedgames/float(totalgames) * 100, wins))
print('Results:')
for i in range(len(bots)):
print(' bot {}: {} wins'.format(bots[i], wins[i]))
if __name__ == "__main__":
## Parse the command line options
parser = ArgumentParser()
parser.add_argument("-s", "--starting-phase",
dest="phase",
help="Which phase the game should start at.",
default=1)
parser.add_argument("-p", "--players",
dest="players",
help="Comma-separated list of player names (enclose with quotes).",
default="rand,bully,rdeep")
parser.add_argument("-r", "--repeats",
dest="repeats",
help="How many matches to play for each pair of bots",
type=int, default=10)
parser.add_argument("-t", "--max-time",
dest="max_time",
help="maximum amount of time allowed per turn in seconds (default: 5)",
type=int, default=5)
options = parser.parse_args()
run_tournament(options)