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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Silas Dohm
sudoku solver
Commits
e9e29398
Commit
e9e29398
authored
2 years ago
by
Silas Dohm
Browse files
Options
Downloads
Patches
Plain Diff
moved work
parent
65eae54d
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
cell.cpp
+0
-3
0 additions, 3 deletions
cell.cpp
cell.h
+1
-2
1 addition, 2 deletions
cell.h
window.cpp
+21
-13
21 additions, 13 deletions
window.cpp
window.h
+4
-8
4 additions, 8 deletions
window.h
with
26 additions
and
26 deletions
cell.cpp
+
0
−
3
View file @
e9e29398
...
...
@@ -57,7 +57,6 @@ void Cell::removeOption(int x)
states
[
x
]
->
setText
(
" "
);
states
[
x
]
->
setDisabled
(
true
);
possibleStates
--
;
options
.
erase
(
std
::
find
(
options
.
begin
(),
options
.
end
(),
x
));
}
blocked
[
x
]
++
;
}
...
...
@@ -65,13 +64,11 @@ void Cell::removeOption(int x)
void
Cell
::
addOption
(
int
x
)
{
blocked
[
x
]
--
;
// if (!states[x]->isEnabled())
if
(
blocked
[
x
]
<
1
)
{
states
[
x
]
->
setText
(
QString
::
number
(
x
+
1
));
states
[
x
]
->
setDisabled
(
false
);
possibleStates
++
;
options
.
push_back
(
x
);
}
}
void
Cell
::
collapsedCellClicked
(
void
)
...
...
This diff is collapsed.
Click to expand it.
cell.h
+
1
−
2
View file @
e9e29398
...
...
@@ -15,7 +15,7 @@ public:
void
resizeEvent
(
QResizeEvent
*
event
);
int
possibleStates
=
9
;
bool
collapsed
=
false
;
std
::
vector
<
int
>
options
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}
;
std
::
array
<
int
,
9
>
blocked
;
private:
QSignalMapper
*
mapper
;
...
...
@@ -23,7 +23,6 @@ private:
QGridLayout
*
gridLayout
;
QPushButton
*
states
[
9
];
QPushButton
*
number
;
std
::
array
<
int
,
9
>
blocked
;
QStackedWidget
*
stackedWidget
;
signals:
...
...
This diff is collapsed.
Click to expand it.
window.cpp
+
21
−
13
View file @
e9e29398
...
...
@@ -9,7 +9,6 @@
#include
<QSpacerItem>
#include
<QTimer>
Window
::
Window
(
QWidget
*
parent
)
:
QWidget
{
parent
}
{
...
...
@@ -54,7 +53,8 @@ Window::Window(QWidget *parent)
{
connect
(
&
grid
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
grid
[
x
+
d
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
grid
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
grid
[
x
+
d
*
9
],
SLOT
(
addOption
(
int
)));
if
(
&
grid
[
x
+
y
*
9
]
!=
&
grid
[
d
+
y
*
9
]){
if
(
&
grid
[
x
+
y
*
9
]
!=
&
grid
[
d
+
y
*
9
])
{
connect
(
&
grid
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
grid
[
d
+
y
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
grid
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
grid
[
d
+
y
*
9
],
SLOT
(
addOption
(
int
)));
}
...
...
@@ -88,19 +88,21 @@ void Window::solveButtonClicked()
if
(
backtracking
)
{
hist
.
back
().
cell
->
collapsedCellClicked
();
if
(
hist
.
back
().
options
.
size
()
==
0
)
std
::
vector
<
int
>
b
;
for
(
int
i
=
0
;
i
<
9
;
i
++
)
if
(
hist
.
back
().
blocked
[
i
]
==
0
)
b
.
push_back
(
i
);
if
(
b
.
size
()
==
0
)
{
hist
.
pop_back
();
continue
;
}
backtracking
=
false
;
int
choiceNr
=
rand
()
%
hist
.
back
().
options
.
size
();
// choose a random number;
int
choiceNr
=
rand
()
%
b
.
size
();
// choose a random number;
repaint
();
// draw cells again
delay
(
delayTime
);
hist
.
back
().
cell
->
collapse
(
hist
.
back
().
options
[
choiceNr
]);
// collapse cell with chosen number;
std
::
vector
<
int
>::
iterator
it
;
it
=
hist
.
back
().
options
.
begin
()
+
choiceNr
;
hist
.
back
().
options
.
erase
(
it
);
hist
.
back
().
cell
->
collapse
(
b
[
choiceNr
]);
// collapse cell with chosen number;
hist
.
back
().
blocked
[
b
[
choiceNr
]]
++
;
}
else
{
...
...
@@ -137,9 +139,15 @@ void Window::solveButtonClicked()
}
int
choiceCell
=
rand
()
%
b
.
size
();
// choose a random cell
int
choiceNr
=
rand
()
%
b
[
choiceCell
]
->
options
.
size
();
// choose a random number;
hist
.
push_back
(
History
(
b
[
choiceCell
],
b
[
choiceCell
]
->
options
,
choiceNr
));
b
[
choiceCell
]
->
collapse
(
b
[
choiceCell
]
->
options
[
choiceNr
]);
// collapse that random cell with chosen number;
std
::
vector
<
int
>
c
;
for
(
int
i
=
0
;
i
<
9
;
i
++
)
{
if
(
b
[
choiceCell
]
->
blocked
[
i
]
==
0
)
c
.
push_back
(
i
);
}
int
choiceNr
=
rand
()
%
c
.
size
();
// choose a random number;
hist
.
push_back
(
History
(
b
[
choiceCell
],
b
[
choiceCell
]
->
blocked
,
c
[
choiceNr
]));
// fix this!!!
b
[
choiceCell
]
->
collapse
(
c
[
choiceNr
]);
// collapse that random cell with chosen number;
}
}
}
...
...
This diff is collapsed.
Click to expand it.
window.h
+
4
−
8
View file @
e9e29398
...
...
@@ -28,16 +28,12 @@ private:
struct
History
{
Cell
*
cell
;
std
::
vector
<
int
>
options
;
History
(
Cell
*
c
,
std
::
vector
<
int
>
in
,
int
choice
)
std
::
array
<
int
,
9
>
blocked
;
History
(
Cell
*
c
,
std
::
array
<
int
,
9
>
in
,
int
choice
)
{
cell
=
c
;
for
(
int
i
=
0
;
i
<
in
.
size
();
i
++
)
{
if
(
i
==
choice
)
continue
;
options
.
push_back
(
in
[
i
]);
}
blocked
=
in
;
blocked
[
choice
]
++
;
}
};
inline
void
delay
(
int
millisecondsWait
);
...
...
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