Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Mathdoku
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tmp1u19
Mathdoku
Commits
be4cb1b3
Commit
be4cb1b3
authored
5 years ago
by
tmp1u19
Browse files
Options
Downloads
Patches
Plain Diff
Solver which solves mathdoku boards up until 5x5
parent
d2689e86
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Solver.java
+39
-33
39 additions, 33 deletions
Solver.java
with
39 additions
and
33 deletions
Solver.java
+
39
−
33
View file @
be4cb1b3
import
javafx.scene.layout.GridPane
;
import
javax.print.attribute.HashDocAttributeSet
;
/**
* this class generates the solution for a mathdoku game
* the backtracking method is used below, generating matrices that respect the
*/
public
class
Solver
{
private
int
n
;
private
int
[][]
sol
;
static
int
[][]
solved
;
private
GridPane
grid
;
private
boolean
found
=
false
;
public
Solver
(
int
n
,
GridPane
grid
)
{
this
.
n
=
n
;
sol
=
new
int
[
n
+
1
][
n
+
1
];
solved
=
new
int
[
n
+
1
][
n
+
1
];
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
int
j
=
1
;
j
<=
n
;
j
++)
{
sol
[
i
][
j
]
=
0
;
}
}
this
.
grid
=
grid
;
}
...
...
@@ -44,52 +52,50 @@ public class Solver {
}
}
for
(
int
i
=
1
;
i
<
col
;
i
++)
{
Handler
.
getCell
(
grid
,
row
-
1
,
i
-
1
).
setValue
(
sol
[
row
][
i
]);
}
for
(
int
i
=
1
;
i
<
row
;
i
++)
{
Handler
.
getCell
(
grid
,
i
-
1
,
col
-
1
).
setValue
(
sol
[
i
][
col
]);
}
for
(
Cage
cage
:
Handler
.
cages
)
{
if
(!
Handler
.
verifyCage
(
cage
))
{
return
false
;
}
}
return
true
;
}
boolean
isSolution
(
int
col
,
int
row
)
{
if
(
col
==
n
&&
row
==
n
)
{
Handler
.
getCell
(
grid
,
row
-
1
,
col
-
1
).
setValue
(
sol
[
row
][
col
]);
return
true
;
}
return
false
;
}
void
out
()
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
int
j
=
1
;
j
<=
n
;
j
++)
{
System
.
out
.
print
(
sol
[
i
][
j
]
+
" "
);
}
System
.
out
.
println
();
}
}
void
backtracking
(
int
row
,
int
col
)
{
first
(
row
,
col
);
while
(
next
(
row
,
col
))
{
if
(
isValid
(
row
,
col
))
{
if
(
isSolution
(
col
,
row
)
&&
Handler
.
verifyCages
())
{
out
();
System
.
out
.
println
();
if
(
isSolution
(
col
,
row
))
{
if
(!
found
)
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
int
j
=
1
;
j
<=
n
;
j
++)
{
Handler
.
getCell
(
grid
,
i
-
1
,
j
-
1
).
setValue
(
sol
[
i
][
j
]);
}
}
if
(
Handler
.
verifyCages
())
{
found
=
true
;
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
solved
[
i
][
j
]
=
sol
[
i
+
1
][
j
+
1
];
}
}
break
;
}
}
}
else
{
if
(
col
==
n
)
{
backtracking
(
row
+
1
,
1
);
}
else
{
backtracking
(
row
,
col
+
1
);
if
(!
found
)
{
if
(
col
==
n
)
{
backtracking
(
row
+
1
,
1
);
}
else
{
backtracking
(
row
,
col
+
1
);
}
}
}
}
...
...
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