private ErrorCode?Move(Coordinant bounds, IEnumerable <BaseRover> rovers) { var previousPosition = Position.Clone(); var predictedPosition = Position.Clone(); //Predict Movement switch (Heading) { case Directionality.N: predictedPosition.yPos++; break; case Directionality.S: predictedPosition.yPos--; break; case Directionality.E: predictedPosition.xPos++; break; case Directionality.W: predictedPosition.xPos--; break; } //Validate Boundary if (predictedPosition.xPos > bounds.xPos) { return(ErrorCode.Eastern_Boundary); } if (predictedPosition.xPos < 0) { return(ErrorCode.Western_Boundary); } if (predictedPosition.yPos > bounds.yPos) { return(ErrorCode.Northern_Boundary); } if (predictedPosition.yPos < 0) { return(ErrorCode.Southern_Boundary); } //Validate Objects if (rovers.Where(z => z != this).Any(r => r.Position.Equals(predictedPosition))) { return(ErrorCode.Unexpected_Drone); } Position = predictedPosition; //Impossible, future proofing if (Position == previousPosition) { return(ErrorCode.Unknown_Movement_Failure); } return(null); }
public override void ConductMission(IEnumerable <BaseRover> roverNetwork, Coordinant bounds) { for (var i = 0; i < this.Mission.Length; i++) { var result = DoCommand(this.Mission[i], bounds, roverNetwork); if (result.HasValue) { this.Error = result; } } }
private void BoundaryTest(char heading, ErrorCode ec) { var rover = new PessimisticRover(new ParsedRoverCommand($"1 1 {heading}", "MMM")); var rovers = new List <BaseRover>(); var bounds = new Coordinant(2, 2); ErrorCode?result = rover.DoCommand('M', bounds, rovers); Assert.IsNull(result); result = rover.DoCommand('M', bounds, rovers); Assert.IsTrue(result.HasValue); Assert.IsTrue(result.Value == ec); }
public void TestRoverCollision() { var rover = new PessimisticRover(new ParsedRoverCommand("1 1 N", "MMM")); var rovers = new List <BaseRover>() { new PessimisticRover(new ParsedRoverCommand("1 3 N", "M")) }; var bounds = new Coordinant(3, 3); ErrorCode?result = rover.DoCommand('M', bounds, rovers); Assert.IsNull(result); result = rover.DoCommand('M', bounds, rovers); Assert.IsTrue(result.HasValue); Assert.IsTrue(result.Value == ErrorCode.Unexpected_Drone); }
public bool TryProcessBounds(int width, int height, ref Coordinant coord) { if (coord.WrapX(width) && _wrapping) { return(false); } if (coord.WrapY(height) && _wrapping) { return(false); } return(true); }
public ErrorCode?DoCommand(char c, Coordinant bounds, IEnumerable <BaseRover> rovers) { var previousPosition = Position; switch (c) { case LEFT: return(TurnLeft()); case RIGHT: return(TurnRight()); case MOVE: return(Move(bounds, rovers)); default: return(ErrorCode.Command_Parse); } }
public RoverMissionController(IMissionCommand mission) { RoverNetwork = new List <BaseRover>(); MissionBounds = mission.Bounds; var debug = (mission.MissionType & MissionType.Debug) > 0; var optimistic = (mission.MissionType & MissionType.Optimistic) > 0; foreach (var roverCommand in mission.RoverMissions) { BaseRover newRover; if (optimistic) { newRover = new OptimisticRover(roverCommand, debug); } else { newRover = new PessimisticRover(roverCommand, debug); } RoverNetwork.Add(newRover); } }
public List <Coordinant> GetNeighborCoords(Coordinant origin) { var points = new List <Coordinant>(); for (var step = 1; step <= Distance; step++) { points.AddRange(new List <Coordinant>() { new Coordinant(origin.X - step, origin.Y), new Coordinant(origin.X + step, origin.Y), new Coordinant(origin.X, origin.Y - step), new Coordinant(origin.X, origin.Y + step) }); if (_diagonals) { points.AddRange(GetDiagonals(step)); } } return(points); }
public override void ConductMission(IEnumerable <BaseRover> roverNetwork, Coordinant bounds) { #if DEBUG Console.WriteLine($"==BEGIN MISSION=="); #endif for (var i = 0; i < this.Mission.Length; i++) { #if DEBUG var c = this.Mission[i]; Console.WriteLine($"{c}: {Position.xPos} {Position.yPos} {Heading}"); #endif var result = DoCommand(this.Mission[i], bounds, roverNetwork); if (result.HasValue) { this.Error = result; break; } } #if DEBUG Console.WriteLine($"==END MISSION=="); #endif }
public LangtonAnt(Coordinant at, bool wrap) : base(at) { Wrap = wrap; }
abstract public void ConductMission(IEnumerable <BaseRover> roverNetwork, Coordinant bounds);