public void LoadLabirintTest()
        {
            //arrange
            const string labirintFilePath = @"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\labirint_test1.txt";

            var expectedMap = new MazeCell[3, 4];

            expectedMap[0, 0] = new MazeCell(0, 0, CellType.Wall);
            expectedMap[0, 1] = new MazeCell(1, 0, CellType.Wall);
            expectedMap[0, 2] = new MazeCell(2, 0, CellType.Wall);
            expectedMap[0, 3] = new MazeCell(3, 0, CellType.Wall);
            expectedMap[1, 0] = new MazeCell(0, 1, CellType.Wall);
            expectedMap[1, 1] = new MazeCell(1, 1, CellType.Start);
            expectedMap[1, 2] = new MazeCell(2, 1, CellType.Exit);
            expectedMap[1, 3] = new MazeCell(3, 1, CellType.Wall);
            expectedMap[2, 0] = new MazeCell(0, 2, CellType.Wall);
            expectedMap[2, 1] = new MazeCell(1, 2, CellType.Wall);
            expectedMap[2, 2] = new MazeCell(2, 2, CellType.Wall);
            expectedMap[2, 3] = new MazeCell(3, 2, CellType.Wall);

            //act
            var io = new MazeIO();

            io.ReadMazeFromFileTaskAsync(labirintFilePath);
            var actualMap = io.CreateMazeMatrix().MazeCells;

            //assert

            CollectionAssert.AreEqual(expectedMap, actualMap, "Матрицы не равны");
        }
Beispiel #2
0
        /// <summary>
        /// 加载搜索结果
        /// </summary>
        public void LoadResult()
        {
            List <IRecovableOperation> operations = new List <IRecovableOperation>();
            List <Vector2Int>          way        = new List <Vector2Int>();
            int finalCost = 0;

            Logger.Instance.PrintInfo("正在载入搜索数据...");
            try
            {
                operations = MazeIO.ReadSearchDataFromFile(resultPath,
                                                           cellObjs.ConvertAll(row => row.ConvertAll(obj => obj.GetComponent <ICellObj>())), out way, out finalCost);
            }
            catch (FileNotFoundException)
            {
                Logger.Instance.PrintError($"无法打开路径为\"{resultPath}\"的搜索文件!");
            }
            catch (Exception)
            {
                Logger.Instance.PrintError("加载搜索数据时出现未知错误!");
            }

            operations.Add(new PathDrawOperation(way, pathDrawer));
            progressMgr.LoadOperationChain(new OperationChain(operations, -1));
            pathDrawer.HidePath();
            StatusBar.Instance.SetFinalCost(finalCost);
            Logger.Instance.PrintInfo("迷宫数据已加载");
        }
Beispiel #3
0
        public void GetLabirintSolutionTest()
        {
            //arrange
            var labirintPath = @"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\GetMazeSolutionTextNew.txt";
            var io           = new MazeIO();

            io.ReadMazeFromFileTaskAsync(labirintPath);
            var map        = io.CreateMazeMatrix();
            var startPlace = map.StartCellPosition;
            var exitPlace  = map.ExitCellPosition;

            var expectedSolution = new List <MazeCell>
            {
                new MazeCell(1, 1),
                new MazeCell(2, 1),
                new MazeCell(3, 1),
                new MazeCell(4, 1),
                new MazeCell(5, 1),
                new MazeCell(5, 2),
                new MazeCell(6, 2),
                new MazeCell(7, 2),
                new MazeCell(7, 1),
            };

            //act
            var solver = new MazePathFinder(map);

            var actualSolution = solver.GetCellsPath(startPlace, exitPlace);

            //assert
            CollectionAssert.AreEqual(expectedSolution, actualSolution, new MazeCellComparer(),
                                      $"\nExpected:{expectedSolution.Count}\nActual:{actualSolution.Count}\n");
        }
        public void LoadLabirint_NoLinesInFile()
        {
            //arrange
            var labirintPath = @"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\empty_maze.txt";

            var expectedSolution = "Файл не содержит ни одной строки";
            //act
            var io   = new MazeIO();
            var task = io.ReadMazeFromFileTaskAsync(labirintPath);

            //assert
            Assert.AreEqual(expectedSolution, task.Exception.InnerException.Message);
        }
        private async Task InfluteMazeControl(string dialogFilePath)
        {
            MazeIO = new MazeIO();
            await MazeIO.ReadMazeFromFileTaskAsync(dialogFilePath);

            Maze = MazeIO.CreateMazeMatrix();
            var finder    = new MazePathFinder(Maze);
            var startCell = Maze.StartCellPosition;
            var exitCell  = Maze.ExitCellPosition;

            SolutionList      = finder.GetCellsPath(startCell, exitCell);
            StartCellPosition = startCell;
            ExitCellPosition  = exitCell;
        }
Beispiel #6
0
        public void GetLabirintSolutionTest_NoSolution()
        {
            //arrange
            var labirintPath = @"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\solver_test_no_solution.txt";
            var io           = new MazeIO();

            io.ReadMazeFromFileTaskAsync(labirintPath);
            var map        = io.CreateMazeMatrix();
            var startPlace = map.StartCellPosition;
            var exitPlace  = map.ExitCellPosition;

            var expectedSolution = new List <MazeCell>();
            //act
            var solver = new MazePathFinder(map);

            //assert
            Assert.ThrowsException <SolutionNotExistException>(() => solver.GetCellsPath(startPlace, exitPlace));
        }
        //[DataRow(@"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\labirint5.txt", false)]
        //[DataRow(@"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\output.txt", true)]
        //[DataRow(@"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\test_outOfRange.txt", false)]
        #endregion
        public void RunSolutionTest_Solution_true(string labirintFilePath, bool correct)
        {
            //arrange
            var expectedPassed = correct;
            var io             = new MazeIO();

            io.ReadMazeFromFileTaskAsync(labirintFilePath);
            var map        = io.CreateMazeMatrix();
            var startPlace = map.StartCellPosition;
            var exitPlace  = map.ExitCellPosition;
            //act
            var solver       = new MazePathFinder(map);
            var tester       = new MazePathSolutionTester(map, startPlace, exitPlace);
            var solution     = solver.GetCellsPath(startPlace, exitPlace);
            var actualPassed = tester.RunSolutionTest(solution);

            //assert
            Assert.AreEqual(expectedPassed, actualPassed);
        }
Beispiel #8
0
        /// <summary>
        /// 加载迷宫, 失败则返回false
        /// </summary>
        /// <returns></returns>
        public bool LoadMaze()
        {
            List <List <MazeState> > maze = new List <List <MazeState> >();

            try
            {
                maze = MazeIO.ReadFromFile(mazePath, out exitPos);
            }
            catch (FileNotFoundException)
            {
                Logger.Instance.PrintError($"无法打开路径为\"{mazePath}\"的迷宫文件!");
            }
            catch (Exception e)
            {
                Logger.Instance.PrintError($"加载迷宫时发生未知错误:{e}");
            }

            // return if empty
            if (maze.Count < 1 || maze[0].Count < 1)
            {
                return(false);
            }
            // convert all to cellObjs;
            cellObjs = new List <List <GameObject> >();
            for (var i = 0; i < maze.Count; i++)
            {
                cellObjs.Add(new List <GameObject>());
                for (var j = 0; j < maze[0].Count; j++)
                {
                    cellObjs[i].Add(cellFactory.GetCellObj(new Vector2Int(i, j), maze[i][j]));
                    cellObjs[i][j].transform.SetParent(transform);
                    var icell = cellObjs[i][j].GetComponent <ICellObj>();
                    icell.Init();
                }
            }

            size = new Vector3(maze.Count, 0, maze[0].Count);
            // transform.position = -size / 2;

            // merge all walls
            StartCoroutine(MergeMesh());
            return(true);
        }
Beispiel #9
0
        public void GetLabirintSolutionTest_Source_equal_Destination_place()
        {
            //arrange
            var labirintPath = @"C:\Users\ia_no\Source\Repos\CodeTechnologyLabs_course3\MazeOperations.Tests\TestInput\labirintD.txt";
            var io           = new MazeIO();

            io.ReadMazeFromFileTaskAsync(labirintPath);
            var map              = io.CreateMazeMatrix();
            var startPlace       = map.StartCellPosition;
            var expectedSolution = "Точка начала совпадает с точкой выхода";

            //act
            var solver = new MazePathFinder(map);
            var ex     = Assert.ThrowsException <StartEqualsFinishException>(
                () => solver.GetCellsPath(startPlace, startPlace));

            //assert
            Assert.AreEqual(expectedSolution, ex.Message);
        }
        private bool Run()
        {
            MazeInputOutput = new MazeIO();
            MazeInputOutput.ReadMazeFromFileTaskAsync(_mazeMapFilePath);
            try
            {
                Maze       = MazeInputOutput.CreateMazeMatrix();
                StartPlace = Maze.StartCellPosition;
                ExitPlace  = Maze.ExitCellPosition;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Tester = new MazePathSolutionTester(Maze, StartPlace, ExitPlace);

            Finder = new MazePathFinder(Maze);

            Solution = Finder.GetCellsPath(StartPlace, ExitPlace);

            if (Tester.RunSolutionTest(Solution))
            {
                Console.OutputEncoding = System.Text.Encoding.Unicode;
                PrintMazeMap(Maze.Height, Maze.Width, Maze.MazeCells);
                for (var i = 0; i < Solution.Count - 1; i++)
                {
                    PrintSolutionPath(Solution[i], Solution[i + 1]);
                }
                PrintStartFinishLabels(StartPlace, ExitPlace);
            }
            else
            {
                return(false);
            }
            return(true);
        }