예제 #1
0
        public static void BeginGuessing()
        {
            int  points        = 0;
            int  fails         = 0;
            bool floatingPoint = true;

            while (true)
            {
                char digit = Console.ReadKey().KeyChar;

                bool isCorrect = DigitChecker.CheckDigit(digit);

                if (isCorrect)
                {
                    points++;
                }
                else
                {
                    // Remove the digit and replace it with the correct one. Change its colour so that it's clear it was wrong.
                    char correctDigit = DigitChecker.GetCorrectDigit(points + fails);
                    Console.Write("\b");

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(correctDigit);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    fails++;

                    if (fails == 3)
                    {
                        // End the game.
                        Console.WriteLine($"\nGame over. You scored {points} points!");
                        return;
                    }
                }

                if (floatingPoint)
                {
                    // Add a comma after the first digit entered.
                    Console.Write('.');
                    floatingPoint = false;
                }
            }
        }
예제 #2
0
        private void DigitButton_Click(object sender, RoutedEventArgs e)
        {
            string digitString = (sender as Button).Content.ToString();
            char   digit       = char.Parse(digitString);

            bool isCorrect = DigitChecker.CheckDigit(digit);

            if (isCorrect)
            {
                DigitsBlock.Text += digit;
                _scoreboard.Points++;
            }
            else
            {
                char correctDigit = DigitChecker.GetCorrectDigit(_scoreboard.Points + _scoreboard.Fails);

                DigitsBlock.Text += correctDigit;

                _scoreboard.Fails++;

                if (_scoreboard.Fails == 3)
                {
                    MessageBox.Show($"Game over! You scored {_scoreboard.Points} points.", "Game over!");

                    foreach (var elem in MainGrid.Children)
                    {
                        if (elem is Button b)
                        {
                            b.IsEnabled = false;
                        }
                    }
                }
            }

            if (_floatingPoint)
            {
                DigitsBlock.Text += '.';
                _floatingPoint    = false;
            }
        }