-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.py
223 lines (167 loc) · 6.3 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
class Point(object):
def __init__(self, point):
self._point = point
self._value = 0
self._possibilities = []
def setPossibileValues(self, possibilities):
self._possibilities = possibilities
def getPossibileValues(self):
return self._possibilities
def getValue(self):
return self._value
def setValue(self, value):
self._value = value
self._possibilities = []
def getCoords(self):
return self._point
def getPossibilityCount(self):
return len(self._possibilities)
def setOnlyPossibility(self):
self.setValue(self._possibilities[0])
def existsPossibility(self, possibility):
return possibility in self._possibilities
def __eq__(self, value):
if self._value == value:
return True
return False
def __repr__(self):
return "Point: " + str(self._point) + " -> " + str(self._possibilities)
class Board(object):
def __init__(self, filename):
self._board = {}
for point in sodoku_points():
self._board[point] = Point(point)
self._load(filename)
def getPoint(self, point):
return self._board[point]
def _load(self, filename):
points = sodoku_points()
board_file = open(filename, 'r')
for data_point in board_file.read():
if data_point is not '\n':
position = next(points)
if data_point is not 'X':
self.getPoint(position).setValue(int(data_point))
def show(self):
for y in range(9):
if y % 3 == 0:
print("-" * 25)
for x in range(9):
if x % 3 == 0:
self._boardPrint('|')
if self.getPoint((x, y)) == 0:
self._boardPrint(" ")
else:
self._boardPrint(self.getPoint((x, y)).getValue())
print("|")
print("-" * 25)
@staticmethod
def _boardPrint(le_string):
print(le_string, end=' ')
@staticmethod
def getSquare(point):
square_points = []
x_i, y_i = point
x = (x_i // 3) * 3
y = (y_i // 3) * 3
for i in range(3):
for j in range(3):
square_points.append((x + i, y + j))
return square_points
@staticmethod
def getRow(point):
row_points = []
x_i, y_i = point
for i in range(9):
row_points.append((i, y_i))
return row_points
@staticmethod
def getColumn(point):
column_points = []
x_i, y_i = point
for i in range(9):
column_points.append((x_i, i))
return column_points
def __iter__(self):
return iter(self._board.items())
def __getitem__(self, point):
return self._board[point]
class Solver(object):
def __init__(self, board):
self._board = board
self._possibilities = []
def findPossibilities(self):
for point in sodoku_points():
if self._board.getPoint(point) == 0:
self._possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for position in self._board.getSquare(point):
self._checkPossibility(position)
for position in self._board.getRow(point):
self._checkPossibility(position)
for position in self._board.getColumn(point):
self._checkPossibility(position)
self._board.getPoint(point).setPossibileValues(self._possibilities)
def _checkPossibility(self, position):
check_value = self._board.getPoint(position).getValue()
if check_value in self._possibilities:
self._possibilities.remove(check_value)
def _isSquareUnique(self, coords, possibility):
for check_position in self._board.getSquare(coords):
if possibility in self._board.getPoint(check_position).getPossibileValues() and check_position != coords:
return False
return True
def _isRowUnique(self, coords, possibility):
for check_position in self._board.getRow(coords):
if possibility in self._board.getPoint(check_position).getPossibileValues() and check_position != coords:
return False
return True
def _isColumnUnique(self, coords, possibility):
for check_position in self._board.getColumn(coords):
if possibility in self._board.getPoint(check_position).getPossibileValues() and check_position != coords:
return False
return True
def _isLineUnique(self, coords, possibility):
if self._isRowUnique(coords, possibility) or self._isColumnUnique(coords, possibility):
return True
return False
def _isUnique(self, coords, possibility):
if self._isLineUnique(coords, possibility) or self._isSquareUnique(coords, possibility):
return True
return False
def applyPossibilities(self):
for coords, point in self._board:
if point.getPossibilityCount() == 1:
point.setOnlyPossibility()
# Find the new possibilities for uniquness to run.
self.findPossibilities()
for point_possibility in point.getPossibileValues():
if self._isUnique(coords, point_possibility):
point.setValue(point_possibility)
def isSolved(self):
for point in sodoku_points():
if self._board.getPoint(point) == 0:
return False
return True
def sodoku_points():
""" A method used to generate the points required for loading and checking
the sodoku grid.
sodoku_points -> Generator((int, int))
"""
for y in range(9):
for x in range(9):
yield (x, y)
le_board = Board("board3.txt")
le_board.show()
le_solver = Solver(le_board)
while(not le_solver.isSolved()):
le_solver.findPossibilities()
inp = ""
inp = input("")
if inp == "p":
for point in sodoku_points():
if le_board.getPoint(point).getPossibilityCount() != 0:
print(str(point) + " -> " + str(le_board.getPoint(point).getPossibileValues()))
le_solver.applyPossibilities()
if inp == "s" or inp == "p":
le_board.show()
le_board.show()