-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
390 lines (293 loc) · 9.61 KB
/
grid.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
from copy import deepcopy
BLANK = 0
WHITE = 1
BLACK = 2
def printGrid(grid):
for i in grid:
print(i)
print("")
class Grid(object):
running = 0
solvable = 0
length = 0
spaces = 0
whites = 0
blacks = 0
tiles = []
"""Initialisation"""
def __init__(self, length):
self.length = length
self.running = 1
#Puzzle is only solvable if given length is > 0 and even
self.solvable = length > 0 and length % 2 == 0
#Create grid of given length
for i in range(length):
self.tiles.append([])
for j in range(length):
self.tiles[i].append(BLANK)
######################PUBLIC METHODS#######################
"""Main Run Loop"""
def run(self):
print("start:")
printGrid(self.tiles)
#Size of grid is not valid
if (not self.solvable): return
#No colour added initially, set puzzle to end with default values
self.spaces = self.countSpaces()
if self.spaces == self.length*self.length:
self.setAllDefaultValues()
return
#Start the recursive solver
self.solve()
"""The recursion function used to solve the puzzle"""
def solve(self):
#Puzzle has been solved
if not self.running: return
#There is a significant event in the loop, go back.
if self.gridCheck(): return
self.spaces = self.countSpaces()
#Apply method 1 and 2
self.method1()
self.method2()
newSpaces = self.countSpaces()
#No change has been made after applying method 1 and 2
if self.spaces == newSpaces:
#Save the state of the grid
state = deepcopy(self.tiles)
#Try a white tile and attempt to solve from there
self.method3(WHITE)
self.solve()
#Puzzle has been solved
if not self.running:
return
#Revert back for a chance to find another path
self.solvable = 1
self.spaces = newSpaces
self.copyState(state)
#Try a black tile and attempt to solve from there
self.method3(BLACK)
self.solve()
#Neither worked, must go back to an earlier state
return
#Method 1 or 2 made a change to the grid
self.spaces = newSpaces
self.solve()
return
#----------------------------------------------------------#
"""Finalise the puzzle with the result."""
def getResults(self):
print("Result:")
printGrid(self.tiles)
#Puzzle was not solved - print unsolvable
if not self.solvable or self.countSpaces() > 0:
print("UNSOLVABLE")
return
#Puzzle was solved - print the indexed of the white coloured tiles*/
pos = 1;
for i in self.tiles:
for j in i:
if j == WHITE:
print(pos),
pos += 1
#----------------------------------------------------------#
"""User input - Adding the colours to the grid"""
def addWhite(self, pos):
self.checkPosition(pos-1, WHITE);
def addBlack(self, pos):
self.checkPosition(pos-1, BLACK);
######################PRIVATE METHODS#######################
def setFinished(self):
self.running = 0
return 1
def setSolvable(self, flag):
self.solvable = flag
return 1
#----------------------------------------------------------#
"""Validate the 'user' number input"""
def checkPosition(self, pos, colour):
#Break puzzle if position is out of bounds
if pos < 0 or pos >= self.length*self.length:
self.setSolvable(0)
return
#Break puzzle if position is taken by the other colour
i = pos / self.length
j = pos % self.length
currentColour = self.tiles[i][j]
if currentColour != BLANK and currentColour != colour:
self.setSolvable(0)
return
self.tiles[i][j] = colour
#----------------------------------------------------------#
"""Check grid status - returns True to stop program, False otherwise."""
def gridCheck(self):
#Check every row for a match of 3 coloured tiles and for
#too much coloured tiles
for i in range(self.length):
for j in range(self.length-1):
if j == 0: continue
if self.check3inLine(self.tiles[i][j-1], self.tiles[i][j], self.tiles[i][j+1]):
return self.setSolvable(0)
#Check every row for too much coloured tiles
self.countRowColours(i);
if self.checkColourLimit(): return 1
#Check every column for a match of 3 coloured tiles
for j in range(self.length):
for i in range(self.length-1):
if i == 0: continue
if self.check3inLine(self.tiles[i-1][j], self.tiles[i][j], self.tiles[i+1][j]):
return self.setSolvable(0)
#Check every column for too much coloured tiles
self.countColumnColours(j);
if self.checkColourLimit(): return 1
#Grid full now - no problems encountered so far, puzzle solved!
if self.spaces == 0:
return self.setFinished()
return 0
#----------------------------------------------------------#
"""Check whether 3 coloured tiles in line match or not"""
def check3inLine(self, a, b, c):
row = (a==b) and (b==c)
return row and (a==WHITE or a==BLACK)
"""Check if one colour count is over the limit of the grid"""
def checkColourLimit(self):
limit = self.length/2
if self.whites > limit or self.blacks > limit:
return self.setSolvable(0)
return 0
"""Count the colours in the indexed row"""
def countRowColours(self, index):
self.whites = 0
self.blacks = 0
for i in range(self.length):
colour = self.tiles[index][i]
if colour == WHITE:
self.whites += 1
elif colour == BLACK:
self.blacks += 1
"""Count the colours in the indexed column"""
def countColumnColours(self, index):
self.whites = 0
self.blacks = 0
for i in range(self.length):
colour = self.tiles[i][index]
if colour == WHITE:
self.whites += 1
elif colour == BLACK:
self.blacks += 1
"""Retrieve the opposite colour to the given colour"""
def getOppositeColour(self, colour):
if colour == WHITE:
return BLACK
return WHITE
"""Count the remaining blank spaces left in the grid"""
def countSpaces(self):
count = 0
for i in self.tiles:
for j in i:
if j == BLANK:
count += 1
return count
"""Return the grid back to the given state"""
def copyState(self, state):
for i in range(self.length):
for j in range(self.length):
self.tiles[i][j] = state[i][j]
#----------------------------------------------------------#
"""Grid is empty - quick fill, 1's and 0's alternating"""
def setAllDefaultValues(self):
colour1 = 1
for i in range(self.length):
for j in range(self.length):
if colour1:
self.tiles[i][j] = WHITE
else:
self.tiles[i][j] = BLACK
colour1 = not colour1 #Alternate colour
colour1 = not colour1 #Alternate colour for next row
#----------------------------------------------------------#
"""Method 1 - Fills in blank tiles using pattern matching"""
def method1(self):
#Horizontal
for i in range(self.length):
for j in range(self.length-1):
if j == 0: continue
self.patternRowMatch(i, j)
#Vertical
for j in range(self.length):
for i in range(self.length-1):
if i == 0: continue
self.patternColumnMatch(i, j)
def patternRowMatch(self, i, j):
tile_prev = self.tiles[i][j-1]
tile = self.tiles[i][j]
tile_next = self.tiles[i][j+1]
if tile != BLANK:
#Previous and current tile are the same and next tile blank
#- the next tile must be the opposite colour
if tile_prev == tile and tile_next == BLANK:
self.tiles[i][j+1] = self.getOppositeColour(tile);
#Current and next tile...
elif tile == tile_next and tile_prev == BLANK:
self.tiles[i][j-1] = self.getOppositeColour(tile);
return;
#Current tile is blank and tiles on both sides have the same colour
#- current tile must be the opposite colour*/
if tile_prev == tile_next and tile_prev != BLANK:
self.tiles[i][j] = self.getOppositeColour(tile_prev);
def patternColumnMatch(self, i, j):
tile_prev = self.tiles[i-1][j]
tile = self.tiles[i][j]
tile_next = self.tiles[i+1][j]
if not tile == BLANK:
if tile_prev == tile and tile_next == BLANK:
self.tiles[i+1][j] = self.getOppositeColour(tile);
elif tile == tile_next and tile_prev == BLANK:
self.tiles[i-1][j] = self.getOppositeColour(tile);
return;
if tile_prev == tile_next and tile_prev != BLANK:
self.tiles[i][j] = self.getOppositeColour(tile_prev);
#----------------------------------------------------------#
"""Method 2 - Fills in the rest of the row/column blanks if
coloured tiles on one side is all used up"""
def method2(self):
#Horizontal
for i in range(self.length):
self.countRowColours(i)
self.fillRowPattern(i)
#Vertical
for j in range(self.length):
self.countColumnColours(j)
self.fillColumnPattern(j)
def fillRowPattern(self, index):
#Ignore - row is already filled up*/
if self.whites+self.blacks == self.length:
return
#If one colour in the row is at its max capacity, fill the blanks,
#fill the rest of the blanks in that row with the opposite colour*/
if self.whites == self.length/2 or self.blacks == self.length/2:
colourFill = WHITE
if (self.blacks < self.whites): colourFill = BLACK
for i in range(self.length):
if (self.tiles[index][i] == BLANK):
self.tiles[index][i] = colourFill
def fillColumnPattern(self, index):
if self.whites+self.blacks == self.length:
return
if self.whites == self.length/2 or self.blacks == self.length/2:
colourFill = WHITE
if self.blacks < self.whites: colourFill = BLACK
for i in range(self.length):
if self.tiles[i][index] == BLANK:
self.tiles[i][index] = colourFill;
#----------------------------------------------------------#
"""Method 3 - Find the next blank tile to place colour"""
def method3(self, colour):
#Find the position of the next blank tile
endCheck = 0
for i in range(self.length):
for j in range(self.length):
if self.tiles[i][j] == BLANK:
endCheck = 1
break
if endCheck: break
self.tiles[i][j] = colour