Пример #1
0
        public int Run()
        {
            NumberGuessingGame game = new NumberGuessingGame(1, 99);

            String entry = "";

            while (!"0".Equals(entry) && !game.GameOver)
            {
                Console.WriteLine("Guess my number that is somewhere from 1 to 99. You have {0} tries left!", game.RoundsLeft);
                Console.WriteLine("Enter your next guess, or 0 to quit:");

                int nr = -1;
                do
                {
                    entry = Console.ReadLine();
                } while (!Int32.TryParse(entry, out nr));

                if (nr != 0)
                {
                    GuessResult result = game.Guess(nr);
                    if (GuessResult.Bingo == result)
                    {
                        Console.WriteLine("That´s it, congratulations!");
                    }
                    else if (GuessResult.YourInputWasTooHigh == result)
                    {
                        Console.WriteLine("The number you should guess is lower than that!");
                    }
                    else
                    {
                        Console.WriteLine("The number you should guess is higher than that!");
                    }
                }
            }

            if (game.GameOver)
            {
                return(game.RoundsLeft);
            }
            else
            {
                // cancelled -> 0 points!
                return(0);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            while (true)
            {
Begin:
                int option;
                try
                {
                    Console.WriteLine("++++++++++++++++++ Hello Console Gamer! ++++++++++++++++++");
                    Console.WriteLine("Choose a game to play:\n" +
                                      "1. Number Guessing Game \t2.Word Guessing Game");


                    option = Convert.ToInt32(Console.ReadLine());

                    if (option < 1 || option > 2)
                    {
                        throw new ArgumentOutOfRangeException("option", "choosen option must be in given options.");
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    Console.WriteLine("\nThere is an Error!\n" +
                                      "Please choose the right option\n");
                    goto Begin;
                }
                catch (Exception)
                {
                    Console.WriteLine("\nThere is an Error!\n" +
                                      "Please choose a valid number\n");
                    goto Begin;
                }

                if (option == 1)
                {
                    var choosenGame = new NumberGuessingGame();
                    choosenGame.Home();
                }
                else if (option == 2)
                {
                    WordGuessingGame.Home();
                }
            }
        }