-
Notifications
You must be signed in to change notification settings - Fork 0
/
NimBase.py
213 lines (175 loc) · 7.97 KB
/
NimBase.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from NimTuples import NimTuples
from enum import Enum
class PlayCondition(Enum) :
Normal = 0
Misere = 1
class NimBase(NimTuples):
# This class provides the following functions:
# __init__(self, run)
# run(self)
# getOutcome(self,t)
# offthegrid(self,t)
# setDimensions(self, dimensions)
# setNomalPlay(self)
# setMiserePlay(self)
def __init__(self, run = False):
# The __init__ function is automatically run when an instance of the
# class is created.
# The parameter run indicates whether the class should run with the
# default parameters set below, or (by default) just create the structures
#
# The class tuples are predefined to () until the setup function is run,
# which automatically creates them according to the given dimension
self.max_dimensions = 0 # The maximum number of dimensions to consider
self.max_depth = 100 # The maximum allowed size of the rectangle in any dimension
# if max_depth is set to 0, then dimensions can be infinite
self.origen = () # The 0 vector
self.preperiod = () # The lowest position in each dimension for which
# the period holds
self.outcomes = {} # a dictionary (key-value pair) of positions and their outcomes
# outcomes are either 'N' or 'P'
self.rectangle = () # The shape of the rectangle needed to work out the period
self.normal_play = True
self.print_report_when_done = False
self.verifyFailures = list()
if run:
self.run()
def run(self):
self.explore()
def compare(self, p, r, d):
self.inc_dim = 0
while (self.inc_dim < d):
if self.getOutcome(p) != self.getOutcome(r):
return False
else:
self.incrementTupleWithCarry(p)
self.incrementTupleWithCarry(r)
return True
def findMatch(self, d):
dim = d
for i in range(self.preperiod[dim], self.rectangle[dim]):
rec_check = self.zeroTupleBelow(self.rectangle, dim)
pre_check = self.setTuplePositionXtoY(rec_check, dim, i)
self.inc_dim = 0
match = True
while self.inc_dim < dim and match is True:
if self.getOutcome(rec_check) != self.getOutcome(pre_check):
match = False
else:
rec_check = self.incrementTupleWithCarry(rec_check)
pre_check = self.incrementTupleWithCarry(pre_check)
if match:
return i
return -1
def explore(self):
explore_dim = 1
while explore_dim <= self.max_dimensions:
# Zero out later dimensions of preperiod, rectangle
self.preperiod = self.zeroTupleAbove(self.preperiod, explore_dim)
self.rectangle = self.zeroTupleAbove(self.rectangle, explore_dim)
self.rectangle = self.incrementTuple(self.rectangle, explore_dim)
# make sure rectangle has not overrun preset maximum in the current dimension
if self.max_depth > 0 and self.rectangle[explore_dim] > self.max_depth:
print("\nError: self.rectangle in dimension {}".format(explore_dim))
print("has exceeded maximum depth of {}".format(self.max_depth))
return False
# check if preperiod and period still holds for new value of rectangle
failure_dimension = self.verify(explore_dim)
if failure_dimension > -1:
# if not, explore at dimension where things fail
explore_dim = failure_dimension
else:
match_pos = self.findMatch(explore_dim)
if match_pos > -1:
self.updatePreperiod(explore_dim, match_pos)
explore_dim += 1
return True
def getOutcome(self,t):
# Input: a tuple
# Output: 'P' or 'N'
#
# This function returns the P/N value in the outcomes dictionary, if it exists
# if not, it calculates it, and stores it in the outcomes dictionary and returns it
if t in self.outcomes:
return self.outcomes[t]
if self.offthegrid(t):
return 'N'
for move in self.moves:
if self.getOutcome(self.addTuples(t,move)) == 'P':
self.outcomes[t] = 'N'
return 'N'
self.outcomes[t] = 'P'
return 'P'
def offthegrid(self,t):
# Input: t, a tuple
# Output: True if t contains a negative scalar
# False otherwise
for idx in range(len(t)):
if t[idx] == -1:
return True
return False
def period(self):
# calculates the period from rectangle and preperiod
t = self.origen
for i in range(len(self.rectangle)):
if self.rectangle[i] == None or self.preperiod[i] == None:
t = self.setTuplePositionXtoY(t, i, None)
else:
t = self.setTuplePositionXtoY(t, i,
(self.rectangle[i] - self.preperiod[i]))
return t
def verify(self, d = None):
explore_dim = d
if explore_dim is None:
explore_dim = 1
for i in range(2, self.max_dimensions + 1):
if self.rectangle[i] > 0:
explore_dim = i
for verify_dim in range(2, explore_dim):
check_tuple = self.zeroTupleBelow(self.rectangle, explore_dim)
self.inc_dim = 0
pre_check = self.setTuplePositionXtoY(check_tuple, verify_dim, self.preperiod[verify_dim])
rec_check = self.setTuplePositionXtoY(check_tuple, verify_dim, self.rectangle[verify_dim])
while self.inc_dim < explore_dim:
if self.getOutcome(pre_check) != self.getOutcome(rec_check):
self.verifyFailures.append(self.rectangle)
return verify_dim
check_tuple = self.incrementTupleWithCarry(check_tuple, verify_dim)
return -1
def setMoves(self, code=None):
#Input: a valid quartenary code string
#Output: nothing
# This function sets self.moves accoring to the code given
# It requires that self.max_dimension is set
self.rulecode = code
# Test various values of rulecode to set it properly
# If rulecode is not set, set it to 0.3333....
if self.rulecode == None:
self.rulecode = "0."
while len(self.rulecode) < self.max_dimensions + 2:
self.rulecode += "3"
# If rulecode entered as a float, convert to a string
if type(self.rulecode) == type(float()):
self.rulecode = str(self.rulecode)
# At this point rulecode should be a string. If not,
# throw
if type(self.rulecode) != type(str()):
raise ValueError("code {} is invalid." +
"Please enter a string or a float\n".format(self.rulecode))
# And add trailing 0's to shorthand codes
while len(self.rulecode) < self.max_dimensions + 2:
self.rulecode += "0"
self.moves = list()
cur_position = 1
for digit in self.rulecode[2:]:
if digit == '1' or digit == '3':
new_move = self.setTuplePositionXtoY(self.origen, cur_position, -1)
self.moves.append(new_move)
if digit == '2' or digit == '3':
for j in range(cur_position + 1, self.max_dimensions +1):
new_move = self.setTuplePositionXtoY(self.origen, j, -1)
new_move = self.setTuplePositionXtoY(new_move, j-cur_position, 1)
self.moves.append(new_move)
cur_position += 1
def updatePreperiod(self, cur_dimension, match_value):
self.preperiod = self.setTuplePositionXtoY(self.preperiod, cur_dimension, match_value)