-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.cpp
149 lines (123 loc) · 4.02 KB
/
solver.cpp
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
#include <QApplication>
#include <QLineEdit>
#include <QDebug>
#include <QString>
#include <QWidget>
#include "solver.h"
solver::solver() {}
void solver::readPuzzle(QWidget *parentWidget)
{
for(int row = 0;row<9;row++){
for(int col=0;col<9;col++){
//get input number for name of lineEdit
int inputNumber = row * 9 + col + 1;
//get input from lineEdit and convert to string
QString objectName=QString("puzzle_%1").arg(inputNumber);
QLineEdit *userInput = parentWidget->findChild<QLineEdit *>(objectName);
if(userInput){
QString input=userInput->text();
//if no input store as 0
if(input.isEmpty()){
puzzle[row][col]=0;
}
//else convert to int and store
else{
puzzle[row][col]=input.toInt();
}
}
}
}
//print for testing
for(int row = 0;row<9;row++){
for(int col=0;col<9;col++){
qDebug()<<"puzzle["<<row<<"]["<<col<<"]="<<puzzle[row][col];
}
}
}
void solver::clearPuzzle(QWidget *parentWidget){
for(int row = 0;row<9;row++){
for(int col=0;col<9;col++){
//calculate input field number
int inputNumber = row * 9 + col + 1;
QString objectName=QString("puzzle_%1").arg(inputNumber);
QLineEdit *userInput = parentWidget->findChild<QLineEdit *>(objectName);
if (userInput) {
userInput->clear();
} else {
// Optionally, handle the case where the QLineEdit wasn't found
qWarning() << "QLineEdit with objectName" << objectName << "not found.";
}
}
}
}
bool solver::solver::inRow(int row, int number) {
for(int col = 0;col<9;col++)
if (puzzle[row][col] == number) {
return true;
}
return false;
}
bool solver::solver::inColumn(int col, int number) {
for (int row = 0; row < 9; row++)
if (puzzle[row][col] == number) {
return true;
}
return false;
}
bool solver::solver::inSquare(int startRow, int startColumn, int num) {
int grid = (startRow / 3) * 3 + (startColumn / 3);
int gridStartRow = gridStart[grid][0];
int gridStartColumn = gridStart[grid][1];
for (int row = 0; row < 3; row++) {
for (int column=0; column < 3; column++) {
if (puzzle[gridStartRow+row][gridStartColumn + column] == num) {
return true;
}
}
}
return false;
}
bool solver::generateSolution(QWidget *parentWidget) {
int row, column;
bool emptyCell = false;
for (row = 0; row < 9; row++) {
for (column = 0; column < 9; column++) {
if (puzzle[row][column] == 0) {
emptyCell = true;
break;
}
}
if (emptyCell) {
break;
}
}
//end as no empty cells
if (!emptyCell) {
return true;
}
//
for (int num = 1; num <= 9; num++) {
if (!inRow(row, num) && !inColumn(column, num) && !inSquare(row,column,num)) {
// Place the number in the puzzle
puzzle[row][column] = num;
int inputNumber = row * 9 + column + 1;
QString objectName = QString("puzzle_%1").arg(inputNumber);
QLineEdit *lineEdit = parentWidget->findChild<QLineEdit *>(objectName);
if (lineEdit) {
lineEdit->setText(QString::number(num));
qDebug() << "Updated QLineEdit:" << objectName << "with value:" << num;
} else {
qWarning() << "QLineEdit with objectName" << objectName << "not found.";
}
QApplication::processEvents();
if (generateSolution(parentWidget)) {
return true;
}
puzzle[row][column] = 0;
if (lineEdit) {
lineEdit->clear();
}
}
}
return false;
}