示例#1
0
        public void MazeConstructorTest()
        {
            var maze = new GenericStruct.Maze(5, 5);

            Assert.AreEqual((5 * 5), maze.Layout.Length);
            Assert.AreEqual(5 - 1, maze.Layout.GetUpperBound(0));
            Assert.AreEqual(5 - 1, maze.Layout.GetUpperBound(1));
            int sum = 0;

            for (int i = 0; i < maze.Layout.GetUpperBound(0); i++)
            {
                for (int j = 0; j < maze.Layout.GetUpperBound(1); j++)
                {
                    sum += maze.Layout[i, j];
                }
            }
            Assert.AreEqual(0, sum);
            Assert.AreEqual(0, maze.StartPos[0]);
            Assert.AreEqual(0, maze.StartPos[1]);
            Assert.AreEqual(0, maze.EndPos[0]);
            Assert.AreEqual(0, maze.EndPos[1]);
            Assert.AreEqual(0, maze.CurrentPos[0]);
            Assert.AreEqual(0, maze.CurrentPos[1]);
            Assert.AreEqual(-1, maze.PathLength);

            maze = new GenericStruct.Maze(3, 2);
            Assert.AreEqual((3 * 2), maze.Layout.Length);
            Assert.AreEqual(3 - 1, maze.Layout.GetUpperBound(0));
            Assert.AreEqual(2 - 1, maze.Layout.GetUpperBound(1));
        }
        /// <summary>
        /// A 3x3 matrix that is uniquely solvable, with path length 5.
        /// First element is the maze, 2nd element is the soln
        /// </summary>
        /// <returns></returns>
        private static List <GenericStruct.Maze> create33Solvable5()
        {
            var output = new List <GenericStruct.Maze>();

            var maze = new GenericStruct.Maze();

            maze.Layout = new int[, ] {
                { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 1 }
            };
            maze.StartPos = new int[] { 0, 0 };
            maze.EndPos   = new int[] { 1, 2 };
            output.Add(maze);

            var solutionMaze = new GenericStruct.Maze();

            solutionMaze.Layout = new int[, ] {
                { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 1 }
            };
            solutionMaze.StartPos   = new int[] { 0, 0 };
            solutionMaze.EndPos     = new int[] { 1, 2 };
            solutionMaze.PathLength = 5;
            output.Add(solutionMaze);

            return(output);
        }
            public void Unique1010MazeTest()
            {
                var mazeSolver = new MazeSolvers.BacktrackingAlgorithm();

                var mazes    = create1010();
                var solnMaze = new GenericStruct.Maze();

                solnMaze = mazeSolver.FindShortestPath(mazes);
                Assert.AreEqual(mazes.PathLength, solnMaze.PathLength);
            }
        /// <summary>
        /// A 2x2 matrix that is unsolvable
        /// </summary>
        /// <returns></returns>
        private static GenericStruct.Maze create22Unsolvable()
        {
            var maze = new GenericStruct.Maze();

            maze.Layout = new int[, ] {
                { 1, 0 }, { 0, 0 }
            };
            maze.StartPos = new int[] { 0, 0 };
            maze.EndPos   = new int[] { 1, 1 };

            return(maze);
        }
            public void Multiple44SolutionTest()
            {
                var mazeSolver = new MazeSolvers.BacktrackingAlgorithm();

                var mazes    = create44With2Soln();
                var solnMaze = new GenericStruct.Maze();

                solnMaze = mazeSolver.FindShortestPath(mazes[0]);
                var answer = mazes[1];

                Assert.AreEqual(answer.PathLength, solnMaze.PathLength);
                Assert.IsTrue(RJM.Personal.Method.AreEqualIntArrays(solnMaze.Layout, answer.Layout));
            }
            public void Unique33MazeTest()
            {
                var mazeSolver = new MazeSolvers.BacktrackingAlgorithm();

                //test for the 3x3 uniquely solvable matrix
                var mazes    = create33Solvable();
                var solnMaze = new GenericStruct.Maze();

                solnMaze = mazeSolver.FindShortestPath(mazes[0]);
                var answer = mazes[1];

                //answer.Layout = new int[,] { { 1, 1, 1 }, { 0, 0, 1 }, { 0, 0, 1 } };
                //answer.PathLength = 4;
                Assert.AreEqual(answer.PathLength, solnMaze.PathLength);
                Assert.IsTrue(RJM.Personal.Method.AreEqualIntArrays(solnMaze.Layout, answer.Layout));
            }
        /// <summary>
        /// A 4x4 matrix with 2 valid solutions, one with path length 5 and the other with path length 7.
        /// First element is the maze, 2nd element is soln with pathLength=5 and 3rd is soln with pathLength = 7
        /// </summary>
        /// <returns></returns>
        private static List <GenericStruct.Maze> create44With2Soln()
        {
            var output = new List <GenericStruct.Maze>();

            var maze = new GenericStruct.Maze();

            maze.Layout = new int[, ] {
                { 1, 1, 1, 1 },
                { 1, 0, 0, 1 },
                { 1, 1, 0, 1 },
                { 0, 1, 1, 1 }
            };
            maze.StartPos = new int[] { 0, 0 };
            maze.EndPos   = new int[] { 2, 3 };
            output.Add(maze);

            var solnMaze = new GenericStruct.Maze();

            solnMaze.Layout = new int[, ] {
                { 1, 1, 1, 1 },
                { 0, 0, 0, 1 },
                { 0, 0, 0, 1 },
                { 0, 0, 0, 0 }
            };
            solnMaze.PathLength = 5;
            output.Add(solnMaze);

            var otherSolnMaze = new GenericStruct.Maze();

            otherSolnMaze.Layout = new int[, ] {
                { 1, 0, 0, 0 },
                { 1, 0, 0, 0 },
                { 1, 1, 0, 1 },
                { 0, 1, 1, 1 }
            };
            otherSolnMaze.PathLength = 7;
            output.Add(otherSolnMaze);

            return(output);
        }
        private static GenericStruct.Maze create1010()
        {
            var maze = new GenericStruct.Maze();

            maze.Layout = new int[, ]
            {
                { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
                { 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 },
                { 0, 0, 1, 0, 1, 1, 1, 0, 0, 1 },
                { 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
                { 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
                { 1, 0, 1, 1, 1, 0, 0, 1, 1, 0 },
                { 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
                { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
                { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
                { 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 }
            };
            maze.StartPos   = new int[] { 0, 0 };
            maze.EndPos     = new int[] { 7, 5 };
            maze.PathLength = 12;

            return(maze);
        }