-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuBoard.py
141 lines (122 loc) · 5.51 KB
/
SudokuBoard.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
from SudokuValue import SVal
class Board:
"""A board of Sudoku values."""
def __init__(self, empty=False):
"""Create a board with all new SVals (unless specified to be instantiated as empty)."""
if empty:
self.board = []
else:
self.board = [
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()],
[SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal(), SVal()]
]
def deep_copy(self):
"""Return a copy of this board."""
new_board = Board(empty=True)
for line in self.board:
new_line = []
new_board.board.append(new_line)
for val in line:
new_line.append(val.deep_copy())
return new_board
def num_possible(self, row, col):
"""Return the number of possible values for the square at (row, col)."""
return self.board[row][col].num_possible()
def is_set(self, row, col):
"""Return whether the square at (row, col) is set."""
return self.board[row][col].is_set()
def set_value(self, row, col, value):
"""Set the value for the square at (row, col)."""
self.board[row][col].set_value(value)
self._remove_possible_row(row, value)
self._remove_possible_col(col, value)
self._remove_possible_inner_square(row, col, value)
def get_minimal_possibilities_square(self):
"""Return (row, col) of a square with minimal remaining possibilities."""
min_possible = 10
(min_r, min_c) = (-1, -1)
for r, line in enumerate(self.board):
for c, val in enumerate(line):
if not val.is_set() and val.num_possible() < min_possible:
min_possible = val.num_possible()
(min_r, min_c) = (r, c)
return min_r, min_c
def get_possible_boards(self, row, col):
"""Return a list of boards created by filling the specified square with each of its possibilities in turn, and
then removing impossible values from the rest of the board.
"""
possible_boards = []
possible_vals = self.board[row][col].get_possible()
for val in possible_vals:
new_board = self.deep_copy()
new_board.set_value(row, col, val)
possible_boards.append(new_board)
return possible_boards
def is_complete(self):
"""Check if all values are set, then the board is complete."""
for line in self.board:
for val in line:
if not val.is_set():
return False
return True
def contains_empty(self):
"""Check if the board contains any vals with an empty list of possibilities, thus indicating a "dead end."
"""
for line in self.board:
for val in line:
if not val.is_set() and val.num_possible() == 0:
return True
return False
def _remove_possible_row(self, idx, value):
"""Remove the specified value from possible values for all SVals in a row."""
for val in self.board[idx]:
val.remove_possible(value)
def _remove_possible_col(self, idx, value):
"""Remove the specified value from possible values for all SVals in a column. Needs to iterate over all rows."""
for row in self.board:
row[idx].remove_possible(value)
def _remove_possible_inner_square(self, row, col, value):
"""Remove the specified value from the list of possible values for all SVals in the 3x3 Sudoku sub-square.
Needs to determine the sub-square upper left corner, and then iterate over all contained values.
* Upper left corner Row = ((row / 3) * 3)
* Upper left corner Col = ((col / 3) * 3)
"""
upper_left_r = row // 3 * 3
upper_left_c = col // 3 * 3
for r in range(upper_left_r, upper_left_r + 3):
for c in range(upper_left_c, upper_left_c + 3):
self.board[r][c].remove_possible(value)
def __repr__(self):
"""String representation of the board.
Desired output:
+---------+---
| 1 2 3 | 4 ...
| 4 5 6 | 7 ...
| 7 8 9 | 1 ...
+---------+---
| 2 3 4 | 5 ...
...
If a square is not set, just include a space.
"""
r_spacer = " " + ("+" + ("---" * 3)) * 3 + "+"
c_spacer = "|"
string = ""
for l_idx, line in enumerate(self.board):
if l_idx % 3 == 0:
string += r_spacer + "\n"
for v_idx, val in enumerate(line):
if v_idx % 3 == 0:
if v_idx == 0:
string += " "
string += c_spacer
string += " " + str(val) + " "
string += c_spacer + "\n"
string += r_spacer
return string