-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoard.py
410 lines (364 loc) · 12.8 KB
/
Board.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import copy
class Board:
"""
Provides the 4x4x4 board for the game, holds all moves,
can be used for lookahead
"""
# constructor
def __init__(self):
""" Creates original Board """
self.b = [[[0 for i in range(4)] for j in range(4)] for k in range(4)]
self.currentLines = {}
for playerNum in [1, 2]:
for numPoints in [1, 2, 3, 4]:
self.currentLines[(playerNum, numPoints)] = set()
self.currentLines[0] = set()
for i in range(76):
self.currentLines[0].add(i)
self.moveList = []
self.pointsList = {}
for playerNum in [0, 1, 2]:
self.pointsList[playerNum] = set()
for i in range(4):
for j in range(4):
for k in range(4):
self.pointsList[0].add((i, j, k))
# make, check and clear moves
def copyAll(self, b):
""" copies the given board object into self.b """
self.currentLines = copy.deepcopy(b.currentLines)
self.moveList = copy.deepcopy(b.moveList)
self.pointsList = copy.deepcopy(b.pointsList)
for x in range(4):
for y in range(4):
for z in range(4):
self.b[x][y][z] = b.b[x][y][z]
def openPoints(self):
"""
Return a list of all open points on the board
"""
return self.pointsList[0]
def myPoints(self, n):
"""
Return a list of all points player n has on the board
"""
return self.pointsList[n]
def otherNumber(self, n):
"""
Returns the other player's number
"""
newN = 0
if n == 1:
newN = 2
elif n == 2:
newN = 1
return newN
def numMoves(self, n):
""" Finds how many moves have been made by each player """
o = self.otherNumber(n)
return [len(self.pointsList[n]), len(self.pointsList[o])]
def move(self, n, p):
""" moves player n at point p, returns false if blocked """
current = self.pointToValue(p)
if (current == 0):
self.b[p[0]][p[1]][p[2]] = n
self.moveList.append((p, n))
self.updateLines(n, p)
self.pointsList[0].remove(p)
self.pointsList[n].add(p)
return True
else:
return False
def clearPoint(self, p):
""" clears a move, returns the previous value there
WARNING: THIS FUNCTION CURRENTLY DOES NOT UDPATE LINES
"""
previous = self.b[p[0]][p[1]][p[2]]
self.b[p[0]][p[1]][p[2]] = 0
if previous != 0:
self.moveList.remove((p, previous))
self.pointsList[0].add(p)
self.pointsList[previous].remove(p)
self.updateLines(previous, p)
return previous
def updateLines(self, n, p):
""" updates the important lines after each move"""
o = self.otherNumber(n)
for line in self.linesForPoint(p):
values = self.lineToValues(line)
playerMoves = values.count(n)
otherMoves = values.count(o)
# print line, playerMoves, otherMoves
# if we just removed a point (it's currently 0)
if self.pointToValue(p) == 0:
if otherMoves == 0 and playerMoves == 0:
self.currentLines[0].add(line)
self.currentLines[(n, 1)].remove(line)
elif otherMoves == 0:
self.currentLines[(n, playerMoves+1)].remove(line)
self.currentLines[(n, playerMoves)].add(line)
elif playerMoves == 0:
self.currentLines[(o, otherMoves)].add(line)
# Otherwise we just added a point, and update accordingly
else:
if playerMoves == 1 and otherMoves == 0:
self.currentLines[0].remove(line)
self.currentLines[(n, 1)].add(line)
elif playerMoves == 1:
self.currentLines[(o, otherMoves)].remove(line)
elif otherMoves == 0:
self.currentLines[(n, playerMoves-1)].remove(line)
self.currentLines[(n, playerMoves)].add(line)
# Find lines
def findLines(self, n, num):
""" old find lines but faster """
return self.currentLines[(n, num)]
# Point/Line conversions
def lineToPoints(self, line):
"""
Given a line number, this will return the set of four
points that make up that line, listed by x, y, and z
"""
if (0 <= line and line < 16):
return self.rowsToPoints(line)
elif (16 <= line and line < 32):
return self.colsToPoints(line)
elif (32 <= line and line < 48):
return self.vrtsToPoints(line)
elif (48 <= line and line < 76):
return self.diasToPoints(line)
else:
return []
def rowsToPoints(self, line):
"""
Given a line number between 0 and 15, this will return the
set of four points that make up that line, listed by x, y, and z
"""
l = line
z = l % 4
y = (l-z)/4
return [(i, y, z) for i in range(4)]
def colsToPoints(self, line):
"""
Given a line number between 16 and 31, this will return the
set of four points that make up that line, listed by x, y, and z
"""
l = line - 16
x = l % 4
z = (l-x)/4
return [(x, i, z) for i in range(4)]
def vrtsToPoints(self, line):
"""
Given a line number between 32 and 47, this will return the
set of four points that make up that line, listed by x, y, and z
"""
l = line - 32
y = l % 4
x = (l-y)/4
return [(x, y, i) for i in range(4)]
def diasToPoints(self, line):
"""
Given a line number between 48 and 75, this will return the
set of four points that make up that line, listed by x, y, and z
"""
if (48 <= line and line < 52):
l = line - 48
x = l % 4
return [(x, i, i) for i in range(4)]
if (52 <= line and line < 56):
l = line - 52
x = l % 4
return [(x, i, 3-i) for i in range(4)]
if (56 <= line and line < 60):
l = line - 56
y = l % 4
return [(i, y, i) for i in range(4)]
if (60 <= line and line < 64):
l = line - 60
y = l % 4
return [(i, y, 3-i) for i in range(4)]
if (64 <= line and line < 68):
l = line - 64
z = l % 4
return [(i, i, z) for i in range(4)]
if (68 <= line and line < 72):
l = line - 68
z = l % 4
return [(i, 3-i, z) for i in range(4)]
if (line == 72):
return [(i, i, i) for i in range(4)]
if (line == 73):
return [(i, i, 3-i) for i in range(4)]
if (line == 74):
return [(i, 3-i, i) for i in range(4)]
if (line == 75):
return [(3-i, i, i) for i in range(4)]
def pointsToLine(self, p1, p2):
"""
Given two points, this will return their line number
if they're in a line, and -1 if they are not
"""
line = -1
# make sure points are valid
for p in p1, p2:
for v in p:
if (v < 0 or v > 3):
return line
# see if they have the same x
if (p1[0] == p2[0]):
# see if they have the same y
if (p1[1] == p2[1]):
# must be vertical
if (p1[2] != p2[2]):
line = 32 + 4*p1[0] + p1[1]
# see if they have the same z
elif (p1[2] == p2[2]):
# must be in y dir
line = 16 + 4*p1[2] + p1[0]
# if both y and z change it must be diagonal
# see if it's coming from 0,0 or 0,3
elif (p1[1] == p1[2]):
# should be coming from 0,0; check
if (p2[1] == p2[2]):
line = 48 + p1[0]
elif (p1[1] == 3 - p1[2]):
# should be coming from 0,0; check
if (p2[1] == 3-p2[2]):
line = 52 + p1[0]
# see if they have the same y
elif (p1[1] == p2[1]):
# see if they have the same z
if (p1[2] == p2[2]):
# must be in x dir
line = 4*p1[1] + p1[2]
# if both x and z change it must be diagonal
# see if it's coming from 0,0 or 0,3
elif (p1[0] == p1[2]):
# should be coming from 0,0; check
if (p2[0] == p2[2]):
line = 56 + p1[1]
elif (p1[0] == 3 - p1[2]):
# should be coming from 0,0; check
if (p2[0] == 3-p2[2]):
line = 60 + p1[1]
# see if they have the same z
elif (p1[2] == p2[2]):
# if both x and y change it must be diagonal
# see if it's coming from 0,0 or 0,3
if (p1[0] == p1[1]):
# should be coming from 0,0; check
if (p2[0] == p2[1]):
line = 64 + p1[2]
elif (p1[0] == 3 - p1[1]):
# should be coming from 0,0; check
if (p2[0] == 3-p2[1]):
line = 68 + p1[2]
# if all change, must be on a main diagonal
# diagnoal 1
elif (p1[0] == p1[1] and p1[0] == p1[2]):
# check p2
if (p2[0] == p2[1] and p2[0] == p2[2]):
line = 72
# diagonal 2
elif (p1[0] == p1[1] and p1[0] == 3 - p1[2]):
# check p2
if (p2[0] == p2[1] and p2[0] == 3 - p2[2]):
line = 73
# diagonal 3
elif (p1[0] == 3 - p1[1] and p1[0] == p1[2]):
# check p2
if (p2[0] == 3 - p2[1] and p2[0] == p2[2]):
line = 74
# diagonal 4
elif (p1[0] == 3 - p1[1] and p1[0] == 3 - p1[2]):
# check p2
if (p2[0] == 3 - p2[1] and p2[0] == 3 - p2[2]):
line = 75
return line
# Searching specific lines
def lineToValues(self, line):
"""
Given a line number, this will return the value of all
four points in a list
"""
values = []
points = self.lineToPoints(line)
for p in points:
current = self.b[p[0]][p[1]][p[2]]
values += [current]
return values
# finding force moves
def findForces(self, n):
"""
Returns pairs of forcing moves
"""
pairs = []
lines = self.findLines(n, 2)
for l in lines:
currentPair = []
points = self.lineToPoints(l)
for p in points:
if self.pointToValue(p) == 0:
currentPair += [p]
pairs += [currentPair]
return pairs
def linesForPoint(self, p):
"""
Returns a list of all the line numbers passing through point p
"""
i = p[0]
j = p[1]
k = p[2]
lines = []
lines += [4*j + k]
lines += [16 + 4*k + i]
lines += [32 + 4*i + j]
if j == k:
lines += [48 + i]
if j == 3 - k:
lines += [52 + i]
if i == k:
lines += [56 + j]
if i == 3 - k:
lines += [60 + j]
if j == i:
lines += [64 + k]
if j == 3 - i:
lines += [68 + k]
if i == j and i == k:
lines += [72]
if i == j and i == 3 - k:
lines += [73]
if i == k and i == 3 - j:
lines += [74]
if j == k and i == 3 - j:
lines += [75]
return lines
def openLinesForPoint(self, n, p, num):
"""
Returns a list of all the line numbers of lines passing through
point p, with no non n moves and num moves by player n
"""
otherN = self.otherNumber(n)
lines = self.linesForPoint(p)
openLines = []
for l in lines:
blocked = False
values = self.lineToValues(l)
p1 = 0
for v in values:
if (v == otherN):
blocked = True
break
elif (v == n):
p1 += 1
if (p1 == num and not blocked):
openLines += [l]
return openLines
def pointToValue(self, p):
""" convert a single point to a value """
return self.b[p[0]][p[1]][p[2]]
def hash(self):
""" this is bad and gross. probably fix later"""
return sum(
[2**i * (self.b[i % 4][(i/4) % 4][i/16]-1) for i in range(64)])