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

HashSet Implement

parent 4cd4fbbe
No related branches found
No related tags found
No related merge requests found
...@@ -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 = 1000000; // 1 mil recursions are allowed private static int maxDepth = 100000; // max recurtion 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
......
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