public static bool Validate(RoverInfo info, RoverMove move) { if (info == null || move == null || !info.IsSuccess || !move.IsSuccess) { return(false); } return(true); }
public Rover AddRover(RoverInfo info, RoverMove move) { if (!RoverManager.Validate(info, move)) { throw new ArgumentException("Error occurred while creating Rover, try again."); } var result = RoverManager.Insert(info, move); return(result); }
public static Rover Insert(RoverInfo info, RoverMove move) { var rover = new Rover { Info = info, Moves = move, IsSuccess = true }; return(rover); }
public static RoverInfo SetupInfo(string input) { RoverInfo roverInfo = new RoverInfo(); var inputList = input.Split(' '); roverInfo.X = inputList[0].ConvertInt(); roverInfo.Y = inputList[1].ConvertInt(); roverInfo.Direction = inputList[2].GetEnum <Compass>(); roverInfo.IsSuccess = true; return(roverInfo); }
/// <summary> /// Move and turn the rover by sending a list of commands. /// </summary> /// <param name="roverCommands">A list of commands in the form of a string (for example "FFFBM"). Valid commands are M move forward, B move backward, L turn left, R turn right</param> /// <returns>Rover info as the position, the direction and if an obstacle was found</returns> /// <remarks>Unknown commands are ignored</remarks> public RoverInfo MoveAndTurn(String roverCommands) { Int32 cont = 0; Char roverCommand; Boolean obstacleFound = false; MarsGrid.MoveRoverResponse response; var roverCommandList = roverCommands.ToUpper().ToCharArray(); if (roverCommandList.Count() > 0) { do { roverCommand = roverCommandList[cont]; switch (roverCommand) { case RoverCommand.MoveForward: response = _marsGrid.MoveRover(roverCommand, _currentRoverDirection, _currentRoverPosition); _currentRoverPosition = response.RoverPosition; obstacleFound = response.RoverHasBeenBlockedByObstacle; break; case RoverCommand.MoveBackward: response = _marsGrid.MoveRover(roverCommand, _currentRoverDirection, _currentRoverPosition); _currentRoverPosition = response.RoverPosition; obstacleFound = response.RoverHasBeenBlockedByObstacle; break; case RoverCommand.TurnRight: _currentRoverDirection = _marsGrid.TurnRoverRight(_currentRoverDirection); break; case RoverCommand.TurnLeft: _currentRoverDirection = _marsGrid.TurnRoverLeft(_currentRoverDirection); break; } cont++; } while (cont < roverCommandList.Count() && obstacleFound == false); } var roverInfo = new RoverInfo() { RoverPosition = _currentRoverPosition, RoverDirection = _currentRoverDirection, RoverHasBeenBlockedByObstacle = obstacleFound }; return(roverInfo); }
public void AddRover_Valid() { RoverInfo roverInfo = new RoverInfo() { X = 1, Y = 2, Direction = Compass.N, IsSuccess = true }; RoverMove roverMove = new RoverMove() { Moves = "LMLMLMLMM", IsSuccess = true }; var result = service.AddRover(roverInfo, roverMove); Assert.IsTrue(result.IsSuccess); }
public void AddRover_InValid() { //invalid test case, because RoverMove is not success RoverInfo roverInfo = new RoverInfo() { X = 1, Y = 2, Direction = Compass.N, IsSuccess = true }; RoverMove roverMove = new RoverMove() { Moves = "LMLMLMLMM", IsSuccess = false }; var result = service.AddRover(roverInfo, roverMove); Assert.IsTrue(result.IsSuccess); }
public static List <RoverInfo> MissionStart(MarsRoverService service, Mission mission) { Console.BackgroundColor = ConsoleColor.Black; foreach (PropertyInfo propertyInfo in mission.GetType().GetProperties()) { switch (propertyInfo.Name) { case nameof(mission.Plateau): if (mission.Plateau.IsSuccess) { break; } Console.Write("Plateau Info :"); Plateau plateau = service.AddPlateu(Console.ReadLine()); mission.Plateau = plateau; break; case nameof(mission.RoverCount): if (mission.RoverCount.IsSuccess) { break; } Console.Write("Rover Count:"); RoverCount roverCount = service.AddRoverCount(Console.ReadLine()); mission.RoverCount = roverCount; var rovers = new List <Rover>(); for (int i = 0; i < roverCount.Count; i++) { rovers.Add(new Rover()); } mission.Rovers = rovers; break; case nameof(mission.Rovers): for (int i = 0; i < mission.RoverCount.Count; i++) { if (mission.Rovers[i].IsSuccess) { break; } Console.WriteLine($"{i} - Rover Setup "); RoverInfo roverInfo = service.SetupRoverInfo(Console.ReadLine()); RoverMove roverMove = service.SetupRoverMove(Console.ReadLine()); mission.Rovers[i] = service.AddRover(roverInfo, roverMove); } Console.WriteLine($" Rover Setup End "); break; default: break; } } var result = new List <RoverInfo>(); for (int i = 0; i < mission.RoverCount.Count; i++) { var rover = mission.Rovers[i]; var serviceResult = service.StartDiscovering(mission.Plateau, rover); result.Add(serviceResult); Console.WriteLine($"Info Mission > {i} - Rover : {serviceResult.X} {serviceResult.Y} {serviceResult.Direction}, IsSuccess:{serviceResult.IsSuccess} "); if (!serviceResult.IsSuccess) { Console.WriteLine($" \t Failed Detail: { serviceResult.ErrorMessage }"); } } ; return(result); }