private static int?CreateRover(Commander commander) { Console.WriteLine($"Rover {commander.NextRoverId()} Starting Position:"); var position = Console.ReadLine().ToUpper().Split(' '); if (position.Length < 3) { PrintError(@"Position needs to be a space delimited list of two positive numbers and a direction (N, E, S, W)"); return(null); } int x, y; if (int.TryParse(position[0], out x) == false) { PrintError("X Coordinate is not valid"); return(null); } ; if (int.TryParse(position[1], out y) == false) { PrintError("Y Coordinate is not valid"); return(null); } ; if (commander.GetMap().IsInBoundary(x, y) == false) { PrintError("Position is out of map boundary"); return(null); } var direction = DirectionEnumHelper.ToEnum(position[2]); if (!direction.HasValue) { PrintError("Direction is not valid."); return(null); } var roverId = commander.CreateRover(x, y, direction.Value); if (roverId.HasValue == false) { PrintError(commander.GetLastError()); } return(roverId); }
public override bool Turn(Turns turn) { RoverState currentState = GetCurrentState(); if (currentState == null) { return(false); } var nextState = RoverState.Clone(currentState); int directionDelta = 0; switch (turn) { case Turns.Left: directionDelta = -1; break; case Turns.Right: directionDelta = 1; break; } int currentDirection = DirectionEnumHelper.ToInt(nextState.direction); int directionLength = DirectionEnumHelper.Count(); int nextDirection = (currentDirection + directionDelta + directionLength) % directionLength; nextState.direction = DirectionEnumHelper.ToEnum(nextDirection).Value; if (IsValidState(nextState) == false) { return(false); } _states.Push(nextState); return(true); }
public string Output() { return($"{x} {y} {DirectionEnumHelper.ToString(direction)}"); }