/// <summary> /// Start Rover deployment and return its position /// </summary> /// <param name="command"></param> /// <returns></returns> public PositionModel DeployRover(string command) { //Check if the command is correct Regex directionCommandPattern = new Regex("^\\d+ \\d+ [NSWE]$"); if (directionCommandPattern.IsMatch(command)) { //Split command to read individual command var splitCommand = command.Split(" "); var xPos = Convert.ToInt32(splitCommand[0]); var yPos = Convert.ToInt32(splitCommand[1]); var plateauSize = _plateauService.GetPlateau(); //throw exception if deployment is out of bound if (xPos > plateauSize.Width || yPos > plateauSize.Height || xPos < 0 || yPos < 0) { throw new Exception("Rover couldnt be deployed"); } return(new PositionModel { XPos = xPos, YPos = yPos, Direction = (Direction)Enum.Parse(typeof(Direction), splitCommand[2]) }); } throw new Exception("Invalid Deployment Command"); }
private void Move(Direction direction, ref int xPos, ref int yPos) { //Get size of plateau var plateauSize = _plateauService.GetPlateau(); //movement position doesnt respect boundary if (xPos > plateauSize.Width || yPos > plateauSize.Height) { throw ThrowException(xPos, yPos, default); } switch (direction) { case Direction.N: yPos += 1; if (yPos < 0 || yPos > plateauSize.Height) { throw ThrowException(xPos, yPos, Direction.N); } break; case Direction.E: xPos += 1; if (xPos < 0 || xPos > plateauSize.Width) { throw ThrowException(xPos, yPos, Direction.E); } break; case Direction.S: yPos -= 1; if (yPos < 0) { throw ThrowException(xPos, yPos, Direction.S); } break; case Direction.W: xPos -= 1; if (xPos < 0) { throw ThrowException(xPos, yPos, Direction.W); } break; } }
/// <summary> /// Executes all the commands /// </summary> /// <param name="cardanoCommand"></param> /// <returns></returns> public async Task ExecuteMission(string[] cardanoCommand) { //Start reading commands line by line for (int i = 1; i < cardanoCommand.Length; i++) { try { //Call deploy method for specific cordinate Console.WriteLine("Rover is ready for deployment"); var deployRover = _roverDeploymentService.DeployRover(cardanoCommand[i]); //Call movement method for specific movements Console.WriteLine("Rover deployed, moving to specified cordinates"); var position = await _roverMovementService.MoveRover(cardanoCommand[i + 1], deployRover, _plateauService.GetPlateau()); Console.WriteLine("Rover is at current position: " + position.XPos + " " + position.YPos + " " + position.Direction); Console.WriteLine("---------------------------------"); } catch (Exception ex) { //Catch exception if deployment or movement fails Console.WriteLine(ex.Message); Console.WriteLine("---------------------------------"); } finally { i++; } } }