Пример #1
0
        public static Rover CreateRover(string definition)
        {
            if (string.IsNullOrEmpty(definition)) throw new WrongRoverDefinitionException("Definition of rover is empty.");

            var coordinatesAndOrientation = definition.Split(' ');

            if(coordinatesAndOrientation.Length != 3) throw new WrongRoverDefinitionException("Definition of rover doesn't have the proper format.");

            Coordinates coordinates;
            IOrientation orientation;

            try
            {
                coordinates = new Coordinates(Convert.ToInt32(coordinatesAndOrientation[0]),
                                                  Convert.ToInt32(coordinatesAndOrientation[1]));

                orientation = OrientationFactory.GenerateOrientation(coordinatesAndOrientation[2]);
            }
            catch (Exception ex)
            {
                throw new WrongRoverDefinitionException("Bad definition.", ex);
            }

            var rover = new Rover();

            rover.Init(coordinates, orientation);

            return rover;
        }
Пример #2
0
        public static void ExploreArea(Planet mars, Rover rover)
        {
            Console.WriteLine(string.Format("Initial rover location- X:{0}, Y:{1}, Direction:{2}", rover.XCoordinate, rover.YCoordinate, rover.RoverDirection.ToString()));
            foreach (char command in rover.Command)
            {
                Console.WriteLine("Processing : " + command);

                if (command == (char)Common.TurnDirection.L)
                {
                    rover.Turn(Common.TurnDirection.L);
                }
                else if (command == (char)Common.TurnDirection.R)
                {
                    rover.Turn(Common.TurnDirection.R);
                }
                else if (command == Common.MoveCommand)
                {
                    mars.MoveRover(rover);
                }
                else
                {
                    Console.WriteLine("Invalid Command: " + command);
                }

                Console.WriteLine(string.Format("{0}, {1}, {2}", rover.XCoordinate, rover.YCoordinate, rover.RoverDirection.ToString()));
            }

            Console.WriteLine(string.Format("Final rover location- X:{0}, Y:{1}, Direction:{2}", rover.XCoordinate, rover.YCoordinate, rover.RoverDirection.ToString()));
        }
Пример #3
0
        public bool AddRover(Rover rover)
        {
            if (!AreCoordinatesValid(rover.XCoordinate, rover.YCoordinate))
            {                 
                 return false;
            }

            this.plataeu[rover.XCoordinate, rover.YCoordinate] = rover;
            return true;
        }
Пример #4
0
        static void Main(string[] args)
        {
            Rover r1 = new Rover(1, 2, Vector.N);
            r1.Execute(L,M,L,M,L,M,L,M,M);
            Assert("Test 1", new Rover(1, 3, Vector.N).Equals(r1));

            Rover r2 = new Rover(3, 3, Vector.E);
            r2.Execute(M,M,R,M,M,R,M,R,R,M);
            Assert("Test 2", new Rover(5, 1, Vector.E).Equals(r2));

            Console.WriteLine("Tests passed!");
            Console.ReadLine();
        }
Пример #5
0
        private static Rover CreateRover(string definition)
        {
            var coordinatesAndOrientation = definition.Split(' ');

            var coordinates = new Coordinates(Convert.ToInt32(coordinatesAndOrientation[0]),
                                              Convert.ToInt32(coordinatesAndOrientation[1]));

            var orientation = OrientationFactory.GenerateOrientation(coordinatesAndOrientation[3]);

            var rover = new Rover();
            rover.Init(coordinates, orientation);

            return rover;
        }
Пример #6
0
 private static void ExecutadCommands(Rover rover, string stringCommands)
 {
     foreach (var command in stringCommands)
     {
         switch(command)
         {
             case 'L':
                 rover.TurnLeft();
                 break;
             case 'R':
                 rover.TurnRight();
                 break;
             case 'M':
                 rover.Move();
                 break;
         }
     }
 }
Пример #7
0
        public static Rover GetRover(string roverPosition, string instructions)
        {
            if (Regex.IsMatch(roverPosition, roverPositionFormat) && Regex.IsMatch(instructions, roverInstructionsFormat))
            {
                string[] roverDetail = roverPosition.Split(' ');
                int x = 0;
                int y = 0;
                if (int.TryParse(roverDetail[0], out x) && int.TryParse(roverDetail[1], out y))
                {
                    Common.Direction dir = (Common.Direction)Enum.Parse(typeof(Common.Direction), roverDetail[2]);
                    Rover rover = new Rover(x, y, dir);
                    rover.Command = instructions;
                    return rover;
                }
            }

            return null;
        }
Пример #8
0
        public bool MoveRover(Rover rover)
        {
            int oldXCoordinate = rover.XCoordinate;
            int oldYCoordinate = rover.YCoordinate;

            int newXCoordinate = oldXCoordinate;
            int newYCoordinate = oldYCoordinate;
            
            switch (rover.RoverDirection)
            {
                case Common.Direction.E:
                    ++newXCoordinate;
                    break;
                case Common.Direction.N:
                    ++newYCoordinate;
                    break;
                case Common.Direction.W:
                    --newXCoordinate;
                    break;
                case Common.Direction.S:
                    --newYCoordinate;
                    break;
            }

            if (!AreCoordinatesValid(newXCoordinate, newYCoordinate))
            {
                Console.WriteLine("Cannot Move. New coordinates are not valid. Skipping this movement.");
                return false;
            }
                                    
            rover.XCoordinate = newXCoordinate;
            rover.YCoordinate = newYCoordinate;
            this.plataeu[oldXCoordinate, oldYCoordinate] = null;
            this.plataeu[newXCoordinate, newYCoordinate] = rover;
            return true;
        }
Пример #9
0
 private static string[] UpdateRover(Rover rover)
 {
     string[] updatedInstructions = rover.position.Split(' ');
     return(updatedInstructions);
 }
Пример #10
0
 public TurnRightCommand(Rover rover)
 {
     this.rover = rover;
 }
Пример #11
0
        /// <summary>
        /// Main method that simulates the entire program
        /// </summary>
        /// <param name="args">File or arguments input</param>
        static public void Main(string[] args)
        {
            bool isFile = false;   // If the input is a file

            string[] input = args; // This is used so that inputs can be parsed if it is a file input


            // If correct file input format
            if (args.Length == 2)
            {
                /*
                 * Try reading in every line from the input file and then parsing it to match argument format
                 */
                try
                {
                    string[] lines     = File.ReadAllLines(args[0]);
                    string   fileInput = "";
                    for (int i = 0; i < lines.Length; i++)
                    {
                        fileInput += lines[i] + " ";
                    }
                    input  = fileInput.Split(' ');
                    isFile = true;
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("File not found");
                }
            }


            // If the argument input is valid or it is a valid file input
            if (ValidInput(args) || isFile)
            {
                try
                {
                    int width  = int.Parse(input[0]);
                    int height = int.Parse(input[1]);

                    if (width <= 0 || height <= 0)
                    {
                        Console.WriteLine("Height and width values must be greater than 0");
                        throw new FormatException();
                    }

                    Grid grid = new Grid(width, height);

                    int numRovers = (input.Length - 2) / 4;


                    // Run through program for each rover
                    for (int i = 0; i < numRovers; i++)
                    {
                        int  x       = int.Parse(input[2 + (i * 4)]);
                        int  y       = int.Parse(input[3 + (i * 4)]);
                        char heading = char.Parse(input[4 + (i * 4)]);

                        if (!ValidRover(width, height, x, y, heading))
                        {
                            Console.WriteLine("Invalid rover starting position/heading");
                            throw new FormatException();
                        }

                        Point startingPosition = new Point(x, y);

                        Rover rover = new Rover(startingPosition, heading);

                        string instructions = input[5 + (i * 4)];


                        // Handle instructions
                        foreach (char instruction in instructions)
                        {
                            if (instruction == (char)Instructions.L || instruction == (char)Instructions.R)
                            {
                                rover.Rotate(instruction);
                            }
                            else if (instruction == (char)Instructions.M)
                            {
                                rover.Move(grid);
                            }
                        }

                        // If the file argument format was chosen, output to file
                        if (isFile)
                        {
                            if (i == 0)
                            {
                                File.WriteAllText(args[1], rover.ToString());
                            }
                            else
                            {
                                File.AppendAllText(args[1], '\n' + rover.ToString());
                            }
                        }
                        // If command line argument format was chosen, output to console
                        else
                        {
                            Console.WriteLine(rover.ToString());
                        }
                    }
                }
                catch (FormatException)
                {
                    Console.Write("Inputs are not the correct format");
                }
            }
            else
            {
                Console.WriteLine("Invalid input");
                Console.WriteLine("Input can be command line arguments with the following format: <grid_width> <grid_height> <rover_x_position> <rover_y_position> <rover_heading> <instructions>");
                Console.WriteLine("Input can be a input and output file with the following format: <input_file> <output_file>");
            }
        }
Пример #12
0
 public TurnLeftCommand(Rover rover)
 {
     this.rover = rover;
 }