From 8700e75836e8f2287d36a363c2ab809d99044217 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:15:46 +0100 Subject: [PATCH] started working on optimisation --- PuzzlePlayer/Binary.cs | 60 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 7dfd394..6fc0a62 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -82,6 +82,19 @@ namespace PuzzlePlayer_Namespace boardState = generatedBoard; } + private static int recursionDepth = 0; + private static int maxDepth = 500000; + + private static void PrintBoard(int[,] board) + { + for (int i = 0; i < board.GetLength(0); i++) + { + for (int j = 0; j < board.GetLength(1); j++) + Debug.Write(board[i, j] == emptySpace ? "." : board[i, j].ToString()); + Debug.WriteLine(""); + } + Debug.WriteLine(new string('-', 20)); + } // 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 @@ -89,6 +102,15 @@ namespace PuzzlePlayer_Namespace // But i wrote all the code myself private static int[,] BackTrackAlgorithm(int[,] board) { + //recursionDepth++; + if (recursionDepth > maxDepth) + { + Console.WriteLine("Max recursion depth reached."); + //return null; + } + + PrintBoard(board); + // check if the board is complete. if so then we can return the result if (IsBoardCompletlyFilledIn(board)) return board; @@ -104,7 +126,11 @@ namespace PuzzlePlayer_Namespace return null; // backtrack } - /* + // check if the current board is already imposible to complete even tho there are still choices left + if (BoardIsImposible(board, choices)) + return null; + + ///* // shuffle the choices in the list around to get different results every time // First answer from: https://stackoverflow.com/questions/273313/randomize-a-listt Random random = new Random(); @@ -123,7 +149,8 @@ namespace PuzzlePlayer_Namespace // do the algorithm for every move foreach (Move m in choices) { - int[,] newBoard = board; + int[,] newBoard = (int[,])board.Clone(); + newBoard[m.x,m.y] = m.changeTo; int[,] result = BackTrackAlgorithm(newBoard); // recursion for every move @@ -133,11 +160,38 @@ namespace PuzzlePlayer_Namespace } //Debug.WriteLine("backtrack want moves empty"); - + recursionDepth--; // if all choices fail then we should also return null return null; } + private static bool BoardIsImposible(int[,] board, List<Move> choices) + { + throw new NotImplementedException(); + + bool isImposible = false; + + List<int[]> locations = new List<int[]>(); + + for (int i = 0; i < board.GetLength(0); i++) + for (int j = 0; j < board.GetLength(1); j++) + { + if(board[i,j] == emptySpace) + locations.Add([i,j]); + } + + foreach (Move m in choices) + { + foreach (int[] i in locations) + { + if (m.x == i[0] && m.y == i[1]) + continue; + } + } + + return isImposible; + } + // get all the possible choices private static List<Move> GetChoices(int[,] board) { -- GitLab