/// <summary> /// Solves a maze using a recursive breath-first approach. /// </summary> public override void SolveMaze() { // Apply all pre-treament logics, if any PreTreatmentLogics.ForEach(p => p.PreTreatMazeToSolve()); // Pick the first satisfactory gridpoint as the starting point var currentMazeGridpoint = MazeToSolve.MazeGridpoints.Values.First(m => m.IsStartPoint && m.DirectionsAvailable.Count > 0); _mazeSolutionElementTree.MazeGridpoint = currentMazeGridpoint; try { RecursivelySearchForSolution(currentMazeGridpoint, _mazeSolutionElementTree); } catch (StackOverflowException) { throw new Exception( "Maze Solver Error: Sorry, this maze is too large to solve using a recursive search algorithm."); } if (!_foundMazeFinishPoint) { throw new Exception("Maze Solver Error: Sorry, maze could not be solved. The maze image provided may be invalid."); } ConstructPathToSolveMaze(_mazeSolutionElementTree); }
/// <summary> /// Solves a maze using a non-recursive breath-first approach. /// </summary> public override void SolveMaze() { // Apply all pre-treament logics, if any PreTreatmentLogics.ForEach(p => p.PreTreatMazeToSolve()); // Set all parent gridpoints to null at the start PathToSolveMaze = MazeToSolve.MazeGridpoints.Select(mazeGridpoint => new MazeSolutionElement { ParentMazeGridpoint = null, MazeGridpoint = mazeGridpoint.Value }).ToList(); var queueToSolveMaze = new Queue <MazeGridpoint>(); // Pick the first satisfactory gridpoint as the starting point var startMazeGridpoint = MazeToSolve.MazeGridpoints.Values.First(m => m.IsStartPoint && m.DirectionsAvailable.Count > 0); queueToSolveMaze.Enqueue(startMazeGridpoint); var finalMazeGridpoint = SearchForSolution(queueToSolveMaze); var finalMazeSolutionElement = PathToSolveMaze.Single(m => m.MazeGridpoint == finalMazeGridpoint); ConstructPathToSolveMaze(finalMazeSolutionElement); }
/// <summary> /// Solves the maze using a brute force algorithm approach. /// </summary> public override void SolveMaze() { // Apply all pre-treament logics, if any PreTreatmentLogics.ForEach(p => p.PreTreatMazeToSolve()); // Set all parent gridpoints to null at the start var currentMazeGridpoint = MazeToSolve.MazeGridpoints.Values.First(m => m.IsStartPoint && m.DirectionsAvailable.Count > 0); // Keep on looking so long as we haven't reached the finish while (!MazeToSolve.CheckIfAtFinish(currentMazeGridpoint)) { currentMazeGridpoint.HasBeenVisited = true; // Picked a direction and go in that direction var directionToProceed = DirectionPickerLogic.PickDirectionToProceed(PathToSolveMaze, currentMazeGridpoint); if (directionToProceed == DirectionEnum.None) { throw new Exception("Maze Solver Error: Sorry, maze could not be solved. The maze image provided may be invalid."); } currentMazeGridpoint = DetermineNextMazeGridPoint(currentMazeGridpoint, directionToProceed); Console.WriteLine("X : " + currentMazeGridpoint.Position.X + ", Y : " + currentMazeGridpoint.Position.Y + " Direction : " + directionToProceed); MazeToSolve.NotifyMazeHasBeenUpdated(); MazeToSolve.NotifyMazeToBeRedrawnUpdated(); } // Once out of the loop, the current gridpoint will be the final gridpoint var finalMazeSolutionElement = new MazeSolutionElement { StepNumber = PathToSolveMaze.Count, MazeGridpoint = currentMazeGridpoint, DirectionProceeded = DirectionEnum.None }; PathToSolveMaze.Add(finalMazeSolutionElement); }