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
|
#include "solverwindow.h"
#include "sudokuwidget.h"
#include "sudoku.h"
#include <QtGui>
#include <QDir>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <cstdlib>
/*
* FIXME
* On windows, this results in a rather awkward directory.
* The homepath should probably be a setting.
* */
const QString HOMEDIR(QDir::homePath() + "/.sudoku");
SolverWindow::SolverWindow()
{
QHBoxLayout *windowlayout = new QHBoxLayout();
// sidebar
QVBoxLayout *sidebarlayout = new QVBoxLayout();
// add load button
QPushButton *loadbutton = new QPushButton(tr("Load"));
sidebarlayout->addWidget(loadbutton);
connect(loadbutton, SIGNAL(clicked()), this, SLOT(load()));
// add save button
QPushButton *savebutton = new QPushButton(tr("Save"));
sidebarlayout->addWidget(savebutton);
connect(savebutton, SIGNAL(clicked()), this, SLOT(saveas()));
// add revert button
QPushButton *revertbutton = new QPushButton(tr("Revert"));
sidebarlayout->addWidget(revertbutton);
connect(revertbutton, SIGNAL(clicked()), this, SLOT(revert()));
// add step constraints button
QPushButton *stepconstraintsbutton = new QPushButton(tr("Constraints"));
sidebarlayout->addWidget(stepconstraintsbutton);
connect(stepconstraintsbutton, SIGNAL(clicked()), this, SLOT(step_constraints()));
// add step coverage button
QPushButton *stepcoveragebutton = new QPushButton(tr("Coverage"));
sidebarlayout->addWidget(stepcoveragebutton);
connect(stepcoveragebutton, SIGNAL(clicked()), this, SLOT(step_coverage()));
// add validate button
QPushButton *validatebutton = new QPushButton(tr("Validate"));
sidebarlayout->addWidget(validatebutton);
connect(validatebutton, SIGNAL(clicked()), this, SLOT(validate()));
// add stretch
sidebarlayout->addStretch(1);
// add clear button
QPushButton *clearbutton = new QPushButton(tr("Clear"));
sidebarlayout->addWidget(clearbutton);
connect(clearbutton, SIGNAL(clicked()), this, SLOT(clear()));
// add sidebar layout
windowlayout->addLayout(sidebarlayout);
// sudoku widget
solverwindow_sudokuwidget = new SudokuWidget();
windowlayout->addWidget(solverwindow_sudokuwidget, 1);
// set window layout
setLayout(windowlayout);
// create home directory
QDir directory;
if (!directory.exists(HOMEDIR)) {
directory.mkdir(HOMEDIR);
}
}
void SolverWindow::load()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open..."), HOMEDIR, "Sudoku (*.sudoku)");
if (filename.isEmpty()) {
return;
}
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Open file"),
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);
}
}
solverwindow_sudokuwidget->set_values(sudoku);
solverwindow_revertstate.assign(sudoku);
file.close();
QApplication::restoreOverrideCursor();
}
void SolverWindow::revert()
{
solverwindow_sudokuwidget->set_values(solverwindow_revertstate);
}
void SolverWindow::saveas()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Save as..."), HOMEDIR, "Sudoku (*.sudoku)");
if (filename.isEmpty()) {
return;
}
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Save file"),
tr("Cannot write 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();
QApplication::restoreOverrideCursor();
return;
}
void SolverWindow::clear()
{
Sudoku sudoku;
solverwindow_sudokuwidget->set_values(sudoku);
}
void SolverWindow::step()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
Sudoku solution(sudoku);
int solved = solution.solve_rules();
if (solved == 0) {
qDebug() << "no solveable cells left!";
return;
}
// compare sudoku and solution values
int index_start = (int) random() % 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::solve()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_rules();
sudoku.validate();
solverwindow_sudokuwidget->set_values(sudoku);
qDebug() << solved << " cells solved";
}
void SolverWindow::search()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int iterations = sudoku.solve_search();
solverwindow_sudokuwidget->set_values(sudoku);
if (iterations > 0) {
qDebug() << "solved in " << iterations << " iterations";
}
}
void SolverWindow::step_constraints()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_constraints();
solverwindow_sudokuwidget->set_values(sudoku);
qDebug() << solved << " cells solved";
}
void SolverWindow::step_coverage()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
int solved = sudoku.solve_coverage();
solverwindow_sudokuwidget->set_values(sudoku);
qDebug() << solved << " cells solved";
}
void SolverWindow::validate()
{
Sudoku sudoku;
solverwindow_sudokuwidget->get_values(sudoku);
if (sudoku.validate()) {
qDebug() << "sudoku is valid";
} else {
qDebug() << "sudoku is not valid";
}
solverwindow_sudokuwidget->set_values(sudoku);
qDebug() << "sudoku validated";
}
|