Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Plateau Size (Example: 5 5)");
            var     sizeInput = Console.ReadLine();
            Plateau plateau   = InputHelper.GetPlateau(sizeInput);

            List <IRover> roverList = new List <IRover>();

            while (true)
            {
                Console.WriteLine("Rover Position (Example: 1 2 N)");
                var roverPositionInput = Console.ReadLine();

                Console.WriteLine("Rover Command (Example: LMLMLMLMM)");
                var roverCommandInput = Console.ReadLine();

                RoverPosition roverPosition = InputHelper.GetRoverPosition(roverPositionInput);

                bool isValidCommandInput = InputHelper.CheckInput(InputHelper.CommandRegexPattern, roverCommandInput);
                if (!isValidCommandInput)
                {
                    throw new InvalidInputException(string.Format("{0} is not valid command", roverCommandInput));
                }

                if (roverPosition != null && plateau != null)
                {
                    IRover rover = new Rover(plateau, roverPosition);
                    rover.Run(roverCommandInput);
                    roverList.Add(rover);
                }

                Console.WriteLine("Add another rover? (Y/N)");
                var addRoverInput = Console.ReadLine();

                if (!addRoverInput.Equals("Y", StringComparison.InvariantCultureIgnoreCase))
                {
                    break;
                }
            }

            foreach (var r in roverList)
            {
                Console.WriteLine(r.GetCurrentLocation());
            }
        }
        /// <summary>
        /// 限制输入
        /// </summary>
        /// <param name="ctrl">控件实例</param>
        /// <param name="mode">输入模式</param>
        public static void LimitInput(this TextBox ctrl, TextInputMode mode)
        {
            if (null == ctrl || TextInputMode.All == mode)
            {
                return;
            }

            ctrl.KeyPress += (sender, e) =>
            {
                //控制字符总是允许输入
                if (InputHelper.IsControlChar(e.KeyChar))
                {
                    return;
                }

                var success = InputHelper.CheckInput(mode, e.KeyChar, () => ctrl.Text, false);
                if (!success)
                {
                    e.Handled = true;
                    return;
                }
            };
        }