Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int      sidePlateau = ValidationInput.getValidatedNumber("Please enter side of Plateau Square(positive integer only):");
            IPlateau marsPlateau = new Plateau(sidePlateau);

            string[] directionArray = { "N", "S", "E", "W" };
            string   dir            = ValidationInput.getValidatedText("Enter the direction the rover is Headed(N,S,E,W):", "Please enter valid value for Direction(N,S,E,W)", directionArray);

            int x = ValidationInput.getValidatedNumber("Enter the Initial XLocation:", true, sidePlateau);
            int y = ValidationInput.getValidatedNumber("Enter the Initial YLocation:", true, sidePlateau);

            string[] commandArray = { "L", "R", "A" };
            string   instrSet     = ValidationInput.getValidatedText("Enter the Commands(L,R,A): ", "Please enter valid value for Commands(L,R,A)", commandArray, false);


            Direction direction = (Direction)System.Enum.Parse(typeof(Direction), dir);

            IMarsRover massRover = new MarsRover(marsPlateau, x, y, direction, instrSet);


            massRover.executeInstruction();
            massRover.showCurrentLocation();

            Console.WriteLine("\n Press any key to exit.");
            Console.ReadKey();
        }
Exemplo n.º 3
0
        public MarsRover MoveBackwards(MarsRover rover)
        {
            var newXCoordinate = rover.XCoordinate - rover.roverState.HorizontalDistance();
            var newYCoordinate = rover.YCoordinate - rover.roverState.VerticalDistance();

            return(map.MoveRoverTo(rover, newXCoordinate, newYCoordinate));
        }
Exemplo n.º 4
0
        public void Rover_Should_GenerateOutput()
        {
            _location.Initialize(new Position(5, 5));
            IRover rover1 = new MarsRover(new Position(1, 2), CompassDirection.North, _location);

            rover1.Rotate(Rotation.Left);
            rover1.Move();
            rover1.Rotate(Rotation.Left);
            rover1.Move();
            rover1.Rotate(Rotation.Left);
            rover1.Move();
            rover1.Rotate(Rotation.Left);
            rover1.Move();
            rover1.Move();

            IRover rover2 = new MarsRover(new Position(3, 3), CompassDirection.East, _location);

            rover2.Move();
            rover2.Move();
            rover2.Rotate(Rotation.Right);
            rover2.Move();
            rover2.Move();
            rover2.Rotate(Rotation.Right);
            rover2.Move();
            rover2.Rotate(Rotation.Right);
            rover2.Rotate(Rotation.Right);
            rover2.Move();

            string output         = rover1.PrintPositionAndCompassDirection() + rover2.PrintPositionAndCompassDirection();
            string expectedString = "1 3 N\r\n5 1 E\r\n";

            Assert.Equal(expectedString, output);
        }
Exemplo n.º 5
0
        private MarsRover GetDefaultRover()
        {
            MarsPlateu marsPlateu = new MarsPlateu(5, 5);
            MarsRover  marsRover  = MarsRover.CreateRover(new MarsRoverPositionMock(1, 2, NavigationDirection.North), marsPlateu);

            return(marsRover);
        }
Exemplo n.º 6
0
        public void RespondToObstacle()
        {
            //Arrange
            Point startingPoint  = new Point(0, 0);
            var   obstacleCoords = new Point(0, 1);
            var   obstacles      = new List <Point>()
            {
                obstacleCoords
            };

            MarsRover.CardinalDirection startingDirection = MarsRover.CardinalDirection.North;
            var       ObstacleDetector = new ObstacleDetector(obstacles);
            MarsRover rover            = new MarsRover(startingPoint, startingDirection, null, ObstacleDetector);

            var moves = new[] { 'f' };

            Point expectedCoordinates = startingPoint;

            MarsRover.CardinalDirection expectedStartingDirection = startingDirection;

            //Act
            rover.Move(moves);

            //Assert
            Assert.AreEqual(expectedCoordinates, rover.Coordinates);     //rover did not move
            Assert.AreEqual(expectedStartingDirection, rover.Direction); //rover did not move
            Assert.AreEqual($"obstacle detected at: ({obstacleCoords})", rover.Status.StatusMessage);
            Assert.AreEqual(RoverStatus.RoverStatusCode.Fail, rover.Status.StatusCode);
        }
Exemplo n.º 7
0
        public void ReportAnEncounteredObstacle(string command, string expectedOutput)
        {
            _marsRover = new MarsRover(_gridObstacle);
            var output = _marsRover.RunCommand(command);

            Assert.Equal(expectedOutput, output);
        }
Exemplo n.º 8
0
        public void RoverDirectionSetWithLowerCaseIsNorth()
        {
            var lowerCaseRover = new MarsRover(0, 0, DirectionConstants.North);
            var roverDirection = lowerCaseRover.roverState.Direction;

            Assert.That(InitialRoverDirection, Is.EqualTo(roverDirection));
        }
Exemplo n.º 9
0
        public void RoverTurning(Char initialDirection, String commands, Char endingDireciton)
        {
            var rover = new MarsRover(InitialXCoordinate, InitialYCoordinate, initialDirection);

            rover.RunRoverWithCommands(commands);
            Assert.That(rover.roverState.Direction, Is.EqualTo(endingDireciton));
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            Console.Write("Enter plateau size: ");
            var plateauParams           = Console.ReadLine();
            var r1PlateauInstance       = new PlateauGrid(plateauParams);
            var r2PlateauInstance       = new PlateauGrid(plateauParams);
            var rover1CommandsToExecute = new List <string>();
            var rover2CommandsToExecute = new List <string>();

            Console.Write("Enter Rover1 deployment Coords and direction: ");
            rover1CommandsToExecute.Add(Console.ReadLine().ToUpper());
            Console.Write("Enter Rover1 movement instructions: ");
            rover1CommandsToExecute.Add(Console.ReadLine().ToUpper());
            Console.Write("Enter Rover2 deployment Coords and direction: ");
            rover2CommandsToExecute.Add(Console.ReadLine().ToUpper());
            Console.Write("Enter rover2 Movement instructions: ");
            rover2CommandsToExecute.Add(Console.ReadLine().ToUpper());

            var rover1 = new MarsRover(r1PlateauInstance);

            rover1.SetRoverDirection(rover1CommandsToExecute);
            var rover2 = new MarsRover(r2PlateauInstance);

            rover2.SetRoverDirection(rover2CommandsToExecute);
            rover1.ExecuteCommands(rover1CommandsToExecute);
            rover2.ExecuteCommands(rover2CommandsToExecute);
            Console.WriteLine();
            Console.WriteLine($"Current Rover Position Heading: {rover1.CoordinateX} {rover1.CoordinateY} {rover1.Direction.ToString()[0]}");
            Console.WriteLine($"Current Rover Position Heading: {rover2.CoordinateX} {rover2.CoordinateY} {rover2.Direction.ToString()[0]}");
        }
Exemplo n.º 11
0
        public MarsRoverShould()
        {
            var grid = new Grid(10, 10);

            grid.AddObstacle(2, 2);
            _marsRover = new MarsRover(grid);
        }
Exemplo n.º 12
0
        public void execute_command_return_position(string command, string expected)
        {
            var rover    = new MarsRover();
            var position = rover.Move(command);

            Assert.That(position, Is.EqualTo(expected));
        }
Exemplo n.º 13
0
        public void NotMoveBeyondGridBoundary(string marsRoverInput, string expectedResult)
        {
            var marsRover = new MarsRover();
            var result    = MarsRover.Move(marsRoverInput);

            result.Should().Be(expectedResult);
        }
        public void TestScenarioMultipleRovers()
        {
            Plateau plateau = new Plateau()
            {
                MaxX = 5, MaxY = 5
            };

            Position positionFirst = new Position()
            {
                Coordinate = new Coordinates {
                    X = 1, Y = 2
                },
                Direction = Directions.N
            };
            MarsRover roverFirst = new MarsRover(positionFirst);
            string    movesFirst = "LMLMLMLMM";

            Position positionSecond = new Position()
            {
                Coordinate = new Coordinates {
                    X = 3, Y = 3
                },
                Direction = Directions.E
            };
            MarsRover roverSecond = new MarsRover(positionSecond);
            string    movesSecond = "MMRMMRMRRM";



            NasaMarsRoverController controller = new NasaMarsRoverController()
            {
                MarsPlateau   = plateau,
                MarsRoverList = new List <MarsRover>()
                {
                    roverFirst, roverSecond
                },
                MoveCommandList = new List <string>()
                {
                    movesFirst, movesSecond
                }
            };


            List <Position> curPositionList = controller.MoveMarsRoversOnPlateau();

            if (curPositionList != null && curPositionList.Count > 0)
            {
                Position firstPosition  = curPositionList[0];
                Position secondPosition = curPositionList[1];

                string actualOutput =
                    $"{firstPosition.Coordinate.X} {firstPosition.Coordinate.Y} {firstPosition.Direction.ToString()} " +
                    $"{secondPosition.Coordinate.X} {secondPosition.Coordinate.Y} {secondPosition.Direction.ToString()}";

                string expectedOutput = @"1 3 N 5 1 E";

                Assert.AreEqual(expectedOutput, actualOutput);
            }
        }
Exemplo n.º 15
0
        public void RotateToOrientationSpecifiedByInput(string roverInput, string expectedOutput)
        {
            var marsRover = new MarsRover();

            var result = MarsRover.Move(roverInput);

            result.Should().Be(expectedOutput);
        }
Exemplo n.º 16
0
        public void Should_Move_Back_When_Seeing_Specific_Direction(int x, int y, Direction direction, int xExpected, int yExpected)
        {
            MarsRover marsRover = new MarsRover(x, y, direction);

            marsRover.GoBack();

            Assert.Equal(new Point(xExpected, yExpected), marsRover.Point);
        }
Exemplo n.º 17
0
        public void When_A_New_Rover_Is_Given_The_Command_FFRFF_The_Position_Should_Be_2_2_E()
        {
            var rover = new MarsRover(new Mars());

            rover.Move("FFRFF");

            Assert.AreEqual("2, 2, E", rover.CurrentPosition);
        }
        public void When_A_New_Rover_Is_Given_The_Command_L_The_Position_Should_Be_0_0_W()
        {
            var rover = new MarsRover(new Mars());

            rover.Move("L");

            Assert.AreEqual("0, 0, W", rover.CurrentPosition);
        }
Exemplo n.º 19
0
        public void RoverReportsObstacle()
        {
            Rover rover = MarsRover.Process(0, 0, Direction.North, new [] { 'f' }, 3, new [] { new Obstacle(0, 1) });

            Rover expectedRover = new Rover(0, 0, Direction.North, true);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 20
0
        public void RotateRover(Direction startDirection, char command, Direction expectedDirection)
        {
            Rover rover = MarsRover.Process(0, 0, startDirection, new [] { command });

            Rover expectedRover = new Rover(0, 0, expectedDirection);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 21
0
        public void ProcessesMultipleCommands()
        {
            Rover rover = MarsRover.Process(0, 0, Direction.North, new [] { 'f', 'f' });

            Rover expectedRover = new Rover(0, 2, Direction.North);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 22
0
        public void MovesRoverBackward()
        {
            Rover rover = MarsRover.Process(0, 0, Direction.North, new [] { 'b', 'b' });

            Rover expectedRover = new Rover(0, -2, Direction.North);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 23
0
        public void Should_Move_Back_When_Out_Of_The_World(int x, int y, Direction direction, int xExpected, int yExpected)
        {
            MarsRover marsRover = new MarsRover(x, y, direction);

            marsRover.GoBack();

            Assert.Equal(new Point(xExpected, yExpected), marsRover.Point);
        }
Exemplo n.º 24
0
        public void ProcessesExtendedCommandSequenceWithCrash()
        {
            Rover rover = MarsRover.Process(0, 0, Direction.North, new [] { 'f', 'f', 'f', 'l', 'f', 'f', 'f', 'r', 'b', 'b', 'b', }, 10, new [] { new Obstacle(-3, 0), new Obstacle(2, 2) });

            Rover expectedRover = new Rover(-3, 1, Direction.North, true);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 25
0
        public void ProcessesExtendedCommandSequenceWithWrap()
        {
            Rover rover = MarsRover.Process(0, 0, Direction.North, new [] { 'f', 'f', 'f', 'r', 'f', 'f', 'f', 'r', 'b' }, 3);

            Rover expectedRover = new Rover(3, -3, Direction.South, false);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 26
0
        public void Should_Turn_Right_When_Seeing_Specific_Direction(Direction direction, Direction directionExpected)
        {
            MarsRover marsRover = new MarsRover(1, 2, direction);

            marsRover.TurnRight();

            Assert.Equal(directionExpected, marsRover.Direction);
        }
Exemplo n.º 27
0
        public void Should_Move_Correctly_With_Command_Without_Obstacle(string command, int x, int y, Direction direction, int xExpected, int yExpected)
        {
            MarsRover marsRover = new MarsRover(x, y, direction);

            marsRover.ExecuteCommand(command);

            Assert.Equal(new Point(xExpected, yExpected), marsRover.Point);
        }
Exemplo n.º 28
0
        public void MoveForwardOnceWhenSpecifiedByInput(string marsRoverInput, string expectedResult)
        {
            var marsRover = new MarsRover();

            var result = MarsRover.Move(marsRoverInput);

            result.Should().Be(expectedResult);
        }
Exemplo n.º 29
0
        public void WrapRoverMovement(Direction startDirection, int startX, int startY, Direction expectedDirection, int expectedX, int expectedY)
        {
            Rover rover = MarsRover.Process(startX, startY, startDirection, new [] { 'f' }, 3);

            Rover expectedRover = new Rover(expectedX, expectedY, expectedDirection);

            MarsRoverAsserts.AssertRoversEqual(expectedRover, rover);
        }
Exemplo n.º 30
0
        public void When_Rover_Is_Positioned_At_North_It_Should_Remain_The_Same()
        {
            IDirection  direction       = new North();
            Coordinates initialPosition = new Coordinates(1, 2);
            MarsRover   rover           = new MarsRover(plateau, initialPosition, direction);

            Assert.That(rover.CurrentDirection, Is.TypeOf <North>());
        }
Exemplo n.º 31
0
 public MarsRover TurnRight(MarsRover rover)
 {
     rover.roverState = rover.roverState.rightTurn();
     return rover;
 }
Exemplo n.º 32
0
 public void TestMarsRoverInitializationAtFourFour()
 {
     var marsRover = new MarsRover(planet, "4,4", 'N', repo);
     Assert.That(marsRover.GetRoverPosition(), Is.EqualTo("4,4"));
 }
Exemplo n.º 33
0
 public void TestMoveRoverWithCommands()
 {
     var marsRover = new MarsRover(planet, "0,0", 'N', repo);
     Assert.That(marsRover.MoveRover("ffrff"), Is.EqualTo("Rover was successfully moved to (2,2)."));
 }
Exemplo n.º 34
0
 public void TestMarsRoverInitializationAtZeroZero()
 {
     var marsRover = new MarsRover(planet, "0,0", 'N', repo);
     Assert.That(marsRover.GetRoverPosition(), Is.EqualTo("0,0"));
 }
Exemplo n.º 35
0
 public MarsRover TurnLeft(MarsRover rover)
 {
     rover.roverState = rover.roverState.leftTurn();
     return rover;
 }
Exemplo n.º 36
0
 public void TestRoverEncountersObstacle()
 {
     var planetWithObstacles = new Planet(50, new [] { new Point { X = 3, Y = 3} } );
     var marsRover = new MarsRover(planetWithObstacles, "3,2", 'N', repo);
     Assert.That(marsRover.MoveRover("ffrff"), Is.EqualTo("Rover encountered obstacle at position (3,3), rover stopped at (3,2)."));
 }
Exemplo n.º 37
0
 void Rover_OnRoverMoved(object sender, MarsRover.Interfaces.EventArguments.RoverEventArgs e)
 {
     this._eventRaised = true;
 }
Exemplo n.º 38
0
 public void TestRoverWithUndefinedCommand()
 {
     var marsRover = new MarsRover(planet, "0,50", 'N', repo);
     Assert.That(marsRover.MoveRover("fzrff"), Is.EqualTo("Error, 'z' is an undefined command."));
 }
Exemplo n.º 39
0
 public void TestRoverEncountersObstacleOnOtherSideOfXAxis()
 {
     var planetWithObstacles = new Planet(50, new[] { new Point { X = 0, Y = -25 } });
     var marsRover = new MarsRover(planetWithObstacles, "0,25", 'N', repo);
     Assert.That(marsRover.MoveRover("ffrff"), Is.EqualTo("Rover encountered obstacle at position (0,-25), rover stopped at (0,25)."));
 }
Exemplo n.º 40
0
 public MarsRover MoveForward(MarsRover rover)
 {
     var newXCoordinate = rover.XCoordinate + rover.roverState.HorizontalDistance();
     var newYCoordinate = rover.YCoordinate + rover.roverState.VerticalDistance();
     return map.MoveRoverTo(rover, newXCoordinate, newYCoordinate);
 }
Exemplo n.º 41
0
 public void TestRoverWrapsAroundAxis()
 {
     var marsRover = new MarsRover(planet, "0,50", 'N', repo);
     Assert.That(marsRover.MoveRover("ffrff"), Is.EqualTo("Rover was successfully moved to (2,-49)."));
 }