Пример #1
0
        public ActionResult <MazeVisualizerDTO> VisualizeMazeSolution(Guid userId, string algorithm, [FromBody] MazeFE maze)
        {
            _logger.LogInformation("GET request for {0} maze visualizer from user with id {1}\n\n", algorithm, userId.ToString());

            try
            {
                var accessToken = Request.Headers["Bearer"];
                var payload     = Authorize(accessToken);
            }
            catch (ApiException e)
            {
                return(Unauthorized(new UnauthorizedError(e.Message)));
            }

            try
            {
                MazeVisualizerDTO mazeVisualizerData = _mazeService.Visualize(maze, algorithm.ToUpper());
                return(Ok(mazeVisualizerData));
            }
            catch (ApiException e)
            {
                if (e.StatusCode == 400)
                {
                    return(BadRequest(new BadRequestError(e.Message)));
                }

                return(NotFound(new NotFoundError(e.Message)));
            }
        }
Пример #2
0
        public MazeVisualizerDTO Visualize(MazeFE mazeFE)
        {
            if (mazeFE.PointList.Count() != mazeFE.Width * mazeFE.Height)
            {
                throw new ApiException(400, "Invalid maze. Width and height don't match the number of points given");
            }

            if (mazeFE.PointList.Count(p => p.Value == 1) != 1)
            {
                throw new ApiException(400, "Invalid maze. No start point given");
            }

            if (mazeFE.PointList.Count(p => p.Value == 2) != 1)
            {
                throw new ApiException(400, "Invalid maze. No end point given");
            }

            Point startPoint = new Point();
            Point endPoint   = new Point();

            // generate matrix from mazeFE
            int[,] matrix = new int[mazeFE.Height, mazeFE.Width];
            foreach (Point p in mazeFE.PointList)
            {
                matrix[p.I, p.J] = 0;

                if (p.Value == 1)
                {
                    startPoint.I     = p.I;
                    startPoint.J     = p.J;
                    startPoint.Value = 1;
                    matrix[p.I, p.J] = 1;
                }
                else if (p.Value == 2)
                {
                    endPoint.I = p.I;
                    endPoint.J = p.J;
                }
                else if (p.Value == 3)
                {
                    matrix[p.I, p.J] = -1;
                }
            }

            if ((startPoint.I == endPoint.I && System.Math.Abs(startPoint.J - endPoint.J) == 1) ||
                (startPoint.J == endPoint.J && System.Math.Abs(startPoint.I - endPoint.I) == 1))
            {
                throw new ApiException(400, "Invalid maze. Start point right next to end point");
            }

            MazeVisualizerDTO solution = Solve(matrix, startPoint, endPoint, mazeFE.Width, mazeFE.Height);

            if (!solution.Solution.Any())
            {
                throw new ApiException(400, "Invalid maze. No solution found");
            }

            return(solution);
        }
Пример #3
0
        public string ValidateMaze(MazeFE mazeFE)
        {
            if (mazeFE.PointList.Count() != mazeFE.Width * mazeFE.Height)
            {
                throw new ApiException(400, "Invalid maze. Width and height don't match the number of points given");
            }

            if (mazeFE.PointList.Count(p => p.Value == 1) != 1)
            {
                throw new ApiException(400, "Invalid maze. No start point given");
            }

            if (mazeFE.PointList.Count(p => p.Value == 2) != 1)
            {
                throw new ApiException(400, "Invalid maze. No end point given");
            }

            Point startPoint = new Point();
            Point endPoint   = new Point();


            int[,] matrix = new int[mazeFE.Height, mazeFE.Width];
            foreach (Point p in mazeFE.PointList)
            {
                matrix[p.I, p.J] = 0;

                if (p.Value == 1)
                {
                    startPoint.I     = p.I;
                    startPoint.J     = p.J;
                    startPoint.Value = 1;
                    matrix[p.I, p.J] = 1;
                }
                else if (p.Value == 2)
                {
                    endPoint.I = p.I;
                    endPoint.J = p.J;
                }
                else if (p.Value == 3)
                {
                    matrix[p.I, p.J] = -1;
                }
            }

            if ((startPoint.I == endPoint.I && System.Math.Abs(startPoint.J - endPoint.J) == 1) ||
                (startPoint.J == endPoint.J && System.Math.Abs(startPoint.I - endPoint.I) == 1))
            {
                throw new ApiException(400, "Invalid maze. Start point right next to end point");
            }

            MazeVisualizerDTO solution = Solve(matrix, startPoint, endPoint, mazeFE.Width, mazeFE.Height);

            if (!solution.Solution.Any())
            {
                throw new ApiException(400, "Invalid maze. No solution found");
            }

            StringBuilder retSolution = new StringBuilder(new string('0', mazeFE.Width * mazeFE.Height));

            foreach (Point p in solution.Solution)
            {
                retSolution.Remove(p.I * mazeFE.Width + p.J, 1);
                retSolution.Insert(p.I * mazeFE.Width + p.J, '1');
            }

            return(retSolution.ToString());
        }