예제 #1
0
        public void TurnAnyDirection(string command, string expected)
        {
            var actual = _rover.Move(command);

            _output.WriteLine(expected);
            _output.WriteLine(actual);

            Assert.True(expected == actual);
        }
예제 #2
0
        public void Move_ReturnCorrectMovement_WhenMovementIsInSurface()
        {
            IRover rover = RoverFactory.CreateRover("1 2 N", surface.Object);

            rover.Move("M");
            rover.Move("R");
            rover.Move("M");

            Assert.Equal(2, rover.X);
            Assert.Equal(3, rover.Y);
            Assert.Equal("E", rover.Direction);
        }
예제 #3
0
        public void Run(IRover rover)
        {
            var newDirection = new Direction();

            switch (rover.RoverLocation.Direction)
            {
            case Direction.East:
            {
                newDirection = Direction.South;
                break;
            }

            case Direction.South:
            {
                newDirection = Direction.West;
                break;
            }

            case Direction.West:
            {
                newDirection = Direction.North;
                break;
            }

            case Direction.North:
            {
                newDirection = Direction.East;
                break;
            }
            }

            RoverLocation newLocation = RoverLocation.Set(rover.RoverLocation.X, rover.RoverLocation.Y, newDirection);

            rover.Move(newLocation);
        }
예제 #4
0
        public string Execute(ILocation location)
        {
            IRover rover = location.GetRover();

            rover.Move();
            return(string.Empty);
        }
예제 #5
0
        public void Test_Scenario_Max55_33E_MRRMMRMRRM()
        {
            ServiceProvider serviceProvider = new ServiceCollection()
                                              .AddTransient <IRover, Rover>()
                                              .BuildServiceProvider();

            var maxLimits = new List <int>()
            {
                5, 5
            };
            Coordinate coordinate = new Coordinate
            {
                Direction = Directions.E,
                X         = 3,
                Y         = 3
            };
            var    moves  = "MRRMMRMRRM";
            IRover _rover = serviceProvider.GetService <IRover>();
            var    output = _rover.Move(maxLimits, moves, coordinate);

            var actual   = $"{output.X} {output.Y} {output.Direction}";
            var expected = "2 3 S";

            Assert.AreEqual(expected, actual);
        }
예제 #6
0
 /// <summary>
 /// Executes the IRover movement commands against the IRover encapsulated in this IRoverController.
 /// </summary>
 public void ExecuteRoverMovementCommands()
 {
     if (_rover.CurrentGraphNode == null)
     {
         throw new Exception("Rover must be placed on a grid before movement can execute");
     }
     foreach (char command in _parsedRoverData.RoverControlCommands)
     {
         if (command == 'L')
         {
             _rover.SetOrientation(((_rover.CurrentOrientation +
                                     _directionsInfoContainer.DirectionsInformation.Count) - 1) %
                                   _directionsInfoContainer.DirectionsInformation.Count);
         }
         else if (command == 'R')
         {
             _rover.SetOrientation(((_rover.CurrentOrientation +
                                     _directionsInfoContainer.DirectionsInformation.Count) + 1) %
                                   _directionsInfoContainer.DirectionsInformation.Count);
         }
         else if (command == 'M')
         {
             _rover.Move();
         }
         else
         {
             throw new Exception("Mal-formed rover control commands data encountered");
         }
     }
 }
예제 #7
0
        static void Main(string[] args)
        {
            try
            {
                ServiceProvider serviceProvider = new ServiceCollection()
                                                  .AddTransient <IRover, Rover>()
                                                  .BuildServiceProvider();
                Input input = new Input();

                SetInputsFromConsole(input);

                List <int> maxLimits = new List <int>()
                {
                    input.MaxX, input.MaxY
                };

                Coordinate coordinate = new Coordinate
                {
                    X         = input.StartX,
                    Y         = input.StartY,
                    Direction = (Directions)Enum.Parse(typeof(Directions), input.StartDirection)
                };
                IRover _rover = serviceProvider.GetService <IRover>();
                _rover.Move(maxLimits, input.Moves, coordinate);
                Console.WriteLine($"{coordinate.X} {coordinate.Y} {coordinate.Direction}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void EastFacingRoverReturnsCorrectDirectionalRoverAfterTurning(Type startType, char direction, Type endType)
        {
            IRover rover  = (IRover)Activator.CreateInstance(startType, 0, 0);
            var    rover2 = rover.Move(direction);

            Assert.IsInstanceOf(endType, rover2);
        }
        private string ParseInstruct(char[] t, IRover rover)
        {
            foreach (var c in t)
            {
                switch (c)
                {
                case 'M':
                    if (!CanMove(rover))
                    {
                        return("invalid instructions:");
                    }

                    rover.Move();
                    break;

                case 'L':
                    rover.TurnLeft();
                    break;

                case 'R':
                    rover.TurnRight();
                    break;
                }
            }

            return("");
        }
        public int[] EastFacingRoverMoveMethodSubtractsAndAddsToCoordinatesCorrectly(Type roverType, int startX, int startY, char movement)
        {
            IRover rover = (IRover)Activator.CreateInstance(roverType, startX, startY);

            rover = rover.Move(movement);
            return(new int[] { rover.X, rover.Y });
        }
예제 #11
0
        public void Test_Scenario_Max55_12N_LMLMLMLMM()
        {
            ServiceProvider serviceProvider = new ServiceCollection()
                                              .AddTransient <IRover, Rover>()
                                              .BuildServiceProvider();

            var maxLimits = new List <int>()
            {
                5, 5
            };
            Coordinate coordinate = new Coordinate
            {
                Direction = Directions.N,
                X         = 1,
                Y         = 2
            };
            string moves  = "LMLMLMLMM";
            IRover _rover = serviceProvider.GetService <IRover>();
            var    output = _rover.Move(maxLimits, moves, coordinate);

            var actual   = $"{output.X} {output.Y} {output.Direction}";
            var expected = "1 3 N";

            Assert.AreEqual(expected, actual);
        }
예제 #12
0
 public void MoveRover(RoverModel model)
 {
     if (model.ErrorTracer == null)
     {
         var result = _rover.Move(model.Movement.ToUpper());
         _rover.ErrorMessage = !result ? MessageConstant.MovementInstructionFail : null;
     }
 }
예제 #13
0
        /// <summary>Performs Move action on a rover.</summary>
        /// <param name="rover">The rover to use.</param>
        public void Execute(IRover rover)
        {
            var success = rover.Move(true);

            if (!success)
            {
                throw new Exception("Rover cannot move forward.");
            }
        }
예제 #14
0
        public void Given_RoverIsPlaced_When_Command_Passed_Sets_Rover_Expected_Direction_Expected_Position(int beginX, int beginY, char beginDirection,
                                                                                                            Direction expectedEndDirection, int expectedEndX, int expectedEndY)
        {
            SetupRover("R1", beginX, beginY, beginDirection);
            _sut.Move("R2R3L4");

            _sut.Direction.Should().Be(expectedEndDirection);
            _sut.Position.Should().Be(new Point(expectedEndX, expectedEndY));
        }
예제 #15
0
        public void Rover_Should_Move()
        {
            // Arrange
            rover.LocationY = 5;
            rover.Direction = Direction.N;

            // Act
            rover.Move();

            // Assert
            Assert.Equal(6, rover.LocationY);
        }
예제 #16
0
        private void MoveRover(string input)
        {
            foreach (char ch in input)
            {
                switch (ch)
                {
                case 'M': _r.Move(); break;

                default: _r.Turn(ch); break;
                }
            }
        }
예제 #17
0
        public Position Deploy(Plateau plateau, Position position, List <Movement> movements)
        {
            if (!CanDeployRover(plateau, position))
            {
                throw new Exception("Rover is outside the defined boundaries");
            }

            _rover.LandingArea = plateau;
            _rover.Position    = position;
            foreach (var instruction in movements)
            {
                _rover.Move(instruction);
            }
            return(_rover.Position);
        }
예제 #18
0
 public string Control()
 {
     try
     {
         rover.PreMove(plateau);
         rover.Move(plateau);
         Console.WriteLine($"{rover.Position.X} {rover.Position.Y} {rover.Direction}");
         return($"{rover.Position.X} {rover.Position.Y} {rover.Direction}");
     }
     catch (Exception)
     {
         Console.WriteLine("Error While Processing Control");
         return("Error While Processing Control");
     }
 }
 public void Execute(MoveRoverCommand command)
 {
     foreach (char movementItem in command._movementList)
     {
         Movement movement;
         try
         {
             movement = (Movement)Enum.Parse(typeof(Movement), movementItem.ToString());
         }
         catch (Exception ex)
         {
             throw new DirectionException(ex.Message, ex.InnerException);
         }
         _rover.Move(movement);
     }
 }
예제 #20
0
        public ActionResult MoveRover(string position, string plateau, string commands)
        {
            position = position.ToUpper();
            commands = commands.ToUpper();
            RoverPositionModel finalPosition = new RoverPositionModel
            {
                X           = Convert.ToInt32(position.Substring(0, 1)),
                Y           = Convert.ToInt32(position.Substring(1, 1)),
                Orientation = position.Substring(2, 1) == "N" ? Orientations.N : position.Substring(2, 1) == "E" ? Orientations.E : position.Substring(2, 1) == "S" ? Orientations.S : Orientations.W
            };
            PlateauModel plateauModel = new PlateauModel
            {
                X = Convert.ToInt32(plateau.Substring(0, 1)),
                Y = Convert.ToInt32(plateau.Substring(1, 1))
            };

            foreach (var command in commands)
            {
                switch (command)
                {
                case ('L'):
                    finalPosition.Orientation = Rover.TurnLeft(finalPosition.Orientation);
                    break;

                case ('R'):
                    finalPosition.Orientation = Rover.TurnRight(finalPosition.Orientation);
                    break;

                case ('M'):
                    finalPosition = Rover.Move(finalPosition);
                    break;

                default:
                    throw new ArgumentException(string.Format("Invalid value: {0}", command));
                }
            }
            if (Rover.IsRoverInsidePlateau(finalPosition, plateauModel))
            {
                return(View("Result", finalPosition));
            }
            ViewBag.Error = "Rover is out of plateau boundaries";
            return(View("OutOfBoundaries"));
        }
예제 #21
0
파일: RoverShould.cs 프로젝트: kkorus/rover
        public void Detect_Obstacles()
        {
            // Arrange
            _planet = new Planet(5, 5, new List <Point> {
                new Point(0, 1), new Point(1, 1)
            });
            _rover = new Rover(_planet);

            const string commands = "RFLFLFRF";
            var          expectedObstaclesPoint = new Point(1, 1);

            // Act
            var moveResult = _rover.Move(commands);

            // Assert
            Assert.True(moveResult.ObstacleDetected);
            Assert.True(moveResult.ObstaclePoint.HasValue);
            moveResult.ObstaclePoint.Should().BeEquivalentTo(expectedObstaclesPoint);
        }
예제 #22
0
        /// <summary>
        /// Runs the simulation for a given Rover
        /// </summary>
        /// <param name="rover">The Rover to run</param>
        public void RunRover(IRover rover)
        {
            while (rover.GetNextMove() != Action.Nothing)
            {
                // Copy the value of the current coordinates
                var currentCoordinates = new Point(rover.Coordinates.X, rover.Coordinates.Y);

                rover.Move();

                if (!rover.Coordinates.Equals(currentCoordinates))
                {
                    if (this.Planet.IsAreaOccupied(rover.Coordinates))
                    {
                        throw new InvalidOperationException("Rover position conflicts with another");
                    }

                    this.Planet.UpdateGridPosition(rover, currentCoordinates);
                }
            }
        }
예제 #23
0
        public void CommandRover(string directions)
        {
            foreach (var direction in directions)
            {
                switch (direction)
                {
                case 'L':
                    _rover.RotateLeft();
                    break;

                case 'R':
                    _rover.RotateRight();
                    break;

                case 'M':
                    _rover.Move();
                    break;
                }
            }
        }
예제 #24
0
        public Boolean Step(IRover rover)
        {
            if (!Actions.MoveNext())
            {
                return(true);
            }

            RoverAction action = Actions.Current;

            switch (action.Instruction)
            {
            case Instruction.CollectPower:
                rover.CollectPower();
                break;

            case Instruction.CollectSample:
                rover.CollectSample();
                break;

            case Instruction.ProcessSamples:
                rover.ProcessSamples();
                break;

            case Instruction.Transmit:
                rover.Transmit();
                break;

            case Instruction.Sense:
                rover.SenseSquare(action.Direction);
                break;

            case Instruction.Move:
                rover.Move(action.Direction);
                break;

            default:
                throw new InvalidOperationException();
            }

            return(rover.IsHalted);
        }
예제 #25
0
        static void Main(string[] args)
        {
            //Test Input:
            //5 5
            //1 2 N
            //LMLMLMLMM
            //3 3 E
            //MMRMMRMRRM
            //Expected Output:
            //1 3 N
            //5 1 E

            string   surf    = Console.ReadLine(); //5 5
            ISurface surface = SurfaceFactory.CreateSurface(surf);

            string rov   = Console.ReadLine(); //1 2 N
            IRover rover = RoverFactory.CreateRover(rov, surface);

            string        moves     = Console.ReadLine(); //LMLMLMLMM
            List <string> movements = MovementFactory.CreateMovement(moves);

            foreach (var movement in movements)
            {
                rover.Move(movement);
            }

            string rov2   = Console.ReadLine(); //3 3 E
            IRover rover2 = RoverFactory.CreateRover(rov2, surface);

            string        moves2     = Console.ReadLine(); //MMRMMRMRRM
            List <string> movements2 = MovementFactory.CreateMovement(moves2);

            foreach (var movement2 in movements2)
            {
                rover2.Move(movement2);
            }

            Console.WriteLine($"{rover.X} {rover.Y} {rover.Direction}");
            Console.WriteLine($"{rover2.X} {rover2.Y} {rover2.Direction}");
            Console.ReadLine();
        }
예제 #26
0
        public void Correct_Movement()
        {
            _rover.X         = 3;
            _rover.Y         = 3;
            _rover.Direction = Compass.East;

            _plateau.LowerLeft  = 5;
            _plateau.UpperRight = 5;


            _plateau.IsInPlateau(_rover);

            _rover.Plateau = _plateau;

            _rover.Move("MMRMMRMRRM");

            var result         = _rover.Direction;
            var expectedResult = Compass.East;

            Assert.Equal(expectedResult, result);
        }
예제 #27
0
        public void RoverMustMoveWithValidParameters()
        {
            // Arrange
            Fixture       fixture            = new Fixture();
            Coordinate    currenctCoordinate = fixture.Create <Coordinate>();
            RoverPosition currenctPosition   = fixture.Build <RoverPosition>()
                                               .With(x => x.Coordinate, currenctCoordinate)
                                               .With(x => x.Direction, RoverDirectionEnum.North)
                                               .Create();

            fixture.Customizations.Add(FixtureCustomizations.PlateauConfiguration());
            IRover sut = fixture.Build <Rover>()
                         .With(x => x.CurrentPosition, currenctPosition)
                         .Create();

            // Act
            sut.Move(MovingDirectionEnum.Left);

            // Assert
            Assert.NotNull(sut.CurrentPosition);
            Assert.Equal(RoverDirectionEnum.West, sut.CurrentPosition.Direction);
        }
예제 #28
0
        /// <summary>
        /// Execute command if its one of the { 'L', 'R', 'M' }
        /// Rover will not move further if it hit the boundary
        /// </summary>
        /// <param name="rover"></param>
        /// <param name="command"></param>
        private void ExecuteCommand(IRover rover, char command)
        {
            switch (command)
            {
            case 'L':
                rover.RotateLeft();
                break;

            case 'R':
                rover.RotateRight();
                break;

            case 'M':
                if (!_plateau.OutOfBound(rover.GetDest().X, rover.GetDest().Y))
                {
                    rover.Move();
                }
                break;

            default:
                throw new InvalidInputException(command + " is not valid input for command!");
            }
        }
예제 #29
0
        public void Run(IRover rover)
        {
            int x = rover.RoverLocation.X;
            int y = rover.RoverLocation.Y;

            switch (rover.RoverLocation.Direction)
            {
            case Direction.East:
            {
                x += 1;
                break;
            }

            case Direction.West:
            {
                x -= 1;
                break;
            }

            case Direction.North:
            {
                y += 1;
                break;
            }

            case Direction.South:
            {
                y -= 1;
                break;
            }
            }

            RoverLocation newLocation = RoverLocation.Set(x, y, rover.RoverLocation.Direction);

            rover.Move(newLocation);
        }
예제 #30
0
 public void Execute(IRover rover)
 {
     rover.Move();
 }
예제 #31
0
파일: RoverShould.cs 프로젝트: kkorus/rover
 public void Throw_An_Exception_When_No_Commands_Given(string emptyCommands)
 {
     Assert.Throws <ArgumentException>(() => _rover.Move(emptyCommands));
 }