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
ab448a9e
Commit
ab448a9e
authored
4 months ago
by
bionic85
Browse files
Options
Downloads
Patches
Plain Diff
HashSet Implement
parent
4cd4fbbe
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
+27
-7
27 additions, 7 deletions
PuzzlePlayer/Binary.cs
with
27 additions
and
7 deletions
PuzzlePlayer/Binary.cs
+
27
−
7
View file @
ab448a9e
...
@@ -78,13 +78,13 @@ namespace PuzzlePlayer_Namespace
...
@@ -78,13 +78,13 @@ namespace PuzzlePlayer_Namespace
int
[,]
startBoard
=
GetClearBoard
(
boardState
.
GetLength
(
0
));
int
[,]
startBoard
=
GetClearBoard
(
boardState
.
GetLength
(
0
));
// generate a board
// generate a board
int
[,]
generatedBoard
=
BackTrackAlgorithm
(
startBoard
);
int
[,]
generatedBoard
=
BackTrackAlgorithm
(
startBoard
,
new
HashSet
<
int
[,
]>
()
);
boardState
=
generatedBoard
;
boardState
=
generatedBoard
;
}
}
private
static
int
recursionDepth
=
0
;
private
static
int
recursionDepth
=
0
;
private
static
int
maxDepth
=
100000
0
;
//
1 mil
recur
s
ion
s are
allowed
private
static
int
maxDepth
=
100000
;
//
max
recur
t
ion
depth that is
allowed
private
static
void
PrintBoard
(
int
[,]
board
)
private
static
void
PrintBoard
(
int
[,]
board
)
{
{
...
@@ -101,7 +101,7 @@ namespace PuzzlePlayer_Namespace
...
@@ -101,7 +101,7 @@ namespace PuzzlePlayer_Namespace
// After searching online about what the best way is to make a random puzzle generator a lot of people pointed towards 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
// I found the information about what a backtracking algorithm is here: https://www.geeksforgeeks.org/introduction-to-backtracking-2/
// I found the information about what a backtracking algorithm is here: https://www.geeksforgeeks.org/introduction-to-backtracking-2/
// But i wrote all the code myself
// But i wrote all the code myself
private
static
int
[,]
BackTrackAlgorithm
(
int
[,]
board
)
private
static
int
[,]
BackTrackAlgorithm
(
int
[,]
board
,
HashSet
<
int
[,
]>
alreadyCheckedBoards
)
{
{
recursionDepth
++;
recursionDepth
++;
if
(
recursionDepth
>
maxDepth
)
if
(
recursionDepth
>
maxDepth
)
...
@@ -114,7 +114,11 @@ namespace PuzzlePlayer_Namespace
...
@@ -114,7 +114,11 @@ namespace PuzzlePlayer_Namespace
// check if the board is complete. if so then we can return the result
// check if the board is complete. if so then we can return the result
if
(
IsBoardCompletlyFilledIn
(
board
))
if
(
IsBoardCompletlyFilledIn
(
board
))
{
recursionDepth
--;
return
board
;
return
board
;
}
// get all the possible choices and per choice do the backtrackAlgorithm
// get all the possible choices and per choice do the backtrackAlgorithm
List
<
Move
>
choices
=
GetChoices
(
board
);
List
<
Move
>
choices
=
GetChoices
(
board
);
...
@@ -123,13 +127,16 @@ namespace PuzzlePlayer_Namespace
...
@@ -123,13 +127,16 @@ namespace PuzzlePlayer_Namespace
// and because we already did a check if the board is completly filled-in, we know that we need to backtrack
// and because we already did a check if the board is completly filled-in, we know that we need to backtrack
if
(
choices
.
Count
==
0
)
if
(
choices
.
Count
==
0
)
{
{
//Debug.WriteLine("backtrack want count ==0")
;
recursionDepth
--
;
return
null
;
// backtrack
return
null
;
// backtrack
}
}
// check if the current board is already imposible to complete even tho there are still choices left
// check if the current board is already imposible to complete even tho there are still choices left
if
(!
BoardIsPosible
(
board
,
choices
))
if
(!
BoardIsPosible
(
board
,
choices
))
{
recursionDepth
--;
return
null
;
return
null
;
}
///*
///*
// shuffle the choices in the list around to get different results every time
// shuffle the choices in the list around to get different results every time
...
@@ -152,18 +159,31 @@ namespace PuzzlePlayer_Namespace
...
@@ -152,18 +159,31 @@ namespace PuzzlePlayer_Namespace
{
{
int
[,]
newBoard
=
(
int
[,])
board
.
Clone
();
//create a shallow clone to avoid multiple paths refrencing the same object
int
[,]
newBoard
=
(
int
[,])
board
.
Clone
();
//create a shallow clone to avoid multiple paths refrencing the same object
newBoard
[
m
.
x
,
m
.
y
]
=
m
.
changeTo
;
newBoard
[
m
.
x
,
m
.
y
]
=
m
.
changeTo
;
if
(
alreadyCheckedBoards
.
Contains
(
newBoard
))
{
recursionDepth
--;
return
null
;
//This point is never reached ????
}
alreadyCheckedBoards
.
Add
(
newBoard
);
int
[,]
result
=
BackTrackAlgorithm
(
newBoard
);
// recursion for every move
int
[,]
result
=
BackTrackAlgorithm
(
newBoard
,
alreadyCheckedBoards
);
// recursion for every move
if
(
result
!=
null
)
if
(
result
!=
null
)
{
recursionDepth
--;
return
result
;
return
result
;
}
}
}
recursionDepth
--;
recursionDepth
--;
// if all choices fail then we should also return null
// if all choices fail then we should also return null
return
null
;
return
null
;
}
}
// checks if the board has a possible outcome to optimise the generating process
// checks if the board has a possible outcome to optimise the generating process
...
...
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