public DroneModel moveDrone(DroneModel drone, string movementInfo) { var moveCode = movementInfo.ToUpper().ToCharArray(); foreach (var move in moveCode) { switch (move) { case 'L': turnLeft(drone); break; case 'R': turnRight(drone); break; case 'M': moveForward(drone); break; default: throw new ArgumentException($"Input must contain 'L', 'R' or 'M'. User has input: '{move}'"); } } return(drone); }
public DroneModel turnRight(DroneModel drone) { var currentDirection = drone.GetDirection(); // Turn right and set updated direction switch (currentDirection) { case CardinalDirections.North: drone.SetDirection(CardinalDirections.East); break; case CardinalDirections.East: drone.SetDirection(CardinalDirections.South); break; case CardinalDirections.South: drone.SetDirection(CardinalDirections.West); break; case CardinalDirections.West: drone.SetDirection(CardinalDirections.North); break; } return(drone); }
public DroneModel setDroneStartPosition(DroneModel drone, string input) { try { var dronePlacement = input.Split(' '); if (dronePlacement.Length > 3 || dronePlacement[2].Any(x => !char.IsLetter(x))) { throw new ArgumentException("Input must be in the correct format. For example '1 2 E'"); } else { drone.xCord = Int32.Parse(dronePlacement[0]); drone.yCord = Int32.Parse(dronePlacement[1]); switch (dronePlacement[2].ToUpper()) { case "N": drone.SetDirection(CardinalDirections.North); break; case "S": drone.SetDirection(CardinalDirections.South); break; case "E": drone.SetDirection(CardinalDirections.East); break; case "W": drone.SetDirection(CardinalDirections.West); break; default: throw new ArgumentException("One of four cardinal directions (NESW) must be entered"); } return(drone); } } catch (Exception ex) { Console.WriteLine(ex); return(drone); } }
static void Main(string[] args) { Movement mapZone = new Movement(); var droneSquad = new List <DroneModel>(); var userInput = ""; while (GridModel.xBoundary == 0 && GridModel.yBoundary == 0) { Console.WriteLine("Input Coordinates for top right of battlefield"); userInput = Console.ReadLine(); mapZone.setMapBoundary(userInput); } while (userInput != "stop") { var drone = new DroneModel(); Console.WriteLine("Input starting position of drone or 'stop' to cease sending out drones"); userInput = Console.ReadLine(); if (userInput.ToLower() == "stop") { continue; } mapZone.setDroneStartPosition(drone, userInput); Console.WriteLine("Input movement information"); userInput = Console.ReadLine(); mapZone.moveDrone(drone, userInput); droneSquad.Add(drone); } Console.WriteLine("Drone final position information: "); foreach (var drone in droneSquad) { Console.WriteLine($"{drone.xCord} {drone.yCord} {drone.GetFinalDirection()}"); } Console.WriteLine("Exiting..."); userInput = Console.ReadLine(); }
public DroneModel moveForward(DroneModel drone) { var currentDirection = drone.GetDirection(); switch (currentDirection) { case CardinalDirections.North: if (GridModel.yBoundary - drone.yCord > 0) { drone.yCord++; } break; case CardinalDirections.East: if (GridModel.xBoundary - drone.xCord > 0) { drone.xCord++; } break; case CardinalDirections.South: if (drone.yCord > 0) { drone.yCord--; } break; case CardinalDirections.West: if (drone.xCord > 0) { drone.xCord--; } break; } return(drone); }