Exemplo n.º 1
0
        /// <summary>
        /// Initialize inputs from console, then do calculations.
        /// </summary>
        public void InitilazeInputsAndCalculate()
        {
            var plateau = new Plateau();

            #region Plateau Size
            var    isUpperRightCoordinatesValid = false;
            string upperRightCoordinatesInput   = "";
            while (!isUpperRightCoordinatesValid)
            {
                Output.WriteLine(ConsoleColor.Yellow, UserFirendlyMessages.ENTER_UPPER_RIGHT_COORDINATES);
                upperRightCoordinatesInput = Console.ReadLine();

                isUpperRightCoordinatesValid = _inputValidator.IsUpperRightCoordinatesValid(upperRightCoordinatesInput);

                if (!isUpperRightCoordinatesValid)
                {
                    Output.WriteLine(ConsoleColor.Red, UserFirendlyMessages.UPPER_RIGHT_COORDINATES_NOT_VALID);
                }
            }

            var upperRightCoordinates = Regex.Replace(upperRightCoordinatesInput.Trim(), @"\s+", " ").Split(" ");
            var upperRightX           = int.Parse(upperRightCoordinates[0]);
            var upperRightY           = int.Parse(upperRightCoordinates[1]);

            plateau.UpperRightCoordinateX = upperRightX;
            plateau.UpperRightCoordinateY = upperRightY;
            #endregion

            var addRover  = true;
            var roverList = new List <Rover>();
            var order     = 1;
            while (addRover)
            {
                #region Validate Position
                string positionInput   = "";
                var    isPositionValid = false;
                while (!isPositionValid)
                {
                    Output.WriteLine(ConsoleColor.Yellow, UserFirendlyMessages.ENTER_POSITION);
                    positionInput   = Console.ReadLine();
                    isPositionValid = _inputValidator.IsRoverPositionValid(positionInput, upperRightX, upperRightY);

                    if (!isPositionValid)
                    {
                        Output.WriteLine(ConsoleColor.Red, UserFirendlyMessages.POSITION_NOT_VALID);
                    }
                }
                #endregion

                #region Validate Command
                var isCommandValid = false;
                var commandInput   = "";
                while (!isCommandValid)
                {
                    Output.WriteLine(ConsoleColor.Yellow, UserFirendlyMessages.ENTER_COMMAND_ROVER);
                    commandInput   = Console.ReadLine();
                    isCommandValid = _inputValidator.IsCommandValid(commandInput);
                    if (!isCommandValid)
                    {
                        Output.WriteLine(ConsoleColor.Red, UserFirendlyMessages.COMMAND_NOT_VALID);
                    }
                }
                #endregion

                #region Create Rover

                var roverPosition = GetPositionByInputString(positionInput);

                roverList.Add(new Rover()
                {
                    Order    = order,
                    Position = roverPosition,
                    Command  = commandInput.Trim().ToUpper()
                });

                #endregion

                var addRoverMenu = new EasyConsoleCore.Menu()
                                   .Add("Add one more rover", () => addRover = true)
                                   .Add("Continue", () => addRover           = false);
                addRoverMenu.Display();
                order++;
            }
            plateau.Rovers = roverList;

            CalculateMovements(plateau);
            DisplayOutputs(plateau);

            Console.ReadLine();
        }