Beispiel #1
0
 public RecursiveBreadthFirstMazeSolution(Maze mazeToSolve) : base(mazeToSolve)
 {
     _mazeSolutionElementTree = new MazeSolutionElementTree();
     PreTreatmentLogics       = new List <PreTreatmentLogic>
     {
         new DetermineAllOpenPathsAtStartPoint(mazeToSolve)
     };
 }
Beispiel #2
0
        // To find the path to the solution, simply unwind the solution element tree
        // from the finsh point to the start using recursion.
        private void ConstructPathToSolveMaze(MazeSolutionElementTree mazeSolutionElementTree)
        {
            if (mazeSolutionElementTree.ParentSolutionElement != null)
            {
                ConstructPathToSolveMaze(mazeSolutionElementTree.ParentSolutionElement);
            }

            var mazeSolutionElement = new MazeSolutionElement
            {
                MazeGridpoint = mazeSolutionElementTree.MazeGridpoint
            };

            PathToSolveMaze.Add(mazeSolutionElement);
            mazeSolutionElementTree.ParentSolutionElement = null;
        }
Beispiel #3
0
        private void RecursivelySearchForSolution(MazeGridpoint currentMazeGridpoint, MazeSolutionElementTree parentMazeSolutionElementTree)
        {
            // No need to keep going on this call if the finish has already been found
            if (_foundMazeFinishPoint)
            {
                return;
            }

            _mazeSolutionElementTree = parentMazeSolutionElementTree;
            _foundMazeFinishPoint    = MazeToSolve.CheckIfAtFinish(currentMazeGridpoint);

            currentMazeGridpoint.HasBeenVisited = true;
            MazeToSolve.NotifyMazeHasBeenUpdated();
            MazeToSolve.NotifyMazeToBeRedrawnUpdated();

            // Now that we've checked the current gridpoint, let's make sure we need to do more work
            if (_foundMazeFinishPoint)
            {
                return;
            }

            // For each direction available that is valid, add an element to the tree and recursively
            // call the search function again.
            foreach (var direction in currentMazeGridpoint.DirectionsAvailable.OpenPaths)
            {
                if (!MazeToSolve.IsValidPositionInMaze(currentMazeGridpoint, direction))
                {
                    continue;
                }

                var nextMazeGridpoint = MazeToSolve.ProceedToNewGridpoint(currentMazeGridpoint, direction);

                if (MazeToSolve.CheckIfAtDeadEnd(nextMazeGridpoint) || nextMazeGridpoint.HasBeenVisited)
                {
                    continue;
                }

                var nextMazeSolutionElement = new MazeSolutionElementTree
                {
                    ParentSolutionElement = parentMazeSolutionElementTree,
                    MazeGridpoint         = nextMazeGridpoint
                };

                Console.WriteLine("X : " + nextMazeGridpoint.Position.X + ", Y : " + nextMazeGridpoint.Position.Y + " Direction : " + direction);
                RecursivelySearchForSolution(nextMazeGridpoint, nextMazeSolutionElement);
            }
        }
        public void PickDirectionToProceed_PassIncorrectTypeOfSolutionElements_ThrowsException()
        {
            var currentMazeGridpoint =
                new MazeGridpoint(new CartesianCoordinate(2, 2), new DirectionsAvailable(), false, false, false);

            var listOfMazeGridpoints = new List <MazeGridpoint>
            {
                currentMazeGridpoint,
                new MazeGridpoint(new CartesianCoordinate(2, 0), new DirectionsAvailable(), true, false, false),
                new MazeGridpoint(new CartesianCoordinate(2, 4), new DirectionsAvailable(), false, true, false)
            };

            var mazeToTest      = new Maze(listOfMazeGridpoints.ToDictionary(m => m.Position, m => m));
            var directionPicker = new StraightLineDirectionPicker(mazeToTest);
            var mazeSolution    = new MazeSolutionElementTree();

            directionPicker.PickDirectionToProceed(mazeSolution, currentMazeGridpoint);
        }
Beispiel #5
0
        public void TreeDepth_TreeHasThreeLayers_ReturnsThree()
        {
            var solutionTree = new MazeSolutionElementTree
            {
                MazeGridpoint         = null,
                ParentSolutionElement = new MazeSolutionElementTree
                {
                    MazeGridpoint         = null,
                    ParentSolutionElement = new MazeSolutionElementTree
                    {
                        MazeGridpoint         = null,
                        ParentSolutionElement = null
                    }
                }
            };

            var treeDepth = solutionTree.TreeDepth;

            Assert.AreEqual(treeDepth, 3);
        }