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
8eddd1ef
Commit
8eddd1ef
authored
2 years ago
by
Silas Dohm
Browse files
Options
Downloads
Patches
Plain Diff
responsive layout
parent
82bd0a58
Branches
Branches containing commit
No related tags found
1 merge request
!3
responsive design
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
cell.cpp
+22
-15
22 additions, 15 deletions
cell.cpp
cell.h
+5
-5
5 additions, 5 deletions
cell.h
window.cpp
+17
-11
17 additions, 11 deletions
window.cpp
window.h
+6
-5
6 additions, 5 deletions
window.h
with
50 additions
and
36 deletions
cell.cpp
+
22
−
15
View file @
8eddd1ef
...
...
@@ -17,8 +17,7 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
for
(
char
i
=
0
;
i
<
9
;
i
++
)
{
m_states
[
i
]
=
new
QPushButton
(
QString
::
number
(
i
+
1
),
this
);
m_states
[
i
]
->
setSizePolicy
(
QSizePolicy
::
MinimumExpanding
,
QSizePolicy
::
MinimumExpanding
);
layout
->
addWidget
(
m_states
[
i
],
i
%
3
,
i
/
3
);
layout
->
addWidget
(
m_states
[
i
],
i
%
3
,
i
/
3
);
m_states
[
i
]
->
setStyleSheet
(
"border:none"
);
connect
(
m_states
[
i
],
SIGNAL
(
clicked
()),
mapper
,
SLOT
(
map
()));
mapper
->
setMapping
(
m_states
[
i
],
i
);
...
...
@@ -27,14 +26,20 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
groupBox
->
setLayout
(
layout
);
m_number
=
new
QPushButton
(
this
);
connect
(
m_number
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
un
()));
connect
(
m_number
,
SIGNAL
(
clicked
()),
this
,
SLOT
(
un
()));
m_stackedWidget
=
new
QStackedWidget
(
this
);
m_stackedWidget
->
addWidget
(
groupBox
);
m_stackedWidget
->
addWidget
(
m_number
);
m_stackedWidget
->
setSizePolicy
(
QSizePolicy
::
MinimumExpanding
,
QSizePolicy
::
MinimumExpanding
);
heightForWidth
(
this
->
width
());
setMinimumSize
(
100
,
100
);
//m_stackedWidget->setFixedSize(70,70);
setMinimumSize
(
70
,
70
);
}
void
Cell
::
resizeEvent
(
QResizeEvent
*
event
)
{
QWidget
::
resizeEvent
(
event
);
m_stackedWidget
->
setFixedSize
(
this
->
size
());
int
smallest
=
this
->
width
()
<
this
->
height
()
?
this
->
width
()
:
this
->
height
();
m_number
->
setStyleSheet
(
"font-size:"
+
QString
::
number
(
smallest
/
2
)
+
"px;"
);
return
;
}
void
Cell
::
collapse
(
int
x
)
...
...
@@ -46,29 +51,31 @@ void Cell::collapse(int x)
}
void
Cell
::
removeOption
(
int
x
)
{
// states[x]->hide();
if
(
m_states
[
x
]
->
isEnabled
())
if
(
m_blocked
[
x
]
==
0
)
{
m_states
[
x
]
->
setDisabled
(
true
);
m_states
[
x
]
->
setText
(
" "
);
possibleStates
--
;
index
.
erase
(
std
::
find
(
index
.
begin
(),
index
.
end
(),
x
));
}
m_blocked
[
x
]
++
;
m_blocked
[
x
]
++
;
}
void
Cell
::
addOption
(
int
x
)
{
m_blocked
[
x
]
--
;
//if (!m_states[x]->isEnabled())
if
(
m_blocked
[
x
]
<
1
)
m_blocked
[
x
]
--
;
//
if (!m_states[x]->isEnabled())
if
(
m_blocked
[
x
]
<
1
)
{
m_states
[
x
]
->
setText
(
QString
::
number
(
x
+
1
));
m_states
[
x
]
->
setDisabled
(
false
);
possibleStates
++
;
index
.
push_back
(
x
);
}
}
void
Cell
::
un
(
void
){
void
Cell
::
un
(
void
)
{
collapsed
=
false
;
m_stackedWidget
->
setCurrentIndex
(
0
);
emit
(
undo
(
m_number
->
text
().
toInt
()
-
1
));
emit
(
undo
(
m_number
->
text
().
toInt
()
-
1
));
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
cell.h
+
5
−
5
View file @
8eddd1ef
...
...
@@ -2,22 +2,22 @@
#define CELL_H
#include
<QWidget>
#include
<QStackedWidget>
class
QPushButton
;
class
QStackedWidget
;
class
Cell
:
public
QWidget
{
Q_OBJECT
public:
explicit
Cell
(
QWidget
*
parent
=
0
);
int
possibleStates
=
9
;
void
resizeEvent
(
QResizeEvent
*
event
);
int
possibleStates
=
9
;
bool
collapsed
=
false
;
std
::
vector
<
int
>
index
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
std
::
vector
<
int
>
index
=
{
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
};
private
:
QPushButton
*
m_states
[
9
];
QPushButton
*
m_number
;
std
::
array
<
int
,
9
>
m_blocked
;
std
::
array
<
int
,
9
>
m_blocked
;
QStackedWidget
*
m_stackedWidget
;
signals
:
...
...
This diff is collapsed.
Click to expand it.
window.cpp
+
17
−
11
View file @
8eddd1ef
...
...
@@ -8,7 +8,7 @@
#include
<QGridLayout>
#include
<QHBoxLayout>
#include
<QVBoxLayout>
#include
<QSpacerItem>
#include
<QSpacerItem>
Window
::
Window
(
QWidget
*
parent
)
:
QWidget
{
parent
}
...
...
@@ -24,17 +24,24 @@ Window::Window(QWidget *parent)
m_slider
->
setValue
(
m_delay
);
connect
(
m_slider
,
SIGNAL
(
valueChanged
(
int
)),
this
,
SLOT
(
setValue
(
int
)));
QHBoxLayout
*
horizontalLayout
=
new
QHBoxLayout
(
this
);
QVBoxLayout
*
verticalLayout
=
new
QVBoxLayout
();
QGridLayout
*
layout
=
new
QGridLayout
();
m_spacer
[
0
]
=
new
QSpacerItem
(
8
,
8
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
m_spacer
[
1
]
=
new
QSpacerItem
(
8
,
8
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Fixed
);
m_spacer
[
2
]
=
new
QSpacerItem
(
8
,
8
,
QSizePolicy
::
Minimum
,
QSizePolicy
::
Expanding
);
verticalLayout
->
addItem
(
m_spacer
[
2
]);
verticalLayout
->
addWidget
(
m_solveButton
);
verticalLayout
->
addWidget
(
m_slider
);
verticalLayout
->
addWidget
(
m_clearButton
);
horizontalLayout
->
addLayout
(
layout
,
5
);
horizontalLayout
->
addLayout
(
verticalLayout
,
1
);
horizontalLayout
->
addLayout
(
layout
,
5
);
horizontalLayout
->
addLayout
(
verticalLayout
,
1
);
layout
->
addItem
(
m_spacer
[
0
],
3
,
3
);
layout
->
addItem
(
m_spacer
[
1
],
7
,
7
);
layout
->
setSpacing
(
0
);
int
dx
=
0
;
int
dy
=
0
;
...
...
@@ -43,9 +50,8 @@ Window::Window(QWidget *parent)
for
(
int
y
=
0
;
y
<
9
;
y
++
)
{
m_cell
[
x
+
y
*
9
].
setParent
(
this
);
layout
->
addWidget
(
&
m_cell
[
x
+
y
*
9
],
y
,
x
);
//connecting signals & slots
layout
->
addWidget
(
&
m_cell
[
x
+
y
*
9
],
y
+
y
/
3
,
x
+
x
/
3
);
// connecting signals & slots
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
)));
...
...
@@ -60,7 +66,7 @@ Window::Window(QWidget *parent)
{
for
(
int
y1
=
b
;
y1
<
b
+
3
;
y1
++
)
{
if
(
x1
==
x
||
y1
==
y
)
if
(
x1
==
x
||
y1
==
y
)
continue
;
connect
(
&
m_cell
[
x
+
y
*
9
],
SIGNAL
(
update
(
int
)),
&
m_cell
[
x1
+
y1
*
9
],
SLOT
(
removeOption
(
int
)));
...
...
@@ -75,7 +81,7 @@ void Window::solveButtonClicked()
{
std
::
list
<
History
*>
hist
;
bool
backtracking
=
false
;
srand
(
time
(
NULL
));
//random seed
srand
(
time
(
NULL
));
//
random seed
while
(
1
)
{
repaint
();
// draw cells again
...
...
@@ -89,8 +95,8 @@ void Window::solveButtonClicked()
continue
;
}
backtracking
=
false
;
int
choiceNr
=
rand
()
%
hist
.
back
()
->
index
.
size
();
// choose a random number;
repaint
();
// draw cells again
int
choiceNr
=
rand
()
%
hist
.
back
()
->
index
.
size
();
// choose a random number;
repaint
();
// draw cells again
QThread
::
msleep
(
m_delay
);
hist
.
back
()
->
cell
->
collapse
(
hist
.
back
()
->
index
[
choiceNr
]);
// collapse cell with chosen number;
std
::
vector
<
int
>::
iterator
it
;
...
...
This diff is collapsed.
Click to expand it.
window.h
+
6
−
5
View file @
8eddd1ef
...
...
@@ -6,6 +6,7 @@
#include
<QProgressBar>
#include
<QSlider>
#include
"cell.h"
class
QSpacerItem
;
class
Window
:
public
QWidget
{
Q_OBJECT
...
...
@@ -15,20 +16,20 @@ public:
private:
QPushButton
*
m_clearButton
;
QPushButton
*
m_solveButton
;
QPushButton
*
grid
[
9
][
9
];
QProgressBar
*
m_progressBar
;
QSlider
*
m_slider
;
std
::
array
<
Cell
,
81
>
m_cell
;
int
m_delay
=
300
;
QSpacerItem
*
m_spacer
[
3
];
struct
History
{
Cell
*
cell
;
std
::
vector
<
int
>
index
;
History
(
Cell
*
c
,
std
::
vector
<
int
>
in
,
int
choice
)
History
(
Cell
*
c
,
std
::
vector
<
int
>
in
,
int
choice
)
{
cell
=
c
;
for
(
int
i
=
0
;
i
<
in
.
size
();
i
++
){
if
(
i
==
choice
)
for
(
int
i
=
0
;
i
<
in
.
size
();
i
++
)
{
if
(
i
==
choice
)
continue
;
index
.
push_back
(
in
[
i
]);
}
...
...
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