-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.py
536 lines (446 loc) · 15.2 KB
/
Sudoku.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
from Square import Square
import unittest
class Board:
board = [[Square(x, y) for x in range(9)] for y in range(9)]
move_list = list()
def __init__(self):
#: list of Square: Doc comment *before* attribute, with type specified
self.board = [[Square(x, y) for x in range(9)] for y in range(9)]
self.move_list = list()
self.answer = None
def __repr__(self):
full = ""
for i in range(9):
for j in range(9):
full += str(self.board[i][j].num)
return full
def __str__(self):
full = "\n"
for i in range(0, 9):
r_str = ""
for j in range(0, 9):
if j % 3 == 0:
r_str += "|"
else:
r_str += " "
r_str += self.num_print(self.board[i][j].num)
full += r_str+"|\n"
if i % 3 == 2:
full += "-------------------\n"
return full
def get_board(self):
return [[self.board[y][x].num for x in range(9)] for y in range(9)]
def initialize_breezy(self):
self.clear_board()
self.answer = [
[3,9,8, 7,1,5, 4,2,6],
[2,7,1, 4,3,6, 5,9,8],
[5,4,6, 8,9,2, 1,3,7],
[6,3,7, 1,5,4, 2,8,9],
[1,2,4, 3,8,9, 6,7,5],
[8,5,9, 2,6,7, 3,4,1],
[4,6,2, 9,7,1, 8,5,3],
[7,8,5, 6,4,3, 9,1,2],
[9,1,3, 5,2,8, 7,6,4],
]
arr = [
[3,9,8, 7,1,5, 4,2,6],
[2,7,1, 4,3,6, 0,0,8],
[5,4,6, 8,9,2, 1,3,7],
[6,3,7, 1,5,4, 2,8,9],
[1,2,4, 3,8,9, 6,7,5],
[8,5,9, 2,6,7, 3,4,1],
[4,6,2, 9,7,1, 8,5,3],
[7,8,5, 6,4,3, 0,1,2],
[9,1,3, 5,2,8, 7,6,4],
]
self.set_board(arr)
def initialize_easy(self):
self.clear_board()
self.answer = [
[3,9,8, 7,1,5, 4,2,6],
[2,7,1, 4,3,6, 5,9,8],
[5,4,6, 8,9,2, 1,3,7],
[6,3,7, 1,5,4, 2,8,9],
[1,2,4, 3,8,9, 6,7,5],
[8,5,9, 2,6,7, 3,4,1],
[4,6,2, 9,7,1, 8,5,3],
[7,8,5, 6,4,3, 9,1,2],
[9,1,3, 5,2,8, 7,6,4],
]
arr = [
[3,9,0, 0,0,0, 0,2,0],
[2,7,0, 0,0,6, 0,9,8],
[0,4,0, 8,9,0, 0,0,7],
[0,0,7, 0,5,0, 0,0,9],
[0,2,0, 0,8,0, 0,7,0],
[8,0,0, 0,6,0, 3,0,0],
[4,0,0, 0,7,1, 0,5,0],
[7,8,0, 6,0,0, 0,1,2],
[0,1,0, 0,0,0, 0,6,4],
]
self.set_board(arr)
def initialize_medium(self):
arr = "020089703" \
"000015008" \
"900000000" \
"090000300" \
"307000201" \
"004000060" \
"000000006" \
"800470000" \
"501930080"
self.clear_board()
self.set_board(arr)
def initialize_hard(self):
self.clear_board()
arr = "007400002" \
"030090050" \
"400008300" \
"800007400" \
"010080090" \
"006100007" \
"001500008" \
"080030070" \
"600002900"
self.set_board(arr)
def initialize_impossible(self):
self.clear_board()
arr = [
[3,9,0, 4,0,0, 0,2,0],
[2,7,0, 0,0,6, 0,9,8],
[0,4,0, 8,9,0, 0,0,7],
[0,0,7, 0,5,0, 0,0,9],
[0,2,0, 0,8,0, 0,7,0],
[8,0,0, 0,6,0, 3,0,0],
[4,0,0, 0,7,1, 0,5,0],
[7,8,0, 6,0,0, 0,1,2],
[0,1,0, 0,0,0, 0,6,4],
]
self.set_board(arr)
def clear_board(self):
self.board = [[Square(x, y) for x in range(9)] for y in range(9)]
def set_board(self, arr):
if type(arr) == list:
for i in range(9):
for j in range(9):
self.board[i][j].reset_open()
self.setPos(i, j, arr[i][j])
elif type(arr) == str:
for i in range(9):
for j in range(9):
self.board[i][j].reset_open()
self.setPos(i, j, int(arr[i*9+j]))
self.initialize_blocked()
def initialize_blocked(self):
"""
Sets blocked positions from input board
:return:
:rtype: None
"""
for x in range(9):
for y in range(9):
# if a number is in the position
if not self.is_empty(x, y):
self.board[x][y].open = None
self.block_intersecting(x, y, self.getPos(x, y).num)
def block_intersecting(self, x, y, num):
quad_x = x//3*3
quad_y = y//3*3
for i in range(0, 9):
self.block_num(x, i, num)
self.block_num(i, y, num)
self.block_num(quad_x + i % 3, quad_y + i//3, num)
@staticmethod
def num_print(num):
"""
Returns display num
:param num: int to be displayed
:type num: int
:return: display
:rtype: string
"""
if num == 0:
return "_"
if num > 9 or num < 0:
raise ValueError("Out of bounds: "+str(num))
return str(num)
def display(self):
"""
Prints Sudoku to console
:return:
:rtype: None
"""
print("")
for i in range(0, 9):
r_str = ""
for j in range(0, 9):
if j % 3 == 0:
r_str += "|"
else:
r_str += " "
r_str += self.num_print(self.board[i][j].num)
print(r_str+"|")
if i % 3 == 2:
print("-------------------")
# print("+-+-+-+-+-+-+-+-+-+")
def setPos(self, x, y, num):
assert(self.is_empty(x, y))
# self.assert_not_blocked(x, y, num)
self.board[x][y].set_num(num)
def getPos(self, x, y):
return self.board[x][y]
def pos_count(self, x, y):
return self.board[x][y].open_count
def is_empty(self, x, y):
return self.board[x][y].is_empty()
def has_single(self, x, y):
return self.board[x][y].has_single()
def block_num(self, x, y, num):
"""
Blocks a number in empty squares from being used.
Returns true if number changed.
:param x: row index
:type x: int
:param y: column index
:type y: int
:param num: num in range(1,9) to block
:type num: int
:return: True if number changed
:rtype: bool
"""
if self.is_empty(x, y):
if self.board[x][y].is_open(num):
self.board[x][y].block_num(num)
return True
return False
def set_block(self, x, y, num):
"""
Sets position and blocks intersecting positions
:param x: row index
:type x: int
:param y: col index
:type y: int
:param num: number
:type num: int
:return:
:rtype: None
"""
self.setPos(x, y, num)
self.block_intersecting(x, y, num)
def assert_not_blocked(self, x, y, num):
"""
Asserts that position can contain that number
:param x: row index
:type x: int
:param y: column index
:type y: int
:param num: number in square
:type num: int
:return: True if there are no intersecting numbers
:rtype: bool
"""
if num == 0:
return
quad_x = x//3*3
quad_y = y//3*3
for i in range(0,9):
assert(self.getPos(x, i).num != num)
assert(self.getPos(i, y).num != num)
assert(self.getPos(quad_x + i % 3, quad_y + i//3).num != num)
ERRORS_COL = 1
ERRORS_ROW = 2
ERRORS_BLK = 4
def is_solvable(self):
solvable = True
errors = dict()
for i in range(9):
col = [True for x in range(9)]
row = [True for x in range(9)]
blk = [True for x in range(9)]
quad_x = i // 3 * 3
quad_y = i % 3 * 3
for j in range(0, 9):
col_i = self.board[j][i].num-1
row_i = self.board[i][j].num-1
blk_i = self.board[quad_x + j//3][quad_y + j % 3].num-1
if col_i != -1:
if col[col_i]:
col[col_i] = False
else:
errors[j, i] = self.ERRORS_COL
# print("Duplicate at", j, i)
solvable = False
if row_i != -1:
if row[row_i] is True:
row[row_i] = False
else:
errors[i, j] = self.ERRORS_ROW
# print("Duplicate at", i, j)
solvable = False
if blk_i != -1:
if blk[blk_i] is True:
blk[blk_i] = False
else:
qx = quad_x + j // 3
qy = quad_y + j % 3
errors[qx, qy] = self.ERRORS_BLK
# print("Duplicate at", qx, qy)
solvable = False
# for k in range(0, 9):
# if col[k] or row[k] or blk[k]:
# if col[k]:
# out = "column"
# elif row[k]:
# out = "row"
# else:
# out = "block"
# print("Fault in", out, i)
# return False
return solvable, errors
def is_solved(self):
for i in range(0,9):
col = [True for x in range(9)]
row = [True for x in range(9)]
blk = [True for x in range(9)]
quad_x = i // 3 * 3
quad_y = i % 3 * 3
for j in range(0, 9):
col_i = self.board[j][i].num-1
row_i = self.board[i][j].num-1
blk_i = self.board[quad_x + j//3][quad_y + j % 3].num-1
if 0 <= col_i < 9:
col[col_i] = False
else:
print("Empty space at", j, i)
return False
if 0 <= row_i < 9:
row[row_i] = False
else:
print("Empty space at", i, j)
return False
if 0 <= blk_i < 9:
blk[blk_i] = False
else:
print("Empty space at", quad_x + j//3, quad_y + j % 3)
return False
for k in range(0, 9):
if col[k] or row[k] or blk[k]:
if col[k]:
out = "column"
elif row[k]:
out = "row"
else:
out = "block"
print("Fault in", out, i)
return False
return True
# def list_errors(self, arr):
# errors = list()
# for i in range(9):
# col = dict()
# row = dict()
# blk = dict()
#
# quad_x = i // 3 * 3
# quad_y = i % 3 * 3
#
# for j in range(9):
# qx_i = quad_x + j//3
# qy_i = quad_y + j % 3
# col_i = self.arr[j][i]-1
# row_i = self.arr[i][j]-1
# blk_i = self.arr[qx_i][qy_i]-1
#
# if col_i in col:
@staticmethod
def intersect_set(x, y):
"""
:param x:
:type x: int
:param y:
:type y: int
:return:
:rtype: set of (int,int)
"""
r_set = set()
quad_x = x//3*3
quad_y = y//3*3
for i in range(9):
r_set.append(x, i)
r_set.append(i, y)
r_set.append(quad_x + i % 3, quad_y + i//3)
r_set.discard(x, y)
return r_set
class TestSudokuMethods(unittest.TestCase):
def test_board_creation(self):
b = Board()
b.initialize_breezy()
self.assertFalse(b.is_solved())
arr = b.answer
b = Board()
b.set_board(arr)
self.assertTrue(b.is_solved())
b = Board()
b.initialize_easy()
self.assertFalse(b.is_solved())
arr = b.answer
b = Board()
b.set_board(arr)
self.assertTrue(b.is_solved())
ALL_OPEN = list([1, 2, 3, 4, 5, 6, 7, 8, 9])
MISSING_ONE = list([2, 3, 4, 5, 6, 7, 8, 9])
def test_block_position(self):
b = Board()
self.assertEqual(b.getPos(0, 0).open_list(), self.ALL_OPEN)
self.assertEqual(b.getPos(0, 0).open, [True, True, True, True, True, True, True, True, True])
b.set_block(0, 0, 1)
for i in range(9):
for j in range(9):
if i is 0 and j is 0:
assert_arr = None
assert_list = list()
elif i is 0 or j is 0 or (0 <= i < 3 and 0 <= j < 3):
assert_arr = [False, True, True, True, True, True, True, True, True]
assert_list = self.MISSING_ONE
else:
assert_arr = [True, True, True, True, True, True, True, True, True]
assert_list = self.ALL_OPEN
self.assertEqual(b.getPos(i, j).open, assert_arr, str(i)+","+str(j))
self.assertEqual(b.getPos(i, j).open_list(), assert_list, str(i)+","+str(j))
b.set_block(8, 8, 9)
self.assertEqual(b.getPos(0, 8).open_list(), list([2,3,4,5,6,7,8]))
self.assertEqual(b.getPos(1, 8).open_list(), list([1,2,3,4,5,6,7,8]))
def test_set_board(self):
b = Board()
b.initialize_breezy()
b.display()
self.assertEqual(b.getPos(0, 0).open_list(), list())
self.assertEqual(b.getPos(0, 0).open_count, 0)
self.assertEqual(b.getPos(1, 6).open_list(), list([5, 9]))
self.assertEqual(b.getPos(1, 6).open_count, 2)
self.assertEqual(b.getPos(1, 7).open_list(), list([9]))
self.assertEqual(b.getPos(1, 7).open_count, 1)
self.assertEqual(b.getPos(7, 6).open_list(), list([9]))
self.assertEqual(b.getPos(7, 6).open_count, 1)
b.set_block(1, 7, 9)
self.assertFalse(b.is_solved())
self.assertEqual(b.getPos(1, 6).open_list(), list([5]))
self.assertEqual(b.getPos(1, 7).open_list(), list([]))
self.assertEqual(b.getPos(7, 6).open_list(), list([9]))
b.set_block(1, 6, 5)
self.assertFalse(b.is_solved())
self.assertEqual(b.getPos(1, 6).open_list(), list([]))
self.assertEqual(b.getPos(1, 7).open_list(), list([]))
self.assertEqual(b.getPos(7, 6).open_list(), list([9]))
b.set_block(7, 6, 9)
self.assertTrue(b.is_solved())
self.assertEqual(b.getPos(1, 6).open_list(), list([]))
self.assertEqual(b.getPos(1, 7).open_list(), list([]))
self.assertEqual(b.getPos(7, 6).open_list(), list([]))
if __name__ == '__main__':
unittest.TestCase()
game = Board()
game.initialize_easy()
print(game.is_solved())
game.display()