-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
112 lines (88 loc) · 3.61 KB
/
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Game File
import node
import network
import agent
class Game:
def __init__(self, graph):
self.graph = graph
self.nodes = [node.Node(i, self.graph) for i in range(len(self.graph))]
self.groupings = []
self.bMoves = []
self.wMoves = []
self.legalMoves = [i for i in range(len(self.graph))]
self.bAgent = None
self.wAgent = None
self.network = network.Network(self.graph)
def run(self, ptb, ptw, bLevel, wLevel):
currentMove = "black"
if ptb == "human":
self.bAgent = agent.Agent("black", "human")
elif ptb == "negamax":
self.bAgent = agent.Agent("black", "negamax")
self.bAgent.maxDepth = bLevel + 1
elif ptb == "random":
self.bAgent = agent.Agent("black", "random")
elif ptb == "montecarlo":
self.bAgent = agent.Agent("black", "montecarlo")
if ptw == "human":
self.wAgent = agent.Agent("white", "human")
elif ptw == "negamax":
self.wAgent = agent.Agent("white", "negamax")
self.wAgent.maxDepth = wLevel + 1
elif ptw == "random":
self.wAgent = agent.Agent("white", "random")
elif ptw == "montecarlo":
self.wAgent = agent.Agent("white", "montecarlo")
while True:
self.network.draw(self.nodes)
decision = ""
if currentMove == "black":
decision = self.bAgent.decisionFunction(self)
self.processMove(decision, "black")
else:
decision = self.wAgent.decisionFunction(self)
self.processMove(decision, "white")
winner = self.findWinner(decision)
if winner != "none":
print(" " + winner.capitalize() + " has won the game!")
break
currentMove = "white" if currentMove == "black" else "black"
# Finish Game
self.network.draw(self.nodes)
input("Press any key to close application")
def processMove(self, location, player):
newStone = self.nodes[location]
#self.groupings.append([newStone]) # Creates new set
if player == "black":
self.bMoves.append(location)
newStone.color = "black"
else:
self.wMoves.append(location)
newStone.color = "white"
self.legalMoves.remove(location)
for i in newStone.neighbors:
nb = self.nodes[i]
nb.updateCircuitNeighbors(newStone)
if nb.color != newStone.color:
continue
nbRoot = self.findRoot(nb)
nb.parent = newStone.id # Set parent of neighbor to new stone
nbRoot.parent = newStone.id
newStone.edges |= nbRoot.edges # Bitwise OR to update edges
def findWinner(self, location):
# whenever someone plays a new stone
# either that stone won the game, or it didn't
# if they won the game, then the group of the new stone will touch all three sides
# to check who won, just check if the group of the new stone touches all three sides
if self.nodes[location].edges == 0b111:
return self.nodes[location].color
return "none"
def findRoot(self, node):
if node.parent != node.id:
node.parent = self.findRoot(self.nodes[node.parent]).id
return self.nodes[node.parent]
def findIndexOfNode(self, node): # Find which group a node resides in
for i in range(len(self.groupings)):
if node in self.groupings[i]:
return i
return -1