-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.py
250 lines (190 loc) · 6.83 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
class Sudoku:
__sudoku = []
def __init__(self, source):
if type(source) is list:
self.__sudoku = source.copy()
if self.is_valid() is not True:
exit("Sudoku is not valid")
else:
self.__sudoku = []
file = open(source, "r")
if file:
for a_line in file.readlines():
tmp = []
for a_char in a_line:
if a_char != "\n":
tmp.append(int(a_char))
self.__sudoku.append(tmp)
else:
exit("File doesn't exists")
def print_sudoku(self):
print('\n')
line_count = 0
for a_line in self.__sudoku:
tmp = ""
case_count = 0
for a_case in a_line:
tmp += ' ' + str(a_case)
if case_count == 2 or case_count == 5:
tmp += ' '
case_count += 1
print(tmp)
if line_count == 2 or line_count == 5:
print("\n")
line_count += 1
print('\n')
def get_sudoku(self):
return self.__sudoku
def is_valid(self):
"""
Fill the constraints list with all the grid constraints
"""
def __lines_are_correct(su):
is_ok = True
for a_line in su:
tmp = []
for a_char in a_line:
if a_char is not 0:
if is_ok is True and a_char not in tmp:
tmp.append(a_char)
else:
is_ok = False
return is_ok
def __columns_are_correct(su):
is_ok = True
for x in range(len(su[0])):
tmp = []
for y in range(len(su)):
a_char = su[y][x]
if a_char is not 0:
if is_ok is True and a_char not in tmp:
tmp.append(a_char)
else:
is_ok = False
return is_ok
def __cases_are_correct(su):
def check_a_case(start_x, start_y):
case_is_ok = True
tmp = []
for x in range(start_x, start_x + 3):
for y in range(start_y, start_y + 3):
a_char = su[y][x]
if a_char is not 0:
if case_is_ok is True and a_char not in tmp:
tmp.append(a_char)
else:
return False
return case_is_ok
all_cases_are_ok = True
if not check_a_case(0, 0) or not check_a_case(0, 3) or not check_a_case(0, 6) or \
not check_a_case(3, 0) or not check_a_case(3, 3) or not check_a_case(3, 6) or \
not check_a_case(6, 0) or not check_a_case(6, 3) or not check_a_case(6, 6):
all_cases_are_ok = False
return all_cases_are_ok
if __lines_are_correct(self.__sudoku) and\
__columns_are_correct(self.__sudoku) and\
__cases_are_correct(self.__sudoku):
return True
else:
return False
def get_forbidden_values_for(self, y, x):
forbidden_values = []
if self.__sudoku[y][x] > 0:
return [1, 2, 3, 4, 5, 6, 7, 8, 9]
# check column
for iter_y in range(len(self.__sudoku[y])):
a_char = self.__sudoku[iter_y][x]
if a_char is not 0 and a_char not in forbidden_values:
forbidden_values.append(a_char)
# check line
for iter_x in range(len(self.__sudoku[x])):
a_char = self.__sudoku[y][iter_x]
if a_char is not 0 and a_char not in forbidden_values:
forbidden_values.append(a_char)
# check case
start_x = 0
start_y = 0
# Compute in which square the value is
if x < 3:
start_x = 0
elif x < 6:
start_x = 3
elif x < 9:
start_x = 6
if y < 3:
start_y = 0
elif y < 6:
start_y = 3
elif y < 9:
start_y = 6
for x in range(start_x, start_x + 3):
for y in range(start_y, start_y + 3):
a_char = self.__sudoku[y][x]
if a_char is not 0 and a_char not in forbidden_values:
forbidden_values.append(a_char)
return forbidden_values
def get_mrv_cell(self):
# We take the cell with the biggest amount of forbidden values
selected_cell = {'y': -1, 'x': -1, 'score': -1}
for y in range(len(self.__sudoku)):
for x in range(len(self.__sudoku[0])):
if self.__sudoku[y][x] == 0:
how_many_constraints = len(self.get_forbidden_values_for(y, x))
if how_many_constraints > selected_cell['score']:
selected_cell = {'y': y, 'x': x, 'score': how_many_constraints}
return selected_cell
def get_possibilities_for(self, y, x):
forbidden = self.get_forbidden_values_for(y, x)
base = [1, 2, 3, 4, 5, 6, 7, 8, 9]
tmp = []
for val in base:
if val not in forbidden:
tmp.append(val)
return tmp
def set(self, y, x, val):
if self.__sudoku[y][x] > 0:
"""
print("--------")
print("NOPE ! ")
self.print_sudoku()
print("y : " + str(y))
print("x : " + str(x))
print("val : " + str(val))
print("remaining : ")
print(self.get_remaining_values())
print("--------")
"""
self.__sudoku[y][x] = val
return self.__sudoku
def hash(self):
res = ""
for line in self.__sudoku:
for char in line:
res += str(char)
return res
def is_resolved(self):
if self.is_valid():
for line in self.__sudoku:
for val in line:
if val is 0:
return False
return True
else:
return False
def resolve(sudoku):
if sudoku.is_resolved():
return True
else:
mrv_cell = sudoku.get_mrv_cell()
x = mrv_cell['x']
y = mrv_cell['y']
possibilities = sudoku.get_possibilities_for(y, x)
if len(possibilities):
for possibility in possibilities:
sudoku.set(y, x, possibility)
res = resolve(sudoku)
if res is True:
return res
else:
print("FAIL")
return False