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
5f261e57
Commit
5f261e57
authored
2 years ago
by
Silas Dohm
Browse files
Options
Downloads
Patches
Plain Diff
renamed
parent
8e5577bf
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
cell.cpp
+31
-31
31 additions, 31 deletions
cell.cpp
cell.h
+7
-7
7 additions, 7 deletions
cell.h
window.cpp
+42
-42
42 additions, 42 deletions
window.cpp
window.h
+9
-9
9 additions, 9 deletions
window.h
with
89 additions
and
89 deletions
cell.cpp
+
31
−
31
View file @
5f261e57
...
@@ -9,67 +9,67 @@
...
@@ -9,67 +9,67 @@
Cell
::
Cell
(
QWidget
*
parent
)
:
QWidget
(
parent
)
Cell
::
Cell
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
{
m_
mapper
=
new
QSignalMapper
(
this
);
mapper
=
new
QSignalMapper
(
this
);
connect
(
m_
mapper
,
SIGNAL
(
mapped
(
int
)),
this
,
SLOT
(
collapse
(
int
)));
connect
(
mapper
,
SIGNAL
(
mapped
(
int
)),
this
,
SLOT
(
collapse
(
int
)));
m_
groupBox
=
new
QGroupBox
();
groupBox
=
new
QGroupBox
();
m_
gridLayout
=
new
QGridLayout
();
gridLayout
=
new
QGridLayout
();
setStyleSheet
(
"border: 1px solid #1e282c; border-radius: 0px;background-color:#52585d;"
);
setStyleSheet
(
"border: 1px solid #1e282c; border-radius: 0px;background-color:#52585d;"
);
for
(
char
i
=
0
;
i
<
9
;
i
++
)
for
(
char
i
=
0
;
i
<
9
;
i
++
)
{
{
m_
states
[
i
]
=
new
QPushButton
(
QString
::
number
(
i
+
1
),
this
);
states
[
i
]
=
new
QPushButton
(
QString
::
number
(
i
+
1
),
this
);
m_
gridLayout
->
addWidget
(
m_
states
[
i
],
i
/
3
,
i
%
3
);
gridLayout
->
addWidget
(
states
[
i
],
i
/
3
,
i
%
3
);
m_
states
[
i
]
->
setStyleSheet
(
"border:none"
);
states
[
i
]
->
setStyleSheet
(
"border:none"
);
connect
(
m_
states
[
i
],
SIGNAL
(
clicked
()),
m_
mapper
,
SLOT
(
map
()));
connect
(
states
[
i
],
SIGNAL
(
clicked
()),
mapper
,
SLOT
(
map
()));
m_
mapper
->
setMapping
(
m_
states
[
i
],
i
);
mapper
->
setMapping
(
states
[
i
],
i
);
m_
blocked
[
i
]
=
0
;
blocked
[
i
]
=
0
;
}
}
m_
groupBox
->
setLayout
(
m_
gridLayout
);
groupBox
->
setLayout
(
gridLayout
);
m_
number
=
new
QPushButton
(
this
);
number
=
new
QPushButton
(
this
);
connect
(
m_
number
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
collapsedCellClicked
()));
connect
(
number
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
collapsedCellClicked
()));
m_
stackedWidget
=
new
QStackedWidget
(
this
);
stackedWidget
=
new
QStackedWidget
(
this
);
m_
stackedWidget
->
addWidget
(
m_
groupBox
);
stackedWidget
->
addWidget
(
groupBox
);
m_
stackedWidget
->
addWidget
(
m_
number
);
stackedWidget
->
addWidget
(
number
);
setMinimumSize
(
70
,
70
);
setMinimumSize
(
70
,
70
);
}
}
void
Cell
::
resizeEvent
(
QResizeEvent
*
event
)
void
Cell
::
resizeEvent
(
QResizeEvent
*
event
)
{
{
QWidget
::
resizeEvent
(
event
);
QWidget
::
resizeEvent
(
event
);
m_
stackedWidget
->
setFixedSize
(
this
->
size
());
stackedWidget
->
setFixedSize
(
this
->
size
());
int
smallest
=
this
->
width
()
<
this
->
height
()
?
this
->
width
()
:
this
->
height
();
int
smallest
=
this
->
width
()
<
this
->
height
()
?
this
->
width
()
:
this
->
height
();
m_
number
->
setStyleSheet
(
"font-size:"
+
QString
::
number
(
smallest
/
2
)
+
"px;"
);
number
->
setStyleSheet
(
"font-size:"
+
QString
::
number
(
smallest
/
2
)
+
"px;"
);
return
;
return
;
}
}
void
Cell
::
collapse
(
int
x
)
void
Cell
::
collapse
(
int
x
)
{
{
collapsed
=
true
;
collapsed
=
true
;
m_
stackedWidget
->
setCurrentIndex
(
1
);
stackedWidget
->
setCurrentIndex
(
1
);
m_
number
->
setText
(
QString
::
number
(
x
+
1
));
number
->
setText
(
QString
::
number
(
x
+
1
));
emit
update
(
x
);
emit
update
(
x
);
}
}
void
Cell
::
removeOption
(
int
x
)
void
Cell
::
removeOption
(
int
x
)
{
{
if
(
m_
blocked
[
x
]
==
0
)
if
(
blocked
[
x
]
==
0
)
{
{
m_
states
[
x
]
->
setText
(
" "
);
states
[
x
]
->
setText
(
" "
);
m_
states
[
x
]
->
setDisabled
(
true
);
states
[
x
]
->
setDisabled
(
true
);
possibleStates
--
;
possibleStates
--
;
options
.
erase
(
std
::
find
(
options
.
begin
(),
options
.
end
(),
x
));
options
.
erase
(
std
::
find
(
options
.
begin
(),
options
.
end
(),
x
));
}
}
m_
blocked
[
x
]
++
;
blocked
[
x
]
++
;
}
}
void
Cell
::
addOption
(
int
x
)
void
Cell
::
addOption
(
int
x
)
{
{
m_
blocked
[
x
]
--
;
blocked
[
x
]
--
;
// if (!
m_
states[x]->isEnabled())
// if (!states[x]->isEnabled())
if
(
m_
blocked
[
x
]
<
1
)
if
(
blocked
[
x
]
<
1
)
{
{
m_
states
[
x
]
->
setText
(
QString
::
number
(
x
+
1
));
states
[
x
]
->
setText
(
QString
::
number
(
x
+
1
));
m_
states
[
x
]
->
setDisabled
(
false
);
states
[
x
]
->
setDisabled
(
false
);
possibleStates
++
;
possibleStates
++
;
options
.
push_back
(
x
);
options
.
push_back
(
x
);
}
}
...
@@ -77,6 +77,6 @@ void Cell::addOption(int x)
...
@@ -77,6 +77,6 @@ void Cell::addOption(int x)
void
Cell
::
collapsedCellClicked
(
void
)
void
Cell
::
collapsedCellClicked
(
void
)
{
{
collapsed
=
false
;
collapsed
=
false
;
m_
stackedWidget
->
setCurrentIndex
(
0
);
stackedWidget
->
setCurrentIndex
(
0
);
emit
undo
(
m_
number
->
text
().
toInt
()
-
1
);
emit
undo
(
number
->
text
().
toInt
()
-
1
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
cell.h
+
7
−
7
View file @
5f261e57
...
@@ -18,13 +18,13 @@ public:
...
@@ -18,13 +18,13 @@ public:
std
::
vector
<
int
>
options
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
std
::
vector
<
int
>
options
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
private
:
private
:
QSignalMapper
*
m_
mapper
;
QSignalMapper
*
mapper
;
QGroupBox
*
m_
groupBox
;
QGroupBox
*
groupBox
;
QGridLayout
*
m_
gridLayout
;
QGridLayout
*
gridLayout
;
QPushButton
*
m_
states
[
9
];
QPushButton
*
states
[
9
];
QPushButton
*
m_
number
;
QPushButton
*
number
;
std
::
array
<
int
,
9
>
m_
blocked
;
std
::
array
<
int
,
9
>
blocked
;
QStackedWidget
*
m_
stackedWidget
;
QStackedWidget
*
stackedWidget
;
signals
:
signals
:
void
update
(
int
x
);
void
update
(
int
x
);
...
...
This diff is collapsed.
Click to expand it.
window.cpp
+
42
−
42
View file @
5f261e57
...
@@ -13,35 +13,35 @@
...
@@ -13,35 +13,35 @@
Window
::
Window
(
QWidget
*
parent
)
Window
::
Window
(
QWidget
*
parent
)
:
QWidget
{
parent
}
:
QWidget
{
parent
}
{
{
m_
solveButton
=
new
QPushButton
(
"solve"
,
this
);
solveButton
=
new
QPushButton
(
"solve"
,
this
);
connect
(
m_
solveButton
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
solveButtonClicked
()));
connect
(
solveButton
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
solveButtonClicked
()));
m_
clearButton
=
new
QPushButton
(
"Clear"
,
this
);
clearButton
=
new
QPushButton
(
"Clear"
,
this
);
connect
(
m_
clearButton
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
clearButtonClicked
()));
connect
(
clearButton
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
clearButtonClicked
()));
m_
slider
=
new
QSlider
(
this
);
slider
=
new
QSlider
(
this
);
m_
slider
->
setRange
(
0
,
1000
);
slider
->
setRange
(
0
,
1000
);
m_
slider
->
setOrientation
(
Qt
::
Horizontal
);
slider
->
setOrientation
(
Qt
::
Horizontal
);
m_
slider
->
setValue
(
m_
delay
);
slider
->
setValue
(
delay
Time
);
connect
(
m_
slider
,
SIGNAL
(
valueChanged
(
int
)),
this
,
SLOT
(
setValue
(
int
)));
connect
(
slider
,
SIGNAL
(
valueChanged
(
int
)),
this
,
SLOT
(
setValue
(
int
)));
m_
horizontalLayout
=
new
QHBoxLayout
(
this
);
horizontalLayout
=
new
QHBoxLayout
(
this
);
m_
verticalLayout
=
new
QVBoxLayout
();
verticalLayout
=
new
QVBoxLayout
();
m_
layout
=
new
QGridLayout
();
layout
=
new
QGridLayout
();
m_
spacer
[
0
]
=
new
QSpacerItem
(
4
,
4
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
spacer
[
0
]
=
new
QSpacerItem
(
4
,
4
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
m_
spacer
[
1
]
=
new
QSpacerItem
(
4
,
4
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
spacer
[
1
]
=
new
QSpacerItem
(
4
,
4
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
m_
spacer
[
2
]
=
new
QSpacerItem
(
8
,
8
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Expanding
);
spacer
[
2
]
=
new
QSpacerItem
(
8
,
8
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Expanding
);
m_
verticalLayout
->
addItem
(
m_
spacer
[
2
]);
verticalLayout
->
addItem
(
spacer
[
2
]);
m_
verticalLayout
->
addWidget
(
m_
solveButton
);
verticalLayout
->
addWidget
(
solveButton
);
m_
verticalLayout
->
addWidget
(
m_
slider
);
verticalLayout
->
addWidget
(
slider
);
m_
verticalLayout
->
addWidget
(
m_
clearButton
);
verticalLayout
->
addWidget
(
clearButton
);
m_
horizontalLayout
->
addLayout
(
m_
layout
,
5
);
horizontalLayout
->
addLayout
(
layout
,
5
);
m_
horizontalLayout
->
addLayout
(
m_
verticalLayout
,
1
);
horizontalLayout
->
addLayout
(
verticalLayout
,
1
);
m_
layout
->
addItem
(
m_
spacer
[
0
],
3
,
3
);
layout
->
addItem
(
spacer
[
0
],
3
,
3
);
m_
layout
->
addItem
(
m_
spacer
[
1
],
7
,
7
);
layout
->
addItem
(
spacer
[
1
],
7
,
7
);
m_
layout
->
setSpacing
(
0
);
layout
->
setSpacing
(
0
);
int
dx
=
0
;
int
dx
=
0
;
int
dy
=
0
;
int
dy
=
0
;
...
@@ -49,16 +49,16 @@ Window::Window(QWidget *parent)
...
@@ -49,16 +49,16 @@ Window::Window(QWidget *parent)
{
{
for
(
int
y
=
0
;
y
<
9
;
y
++
)
for
(
int
y
=
0
;
y
<
9
;
y
++
)
{
{
m_
cell
[
x
+
y
*
9
].
setParent
(
this
);
cell
[
x
+
y
*
9
].
setParent
(
this
);
m_
layout
->
addWidget
(
&
m_
cell
[
x
+
y
*
9
],
y
+
y
/
3
,
x
+
x
/
3
);
layout
->
addWidget
(
&
cell
[
x
+
y
*
9
],
y
+
y
/
3
,
x
+
x
/
3
);
// connecting signals & slots
// connecting signals & slots
for
(
int
d
=
0
;
d
<
9
;
d
++
)
// rows and columns
for
(
int
d
=
0
;
d
<
9
;
d
++
)
// rows and columns
{
{
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
m_
cell
[
x
+
d
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
cell
[
x
+
d
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
m_
cell
[
d
+
y
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
cell
[
d
+
y
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
m_
cell
[
x
+
d
*
9
],
SLOT
(
addOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
cell
[
x
+
d
*
9
],
SLOT
(
addOption
(
int
)));
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
m_
cell
[
d
+
y
*
9
],
SLOT
(
addOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
cell
[
d
+
y
*
9
],
SLOT
(
addOption
(
int
)));
}
}
int
a
=
x
/
3
*
3
;
int
a
=
x
/
3
*
3
;
int
b
=
y
/
3
*
3
;
int
b
=
y
/
3
*
3
;
...
@@ -68,9 +68,9 @@ Window::Window(QWidget *parent)
...
@@ -68,9 +68,9 @@ Window::Window(QWidget *parent)
{
{
if
(
x1
==
x
||
y1
==
y
)
if
(
x1
==
x
||
y1
==
y
)
continue
;
continue
;
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
m_
cell
[
x1
+
y1
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
cell
[
x1
+
y1
*
9
],
SLOT
(
removeOption
(
int
)));
connect
(
&
m_
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
m_
cell
[
x1
+
y1
*
9
],
SLOT
(
addOption
(
int
)));
connect
(
&
cell
[
x
+
y
*
9
],
SIGNAL
(
undo
(
int
)),
&
cell
[
x1
+
y1
*
9
],
SLOT
(
addOption
(
int
)));
}
}
}
}
}
}
...
@@ -85,7 +85,7 @@ void Window::solveButtonClicked()
...
@@ -85,7 +85,7 @@ void Window::solveButtonClicked()
while
(
1
)
while
(
1
)
{
{
repaint
();
// draw cells again
repaint
();
// draw cells again
delay
(
m_
delay
);
delay
(
delay
Time
);
if
(
backtracking
)
if
(
backtracking
)
{
{
hist
.
back
().
cell
->
collapsedCellClicked
();
hist
.
back
().
cell
->
collapsedCellClicked
();
...
@@ -97,7 +97,7 @@ void Window::solveButtonClicked()
...
@@ -97,7 +97,7 @@ void Window::solveButtonClicked()
backtracking
=
false
;
backtracking
=
false
;
int
choiceNr
=
rand
()
%
hist
.
back
().
options
.
size
();
// choose a random number;
int
choiceNr
=
rand
()
%
hist
.
back
().
options
.
size
();
// choose a random number;
repaint
();
// draw cells again
repaint
();
// draw cells again
delay
(
m_
delay
);
delay
(
delay
Time
);
hist
.
back
().
cell
->
collapse
(
hist
.
back
().
options
[
choiceNr
]);
// collapse cell with chosen number;
hist
.
back
().
cell
->
collapse
(
hist
.
back
().
options
[
choiceNr
]);
// collapse cell with chosen number;
std
::
vector
<
int
>::
iterator
it
;
std
::
vector
<
int
>::
iterator
it
;
it
=
hist
.
back
().
options
.
begin
()
+
choiceNr
;
it
=
hist
.
back
().
options
.
begin
()
+
choiceNr
;
...
@@ -111,17 +111,17 @@ void Window::solveButtonClicked()
...
@@ -111,17 +111,17 @@ void Window::solveButtonClicked()
{
{
for
(
int
y
=
0
;
y
<
9
;
y
++
)
for
(
int
y
=
0
;
y
<
9
;
y
++
)
{
{
if
(
m_
cell
[
x
+
y
*
9
].
collapsed
)
if
(
cell
[
x
+
y
*
9
].
collapsed
)
continue
;
continue
;
else
if
(
m_
cell
[
x
+
y
*
9
].
possibleStates
<
minEtropy
)
else
if
(
cell
[
x
+
y
*
9
].
possibleStates
<
minEtropy
)
{
{
minEtropy
=
m_
cell
[
x
+
y
*
9
].
possibleStates
;
minEtropy
=
cell
[
x
+
y
*
9
].
possibleStates
;
b
.
clear
();
b
.
clear
();
b
.
push_back
(
&
m_
cell
[
x
+
y
*
9
]);
b
.
push_back
(
&
cell
[
x
+
y
*
9
]);
}
}
else
if
(
m_
cell
[
x
+
y
*
9
].
possibleStates
==
minEtropy
)
else
if
(
cell
[
x
+
y
*
9
].
possibleStates
==
minEtropy
)
{
{
b
.
push_back
(
&
m_
cell
[
x
+
y
*
9
]);
b
.
push_back
(
&
cell
[
x
+
y
*
9
]);
}
}
}
}
}
}
...
@@ -154,7 +154,7 @@ inline void Window::delay(int millisecondsWait)
...
@@ -154,7 +154,7 @@ inline void Window::delay(int millisecondsWait)
}
}
void
Window
::
clearButtonClicked
()
void
Window
::
clearButtonClicked
()
{
{
for
(
auto
&
x
:
m_
cell
)
for
(
auto
&
x
:
cell
)
{
{
if
(
x
.
collapsed
)
if
(
x
.
collapsed
)
x
.
collapsedCellClicked
();
x
.
collapsedCellClicked
();
...
@@ -163,6 +163,6 @@ void Window::clearButtonClicked()
...
@@ -163,6 +163,6 @@ void Window::clearButtonClicked()
}
}
void
Window
::
setValue
(
int
s
)
void
Window
::
setValue
(
int
s
)
{
{
m_
delay
=
s
;
delay
Time
=
s
;
return
;
return
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
window.h
+
9
−
9
View file @
5f261e57
...
@@ -16,15 +16,15 @@ public:
...
@@ -16,15 +16,15 @@ public:
explicit
Window
(
QWidget
*
parent
=
nullptr
);
explicit
Window
(
QWidget
*
parent
=
nullptr
);
private:
private:
QPushButton
*
m_
clearButton
;
QPushButton
*
clearButton
;
QPushButton
*
m_
solveButton
;
QPushButton
*
solveButton
;
QSlider
*
m_
slider
;
QSlider
*
slider
;
std
::
array
<
Cell
,
81
>
m_
cell
;
std
::
array
<
Cell
,
81
>
cell
;
int
m_
delay
=
300
;
int
delay
Time
=
300
;
QSpacerItem
*
m_
spacer
[
3
];
QSpacerItem
*
spacer
[
3
];
QHBoxLayout
*
m_
horizontalLayout
;
QHBoxLayout
*
horizontalLayout
;
QVBoxLayout
*
m_
verticalLayout
;
QVBoxLayout
*
verticalLayout
;
QGridLayout
*
m_
layout
;
QGridLayout
*
layout
;
struct
History
struct
History
{
{
Cell
*
cell
;
Cell
*
cell
;
...
...
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