-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgameTree.py
More file actions
70 lines (40 loc) · 1.44 KB
/
gameTree.py
File metadata and controls
70 lines (40 loc) · 1.44 KB
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
import os
import pickle
"""
This file is a WIP. The idea was to generalize the logic used in Meagans_Tripartite_Graphs.py for all sorts of games.
"""
def getNimValue(board):
global previousData
reducedValue = board.reduce()
dataKey = str(reducedValue)
# Check if this parentBoard has a nim value that has already been calculated
nimValue = previousData.get(dataKey, None)
if nimValue is not None:
return nimValue
childBoards = []
for move in board.getAvailableMoves():
childGraphs.append(removeVertex(reducedValue, vertex))
for edgeMove in getEdgeMoves(reducedValue):
childGraphs.append(removeEdge(reducedValue, edgeMove))
childNimValues = set()
for graph in childGraphs:
childNimValues.add(getNimValue(graph))
childNimValues = list(childNimValues)
childNimValues.sort()
for i in range(len(childNimValues)):
if i != childNimValues[i]:
previousData[dataKey] = i
return i
# add the next highest Nim Value in childNimValue
previousData[dataKey] = len(childNimValues)
return len(childNimValues)
def generateTree(board):
global previousData
if os.path.exists(board.__class__.saveFile):
with open(board.__class__.saveFile, "rb") as file:
previousData = pickle.load(file)
else:
previousData = {}
return getNimValue(board)
if __name__ == '__main__':
previousData = None