public static bool TryMoveForward(Coordinate plateauCoordinate, Position roverPosition, out Coordinate nextCoordinate) { Coordinate calculatedCoordinate = roverPosition.Coordinate; switch (roverPosition.CompassDirection) { case CompassDirection.North: calculatedCoordinate.Y = calculatedCoordinate.Y + 1; break; case CompassDirection.West: calculatedCoordinate.X = calculatedCoordinate.X - 1; break; case CompassDirection.South: calculatedCoordinate.Y = calculatedCoordinate.Y - 1; break; case CompassDirection.East: calculatedCoordinate.X = calculatedCoordinate.X + 1; break; default: throw new ArgumentException(); } bool isValidCoordinate = CoordinateHelper.IsValidCoordinate(plateauCoordinate, calculatedCoordinate); nextCoordinate = isValidCoordinate ? calculatedCoordinate : new Coordinate(); return(isValidCoordinate); }
public static bool IsValidInitialRover(Coordinate plateauCoordinate, string command) { // Valid initial rover position command format must be "{0} {1} {2}" return(command.Length == 5 && command[1] == ' ' && command[3] == ' ' && int.TryParse(command[0].ToString(), out int roverPositionCoordinateX) && int.TryParse(command[2].ToString(), out int roverPositionCoordinateY) && CoordinateHelper.IsValidCoordinate(plateauCoordinate , new Coordinate { X = roverPositionCoordinateX, Y = roverPositionCoordinateY }) && IsValidCompass(command[4].ToString())); }