Example #1
0
        public void TestInvalidBoardPosition()
        {
            // arrange
            ToyBoard toyBoard = new ToyBoard(5, 5);
            Position position = new Position(6, 6);

            // act
            var result = toyBoard.IsValidPosition(position);

            // assert
            Assert.IsFalse(result);
        }
Example #2
0
        public void TestValidBoardPosition()
        {
            // arrange
            ToyBoard toyBoard = new ToyBoard(5, 5);
            Position position = new Position(1, 4);

            // act
            var result = toyBoard.IsValidPosition(position);

            // assert
            Assert.IsTrue(result);
        }
        public void TestInvalidBehaviourPlace()
        {
            // arrange
            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            // act
            simulator.ProcessCommand("PLACE 9,7,EAST".Split(' '));

            // assert
            Assert.IsNull(robot.Position);
        }
        public void TestValidBehaviourMove()
        {
            // arrange
            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            // act
            simulator.ProcessCommand("PLACE 0,0,NORTH".Split(' '));
            simulator.ProcessCommand("MOVE".Split(' '));

            // assert
            Assert.AreEqual("Output: 0,1,NORTH", simulator.GetReport());
        }
        public void TestValidBehaviourPlace()
        {
            // arrange
            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            // act
            simulator.ProcessCommand("PLACE 1,4,EAST".Split(' '));

            // assert
            Assert.AreEqual(1, robot.Position.X);
            Assert.AreEqual(4, robot.Position.Y);
            Assert.AreEqual(Direction.East, robot.Direction);
        }
        public void TestValidBehaviourReport3()
        {
            // arrange
            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            // act
            simulator.ProcessCommand("PLACE 0,0,NORTH".Split(' '));
            simulator.ProcessCommand("LEFT".Split(' '));

            var output = simulator.ProcessCommand("REPORT".Split(' '));

            // assert
            Assert.AreEqual("Output: 0,0,WEST", output);
        }
        public void TestInvalidBehaviourMove()
        {
            // arrange
            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            // act
            simulator.ProcessCommand("PLACE 2,2,NORTH".Split(' '));
            simulator.ProcessCommand("MOVE".Split(' '));
            simulator.ProcessCommand("MOVE".Split(' '));
            // if the robot goes out of the board it ignores the command
            simulator.ProcessCommand("MOVE".Split(' '));

            // assert
            Assert.AreEqual("Output: 2,4,NORTH", simulator.GetReport());
        }
Example #8
0
        public static void Main(string[] args)
        {
            string description = ToyRobotApp.Constants.Messages.MENU;

            IToyBoard    toyBoard    = new ToyBoard(5, 5);
            IInputParser inputParser = new InputParser();
            IToyRobot    robot       = new ToyRobot();
            var          simulator   = new ToyAction(robot, toyBoard, inputParser);

            var stopApplication = false;

            Console.WriteLine(description);
            do
            {
                var command = Console.ReadLine();
                if (command == null)
                {
                    continue;
                }

                if (command.ToUpper().Equals("EXIT"))
                {
                    stopApplication = true;
                }
                else
                {
                    try
                    {
                        var output = simulator.ProcessCommand(command.Split(' '));
                        if (!string.IsNullOrEmpty(output))
                        {
                            Console.WriteLine(output);
                        }
                    }
                    catch (ArgumentException exception)
                    {
                        Console.WriteLine(exception.Message);
                    }
                }
            } while (!stopApplication);
        }
Example #9
0
        public string ProcessCommand(string[] input)
        {
            var command = InputParser.ParseCommand(input);

            if (command != Command.Place && ToyRobot.Position == null)
            {
                return(string.Empty);
            }

            switch (command)
            {
            case Command.Place:
                var placeCommandParameter = InputParser.ParseCommandParameter(input);
                if (ToyBoard.IsValidPosition(placeCommandParameter.Position))
                {
                    ToyRobot.Place(placeCommandParameter.Position, placeCommandParameter.Direction);
                }
                break;

            case Command.Move:
                var newPosition = ToyRobot.GetNextPosition();
                if (ToyBoard.IsValidPosition(newPosition))
                {
                    ToyRobot.Position = newPosition;
                }
                break;

            case Command.Left:
                ToyRobot.RotateLeft();
                break;

            case Command.Right:
                ToyRobot.RotateRight();
                break;

            case Command.Report:
                return(GetReport());
            }
            return(string.Empty);
        }