public JsonResult Process(SettingsModel data) { List <MarsRover> marsRoverResults = new List <MarsRover>(); // Iterate all of the Rovers from the payload foreach (var item in data.MarsRovers) { // Create a new instance of the MarsRover based on the view model MarsRover marsRover = new MarsRover(item, data.Grid); // Filter the command list var instructions = Regex.Replace(item.Instructions, "/[^LRM]+/gi", "").ToCharArray(); // Iterate each command in the command list foreach (char instruction in instructions) { // Perform the command and stop iteration if Rover gets out of the bounds if (!marsRover.PerformInstruction(instruction)) { break; } } // Add the Rover to result list marsRoverResults.Add(marsRover); } return(Json(marsRoverResults)); }
public void MarsRover_ProcessInstructionsTest(int limitX, int limitY, int x, int y, char z, string instructions, int expectedX, int expectedY, char expectedZ, bool expectedStatus) { // Initialize instance of MarsRover to test MarsRover marsRover = new MarsRover { InitialPosition = new Position { X = x, Y = y, Z = z }, LastPosition = new Position { X = x, Y = y, Z = z }, Grid = new Grid { LimitX = limitX, LimitY = limitY }, Instructions = instructions, Status = true }; // Iterate through command list foreach (var instruction in instructions.ToCharArray()) { // Perform command if (!marsRover.PerformInstruction(instruction)) { break; } } // Verify that the last X position is equals to the expected result bool lastXequalsToExpected = marsRover.LastPosition.X == expectedX; // Verify that the last Y position is equals to the expected result bool lastYequalsToExpected = marsRover.LastPosition.Y == expectedY; // Verify that the last Z orientation is equals to the expected result bool lastZequalsToExpected = marsRover.LastPosition.Z == expectedZ; // Verify that full position is equal to expected bool lastPositionEqualsToExpected = lastXequalsToExpected && lastYequalsToExpected && lastZequalsToExpected; // Assert: Initial position must be the same as expected Assert.True(lastPositionEqualsToExpected, "Mars Rover's last position does not correspond to expected position"); // Assert: Status should be the same as expected Assert.True(marsRover.Status == expectedStatus, "Mars Rover's status does not correspond to expected status"); }