public Position Handle(string instruction, Position currentPosition) { var xCoordinate = currentPosition.X; var yCoordinate = currentPosition.Y; var direction = currentPosition.Direction; if (instruction == "M") { if (direction == Direction.North) yCoordinate++; else if (direction == Direction.East) xCoordinate++; else if (direction == Direction.South) yCoordinate--; else if (direction == Direction.West) xCoordinate--; } else if (instruction == "L") { direction = direction.TurnLeft(); } else if (instruction == "R") { direction = direction.TurnRight(); } else { throw new ArgumentException( "Invalid instruction", "instruction"); } return new Position(xCoordinate, yCoordinate, direction); }
public void XDimensionMoveTest() { var testRover = new Rover("test1", new Position() { X = 2, Y = 2 }, OrientationEnum.W, new MockComModule(), new MockTerrain() { Grid = new IRover[5, 5] }); testRover.Move(); var expectedPosition = new Position() { X = 1, Y = 2 }; Assert.AreEqual(testRover.Position, expectedPosition); testRover.Orientation = OrientationEnum.E; testRover.Move(); expectedPosition.X = 2; Assert.AreEqual(testRover.Position, expectedPosition); }
public void DeployTo(IPlanet planet, int landingPointX, int landingPointY, Movement.CardinalHeading landingCardinalHeading) { this.DeployedTo = planet; if (landingPointX < 0 || landingPointY < 0 || landingPointX > planet.Size.Width || landingPointY > planet.Size.Height) { throw new ArgumentException("Rover cannot be deployed to a point outside the bounds of a planet."); } this.Position = new Position(landingPointX, landingPointY, landingCardinalHeading); }
public void YDimensionMoveTest() { var testRover = new Rover("test1", new Position() { X = 2, Y = 2 }, OrientationEnum.N, new MockComModule(), new MockTerrain() { Grid = new IRover[5, 5] }); //Test movement on the Y dimension testRover.Move(); var expectedPosition = new Position() { X = 2, Y = 3 }; Assert.AreEqual(testRover.Position, expectedPosition); testRover.Orientation = OrientationEnum.S; testRover.Move(); expectedPosition.Y = 2; Assert.AreEqual(testRover.Position, expectedPosition); }
public void ProcessInstructions(string instructions) { for (int i = 0; i < instructions.Length; i++ ) { var instruction = instructions[i].ToString(); var updatedPosition = _instructionHandler.Handle(instruction, Position); var isUpdatdPositionValid = _plateau.IsPositionOnPlateau(updatedPosition); if (!isUpdatdPositionValid) throw new InvalidOperationException("The rover cannot move outside of the plateau"); Position = updatedPosition; } }
public void AddObstacle(Obstacle obstacle, int atPointX, int atPointY) { if (this.Obstacles == null) { this.Obstacles = new Dictionary<Obstacle, Position>(); } if (atPointX < 0 || atPointY < 0 || atPointX > this.Size.Width || atPointY > this.Size.Height) { throw new ArgumentException("Obstacle cannot be placed outside the bounds of a planet."); } var position = new Position(atPointX, atPointY); this.Obstacles.Add(obstacle, position); }
public bool HasObstacleAtPosition(Position position) { return this.Obstacles != null && this.Obstacles.Any(o => o.Value.Point.X == position.Point.X && o.Value.Point.Y == position.Point.Y); }
public Position Move(Position currentPostion,int unitsToMove) { return currentPostion.MoveVerticaly (unitsToMove); }
public Location(Position position, Orientation orientation) { _position = position; _orientation = orientation; }
public Position Move(Position currentPostion,int unitsToMove) { return currentPostion.MoveHorizontally (unitsToMove); }
public Rover(Position initialPosition, Plateau plateau, IInstructionHandler instructionHandler) { Position = initialPosition; _plateau = plateau; _instructionHandler = instructionHandler; }