Example #1
0
        /// <summary>
        /// Starts the game
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartGame_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Make the game inputs visible
                btnStartGame.Visibility = Visibility.Hidden;
                btnSubmit.Visibility    = Visibility.Visible;
                boxAnswer.Visibility    = Visibility.Visible;

                // Focus on the Answer box
                Dispatcher.BeginInvoke(new Action(() => boxAnswer.Focus()));

                // Create the game
                ArithmeticianGame          = new ArithmeticianGame(CurrentUser, GameType);
                lblCurrentQuestion.Content = ArithmeticianGame.CurrentQuestion;

                //Tell it which method will handle the click event
                ArithmeticianGame.GameTimer.Tick += new EventHandler(GameTimer_Tick);
                ArithmeticianGame.GameTimer.Start();

                // Display the question
                lblQuestion.Content = ArithmeticianGame.GenerateQuestion();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }
Example #2
0
        /// <summary>
        /// Submits the Answer to the game
        /// </summary>
        private async Task SubmitAnswer()
        {
            // Check the answer
            int.TryParse(boxAnswer.Text.ToString(), out int answer);
            if (ArithmeticianGame.CheckAnswer(lblQuestion.Content.ToString(), answer))
            {
                lblPreviousCorrect.Content = "O";
                ArithmeticianGame.GameScore.CorrectAnswerCount++;
                lblPreviousCorrect.Foreground = Brushes.Green;
            }
            else
            {
                lblPreviousCorrect.Content = "X";
                ArithmeticianGame.GameScore.IncorrectAnswerCount--;
                lblPreviousCorrect.Foreground = Brushes.Red;
            }

            // Finish the game if it was the tenth question
            if (ArithmeticianGame.CurrentQuestion >= 10)
            {
                FinishGame();
                return;
            }

            ArithmeticianGame.CurrentQuestion++;

            // Change the labels then clear and focus the answer box
            lblCurrentQuestion.Content = ArithmeticianGame.CurrentQuestion;
            lblQuestion.Content        = ArithmeticianGame.GenerateQuestion();
            boxAnswer.Text             = null;
            boxAnswer.Focus();

            // Show the if the previous answer is correct or not for a second
            await Task.Delay(1000);

            var previousCorrect = lblPreviousCorrect.Content as string;

            if (!string.IsNullOrWhiteSpace(previousCorrect))
            {
                lblPreviousCorrect.Content    = null;
                lblPreviousCorrect.Background = Brushes.Transparent;
            }
        }