summaryrefslogtreecommitdiff
path: root/src/solverwindow.cc
blob: 7f88b5f9bfd6106a9c5f4351910b91e2e5f96855 (plain)
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

#include "solverwindow.h"
#include "sudokuwidget.h"
#include "sudoku.h"

#include <QtGui>
#include <QDir>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>

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(save()));
	
	// 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 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);
	
	// 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.set_value(row, column, i);
		}
	}
	solverwindow_sudokuwidget->set_values(sudoku);
	
	file.close();
	
	QApplication::restoreOverrideCursor();
}

void SolverWindow::save()
{
	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.value(row, column);
			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_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";
}