public bool MoveForward(RoverLocation location, out ObstacleCoOrdinates obstacle) { RoverLocation newLocation = new RoverLocation(location.CurrentX, location.CurrentY, location.CurrentDirection); obstacle = new ObstacleCoOrdinates(); switch (location.CurrentDirection) { case Direction.N: newLocation.CurrentX = IncrementX(location.CurrentX); break; case Direction.S: newLocation.CurrentX = DecrementX(location.CurrentX); break; case Direction.E: newLocation.CurrentY = IncrementY(location.CurrentY); break; case Direction.W: newLocation.CurrentY = DecrementY(location.CurrentY); break; } bool obstacleAtNewLocation = CheckIfObstacleAtLocation(newLocation.CurrentX, newLocation.CurrentY); if (obstacleAtNewLocation) { obstacle.XCoOrdinate = newLocation.CurrentX; obstacle.YCoOrdinate = newLocation.CurrentY; return(true); } else { location.CurrentX = newLocation.CurrentX; location.CurrentY = newLocation.CurrentY; location.CurrentDirection = newLocation.CurrentDirection; return(false); } }
public RoverLocation MoveRover(string moveCommand, out ObstacleCoOrdinates obstacleCoOrdinates) { bool obstacleHit = false; obstacleCoOrdinates = new ObstacleCoOrdinates(); foreach (char move in moveCommand) { switch (move) { case 'F': obstacleHit = grid.MoveForward(currentPosition, out obstacleCoOrdinates); break; case 'B': obstacleHit = grid.MoveBackward(currentPosition, out obstacleCoOrdinates); break; case 'R': grid.MoveRight(currentPosition); break; case 'L': grid.MoveLeft(currentPosition); break; default: Console.WriteLine($"Move '{move}' is not a valid move."); break; } if (obstacleHit) { break; } } return(currentPosition); }