Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
sudoku solver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Silas Dohm
sudoku solver
Commits
2c0ceaab
Commit
2c0ceaab
authored
3 years ago
by
Silas Dohm
Browse files
Options
Downloads
Patches
Plain Diff
collapse reverse functionality added
parent
dee1cc35
No related branches found
No related tags found
1 merge request
!1
Master
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
cell.cpp
+40
-16
40 additions, 16 deletions
cell.cpp
cell.h
+12
-5
12 additions, 5 deletions
cell.h
window.cpp
+75
-12
75 additions, 12 deletions
window.cpp
window.h
+3
-2
3 additions, 2 deletions
window.h
with
130 additions
and
35 deletions
cell.cpp
+
40
−
16
View file @
2c0ceaab
...
@@ -14,28 +14,52 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
...
@@ -14,28 +14,52 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
float
w
=
width
()
/
3.0
;
float
w
=
width
()
/
3.0
;
float
h
=
height
()
/
3.0
;
float
h
=
height
()
/
3.0
;
QSignalMapper
*
mapper
=
new
QSignalMapper
(
this
);
QSignalMapper
*
mapper
=
new
QSignalMapper
(
this
);
connect
(
mapper
,
SIGNAL
(
mapped
(
int
)),
this
,
SLOT
(
slotButtonClicked
(
int
)));
connect
(
mapper
,
SIGNAL
(
mapped
(
int
)),
this
,
SLOT
(
collapse
(
int
)));
for
(
char
i
=
0
;
i
<
9
;
i
++
)
for
(
char
i
=
0
;
i
<
9
;
i
++
)
{
{
states
[
i
]
=
new
QPushButton
(
QString
::
number
(
i
),
this
);
m_
states
[
i
]
=
new
QPushButton
(
QString
::
number
(
i
+
1
),
this
);
states
[
i
]
->
setGeometry
(
i
%
3
*
w
,
i
/
3
*
h
,
w
,
h
);
m_
states
[
i
]
->
setGeometry
(
i
%
3
*
w
,
i
/
3
*
h
,
w
,
h
);
states
[
i
]
->
setStyleSheet
(
"border:none"
);
m_
states
[
i
]
->
setStyleSheet
(
"border:none"
);
connect
(
states
[
i
],
SIGNAL
(
clicked
()),
mapper
,
SLOT
(
map
()));
connect
(
m_
states
[
i
],
SIGNAL
(
clicked
()),
mapper
,
SLOT
(
map
()));
mapper
->
setMapping
(
states
[
i
],
i
);
mapper
->
setMapping
(
m_
states
[
i
],
i
);
}
}
collapsed
=
new
QPushButton
(
this
);
m_number
=
new
QPushButton
(
this
);
collapsed
->
setFixedSize
(
100
,
100
);
m_number
->
setFixedSize
(
100
,
100
);
collapsed
->
setStyleSheet
(
"font-size:44px"
);
m_number
->
setStyleSheet
(
"font-size:44px"
);
collapsed
->
hide
();
m_number
->
hide
();
connect
(
m_number
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
un
()));
}
}
void
Cell
::
slotButtonClicked
(
int
x
)
void
Cell
::
collapse
(
int
x
)
{
{
collapsed
->
show
();
collapsed
=
true
;
collapsed
->
setText
(
QString
::
number
(
x
));
m_number
->
show
();
emit
(
collapse
(
x
));
m_number
->
setText
(
QString
::
number
(
x
+
1
));
emit
(
update
(
x
));
}
}
void
Cell
::
foo
(
int
x
)
void
Cell
::
removeOption
(
int
x
)
{
// states[x]->hide();
if
(
m_states
[
x
]
->
isEnabled
())
{
{
states
[
x
]
->
hide
();
m_states
[
x
]
->
setDisabled
(
true
);
possibleStates
--
;
index
.
erase
(
std
::
find
(
index
.
begin
(),
index
.
end
(),
x
));
}
}
void
Cell
::
addOption
(
int
x
)
{
if
(
!
m_states
[
x
]
->
isEnabled
())
{
m_states
[
x
]
->
setDisabled
(
false
);
possibleStates
++
;
index
.
push_back
(
x
);
}
}
void
Cell
::
un
(
void
){
collapsed
=
false
;
m_number
->
hide
();
emit
(
undo
(
m_number
->
text
().
toInt
()
-
1
));
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
cell.h
+
12
−
5
View file @
2c0ceaab
...
@@ -9,16 +9,23 @@ class Cell : public QWidget
...
@@ -9,16 +9,23 @@ class Cell : public QWidget
Q_OBJECT
Q_OBJECT
public:
public:
explicit
Cell
(
QWidget
*
parent
=
0
);
explicit
Cell
(
QWidget
*
parent
=
0
);
int
possibleStates
=
9
;
bool
collapsed
=
false
;
std
::
vector
<
int
>
index
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
private
:
private
:
QPushButton
*
states
[
9
];
QPushButton
*
m_states
[
9
];
QPushButton
*
collapsed
;
QPushButton
*
m_number
;
signals
:
signals
:
void
collapse
(
int
x
);
void
update
(
int
x
);
void
undo
(
int
x
);
public
slots
:
public
slots
:
void
foo
(
int
x
);
void
addOption
(
int
x
);
void
removeOption
(
int
x
);
void
collapse
(
int
x
);
private
slots
:
private
slots
:
void
slotButtonClicked
(
int
x
);
void
un
(
void
);
};
};
#endif // CELL_H
#endif // CELL_H
\ No newline at end of file
This diff is collapsed.
Click to expand it.
window.cpp
+
75
−
12
View file @
2c0ceaab
...
@@ -2,14 +2,24 @@
...
@@ -2,14 +2,24 @@
#include
<QApplication>
#include
<QApplication>
#include
<QStandardPaths>
#include
<QStandardPaths>
#include
<QDebug>
#include
<QDebug>
#include
<QThread>
#include
<QProgressBar>
#include
<QSlider>
Window
::
Window
(
QWidget
*
parent
)
Window
::
Window
(
QWidget
*
parent
)
:
QWidget
{
parent
}
:
QWidget
{
parent
}
{
{
m_counter
=
0
;
setFixedWidth
(
1600
);
// quitbutton
m_button
=
new
QPushButton
(
"solve"
,
this
);
// m_buttonQuit = new QPushButton("quit", this);
connect
(
m_button
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
solveButtonClicked
()));
// m_buttonQuit->setGeometry(0, height() - 30, width(), 30);
m_button
->
setGeometry
(
1200
,
height
()
-
30
,
width
()
-
1200
,
30
);
m_slider
=
new
QSlider
(
this
);
m_slider
->
setRange
(
0
,
1000
);
m_slider
->
setOrientation
(
Qt
::
Horizontal
);
m_slider
->
setValue
(
m_delay
);
m_slider
->
setGeometry
(
1200
,
height
()
-
60
,
width
()
-
1200
,
30
);
connect
(
m_slider
,
SIGNAL
(
valueChanged
(
int
)),
this
,
SLOT
(
setValue
(
int
)));
int
dx
=
0
;
int
dx
=
0
;
int
dy
=
0
;
int
dy
=
0
;
for
(
int
x
=
0
;
x
<
9
;
x
++
)
for
(
int
x
=
0
;
x
<
9
;
x
++
)
...
@@ -36,20 +46,73 @@ Window::Window(QWidget *parent)
...
@@ -36,20 +46,73 @@ Window::Window(QWidget *parent)
{
{
for
(
int
y
=
0
;
y
<
9
;
y
++
)
for
(
int
y
=
0
;
y
<
9
;
y
++
)
{
{
for
(
int
d
=
0
;
d
<
9
;
d
++
)
for
(
int
d
=
0
;
d
<
9
;
d
++
)
//rows and columns
{
{
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
collapse
(
int
)),
m_cell
[
x
][
d
],
SLOT
(
foo
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
update
(
int
)),
m_cell
[
x
][
d
],
SLOT
(
removeOption
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
collapse
(
int
)),
m_cell
[
d
][
y
],
SLOT
(
foo
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
update
(
int
)),
m_cell
[
d
][
y
],
SLOT
(
removeOption
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
undo
(
int
)),
m_cell
[
x
][
d
],
SLOT
(
addOption
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
undo
(
int
)),
m_cell
[
d
][
y
],
SLOT
(
addOption
(
int
)));
}
}
int
a
=
x
/
3
*
3
;
int
a
=
x
/
3
*
3
;
int
b
=
y
/
3
*
3
;
int
b
=
y
/
3
*
3
;
for
(
int
x1
=
a
;
x1
<
a
+
3
;
x1
++
)
for
(
int
x1
=
a
;
x1
<
a
+
3
;
x1
++
)
//square
{
{
for
(
int
y1
=
b
;
y1
<
b
+
3
;
y1
++
)
for
(
int
y1
=
b
;
y1
<
b
+
3
;
y1
++
)
{
{
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
collapse
(
int
)),
m_cell
[
x1
][
y1
],
SLOT
(
foo
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
update
(
int
)),
m_cell
[
x1
][
y1
],
SLOT
(
removeOption
(
int
)));
connect
(
m_cell
[
x
][
y
],
SIGNAL
(
undo
(
int
)),
m_cell
[
x1
][
y1
],
SLOT
(
addOption
(
int
)));
}
}
}
}
}
void
Window
::
solveButtonClicked
()
{
srand
(
time
(
NULL
));
while
(
1
)
{
std
::
vector
<
Cell
*>
b
;
//vector of cells with least entropy
int
minEtropy
=
10
;
for
(
int
x
=
0
;
x
<
9
;
x
++
)
//loop through all cells and fill list b --> this could be optimzed
{
for
(
int
y
=
0
;
y
<
9
;
y
++
)
{
if
(
m_cell
[
x
][
y
]
->
collapsed
)
continue
;
else
if
(
m_cell
[
x
][
y
]
->
possibleStates
<
minEtropy
)
{
minEtropy
=
m_cell
[
x
][
y
]
->
possibleStates
;
b
.
clear
();
b
.
push_back
(
m_cell
[
x
][
y
]);
}
}
else
if
(
m_cell
[
x
][
y
]
->
possibleStates
==
minEtropy
)
{
b
.
push_back
(
m_cell
[
x
][
y
]);
}
}
}
}
}
}
if
(
b
.
size
()
==
0
)
//if b is empty -> solved
{
qInfo
(
"Solved!!"
);
return
;
}
else
if
(
minEtropy
==
0
){
qInfo
(
"dead End reached"
);
return
;
}
int
choiceCell
=
rand
()
%
b
.
size
();
//choose a random cell
int
choiceNr
=
rand
()
%
b
[
choiceCell
]
->
index
.
size
();
//choose a random number;
b
[
choiceCell
]
->
collapse
(
b
[
choiceCell
]
->
index
[
choiceNr
]);
//collapse that random cell with chosen number;
repaint
();
//draw cells again
QThread
::
msleep
(
m_delay
);
}
// qInfo("%d---:)");
}
void
Window
::
setValue
(
int
s
){
m_delay
=
s
;
return
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
window.h
+
3
−
2
View file @
2c0ceaab
...
@@ -18,11 +18,12 @@ private:
...
@@ -18,11 +18,12 @@ private:
QProgressBar
*
m_progressBar
;
QProgressBar
*
m_progressBar
;
QSlider
*
m_slider
;
QSlider
*
m_slider
;
Cell
*
m_cell
[
9
][
9
];
Cell
*
m_cell
[
9
][
9
];
int
m_
counter
;
int
m_
delay
=
300
;
signals:
signals:
void
counterReached
();
void
counterReached
();
private
slots
:
private
slots
:
void
setValue
(
int
s
);
void
solveButtonClicked
();
};
};
#endif // WINDOW_H
#endif // WINDOW_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment