private IEnumerable <Rover> MoveRovers(List <Rover> rovers, PlateauSize plateauSize) { foreach (var rover in rovers) { List <TurningDirections> directions = new List <TurningDirections>(Util.BuildMovements(rover.Movements)); foreach (var turnToDirection in directions) { if (turnToDirection == TurningDirections.L) { rover.direction = TurnLeft(rover.direction, turnToDirection); } else if (turnToDirection == TurningDirections.R) { rover.direction = TurnRight(rover.direction, turnToDirection); } else if (turnToDirection == TurningDirections.M) { rover.Location = MoveForward(rover.direction, rover.Location, plateauSize); } } yield return(rover); } }
public void BuildRover_ReturnsNotNull() { var plateauSize = new PlateauSize(5, 5); var rovers = new List <Rover>(); //arange _iMoveRover.Stub(o => o.Move(Arg <List <Rover> > .Is.Anything, Arg <PlateauSize> .Is.Anything)).Return(new List <Rover>()); //act var result = _iMoveRover.Move(rovers, plateauSize); //assert Assert.IsNotNull(result); }
static void Main(string[] args) { IBuildRover iBUildRover = new BuildRover(); IMoveRover iMoveRover = new MoveRover(); IBuildCoordinates iBuildCoordinates = new BuildCoordinates(); var plateauSize = new PlateauSize(4, 4); //Create coordinates for the first rover var roverLocations = new Locations(1, 2); var cardinalDirection = CardinalDirections.N; var movements = "LMLMLMLMM"; //Create coordinates for the second rover var rover2Locations = new Locations(3, 3); var cardinalDirection2 = CardinalDirections.E; var movements2 = "MMRMMRMRRM"; //Build first rover var coordinates = iBuildCoordinates.Build(roverLocations, cardinalDirection); var startingRover = iBUildRover.Build(coordinates, movements); startingRover.Id = 1; //Build second rover var coordinates2 = iBuildCoordinates.Build(rover2Locations, cardinalDirection2); var startingRover2 = iBUildRover.Build(coordinates2, movements2); startingRover2.Id = 2; var startingRovers = new List <Rover>() { startingRover, startingRover2 }; //Move rovers var arrivedRovers = iMoveRover.Move(startingRovers, plateauSize); foreach (var rover in arrivedRovers) { Console.WriteLine(string.Format("Rover {0} final position: ", rover.Id) + rover.Location.X.ToString() + rover.Location.Y.ToString() + rover.direction); } Console.ReadKey(); }
public void InitializeRovers(string initMessage) { var sections = initMessage.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); var plateauSizeSubsections = sections[0].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); PlateauSize plateauSize; if (uint.TryParse(plateauSizeSubsections[0], out uint byLatitude) && uint.TryParse(plateauSizeSubsections[1], out uint byLongtitude)) { plateauSize = new PlateauSize(byLatitude, byLongtitude); } else { throw new ArgumentException("Plateau size message is incorrectly formed!"); } if (Math.DivRem(sections.Length - 1, 2, out int roversCount) < 0) { if (roversCount != _rovers.Length) { throw new ArgumentException("Incorrectly formed init command: rovers count does not match!"); } } else { throw new ArgumentException("Incorrectly formed init command: cannot count the rovers"); } var roverCounter = 0; for (int i = 1; i < sections.Length; i += 2) { var rover = _rovers[roverCounter]; // TODO: Command handlers } }
private Locations MoveForward(CardinalDirections roverDirection, Locations roverLocation, PlateauSize plateauSize) { if (roverDirection == CardinalDirections.N && roverLocation.Y <= plateauSize.UpperLocations.Y) { roverLocation.Y++; } else if (roverDirection == CardinalDirections.E && roverLocation.X <= plateauSize.UpperLocations.X) { roverLocation.X++; } else if (roverDirection == CardinalDirections.S && roverLocation.Y >= plateauSize.LowerLocations.Y) { roverLocation.Y--; } else if (roverDirection == CardinalDirections.W && roverLocation.X >= plateauSize.LowerLocations.X) { roverLocation.X--; } return(roverLocation); }
public List <Rover> Move(List <Rover> rovers, PlateauSize plateauSize) { return(new List <Rover>(MoveRovers(rovers, plateauSize))); }