Beispiel #1
0
        public void LeftCommand(int inputX, int inputY, int inputDirection, int expectedX, int expectedY, int expectedDirection)
        {
            var robot     = new Robot();
            var simulator = new RobotSimulatorCommand();

            simulator.Place(robot, new Position()
            {
                XAxis             = inputX,
                YAxis             = inputY,
                CardinalDirection = (CardinalDirection)inputDirection
            });

            simulator.Left(robot);

            var positionStatus = simulator.Report(robot);

            Assert.True(positionStatus.CurrentPosition.XAxis == expectedX &&
                        positionStatus.CurrentPosition.YAxis == expectedY &&
                        positionStatus.CurrentPosition.CardinalDirection == (CardinalDirection)expectedDirection
                        );
        }
Beispiel #2
0
        public void RunMultipleCommands_InputX0Y0East_ReturnX3Y3North()
        {
            var robot     = new Robot();
            var simulator = new RobotSimulatorCommand();

            simulator.Place(robot, new Position()
            {
                XAxis             = 1,
                YAxis             = 2,
                CardinalDirection = CardinalDirection.East
            });

            simulator.Move(robot);
            simulator.Move(robot);
            simulator.Left(robot);
            simulator.Move(robot);

            var positionStatus = simulator.Report(robot);

            Assert.True(positionStatus.CurrentPosition.XAxis == 3 &&
                        positionStatus.CurrentPosition.YAxis == 3 &&
                        positionStatus.CurrentPosition.CardinalDirection == CardinalDirection.North
                        );
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            string         commandSelected     = null;
            Robot          robot               = null;
            var            simulatorCommand    = new RobotSimulatorCommand();
            PositionStatus robotPositionStatus = null;

            do
            {
                try
                {
                    Console.WriteLine("What do you want to do? Please select one of the actions below:");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("C - Create a robot");
                    Console.WriteLine("P - Place the robot on a starting position");
                    Console.WriteLine("M - Move a robot");
                    Console.WriteLine("L - Move the robot 90 degrees to the Left");
                    Console.WriteLine("R - Move the robot 90 degrees to the Right");
                    Console.WriteLine("S - Show the robot's current position");
                    Console.WriteLine("Q - Quit");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Enter command here:");
                    commandSelected = Console.ReadLine();

                    switch (commandSelected.ToUpper())
                    {
                    case "C":
                        robot = new Robot();
                        Console.WriteLine("Robot created.");
                        Console.WriteLine("*******************");
                        break;

                    case "P":
                        Console.WriteLine("Please enter its starting position (x, y, [NORTH|SOUTH|EAST|WEST]):");
                        var input = Console.ReadLine();

                        if (!Regex.IsMatch(input, @"\(\d,\d,(NORTH|SOUTH|EAST|WEST|north|south|east|west)\)"))
                        {
                            Console.WriteLine("Invalid coordinates. Please try again");
                            break;
                        }

                        var inputItems = input.TrimStart('(').TrimEnd(')').Split(',');

                        int.TryParse(inputItems[0], out var xAxis);
                        int.TryParse(inputItems[1], out var yAxis);
                        Enum.TryParse(typeof(CardinalDirection), inputItems[2], true, out var direction);

                        robot.Position      = new Position(xAxis, yAxis, (CardinalDirection)direction);
                        robotPositionStatus = new PositionStatus(robot.Position);
                        if (!robotPositionStatus.IsPositionValid)
                        {
                            Console.WriteLine($"{robotPositionStatus.Message}");
                            break;
                        }

                        simulatorCommand.Place(robot, robot.Position);
                        Console.WriteLine("Robot now positioned.");
                        Console.WriteLine("*******************");
                        break;

                    case "M":
                        simulatorCommand.Move(robot);
                        Console.WriteLine("Robot has been moved");
                        Console.WriteLine("*******************");
                        break;

                    case "L":
                        simulatorCommand.Left(robot);
                        Console.WriteLine("Robot has moved to its left.");
                        Console.WriteLine("*******************");
                        break;

                    case "R":
                        simulatorCommand.Right(robot);
                        Console.WriteLine("Robot has moved to its right.");
                        Console.WriteLine("*******************");
                        break;

                    case "S":
                        robotPositionStatus = new PositionStatus(robot.Position);

                        Console.WriteLine($"Robot is placed at ({robotPositionStatus.CurrentPosition.XAxis}," +
                                          $"{robotPositionStatus.CurrentPosition.YAxis}) " +
                                          $"{Enum.GetName(typeof(CardinalDirection), robotPositionStatus.CurrentPosition.CardinalDirection)}");
                        break;

                    default:
                        Console.WriteLine("Invalid selection");
                        Console.WriteLine("*******************");
                        break;
                    }
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            } while (commandSelected is null || commandSelected != "Q" || commandSelected != "q");

            Environment.Exit(0);
        }