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
5057d8cf
Commit
5057d8cf
authored
3 months ago
by
bionic85
Browse files
Options
Downloads
Patches
Plain Diff
whaaaaaaat hoe dan kkr kkr ding
board to string dingen en from string dingen en hashset update thingy
parent
ab448a9e
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
+51
-7
51 additions, 7 deletions
PuzzlePlayer/Binary.cs
with
51 additions
and
7 deletions
PuzzlePlayer/Binary.cs
+
51
−
7
View file @
5057d8cf
...
...
@@ -78,13 +78,12 @@ namespace PuzzlePlayer_Namespace
int
[,]
startBoard
=
GetClearBoard
(
boardState
.
GetLength
(
0
));
// generate a board
int
[,]
generatedBoard
=
BackTrackAlgorithm
(
startBoard
,
new
HashSet
<
int
[,
]
>
());
int
[,]
generatedBoard
=
BackTrackAlgorithm
(
startBoard
,
new
HashSet
<
string
>());
boardState
=
generatedBoard
;
}
private
static
int
recursionDepth
=
0
;
private
static
int
maxDepth
=
100000
;
// max recurtion depth that is allowed
private
static
void
PrintBoard
(
int
[,]
board
)
{
...
...
@@ -97,11 +96,54 @@ namespace PuzzlePlayer_Namespace
Debug
.
WriteLine
(
new
string
(
'-'
,
20
));
}
private
static
string
BoardToString
(
int
[,]
board
)
{
string
result
=
""
;
for
(
int
i
=
0
;
i
<
board
.
GetLength
(
0
);
i
++)
{
for
(
int
j
=
0
;
j
<
board
.
GetLength
(
1
);
j
++)
{
result
+=
(
board
[
i
,
j
]
==
emptySpace
?
"."
:
board
[
i
,
j
].
ToString
());
}
result
+=
"|"
;
}
return
result
;
}
private
static
int
[,]
BoardFromString
(
string
board
)
{
string
[]
parts
=
board
.
Split
(
'|'
,
StringSplitOptions
.
RemoveEmptyEntries
);
int
[,]
result
=
new
int
[
parts
.
Length
,
parts
.
Length
];
for
(
int
i
=
0
;
i
<
result
.
GetLength
(
0
);
i
++)
{
for
(
int
j
=
0
;
j
<
result
.
GetLength
(
1
);
j
++)
{
string
s
=
parts
[
i
][
j
].
ToString
();
// convert char to string
if
(
s
==
"."
)
result
[
i
,
j
]
=
emptySpace
;
else
if
(
s
==
"1"
||
s
==
"0"
)
result
[
i
,
j
]
=
int
.
Parse
(
s
);
// convert string to int
else
return
null
;
//invalid input string
}
}
return
result
;
}
private
static
int
recursionDepth
=
0
;
private
static
int
maxDepth
=
100000
;
// max recurtion depth that is allowed
// 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
// 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
private
static
int
[,]
BackTrackAlgorithm
(
int
[,]
board
,
HashSet
<
int
[,
]>
alreadyCheckedBoards
)
// It uses a HashSet to blablalblalbllablalblablablal
private
static
int
[,]
BackTrackAlgorithm
(
int
[,]
board
,
HashSet
<
string
>
alreadyCheckedBoards
)
{
recursionDepth
++;
if
(
recursionDepth
>
maxDepth
)
...
...
@@ -161,13 +203,15 @@ namespace PuzzlePlayer_Namespace
newBoard
[
m
.
x
,
m
.
y
]
=
m
.
changeTo
;
if
(
alreadyCheckedBoards
.
Contains
(
newBoard
))
string
newBoardString
=
BoardToString
(
newBoard
);
if
(
alreadyCheckedBoards
.
Contains
(
newBoardString
))
{
recursionDepth
--;
return
null
;
//This point is never reached ????
return
null
;
}
alreadyCheckedBoards
.
Add
(
newBoard
);
alreadyCheckedBoards
.
Add
(
newBoard
String
);
int
[,]
result
=
BackTrackAlgorithm
(
newBoard
,
alreadyCheckedBoards
);
// recursion for every move
...
...
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