/// <summary> /// Executes one command /// </summary> /// <param name="command"></param> /// <returns>If the command doesn't fail</returns> private bool Move(Commands command) { switch (command) { case Commands.A: /* Check if we have to increase/decrease X or Y coordinate. */ int x = this.CurrentCardinalPoint == CardinalPoint.West ? -1 : this.CurrentCardinalPoint == CardinalPoint.East ? 1 : 0; int y = this.CurrentCardinalPoint == CardinalPoint.South ? -1 : this.CurrentCardinalPoint == CardinalPoint.North ? 1 : 0; Coordinates toAdd = new Coordinates(x, y); Coordinates newCoordinates = this.CurrentCoordinates.Add(toAdd); if (this.squareSize.IsWithinBounds(newCoordinates)) { this.CurrentCoordinates = newCoordinates; return(true); } return(false); case Commands.L: this.CurrentCardinalPoint = this.CurrentCardinalPoint.Rotate(CardinalPoint.Rotation.CounterClockwise); return(true); case Commands.R: this.CurrentCardinalPoint = this.CurrentCardinalPoint.Rotate(CardinalPoint.Rotation.Clockwise); return(true); default: return(false); } }
public static CardinalPoint ParseCardinalPoint(string cardinalPointLine) { if (!CardinalPoint.TryParse(cardinalPointLine, out CardinalPoint parsed)) { throw new ArgumentException($"The orientation must be one of the following values ({string.Join(", ", CardinalPoint.All.Select(cp => cp.ToString()))})!"); } return(parsed); }
public RoverController(Size squareSize, Coordinates initialCoordinates, CardinalPoint initialOrientation) { if (squareSize.IsEmpty) { throw new ArgumentOutOfRangeException(nameof(squareSize), "Can't be 0, 0"); } if (initialCoordinates.X < 0 || initialCoordinates.Y < 0) { throw new ArgumentOutOfRangeException(nameof(initialCoordinates), "Should be 0 or positive."); } this.squareSize = squareSize; this.CurrentCoordinates = initialCoordinates; this.CurrentCardinalPoint = initialOrientation; }
public static void Main() { Console.WriteLine("Welcome to the Rover Controller!"); try { /* Ask for the initial values */ string sizeLine = ReadLine("Enter the size of the square (Width Height):"); Size size = ParseSize(sizeLine); string coordinatesLine = ReadLine("Enter the initial coordinates (X, Y):"); Coordinates coordinates = ParseCoordinates(coordinatesLine); string cardinalPointLine = ReadLine($"Enter the initial orientation ({string.Join(", ", CardinalPoint.All.Select(cp => cp.ToString()))}):"); CardinalPoint cardinalPoint = ParseCardinalPoint(cardinalPointLine); RoverController controller = new RoverController(size, coordinates, cardinalPoint); /* Ask for commands until empty line. */ string commandLine = null; string commandLineMessage = $"Enter one or more commands without any separation ({string.Join(", ", Enum.GetValues<Commands>())}):"; while (!string.IsNullOrEmpty(commandLine = ReadLine(commandLineMessage))) { Commands[] commands = ParseCommands(commandLine); if (!controller.Handle(commands)) { Console.WriteLine($"False, {controller.CurrentCardinalPoint}, {controller.CurrentCoordinates}."); return; } } Console.WriteLine($"True, {controller.CurrentCardinalPoint}, {controller.CurrentCoordinates}."); } catch (Exception ex) { Console.WriteLine(ex.Message); } }