コード例 #1
0
ファイル: RoverFacade.cs プロジェクト: MentatRus/Patter2018
 //Выполняет введенную команду
 public void ExecuteCommand(string input)
 {
     if (input.Length == 1)
     {
         char commandDirection = input[0];
         //Отмена последней команды
         if (commandDirection == 'c')
         {
             //Если стек команд не пуст
             if (rover._commands.Count != 0)
             {
                 //Взять и удалить последнюю команду из стека
                 ICommand c = rover._commands.Pop();
                 //Отменить эту команду
                 c.Undo();
             }
         }
         else
         {
             //Создать новую команду на передвижение
             var command = new RoverCommand(commandDirection, rover);
             //Выполнить ее
             command.Execute();
         }
     }
 }
コード例 #2
0
ファイル: Rover.cs プロジェクト: diegobg/MarsRover
        public void AddCommand(RoverCommand command)
        {
            if (_commandQueue.Count == 5)
            {
                throw new CommandQueueDepthExceeded();
            }

            _commandQueue.Enqueue(command);
        }
コード例 #3
0
        public void Should_Create_RoverCommand()
        {
            string[] input = {"5 5", "1 2 N", "LMLMLMLMM", "3 3 E", "MMRMMRMRRM"};
            var roverFactoryMock = MockRepository.GenerateMock<IRoverFactory>();
            roverFactoryMock.Stub(x => x.GetRover(Arg<string>.Is.Anything,
                                                    Arg<string>.Is.Anything,
                                                    Arg<int>.Is.Anything,
                                                    Arg<int>.Is.Anything))
            .Return(null);

            IRoverCommand result = new RoverCommand(input, roverFactoryMock);

            Assert.NotNull(result);
        }
コード例 #4
0
ファイル: Rover.cs プロジェクト: mahmutozben/MarsRover
        public Position Start(Position position, string commands)
        {
            Console.WriteLine($"Current location : {LocationInfo(position)}");

            ICommand command   = new RoverCommand();
            var      direction = Direction.GetDirection(position.Direction);

            foreach (var cmd in commands)
            {
                switch (cmd)
                {
                case Commands.Left:
                    direction = command.TurnLeft((Directions)direction.Id);
                    position.SetDirection((Directions)direction.Id);
                    break;

                case Commands.Right:
                    direction = command.TurnRight((Directions)direction.Id);
                    position.SetDirection((Directions)direction.Id);
                    break;

                case Commands.Move:
                {
                    if (IsThereAnyDeathHere(position))
                    {
                        Console.WriteLine("You cannot go further. Try another direction please..");
                        var newCommand = Console.ReadLine();
                        this.Start(position, newCommand);
                    }

                    position = command.Move(position);

                    if (CheckPosition(position) == RoverStatus.Dead)
                    {
                        Console.WriteLine("Rest in peace!");
                        commands = string.Empty;
                    }
                }
                break;

                default:
                    throw new Exception($"command : {command} is not invalid.");
                }
                Console.WriteLine(LocationInfo(position));
            }

            return(position);
        }
コード例 #5
0
        public void GetAllRoverLocations_Should_Return_Original_Locations()
        {
            string[] input = { "5 5", "1 2 N", "LMLMLMLMM", "3 3 E", "MMRMMRMRRM" };
            var roverFactoryMock = MockRepository.GenerateMock<IRoverFactory>();
            var roverMock = MockRepository.GenerateMock<IRover>();
            roverMock.Stub(x => x.CurrentPosition()).Return("returnme!");
            roverFactoryMock.Stub(x => x.GetRover(Arg<string>.Is.Anything,
                                                    Arg<string>.Is.Anything,
                                                    Arg<int>.Is.Anything,
                                                    Arg<int>.Is.Anything))
            .Return(roverMock);
            IRoverCommand sut = new RoverCommand(input, roverFactoryMock);

            string result = sut.GetAllRoverLocations();

            Assert.AreEqual("returnme!\nreturnme!\n", result);
        }
コード例 #6
0
        public string TakeInput(List <string> inputList)
        {
            var output = String.Empty;

            _roberCommand = new RoverCommand();
            var mapDimension = inputList[0].Split(' ');

            Map.MaxXCoordinate = int.Parse(mapDimension[0]);
            Map.MaxYCoordinate = int.Parse(mapDimension[1]);
            var initialPosition = inputList[1].Split(' ');

            _rover = new Rover(int.Parse(initialPosition[0]), int.Parse(initialPosition[1]), char.Parse(initialPosition[2]));
            var instructionList = inputList[2];

            foreach (var command in instructionList)
            {
                _rover.CurrentPosition = _roberCommand.GetUpdatedPositionOnExecutingCommand(command, _rover.CurrentPosition);
            }
            output = $"{_rover.CurrentPosition.XCoordinate} {_rover.CurrentPosition.YCoordinate} {(char)_rover.CurrentPosition.CurrentDirection}";
            return(output);
        }