From 5057d8cf5f1fc5c35c912d6f697645320327b532 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:23:45 +0100 Subject: [PATCH] whaaaaaaat hoe dan kkr kkr ding board to string dingen en from string dingen en hashset update thingy --- PuzzlePlayer/Binary.cs | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 7bc00fd..e87269a 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -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 -- GitLab