-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.py
27 lines (23 loc) · 896 Bytes
/
game.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
from types import SimpleNamespace
from players import RandomPlayer
def play(state, player1=None, player2=None):
players = {1: player1 or RandomPlayer(),
-1: player2 or RandomPlayer()}
states = []
while not state.gameover():
player = players[state.player()]
action = player.next_action(state)
state = state.move(action)
states.append(SimpleNamespace(action=action, state=state))
return states, state.winner()
def simulate(state, play_count, player1=None, player2=None):
import click
plays = []
label = f'Simulating {play_count} games...'
with click.progressbar(label=label, length=play_count) as bar:
for _ in range(play_count):
states, winner = play(state, player1, player2)
if winner != 0:
plays.append((states, winner))
bar.update(1)
return plays