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
|
#include "settings.h"
#include "solverwindow.h"
#include "sudokuwidget.h"
#include "sudoku.h"
#include "config.h"
#include <QtWidgets>
#include <QDir>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
SolverWindow::SolverWindow()
{
QHBoxLayout *windowlayout = new QHBoxLayout();
// sudoku widget
solverwindow_sudokuwidget = new SudokuWidget();
windowlayout->addWidget(solverwindow_sudokuwidget, 1);
// set window layout
setLayout(windowlayout);
}
const bool SolverWindow::showHintUnique() const
{
return solverwindow_sudokuwidget->showHintUnique();
}
void SolverWindow::openFromFile(const QString & filename)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Error opening file"),
tr("Could not open file \"%1\":\n%2.")
.arg(filename)
.arg(file.errorString()));
QFileInfo fileinfo(filename);
emit statusChanged(tr("Could not open file \"%1\"").arg(fileinfo.fileName()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
QTextStream textstream(&file);
Sudoku sudoku;
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9 ; column++) {
int i;
textstream >> i;
sudoku.cell(row, column).set_value(i);
}
}
file.close();
solverwindow_sudokuwidget->set_values(sudoku);
solverwindow_filename = filename;
QFileInfo fileinfo(filename);
emit statusChanged(tr("Opened file \"%1\"").arg(fileinfo.fileName()));
QApplication::restoreOverrideCursor();
}
void SolverWindow::saveToFile(const QString & filename)
{
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Error saving file"),
tr("Could not save file \"%1\":\n%2.")
.arg(filename)
.arg(file.errorString()));
QFileInfo fileinfo(filename);
emit statusChanged(tr("Could not save file \"%1\"").arg(fileinfo.fileName()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
QTextStream textstream(&file);
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
for (int row = 0; row < 9; row++) {
for (int column = 0; column < 9 ; column++) {
textstream << sudoku.cell(row, column).value();
if (column < 8) {
textstream << ' ';
if ((column % 3) == 2) {
textstream << ' ';
}
} else {
textstream << '\n';
}
}
if ((row < 8) && ((row % 3) == 2)) {
textstream << '\n';
}
}
file.close();
solverwindow_filename = filename;
QFileInfo fileinfo(filename);
emit statusChanged(tr("Saved file \"%1\"").arg(fileinfo.fileName()));
QApplication::restoreOverrideCursor();
}
void SolverWindow::doOpen()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open"), globalSettings().homePath(), "Sudoku (*.sudoku)");
if (!filename.isEmpty()) {
openFromFile(filename);
}
}
void SolverWindow::doRevert()
{
if (!solverwindow_filename.isEmpty()) {
QFileInfo fileinfo(solverwindow_filename);
if (QMessageBox::warning(this, tr("Revert?"), tr("Revert the game to the previously saved file \"%1\"?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
openFromFile(solverwindow_filename);
}
}
}
bool SolverWindow::confirmOverwrite(const QString & filename)
{
QFile file (filename);
if (file.exists()) {
QFileInfo fileinfo(file);
return (QMessageBox::warning(this, tr("Overwrite file?"), tr("The file \"%1\" already exists.\nDo you wish to overwrite it?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes);
} else {
return true;
}
}
void SolverWindow::doSave()
{
if (solverwindow_filename.isEmpty()) {
// no file name is set, use the Save As dialog
doSaveAs();
} else if (confirmOverwrite(solverwindow_filename)) {
saveToFile(solverwindow_filename);
}
}
void SolverWindow::doSaveAs()
{
QString filename;
do {
filename = QFileDialog::getSaveFileName(this, tr("Save as..."), globalSettings().homePath(), "Sudoku (*.sudoku)", 0, QFileDialog::DontConfirmOverwrite);
if (filename.isEmpty()) {
return;
}
} while (!confirmOverwrite(filename));
saveToFile(filename);
}
void SolverWindow::doNew()
{
if (QMessageBox::question(this, tr("New"), tr("Start a new game?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
Sudoku sudoku;
solverwindow_sudokuwidget->set_values(sudoku);
solverwindow_filename.clear();
emit statusChanged(PACKAGE_STRING);
}
}
void SolverWindow::doStep()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
if (!sudoku.validate()) {
QMessageBox::warning(this, tr("Step"), tr("This sudoku is not valid!"));
return;
}
Sudoku solution(sudoku);
int solved = solution.solve_coverage() + solution.solve_constraints();
if (solved == 0) {
QMessageBox::warning(this, tr("Step"), tr("No more cells to solve!"));
return;
}
// compare sudoku and solution values
int index_start = qrand() % 81;
int index_current = index_start;
do {
int column = index_current % 9;
int row = (index_current - column) / 9;
if ((sudoku.cell(row,column).value() == 0) && (solution.cell(row,column).value() != 0)) {
sudoku.cell(row,column).set_value(solution.cell(row, column).value());
solverwindow_sudokuwidget->set_values(sudoku);
return;
}
index_current = (index_current + 1) % 81;
} while (index_current != index_start);
}
void SolverWindow::doGuess()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
if (!sudoku.validate()) {
QMessageBox::warning(this, tr("Guess"), tr("This sudoku is not valid!"));
return;
}
Sudoku solution(sudoku);
int solved = solution.solve_search();
if (solved == 0) {
QMessageBox::warning(this, tr("Guess"), tr("No more cells to solve!"));
return;
}
// compare sudoku and solution values
int index_start = qrand() % 81;
int index_current = index_start;
do {
int column = index_current % 9;
int row = (index_current - column) / 9;
if ((sudoku.cell(row,column).value() == 0) && (solution.cell(row,column).value() != 0)) {
sudoku.cell(row,column).set_value(solution.cell(row, column).value());
solverwindow_sudokuwidget->set_values(sudoku);
return;
}
index_current = (index_current + 1) % 81;
} while (index_current != index_start);
}
void SolverWindow::doSolve()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
bool is_valid = sudoku.validate();
sudoku.solve_rules();
bool is_solved = sudoku.solved();
solverwindow_sudokuwidget->set_values(sudoku);
if (!is_valid) {
QMessageBox::warning(this, tr("Find solution"), tr("This sudoku is not valid!"));
} else if (is_solved) {
QMessageBox::information(this, tr("Find solution"), tr("This sudoku has been solved."));
} else {
QMessageBox::warning(this, tr("Find solution"), tr("This sudoku can not be solved by applying the rules!"));
}
}
void SolverWindow::doSearch()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
bool is_valid = sudoku.validate();
sudoku.solve_search();
bool is_solved = sudoku.solved();
solverwindow_sudokuwidget->set_values(sudoku);
if (!is_valid) {
QMessageBox::warning(this, tr("Find solution"), tr("This sudoku is not valid!"));
} else if (is_solved) {
QMessageBox::information(this, tr("Find solution"), tr("This sudoku has been solved."));
} else {
QMessageBox::warning(this, tr("Find solution"), tr("This sudoku can not be solved!"));
}
}
void SolverWindow::doValidate()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
bool is_valid = sudoku.validate();
solverwindow_sudokuwidget->set_values(sudoku);
if (!is_valid) {
QMessageBox::warning(this, tr("Validate"), tr("This sudoku is not valid!"));
return;
}
bool is_solved = sudoku.solved();
if (is_solved) {
QMessageBox::information(this, tr("Validate"), tr("This sudoku has been solved."));
return;
}
sudoku.solve_rules();
is_solved = sudoku.solved();
if (is_solved) {
QMessageBox::information(this, tr("Validate"), tr("This sudoku can be solved by applying the rules."));
return;
}
sudoku.solve_search();
is_solved = sudoku.solved();
if (is_solved) {
QMessageBox::information(this, tr("Validate"), tr("This sudoku can be solved but needs guessing."));
return;
}
QMessageBox::warning(this, tr("Validate"), tr("This sudoku is valid but can not be solved!"));
}
void SolverWindow::doShowHintUnique()
{
solverwindow_sudokuwidget->setShowHintUnique(!solverwindow_sudokuwidget->showHintUnique());
}
void SolverWindow::step_constraints()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_constraints();
solverwindow_sudokuwidget->set_values(sudoku);
}
void SolverWindow::step_coverage()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_coverage();
solverwindow_sudokuwidget->set_values(sudoku);
}
|