Exemple #1
0
        /// <summary>
        /// Check if the users answer is correct
        /// </summary>
        /// <param name="answer"></param>
        public void CheckIfAnswerCorrect(int answer, Question question)
        {
            // Get the correct answer from the list of answers
            var correctAnswer = question.answers.First(x => x.Id == question.correctAnswer);

            if (answer != question.correctAnswer)
            {
                // Set the users answer to false / wrong
                question.IsCorrect = false;

                // Get the matching answer to teh users answer from the lsit of answers
                var inCorrectUsersAnswer = question.answers.First(x => x.Id == question.UsersGuess);

                // Display the failure message using the users incorrect answer, and the correct answer
                QuizMessageOutputs.FailureMessage(inCorrectUsersAnswer.Answer, correctAnswer.Answer);
            }
            else
            {
                // Set the users answer to true / correct
                question.IsCorrect = true;

                // Display the success message using the correct answer
                QuizMessageOutputs.SuccessMessage(correctAnswer.Answer);
            }
        }
Exemple #2
0
        /// <summary>
        /// Check the users answer to ensure write type of data input, and input is possible answer
        /// </summary>
        /// <param name="answers"></param>
        /// <returns></returns>
        private int CheckUserInput(Question question)
        {
            // Get the users answer
            var usersAnswer = Console.ReadLine();

            // Get a list of all possible answers in the question
            List <int> allPossibleAnswers = GetAllPossibleAnswers(question.answers);

            // Create a variable to hold the int value of the answer
            int answer;

            // Run so long as the user does not guess an answer that is a possible answer
            while (usersAnswer == null || !Int32.TryParse(usersAnswer, out answer) || !allPossibleAnswers.Contains(answer))
            {
                // Make sure that the users answer is not some random high number
                usersAnswer = usersAnswer.ToString();

                // Prompt the user to reenter thier answer
                QuizMessageOutputs.ReenterMessage(usersAnswer);

                // Get the users answer again
                usersAnswer = Console.ReadLine();
            }

            // Record the users guess
            question.UsersGuess = answer;

            // Return the answer
            return(answer);
        }
Exemple #3
0
        public void RunQuiz(List <Question> questions)
        {
            if (questions == null)
            {
                Console.WriteLine("There was a problem with the quiz questions.");
            }

            foreach (var question in questions)
            {
                // Display the question
                QuizMessageOutputs.DisplayQuestion(question);

                // Get the users answer
                int answer = CheckUserInput(question);

                // Check if answer is correct
                CheckIfAnswerCorrect(answer, question);

                // Add to users score if need be
                AdjustScore(question);
            }

            QuizMessageOutputs.DisplayScore(quizScore);
        }