Exemplo n.º 1
0
 public Rover(Area area, RoverPosition roverPosition, string movementLetters)
 {
     this.Area            = area;
     this.RoverPosition   = roverPosition;
     this.MovementLetters = movementLetters;
     this.Messages        = new Messages();
 }
Exemplo n.º 2
0
        public static RoverPosition Turn(this RoverPosition input, char subCommand)
        {
            switch (subCommand)
            {
            case 'L':
                if (input == RoverPosition.West)
                {
                    return(RoverPosition.South);
                }
                else
                {
                    return((RoverPosition)(input - 1));
                }

            case 'R':
                if (input == RoverPosition.South)
                {
                    return(RoverPosition.West);
                }
                else
                {
                    return((RoverPosition)(input + 1));
                }
            }

            return(RoverPosition.None);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            RoverPosition  roverPosition = new RoverPosition();
            RoverDirection directions    = new RoverDirection();
            Plateau        plateau       = new Plateau();
            string         Coordinate;

            Console.WriteLine("Enter the Plateau X Y coordinate");
            Coordinate          = Console.ReadLine();
            plateau.xCoordinate = int.Parse(Coordinate.Split(' ')[0]);
            plateau.yCoordinate = int.Parse(Coordinate.Split(' ')[1]);
            try
            {
                Console.WriteLine("Enter the X Y Direction coordinate");
                Coordinate = Console.ReadLine();
                roverPosition.xCoordinate = int.Parse(Coordinate.Split(' ')[0]);
                roverPosition.yCoordinate = int.Parse(Coordinate.Split(' ')[1]);
                directions.Direction      = (Directions)Enum.Parse(typeof(Directions), Coordinate.Split(' ')[2]);

                Console.WriteLine("Enter the Command LRM(Left-Right-Move)");
                string commands = Console.ReadLine();

                Process(commands, directions, roverPosition);
                Console.WriteLine($"{roverPosition.xCoordinate} {roverPosition.yCoordinate} {directions.Direction}");
                Console.ReadKey();
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine(DirectionsConstant.WelcomeMessage);
            MessageHelper.DisplayMaxCoordinateMessage();
            Console.Write(DirectionsConstant.MaxPointsEntry);

            var maxCoordinateResult = InputHelper.CheckAndGetMaxCoordinate();

            while (!maxCoordinateResult.Item1)
            {
                Console.WriteLine(DirectionsConstant.ErrorMessage);
                Console.Write(DirectionsConstant.MaxPointsEntry);
                maxCoordinateResult = InputHelper.CheckAndGetMaxCoordinate();
            }

            MessageHelper.DisplayStartCoordinateMessage();
            Console.Write(DirectionsConstant.StartPointsEntry);

            var startCoordinateResult = InputHelper.CheckAndGetStartCoordinate();

            while (!startCoordinateResult.Item1)
            {
                Console.WriteLine(DirectionsConstant.ErrorMessage);
                Console.Write(DirectionsConstant.StartPointsEntry);
                startCoordinateResult = InputHelper.CheckAndGetStartCoordinate();
            }

            MessageHelper.DisplayStepsMessage();
            Console.Write(DirectionsConstant.StepsEntry);

            var stepsResult = InputHelper.CheckAndGetSteps();

            while (!stepsResult.Item1)
            {
                Console.WriteLine(DirectionsConstant.ErrorMessage);
                Console.Write(DirectionsConstant.StepsEntry);
                stepsResult = InputHelper.CheckAndGetSteps();
            }

            var direction = new RoverPosition(startCoordinateResult.Item2, stepsResult.Item2, maxCoordinateResult.Item2);

            try
            {
                var lastCoordinate = direction.Move();
                Console.WriteLine(lastCoordinate.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.ReadLine();
        }
Exemplo n.º 5
0
        public static char ToChar(this RoverPosition input)
        {
            switch (input)
            {
            case RoverPosition.North:
                return('N');

            case RoverPosition.South:
                return('S');

            case RoverPosition.West:
                return('W');

            case RoverPosition.East:
                return('E');
            }

            return('?');
        }
Exemplo n.º 6
0
        static RoverPosition Move(RoverDirection direction, RoverPosition roverPosition)
        {
            if (direction.Direction == Directions.N)
            {
                roverPosition.yCoordinate++;
            }
            else if (direction.Direction == Directions.E)
            {
                roverPosition.xCoordinate++;
            }
            else if (direction.Direction == Directions.S)
            {
                roverPosition.yCoordinate--;
            }
            else if (direction.Direction == Directions.W)
            {
                roverPosition.xCoordinate--;
            }

            return(roverPosition);
        }
Exemplo n.º 7
0
        static void Process(string commands, RoverDirection directions, RoverPosition roverPosition)
        {
            foreach (char item in commands)
            {
                switch (item)
                {
                case ('L'):
                    directions = TurnLeft(directions);
                    break;

                case ('R'):
                    directions = TurnRight(directions);
                    break;

                case ('M'):
                    roverPosition = Move(directions, roverPosition);
                    break;

                default:
                    throw new ArgumentException(string.Format("Error: {0}", item));
                }
            }
        }
Exemplo n.º 8
0
        public static void ProcessComamndSet()
        {
            var horizontalSize = int.Parse(CommandSet.First()[0].ToString()) + 1;
            var verticalSize   = int.Parse(CommandSet.First()[2].ToString()) + 1;

            var field = new RoverPosition[horizontalSize, verticalSize];

            for (int i = 0; i < horizontalSize; i++)
            {
                for (int j = 0; j < verticalSize; j++)
                {
                    field[i, j] = RoverPosition.None;
                }
            }

            var roverCount = (CommandSet.Count - 1) / 2;

            for (int i = 1; i < roverCount * 2; i += 2)
            {
                var xPos = int.Parse(CommandSet[i].Split(" ")[0]);
                var yPos = int.Parse(CommandSet[i].Split(" ")[1]);

                if (field[xPos, yPos] == RoverPosition.None) // Spot is available deploy it!
                {
                    field[xPos, yPos] = CommandSet[i].Last().Deploy();

                    foreach (var subCommand in CommandSet[i + 1].ToCharArray())
                    {
                        if (subCommand == 'M') // Move...
                        {
                            if (field[xPos, yPos] == RoverPosition.East)
                            {
                                field[xPos, yPos] = RoverPosition.None;
                                xPos += 1;
                                field[xPos, yPos] = RoverPosition.East;
                            }
                            else if (field[xPos, yPos] == RoverPosition.West)
                            {
                                field[xPos, yPos] = RoverPosition.None;
                                xPos = xPos - 1;
                                field[xPos, yPos] = RoverPosition.West;
                            }
                            else if (field[xPos, yPos] == RoverPosition.North)
                            {
                                field[xPos, yPos] = RoverPosition.None;
                                yPos = yPos + 1;
                                field[xPos, yPos] = RoverPosition.North;
                            }
                            else if (field[xPos, yPos] == RoverPosition.South)
                            {
                                field[xPos, yPos] = RoverPosition.None;
                                yPos = yPos - 1;
                                field[xPos, yPos] = RoverPosition.South;
                            }
                        }
                        else // Turn...
                        {
                            field[xPos, yPos] = field[xPos, yPos].Turn(subCommand);
                        }
                    }
                }

                Console.WriteLine($"{xPos} {yPos} {field[xPos, yPos].ToChar()}");
            }
        }
Exemplo n.º 9
0
 public Rover CreateRover(Area area, RoverPosition roverPosition, string movementLetter)
 {
     return(new Rover(area, roverPosition, movementLetter));
 }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            Console.Write("Enter the plateau's upper-right coordinates (ex. 5 5) :");
            string line = Console.ReadLine();

            Coordinate upperRightCoordinate = MarsRoverCommandHelper.PopulateCoordinate(line);

            if (upperRightCoordinate is default(Coordinate))
            {
                Console.WriteLine("The input is not valid.");
                return;
            }

            // generate the rover and plateau
            Coordinate    lowerLeftCoordinate  = new Coordinate();
            PlateauBounds plateauBounds        = new PlateauBounds(upperRightCoordinate, lowerLeftCoordinate);
            IPlateau      plateau              = new Plateau(plateauBounds);
            Coordinate    roverCurrentPosition = new Coordinate();
            RoverPosition roverPosition        = new RoverPosition(roverCurrentPosition, RoverDirectionEnum.North);
            IRover        rover = new Rover(plateau, roverPosition);

            while (true)
            {
                Console.Clear();
                Console.WriteLine(string.Format("Plateau's bounds are: Upper-Right = ({0},{1}) Lower-Left = ({2},{3})" + Environment.NewLine,
                                                plateau.Bounds.UpperRight.X,
                                                plateau.Bounds.UpperRight.Y,
                                                plateau.Bounds.LowerLeft.X,
                                                plateau.Bounds.LowerLeft.Y));

                Console.Write("Enter the rover's start position (ex. 1 1 N) :");
                line = Console.ReadLine();
                rover.CurrentPosition = MarsRoverCommandHelper.PopulateRoverPosition(line);

                if (rover.CurrentPosition is default(RoverPosition))
                {
                    Console.WriteLine("The input is not valid.");
                    return;
                }

                Console.Write("Enter rover's moving sequence (ex. LMRMRMM) :");
                line = Console.ReadLine();
                IEnumerable <MovingDirectionEnum> movingDirectionList = MarsRoverCommandHelper.PopulateMovingDirectionList(line);

                if (movingDirectionList.Any() == false)
                {
                    Console.WriteLine("The input is not valid.");
                    return;
                }

                foreach (MovingDirectionEnum direction in movingDirectionList)
                {
                    rover.Move(direction);
                }

                Console.WriteLine(string.Format(Environment.NewLine + "Current Position is: {0} {1} {2}", rover.CurrentPosition.Coordinate.X, rover.CurrentPosition.Coordinate.Y, rover.CurrentPosition.Direction));

                Console.Write(Environment.NewLine + "0: Exit \n1: Retry\nSelect :");
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key != ConsoleKey.D1 && key.Key != ConsoleKey.NumPad1)
                {
                    return;
                }
            }
        }