Ejemplo n.º 1
0
        private static void GetUserInput()
        {
            //NUMBER PLAYERS
            InputValidationDelegate numberPlayersValidateDelegate =
                (input) => {
                int inputInt = Int32.Parse(input);
                if (1 < inputInt && inputInt < 6)
                {
                    return(true);
                }
                return(false);
            };
            InputWhenValidDelegate whenPlayersValidated = (input) => numberPlayers = Int32.Parse(input);

            //Send delegate into function that asks for input
            GetInput("Please insert the number of players [2-5]",
                     numberPlayersValidateDelegate, whenPlayersValidated);

            //WIN CONDITION
            InputValidationDelegate winConditionValidationDelegate =
                (input) => {
                int inputInt = Int32.Parse(input);
                if (1 < inputInt && inputInt < boardSize)
                {
                    return(true);
                }
                return(false);
            };
            InputWhenValidDelegate whenWinConditionValidated = (input) => winCondition = Int32.Parse(input);

            //Send delegate into function that asks for input
            GetInput($"Please chose win condition. Recommended is 3, 4 or 5",
                     winConditionValidationDelegate, whenWinConditionValidated);

            //BOARD SIZE
            InputValidationDelegate boardSizeValidationDelegate =
                (input) => {
                int inputInt = Int32.Parse(input);
                if (2 < inputInt)
                {
                    return(true);
                }
                return(false);
            };
            InputWhenValidDelegate whenBoardSizeValidated = (input) => boardSize = Int32.Parse(input);

            //Send delegate into function that asks for input
            GetInput($"Please chose boardsize. The recommended board size for {numberPlayers.ToString()} players is {playersRecBoardSize[numberPlayers]}",
                     boardSizeValidationDelegate, whenBoardSizeValidated);
        }
Ejemplo n.º 2
0
        private static void GetInput(string message, InputValidationDelegate inputStringValidationDelegate, InputWhenValidDelegate doWhenValidatedDelegate)
        {
            bool inputValid = false;

            do
            {
                Console.WriteLine(message);
                try
                {
                    string inputString = Console.ReadLine();
                    if (inputStringValidationDelegate(inputString))
                    {
                        doWhenValidatedDelegate(inputString);
                        inputValid = true;
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid input. Try again.");
                }
            } while (!inputValid);
        }