Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
puzzleplayer
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
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Floris
puzzleplayer
Commits
8700e758
Commit
8700e758
authored
4 months ago
by
bionic85
Browse files
Options
Downloads
Patches
Plain Diff
started working on optimisation
parent
218ec63c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
PuzzlePlayer/Binary.cs
+57
-3
57 additions, 3 deletions
PuzzlePlayer/Binary.cs
with
57 additions
and
3 deletions
PuzzlePlayer/Binary.cs
+
57
−
3
View file @
8700e758
...
...
@@ -82,6 +82,19 @@ namespace PuzzlePlayer_Namespace
boardState
=
generatedBoard
;
}
private
static
int
recursionDepth
=
0
;
private
static
int
maxDepth
=
500000
;
private
static
void
PrintBoard
(
int
[,]
board
)
{
for
(
int
i
=
0
;
i
<
board
.
GetLength
(
0
);
i
++)
{
for
(
int
j
=
0
;
j
<
board
.
GetLength
(
1
);
j
++)
Debug
.
Write
(
board
[
i
,
j
]
==
emptySpace
?
"."
:
board
[
i
,
j
].
ToString
());
Debug
.
WriteLine
(
""
);
}
Debug
.
WriteLine
(
new
string
(
'-'
,
20
));
}
// generates a random board with a backtracking algorithm
// After searching online about what the best way is to make a random puzzle generator a lot of people pointed towards a backtracking algorithm
...
...
@@ -89,6 +102,15 @@ namespace PuzzlePlayer_Namespace
// But i wrote all the code myself
private
static
int
[,]
BackTrackAlgorithm
(
int
[,]
board
)
{
//recursionDepth++;
if
(
recursionDepth
>
maxDepth
)
{
Console
.
WriteLine
(
"Max recursion depth reached."
);
//return null;
}
PrintBoard
(
board
);
// check if the board is complete. if so then we can return the result
if
(
IsBoardCompletlyFilledIn
(
board
))
return
board
;
...
...
@@ -104,7 +126,11 @@ namespace PuzzlePlayer_Namespace
return
null
;
// backtrack
}
/*
// check if the current board is already imposible to complete even tho there are still choices left
if
(
BoardIsImposible
(
board
,
choices
))
return
null
;
///*
// shuffle the choices in the list around to get different results every time
// First answer from: https://stackoverflow.com/questions/273313/randomize-a-listt
Random
random
=
new
Random
();
...
...
@@ -123,7 +149,8 @@ namespace PuzzlePlayer_Namespace
// do the algorithm for every move
foreach
(
Move
m
in
choices
)
{
int
[,]
newBoard
=
board
;
int
[,]
newBoard
=
(
int
[,])
board
.
Clone
();
newBoard
[
m
.
x
,
m
.
y
]
=
m
.
changeTo
;
int
[,]
result
=
BackTrackAlgorithm
(
newBoard
);
// recursion for every move
...
...
@@ -133,11 +160,38 @@ namespace PuzzlePlayer_Namespace
}
//Debug.WriteLine("backtrack want moves empty");
recursionDepth
--;
// if all choices fail then we should also return null
return
null
;
}
private
static
bool
BoardIsImposible
(
int
[,]
board
,
List
<
Move
>
choices
)
{
throw
new
NotImplementedException
();
bool
isImposible
=
false
;
List
<
int
[
]>
locations
=
new
List
<
int
[
]>
();
for
(
int
i
=
0
;
i
<
board
.
GetLength
(
0
);
i
++)
for
(
int
j
=
0
;
j
<
board
.
GetLength
(
1
);
j
++)
{
if
(
board
[
i
,
j
]
==
emptySpace
)
locations
.
Add
([
i
,
j
]);
}
foreach
(
Move
m
in
choices
)
{
foreach
(
int
[]
i
in
locations
)
{
if
(
m
.
x
==
i
[
0
]
&&
m
.
y
==
i
[
1
])
continue
;
}
}
return
isImposible
;
}
// get all the possible choices
private
static
List
<
Move
>
GetChoices
(
int
[,]
board
)
{
...
...
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