/// <summary> /// Executes the robor action. /// </summary> /// <remarks> /// Lost robots will not move. /// Move to known lost places will be ignored. /// If a robors is lost a landmark will be added. /// </remarks> public void Execute(Robot robot, IPlanetMap map) { // Troubled robots can't move if (robot.IsLost()) { return; } var nextCoords = map.GetNextCoordinates(robot.Coordinates, robot.Orientation); // If any lost landmark in current position // Get next position to check if next coord it's marked as lost // Already recorded as lost landmarks will be avoided var lostLandmarks = map.GetLostLandmarks(); var nextIsLost = lostLandmarks.TryGetValue(robot.Coordinates, out var marks) && marks.Any(m => m.LostRecordedCoords == nextCoords); if (nextIsLost) { return; } if (map.IsOutOfBounds(nextCoords)) { // If next position is out of bounds, robot will be lost // and a lost landmark should be marked at last seen coordinates robot.Troubles.Add(new LostRobotTrouble()); map.AddLandmark(robot.Coordinates, new LostLandmark(nextCoords)); } else { robot.Coordinates = nextCoords; } }
/// <summary> /// Executes the robor action /// </summary> public void Execute(Robot robot, IPlanetMap map) { // Troubled robots can't move if (robot.IsLost()) { return; } // Get current orientation // Change orientation robot.Orientation = map.TurnLeft(robot.Orientation); }
/// <summary> /// Returns lost landmarks from a mao. /// </summary> public static IDictionary <Coordinates, IEnumerable <LostLandmark> > GetLostLandmarks(this IPlanetMap map) { return(map.GetLandmarks() .Where(p => p.Value.OfType <LostLandmark>().Any()) .ToDictionary(p => p.Key, p => p.Value.OfType <LostLandmark>())); }
/// <summary> /// Creates an instance. /// </summary> public void MapPlanet(IPlanetMap map) { Map = map; }