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
|
#include "solverwindow.h"
#include "sudokuwidget.h"
#include "sudoku.h"
#include <QtGui>
#include <QDir>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
/*
* FIXME
* On windows, this results in a rather awkward directory.
* The homepath should probably be a setting.
* This should move to mainwindow
* */
const QString HOMEDIR(QDir::homePath() + "/.sudoku");
SolverWindow::SolverWindow()
{
QHBoxLayout *windowlayout = new QHBoxLayout();
// sudoku widget
solverwindow_sudokuwidget = new SudokuWidget();
windowlayout->addWidget(solverwindow_sudokuwidget, 1);
// set window layout
setLayout(windowlayout);
// create home directory
// FIXME this should move to mainwindow
QDir directory;
if (!directory.exists(HOMEDIR)) {
directory.mkdir(HOMEDIR);
}
}
void SolverWindow::openFromFile(const QString & filename)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Open"),
tr("Could not open file \"%1\":\n%2.")
.arg(filename)
.arg(file.errorString()));
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;
QApplication::restoreOverrideCursor();
}
void SolverWindow::saveToFile(const QString & filename)
{
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Save"),
tr("Could not write to file \"%1\":\n%2.")
.arg(filename)
.arg(file.errorString()));
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;
QApplication::restoreOverrideCursor();
}
void SolverWindow::doOpen()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open"), HOMEDIR, "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 state of the game to the previously saved file \"%1\"?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
openFromFile(solverwindow_filename);
}
}
}
void SolverWindow::doSave()
{
if (solverwindow_filename.isEmpty()) {
// no file name is set, use the Save As dialog
doSaveAs();
} else {
// confirm overwriting an existing file
QFile file (solverwindow_filename);
if (file.exists()) {
QFileInfo fileinfo(file);
if (QMessageBox::warning(this, tr("Overwrite file?"), tr("The file \"%1\" already exists. Do you wish to overwrite it?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
return;
}
}
saveToFile(solverwindow_filename);
}
}
void SolverWindow::doSaveAs()
{
// QFileDialog::getSaveFileName() warns about existing files
QString filename = QFileDialog::getSaveFileName(this, tr("Save as..."), HOMEDIR, "Sudoku (*.sudoku)");
if (!filename.isEmpty()) {
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();
}
}
void SolverWindow::doStep()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
Sudoku solution(sudoku);
int solved = solution.solve_rules();
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);
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()
{
// TODO detect invalid and solved states
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_rules();
sudoku.validate();
solverwindow_sudokuwidget->set_values(sudoku);
if (solved == 0) {
QMessageBox::warning(this, tr("Solve rules"), tr("No more cells to solve!"));
return;
}
}
void SolverWindow::doSearch()
{
// TODO detect invalid and solved states
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int iterations = sudoku.solve_search();
sudoku.validate();
solverwindow_sudokuwidget->set_values(sudoku);
if (iterations == 0) {
QMessageBox::warning(this, tr("Find solution"), tr("No more cells to solve!"));
return;
}
}
void SolverWindow::doValidate()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
bool is_valid = sudoku.validate();
solverwindow_sudokuwidget->set_values(sudoku);
if (is_valid) {
QMessageBox::information(this, tr("Validate"), tr("Sudoku is valid."));
} else {
QMessageBox::warning(this, tr("Validate"), tr("Sudoku is not valid!"));
}
}
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);
}
|