-
Notifications
You must be signed in to change notification settings - Fork 2
/
gameplay.py
59 lines (45 loc) · 1.68 KB
/
gameplay.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
"""
Function to play a game given a policy for each player and keep track of
the game as a NetworkX game tree.
Requires the NetworkX graph package, which is included in Anaconda
"""
import networkx as nx
from gamestate import GameState
from policies import MCTSPolicy
class StateNode(object):
def __init__(self, board):
self.state = board
self.parent = None
self.child = None
def play_game(player_policies):
"""
:param player_policies: List of policy classes for players X and O
which determine how each player moves given a particular state. Each
policy class should inherit from Policy.
:return: Returns a NetworkX Graph object describing the game
"""
game = GameState()
# Keep track of the game tree
G = nx.DiGraph()
# Todo: use the newly implemented hashing method
G.add_node(str(game))
root = str(game)
current = root
plies = 0
# Inform the player policies that a new game is starting (so they can reset their current move pointers)
for player_policy in player_policies:
if type(player_policy) is MCTSPolicy:
player_policy.reset_game()
while game.winner() is None:
for player_policy in player_policies:
plies += 1
print("\n================ ( Ply #{}. It is {}'s move. ) ================".format(plies, game.turn()))
game.move(*player_policy.move(game))
previous = current
G.add_node(str(game))
current = str(game)
G.add_edge(previous, current)
if game.winner() is not None:
break
print('Game over. Winner is {}.'.format(game.winner()))
return G, game.winner()