Skip to content
Snippets Groups Projects
Commit 5057d8cf authored by bionic85's avatar bionic85
Browse files

whaaaaaaat hoe dan kkr kkr ding

board to string dingen en from string dingen en hashset update thingy
parent ab448a9e
No related branches found
No related tags found
No related merge requests found
......@@ -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(newBoardString);
int[,] result = BackTrackAlgorithm(newBoard, alreadyCheckedBoards); // recursion for every move
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment