示例#1
0
文件: Robot.cs 项目: CarlosVV/amcro
        /// <summary>
        ///  Main Method to be used by passing the complete structure request with instructions
        /// </summary>
        /// <param name="request">Request class containing the Map, Start, Commands and Battery Status</param>
        /// <returns></returns>
        public CleaningResult Run(CleaningRequest request)
        {
            if (IsValidRequest(request))
            {
                return(null);
            }

            var result = new CleaningResult();

            Map     = request.Map;
            Battery = new Battery {
                Status = request.Battery
            };
            CurrentCoordinate  = request.Start;
            PreviousCoordinate = CurrentCoordinate;
            CurrentFacing      = (Facing)Enum.Parse(typeof(Facing), request.Start.Facing);
            Visited.Add(new Coordinate {
                X = CurrentCoordinate.X, Y = CurrentCoordinate.Y
            });

            foreach (var command in request.Commands)
            {
                CurrentRobotAction = (RobotAction)Enum.Parse(typeof(RobotAction), command);
                ExecuteCommand();

                if (IsObstacle())
                {
                    CurrentCoordinate = PreviousCoordinate;
                    IsRunningObstacleBackOffStrategy = true;
                    var alternativeIndex = 0;
                    do
                    {
                        var altCommands = AlternativeActions[alternativeIndex];
                        foreach (var altCommand in altCommands)
                        {
                            CurrentRobotAction = (RobotAction)Enum.Parse(typeof(RobotAction), altCommand);
                            ExecuteCommand();
                        }

                        if (!IsObstacle())
                        {
                            break;
                        }
                        else
                        {
                            CurrentCoordinate = PreviousCoordinate;
                            IsRunningObstacleBackOffStrategy = true;
                        }

                        alternativeIndex++;
                    }while (IsRunningObstacleBackOffStrategy && alternativeIndex < AlternativeActions.GetLength(0));

                    IsRunningObstacleBackOffStrategy = false;
                }
            }

            result.Visited = Visited.OrderBy(m => m.X).ThenBy(m => m.Y).ToList();
            result.Cleaned = Cleaned.OrderBy(m => m.X).ThenBy(m => m.Y).ToList();
            result.Final   = new Position {
                X = CurrentCoordinate.X, Y = CurrentCoordinate.Y, Facing = CurrentFacing.ToString()
            };
            result.Battery = Battery.Status;

            return(result);
        }
        public Result Run(Request request)
        {
            if (IsValidRequest(request))
            {
                return(null);
            }

            Init();
            var result = new Result();

            Map     = request.Map;
            Battery = new Battery {
                Status = request.Battery
            };
            CurrentCoordinate  = request.Start;
            PreviousCoordinate = CurrentCoordinate;
            CurrentFacing      = (Facing)Enum.Parse(typeof(Facing), request.Start.Facing);
            Visited.Add(new Coordinate {
                X = CurrentCoordinate.X, Y = CurrentCoordinate.Y
            });

            for (int i = 0; i < request.Commands.Length; i++)
            {
                CurrentAction = (Action)Enum.Parse(typeof(Action), request.Commands[i]);
                ExecCommand();

                if (IsObstacle())
                {
                    CurrentCoordinate        = PreviousCoordinate;
                    IsRunningBackOffStrategy = true;
                    for (int j = 0; j < AlternativeActions.Path.Length; j++)
                    {
                        if (IsRunningBackOffStrategy)
                        {
                            for (int k = 0; k < AlternativeActions.Path[j].Length; k++)
                            {
                                CurrentAction = (Action)Enum.Parse(typeof(Action), AlternativeActions.Path[j][k]);
                                ExecCommand();
                            }

                            if (!IsObstacle())
                            {
                                IsRunningBackOffStrategy = false;
                                break;
                            }
                            else
                            {
                                CurrentCoordinate        = PreviousCoordinate;
                                IsRunningBackOffStrategy = true;
                            }
                        }
                    }
                }
            }

            result.Visited = Visited.OrderBy(m => m.X).ThenBy(m => m.Y).ToList();
            result.Cleaned = Cleaned.OrderBy(m => m.X).ThenBy(m => m.Y).ToList();
            result.Final   = new Position {
                X = CurrentCoordinate.X, Y = CurrentCoordinate.Y, Facing = CurrentFacing.ToString()
            };
            result.Battery = Battery.Status;

            return(result);
        }