コード例 #1
0
        static ComputerPlayer()
        {
            Console.Clear();
            Console.WriteLine("For this game, you will think of a number between 1 and 100");
            Console.WriteLine("The computer will print it's guess on the screen and you'll reply with");
            Console.WriteLine("L: Too low, H: Too high, C: correct number");
            Console.WriteLine("Hit any key when you are ready to play");
            Console.ReadKey();

            int[] arr = new int[2] {
                1, 100
            };

            for (int i = 1; i < 100; i++)
            {
                int guess = BisectionMethod.ComputerGuess(arr[0], arr[1]);

                Console.WriteLine($"\nIs your number {guess}");



                var userInput = Console.ReadKey();

                while (userInput.Key != ConsoleKey.L && userInput.Key != ConsoleKey.H && userInput.Key != ConsoleKey.C)
                {
                    Console.WriteLine();
                    Console.WriteLine("Invalid input. Try Again.");
                    userInput = Console.ReadKey();
                }

                if (userInput.Key == ConsoleKey.L)
                {
                    arr[0] = (arr[0] + arr[1]) / 2 + 1;
                }

                if (userInput.Key == ConsoleKey.H)
                {
                    arr[1] = (arr[0] + arr[1]) / 2 - 1;
                }

                if (userInput.Key == ConsoleKey.C)
                {
                    Console.WriteLine($"\nYay! I got the answer and it took me {i} tries.");
                    Console.WriteLine("Hit any key return to menu");
                    Console.ReadKey();
                    arr[0] = 1;
                    arr[1] = 100;

                    Console.Clear();
                    Menu.MenuOptions();
                }
            }
        }
コード例 #2
0
ファイル: InitialGame.cs プロジェクト: ral1988/CSExercises
        static InitialGame()
        {
            var check = new CheckNumbers();

            Console.Clear();

            Console.WriteLine("I am going to show you how I can guess your number using the bisection algorithm");
            Console.WriteLine("Enter a number between 1-10: ");
            int numberToGuess = BisectionMethod.NumberGen(1, 10);

            for (int i = 1; i < 10; i++)
            {
                string input = Console.ReadLine();
                check.InbetweenNumbersCheck(1, 10, input);

                int convertToInt = int.Parse(input);

                if (convertToInt > numberToGuess)
                {
                    Console.WriteLine("You guessed too high. Try again");
                }

                if (convertToInt < numberToGuess)
                {
                    Console.WriteLine("You guessed too low. Try again");
                }

                if (convertToInt == numberToGuess)
                {
                    Console.WriteLine("You guessed correctly!");
                    Console.WriteLine($"It took you {i} tries to guess {numberToGuess}!");
                    Console.WriteLine("Hit any key to continue");
                    Console.ReadKey();

                    Console.Clear();
                    Menu.MenuOptions();
                }
            }
        }