/// <summary> /// Place overload using a Position type argument. /// </summary> /// <param name="id"></param> /// <param name="placePosition"></param> public void Place(Position placePosition) { //Validate the coordinate values of the place position againt the playground constraints. IValidator positionValidator = new Validator().Validate(placePosition, Playground); if (positionValidator.IsValid) { //Check if exist an active robot. PLACE a new robot or USE command enable a robot. if (ActiveRobot != 0) { //Place a existing robot if it is not placed because has been removed. if (!RobotParking[(int)ActiveRobot].IsPlaced) { RobotParking[(int)ActiveRobot].Place(placePosition); } else { Console.WriteLine("Currently active robot is placed, try USE comamnd to reset the active robot or USE [Id] to place a removed robot."); } } else { //First check if the max robots limit is reached before create a new robot. if (RobotParking.Count <= (MaxRobots - 1)) { var newId = RobotCounter; var newRobot = new Robot() { Id = newId, IsActivated = true }; //Place a new robot on the playground. if (newRobot.Place(placePosition)) { ActiveRobot = newId; } RobotParking.Add(newId, newRobot); RobotCounter++; } else { Console.WriteLine($"The limit of robots existing simultaneously was reached. The limit is {MaxRobots}"); } } } else { Console.WriteLine("The next position of the robot is not valid."); Console.WriteLine("Error summary:"); foreach (var error in positionValidator.Errors) { Console.WriteLine($"({error.Number}):{error.Message}"); } } }
/// <summary> /// Revert a specific robot to the last position. /// </summary> /// <param name="id"></param> public void Undo(int id) { if (RobotParking.ContainsKey(id)) { RobotParking[id].Undo(); } else { // Provided Id not exist in the roboparking dictionary. Console.WriteLine("A robot with that Id does not exist."); } }
/// <summary> /// Remove the active robot from the robot parking. /// </summary> public void Destroy() { if (ActiveRobot != 0) { //Remove the robot from the parking and reset the active robot. RobotParking.Remove(ActiveRobot); ActiveRobot = 0; } else { // There's not an active robot. Console.WriteLine("There's no active robot. Try USE [Id] to select a robot."); } }
/// <summary> /// Move a specific robot to the face direction by id. /// </summary> /// <param name="id"></param> /// <returns></returns> public void Move(int id) { if (RobotParking.ContainsKey(id)) { var predictedPosition = new Position(); predictedPosition.X = RobotParking[id].CurrentPosition.X; predictedPosition.Y = RobotParking[id].CurrentPosition.Y; predictedPosition.FaceTo = RobotParking[id].CurrentPosition.FaceTo; // Predicted new position. switch (predictedPosition.FaceTo) { case OrientationEnum.NORTH: predictedPosition.Y += StepSize; break; case OrientationEnum.SOUTH: predictedPosition.Y -= StepSize; break; case OrientationEnum.EAST: predictedPosition.X += StepSize; break; case OrientationEnum.WEST: predictedPosition.X -= StepSize; break; } // Check if predicted position is valid according playground contraints. var positionValidator = new Validator().Validate(predictedPosition, Playground); if (positionValidator.IsValid) { // Do a real movement. RobotParking[id].Move(StepSize); } else { Console.WriteLine("The next position of the robot is not valid."); Console.WriteLine("Error summary:"); foreach (var error in positionValidator.Errors) { Console.WriteLine($"({error.Number}):{error.Message}"); } } } else { // Provided Id not exist in the roboparking dictionary. Console.WriteLine("A robot with that Id does not exist."); } }
/// <summary> /// Show the position of a specific robot by its id. /// </summary> /// <param name="id"></param> /// <returns></returns> public void Report(int id) { if (RobotParking.ContainsKey(id)) { var position = RobotParking[id].Report(); Console.WriteLine(""); Console.Write($" Position ({id}): {position.X},{position.Y}, {Enum.GetName(typeof(OrientationEnum), position.FaceTo)}"); Console.WriteLine(""); } else { // Provided Id not exist in the roboparking dictionary. Console.WriteLine("A robot with that Id does not exist."); } }
/// <summary> /// Remove a specific robot from the robot parking by its id. /// </summary> /// <param name="id"></param> public void Destroy(int id) { if (RobotParking.ContainsKey(id)) { //Remove the robot from the parking. RobotParking.Remove(id); //if the id is the current active robot, reset the active robot. If not remains the current value. if (ActiveRobot == id) { ActiveRobot = 0; } } else { // Provided Id not exist in the roboparking dictionary. Console.WriteLine("A robot with that Id does not exist."); } }
/// <summary> /// Show the log of a specific robot. /// </summary> /// <param name="id"></param> public void ShowLog(int id) { if (RobotParking.ContainsKey(id)) { Console.WriteLine(""); Console.WriteLine(" Log Summary Robot[{id}]:"); Console.WriteLine(" ------------------------------------------------------------------"); foreach (var entry in RobotParking[id].Log) { Console.WriteLine($" {entry.Time}:{entry.Position.X}, {entry.Position.Y}, {Enum.GetName(typeof(OrientationEnum), entry.Position.FaceTo)}"); } Console.WriteLine(" ------------------------------------------------------------------"); Console.WriteLine(""); } else { // Provided Id not exist in the roboparking dictionary. Console.WriteLine("A robot with that Id does not exist."); } }