Пример #1
0
 private bool IsValidRequest(CleaningRequest request)
 {
     return
         (request == null || request.Battery == 0 ||
          request.Commands == null || request.Commands.Length == 0 ||
          request.Commands.Any(m => string.IsNullOrWhiteSpace(m)) ||
          !request.Commands.Any(m => (new string[] { "TR", "TL", "A", "B", "C" }).Contains(m)) ||
          request.Map == null || request.Start == null);
 }
Пример #2
0
        /// <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);
        }