-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
451 lines (381 loc) · 15.6 KB
/
main.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
import sys
from collections import namedtuple
from itertools import product
import numpy as np
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QVariant, QModelIndex, QRegExp, pyqtSlot, QRunnable, QThreadPool, pyqtSignal, QObject
from PyQt5.QtGui import QIntValidator, QRegExpValidator, QValidator
from PyQt5.QtWidgets import QApplication, QDataWidgetMapper, QItemDelegate, QTableView
import pandas as pd
from QT_Designes import soduku_window
import json
import time
class NotEmptyValidator(QValidator):
def validate(self, text, pos):
if text in ["1", "2", "3", "4", "5", "6", "7", "8", "9", ""]:
state = QIntValidator.Acceptable
else:
state = QIntValidator.Invalid
return state, text, pos
class manager(QtCore.QAbstractTableModel):
def __init__(self, data=None):
super(manager, self).__init__()
self._data = data # or pd.DataFrame("", index=range(9), columns=["A", "B", "C", "D", "E", "F", "G", "H", "I"])
def data(self, index, role):
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
return str(self._data.iloc[row, col])
if role == Qt.EditRole:
return str(self._data.iloc[row, col])
else:
QVariant
def rowCount(self, parent):
return len(self._data.values)
def update(self):
pass
def columnCount(self, parent):
return self._data.columns.size
def setData(self, index, value, role):
if not index.isValid() or role != Qt.EditRole:
return False
row = index.row()
col = index.column()
if role == Qt.EditRole:
self._data.iloc[row, col] = str(value)
self.dataChanged.emit(index, index)
return True
return True
def clearData(self):
print(self._data)
self._data = pd.DataFrame("",["0","1","2","3","4","5","6","7","8"],["A","B","C","D","E","F","G","H","I"])
index1 = self.createIndex(0, 0)
index2 = self.createIndex(8, 8)
self.dataChanged.emit(index1, index2)
class Soduku(QtWidgets.QMainWindow, soduku_window.Ui_MainWindow):
def __init__(self, parent=None):
super(Soduku, self).__init__(parent)
self.setupUi(self)
self.pushButton_2.clicked.connect(self.solve)
self.initLine()
self.colums = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
self.progressBar.setMaximum(100)
self.progressBar.setMinimum(0)
self.progressBar.setValue(0)
# self.progressBar.hide()
self.pushButton.clicked.connect(self.clear)
data = {}
for column in self.colums:
data[column] = [v for k, v in sorted({x.objectName(): x.text() for x in
self.findChildren(QtWidgets.QLineEdit,
QtCore.QRegExp(f"{column}"))}.items())]
self.df = pd.DataFrame.from_dict(json.load(open("test.json")))
# self.df = pd.DataFrame.from_dict(data, dtype="int")
self.model = manager(self.df)
self.mapperA = QDataWidgetMapper()
self.mapperA.setModel(self.model)
self.mapperA.setCurrentIndex(0)
self.mapperA.addMapping(self.A1, 0)
self.mapperA.addMapping(self.B1, 1)
self.mapperA.addMapping(self.C1, 2)
self.mapperA.addMapping(self.D1, 3)
self.mapperA.addMapping(self.E1, 4)
self.mapperA.addMapping(self.F1, 5)
self.mapperA.addMapping(self.G1, 6)
self.mapperA.addMapping(self.H1, 7)
self.mapperA.addMapping(self.I1, 8)
self.mapperB = QDataWidgetMapper()
self.mapperB.setModel(self.model)
self.mapperB.setCurrentIndex(1)
self.mapperB.addMapping(self.A2, 0)
self.mapperB.addMapping(self.B2, 1)
self.mapperB.addMapping(self.C2, 2)
self.mapperB.addMapping(self.D2, 3)
self.mapperB.addMapping(self.E2, 4)
self.mapperB.addMapping(self.F2, 5)
self.mapperB.addMapping(self.G2, 6)
self.mapperB.addMapping(self.H2, 7)
self.mapperB.addMapping(self.I2, 8)
self.mapperC = QDataWidgetMapper()
self.mapperC.setModel(self.model)
self.mapperC.setCurrentIndex(2)
self.mapperC.addMapping(self.A3, 0)
self.mapperC.addMapping(self.B3, 1)
self.mapperC.addMapping(self.C3, 2)
self.mapperC.addMapping(self.D3, 3)
self.mapperC.addMapping(self.E3, 4)
self.mapperC.addMapping(self.F3, 5)
self.mapperC.addMapping(self.G3, 6)
self.mapperC.addMapping(self.H3, 7)
self.mapperC.addMapping(self.I3, 8)
self.mapperD = QDataWidgetMapper()
self.mapperD.setModel(self.model)
self.mapperD.setCurrentIndex(3)
self.mapperD.addMapping(self.A4, 0)
self.mapperD.addMapping(self.B4, 1)
self.mapperD.addMapping(self.C4, 2)
self.mapperD.addMapping(self.D4, 3)
self.mapperD.addMapping(self.E4, 4)
self.mapperD.addMapping(self.F4, 5)
self.mapperD.addMapping(self.G4, 6)
self.mapperD.addMapping(self.H4, 7)
self.mapperD.addMapping(self.I4, 8)
self.mapperE = QDataWidgetMapper()
self.mapperE.setModel(self.model)
self.mapperE.setCurrentIndex(4)
self.mapperE.addMapping(self.A5, 0)
self.mapperE.addMapping(self.B5, 1)
self.mapperE.addMapping(self.C5, 2)
self.mapperE.addMapping(self.D5, 3)
self.mapperE.addMapping(self.E5, 4)
self.mapperE.addMapping(self.F5, 5)
self.mapperE.addMapping(self.G5, 6)
self.mapperE.addMapping(self.H5, 7)
self.mapperE.addMapping(self.I5, 8)
self.mapperF = QDataWidgetMapper()
self.mapperF.setModel(self.model)
self.mapperF.setCurrentIndex(5)
self.mapperF.addMapping(self.A6, 0)
self.mapperF.addMapping(self.B6, 1)
self.mapperF.addMapping(self.C6, 2)
self.mapperF.addMapping(self.D6, 3)
self.mapperF.addMapping(self.E6, 4)
self.mapperF.addMapping(self.F6, 5)
self.mapperF.addMapping(self.G6, 6)
self.mapperF.addMapping(self.H6, 7)
self.mapperF.addMapping(self.I6, 8)
self.mapperG = QDataWidgetMapper()
self.mapperG.setModel(self.model)
self.mapperG.setCurrentIndex(6)
self.mapperG.addMapping(self.A7, 0)
self.mapperG.addMapping(self.B7, 1)
self.mapperG.addMapping(self.C7, 2)
self.mapperG.addMapping(self.D7, 3)
self.mapperG.addMapping(self.E7, 4)
self.mapperG.addMapping(self.F7, 5)
self.mapperG.addMapping(self.G7, 6)
self.mapperG.addMapping(self.H7, 7)
self.mapperG.addMapping(self.I7, 8)
self.mapperH = QDataWidgetMapper()
self.mapperH.setModel(self.model)
self.mapperH.setCurrentIndex(7)
self.mapperH.addMapping(self.A8, 0)
self.mapperH.addMapping(self.B8, 1)
self.mapperH.addMapping(self.C8, 2)
self.mapperH.addMapping(self.D8, 3)
self.mapperH.addMapping(self.E8, 4)
self.mapperH.addMapping(self.F8, 5)
self.mapperH.addMapping(self.G8, 6)
self.mapperH.addMapping(self.H8, 7)
self.mapperH.addMapping(self.I8, 8)
self.mapperI = QDataWidgetMapper()
self.mapperI.setModel(self.model)
self.mapperI.setCurrentIndex(8)
self.mapperI.addMapping(self.A9, 0)
self.mapperI.addMapping(self.B9, 1)
self.mapperI.addMapping(self.C9, 2)
self.mapperI.addMapping(self.D9, 3)
self.mapperI.addMapping(self.E9, 4)
self.mapperI.addMapping(self.F9, 5)
self.mapperI.addMapping(self.G9, 6)
self.mapperI.addMapping(self.H9, 7)
self.mapperI.addMapping(self.I9, 8)
self.threadpool = QThreadPool()
index1 = self.model.createIndex(0, 0)
index2 = self.model.createIndex(8, 8)
self.model.dataChanged.emit(index1, index2)
self.view = QtWidgets.QTableView()
self.view.setModel(self.model)
def clear(self):
self.model.clearData()
def initLine(self):
for child in self.findChildren(QtWidgets.QLineEdit):
validator = NotEmptyValidator(child)
child.setValidator(validator)
def solve(self):
self.start = time.time()
# Do a easy solve
solve = Solve(self.model._data)
solve.signals.result.connect(self.result)
solve.signals.finished.connect(self.finished)
self.threadpool.start(solve)
# If easy solved gave no solution, try to to guess one number and solve!
def solve_2(self, dataframe):
self.progressBar.show()
solve2 = Solve2(dataframe)
solve2.signals.result.connect(self.result2)
solve2.signals.finished.connect(self.finished)
solve2.signals.progress.connect(self.progress)
self.threadpool.start(solve2)
def progress(self, progress):
self.progressBar.setValue(progress)
def result(self, dataframe):
print(dataframe)
self.model._data = dataframe
index1 = self.model.createIndex(0, 0)
index2 = self.model.createIndex(8, 8)
self.model.dataChanged.emit(index1, index2)
if "" in dataframe.values:
pass
# If easy solved gave no solution, try to to guess one number and solve!
# self.solve_2(dataframe)
def result2(self, dataframe):
self.model._data = dataframe
index1 = self.model.createIndex(0, 0)
index2 = self.model.createIndex(8, 8)
self.model.dataChanged.emit(index1, index2)
if "" in dataframe.values:
self.progressBar.setTextVisible(True)
self.progressBar.setFormat("Failed to solve current soduku")
self.progressBar.setAlignment(Qt.AlignCenter)
def finished(self):
self.progressBar.setValue(100)
self.progressBar.setFormat(f"Solved in {(time.time() - self.start):.2f} s ")
self.progressBar.setAlignment(Qt.AlignCenter)
def thread_complete(self):
print("Done")
def product_dict(self, inp):
return (dict(zip(inp.keys(), values)) for values in product(*inp.values()))
class WorkerSignals(QObject):
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(pd.DataFrame)
progress = pyqtSignal(int)
class Solve(QRunnable):
def __init__(self, dataframe):
super(Solve, self).__init__()
self.solved = False
self.a = pd.Series(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
self.df = dataframe.copy()
self.signals = WorkerSignals()
def run(self) -> None:
solved = self.SodukuAlgo(self.df)
if solved:
self.signals.finished.emit()
self.signals.result.emit(self.df)
else:
self.signals.result.emit(self.df)
def getneigbors(self,dataframe,missing, index):
row = int(index[0])
col = index[1]
col_i = dataframe.columns.get_loc(col)
for number in missing:
if int(row) > 0:
Hor1 = dataframe.iloc[int(row) - 1, :]
else:
Hor1 = self.a
continue
if int(row) < 8:
Hor2 = dataframe.iloc[int(row) + 1, :]
else:
Hor2 = self.a
continue
if col_i > 0:
Ver1 = dataframe.iloc[:, col_i - 1]
else:
Ver1 = self.a
continue
if col_i < 8:
Ver2 = dataframe.iloc[:, col_i + 1]
else:
Ver2 = self.a
continue
if number in Hor1.values:
if number in Hor2.values:
if number in Ver1.values:
if number in Ver2.values:
return True
return False
def SodukuAlgo(self, dataframe, test=False):
self.run = True
while self.run:
self.run = False
for index in self.getempty(dataframe):
missing = self.findMissing(dataframe, index)
#If only missing one number in row or culumn insert number
if len(missing) == 1:
self.run = True
self.InsertNumber(dataframe, missing[0], index, test=test)
continue
elif len(missing) == 0:
"""If we get zero missing during an evaluation the solving has failed during Solve2 where we guess numbers"""
self.run = False
break
#if neigboring rows,columns contains number, insert number
if self.getneigbors(dataframe,missing,index):
self.run = True
self.InsertNumber(dataframe, missing[0], index, test=test)
return "" not in dataframe.values
def findMissing(self, dataframe, index):
row = int(index[0])
col = index[1]
horizontal = dataframe.iloc[int(row)]
vertical = dataframe[col]
if int(row) < 3:
if col in ["A", "B", "C"]:
box = dataframe.iloc[0:3, 0:3].unstack()
elif col in ["D", "E", "F"]:
box = dataframe.iloc[0:3, 3:6].unstack()
elif col in ["G", "H", "I"]:
box = dataframe.iloc[0:3, 6:9].unstack()
elif int(row) > 2 and int(row) < 6:
if col in ["A", "B", "C"]:
box = dataframe.iloc[3:6, 0:3].unstack()
elif col in ["D", "E", "F"]:
box = dataframe.iloc[3:6, 3:6].unstack()
elif col in ["G", "H", "I"]:
box = dataframe.iloc[3:6, 6:9].unstack()
elif int(row) > 5:
if col in ["A", "B", "C"]:
box = dataframe.iloc[6:9, 0:3].unstack()
elif col in ["D", "E", "F"]:
box = dataframe.iloc[6:9, 3:6].unstack()
elif col in ["G", "H", "I"]:
box = dataframe.iloc[6:9, 6:9].unstack()
return self.a.mask(self.a.isin(pd.concat([horizontal, vertical, box]).values)).dropna().to_list()
def getempty(self, dataframe):
result = []
for col in dataframe.columns:
for row in dataframe[col].where(dataframe[col].values == "").dropna().index:
result.append((f"{row}{col}"))
return result
def InsertNumber(self, dataframe, number, index, test=False):
row = int(index[0])
col = index[1]
dataframe[col][row] = number[0]
class Solve2(Solve):
def __init__(self, dataframe):
super(Solve, self).__init__()
self.solved = False
self.a = pd.Series(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
self.df = dataframe.copy()
self.df_original = dataframe
self.signals = WorkerSignals()
def run(self):
empty_indexes = self.getempty(self.df)
b = {}
for aa in empty_indexes:
missing = self.findMissing(self.df, aa)
missing.append("")
b[aa] = missing
for enum, index in enumerate(b):
if self.solved: break
self.signals.progress.emit(int((enum / len(b)) * 100.0))
row = index[0]
col = index[1]
missing = b[index]
for x in missing:
self.df[col][row] = x
self.solved = self.SodukuAlgo(self.df)
if self.solved:
self.signals.result.emit(self.df)
self.signals.finished.emit()
break
self.df = self.df_original.copy()
if not self.solved:
self.signals.result.emit(self.df_original)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Soduku()
form.show()
app.exec_()