Ejemplo n.º 1
0
        /// <summary>
        /// Helper function that downloads question from the API
        /// </summary>
        /// <returns>Array of questions</returns>
        QuizQuestion[] DownloadQuestions()
        {
            Func <string, string> decode = s => WebUtility.HtmlDecode((string)s);

            url = GenerateAPIUrl();
            var json     = FetchJSON(url);
            var response = JObject.Parse(json);


            if ((int)response["response_code"] != 0)
            {
                QuizAPI.ExitApp("Invalid response. Questions could not be downloaded.");
            }

            JArray questionArray = (JArray)response["results"];

            return(questionArray.Select(q => new QuizQuestion()
            {
                Category = decode((string)q["category"]),
                Type = decode((string)q["type"]),
                Difficulty = decode((string)q["difficulty"]),
                Question = decode((string)q["question"]),
                CorrectAnswer = decode((string)q["correct_answer"]),
                IncorrectAnswers = ((JArray)q["incorrect_answers"]).Select(cat => decode((string)cat)).ToArray()
            }).ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts the quiz game
        /// </summary>
        public void Start()
        {
            var quizApi    = new QuizAPI();
            var categories = quizApi.GetCategoryList();

            Console.WriteLine("Choose your category");
            var categoryIndex = FetchIndexFromList(categories);

            quizApi.SetCategory(categoryIndex);

            Console.Clear();
            Console.Write("Amount of questions: ");
            int amount;

            while (!int.TryParse(Console.ReadLine(), out amount))
            {
                Console.WriteLine("Enter a valid number!");
                Console.Write("Amount of questions: ");
            }
            quizApi.SetQuestionAmount(amount);

            var diffs = Enum.GetValues(typeof(QuizAPI.Difficulty)).Cast <QuizAPI.Difficulty>().ToList();

            Console.Clear();
            Console.WriteLine("Choose your difficulty");
            var diffIndex = FetchIndexFromList(diffs);

            quizApi.SetDifficulty((QuizAPI.Difficulty)diffIndex);

            var qManager = quizApi.GetQuestionManager();

            while (qManager.HasNextQuestion())
            {
                Console.Clear();

                var question = qManager.NextQuestion();
                Console.WriteLine($"Question #{questionNumber++}: {question.Question} | Score [{qManager.Score}/{qManager.Count}]");

                var answers = CreateResultsList(question);
                var index   = FetchIndexFromList(answers);

                if (qManager.CheckAnswer(answers[index]))
                {
                    Console.WriteLine("Correct!");
                }
                else
                {
                    Console.WriteLine("Incorrect");
                }

                Console.ReadLine();
            }

            Console.Clear();
            Console.WriteLine($"Good Game. Your score is {qManager.Score}");
            Console.ReadLine();
        }