Exemplo n.º 1
0
        private static QuizItem GetRandomQuizItem(QuizRound round, out int index)
        {
            index = -1;
            if (_totalQuestions <= round.Answers.Count())
            {
                return(null);
            }

            var props = _root.Quiz.GetType().GetProperties();

            var val = _random.Next(0, _totalQuestions);

            var temp = round.Answers.SingleOrDefault(x => x.QuestionIndex == val);

            if (temp == null)
            {
                round.Answers.Add(new Answer {
                    QuestionIndex = val
                });
            }
            else
            {
                while (round.Answers.FirstOrDefault(x => x.QuestionIndex == val) != null)
                {
                    val = _random.Next(0, _totalQuestions);
                }

                round.Answers.Add(new Answer {
                    QuestionIndex = val
                });
            }

            var propInfo = props[val];
            var item     = propInfo.GetValue(_root.Quiz, null) as QuizItem;

            if (item != null)
            {
                _questionTotal = item.Options.Count();
            }

            index = val;
            return(item);
        }
Exemplo n.º 2
0
        private static void RunQuiz()
        {
            _totalQuestions = _root.Quiz.GetType().GetProperties().Count();

            Console.WriteLine(" ** Please enter a numeric value for each question.\n**" +
                              "Enter quit or exit to stop the quiz at any time.\n\n");

            var round = new QuizRound {
                Index = _round, Answers = new List <Answer>()
            };

            while (true)
            {
                int index;
                var item = GetRandomQuizItem(round, out index);

                if (item == null)
                {
                    break;
                }

                Console.WriteLine($"\n{item.Question}");
                for (var i = 1; i <= item.Options.Count; i++)
                {
                    Console.WriteLine($"{i}. {item.Options[i - 1]}");
                }

                _input = Console.ReadLine();
                if (Exiting(_input))
                {
                    break;
                }

                int value;
                while ((!int.TryParse(_input, out value) || value < 1 || value > item.Options.Count) && !Exiting(_input))
                {
                    Console.WriteLine($"Enter numeric value (1-{item.Options.Count})");
                    _input = Console.ReadLine();

                    if (Exiting(_input))
                    {
                        break;
                    }
                }

                if (Exiting(_input))
                {
                    break;
                }

                var answer = round.Answers.SingleOrDefault(x => x.QuestionIndex == index);
                if (answer != null)
                {
                    answer.Actual = item.Answer;
                    answer.Chosen = value;
                }
            }

            var correct = round.Answers.Count(answer => answer.Actual == answer.Chosen);

            var credit = 100 / _totalQuestions;

            round.Score = credit * correct;

            Console.WriteLine($"\nCurrent score: {round.Score}\n");

            var prevScores = GetPreviousScores();

            if (prevScores.Any())
            {
                Console.WriteLine($"Previous scores: {string.Join(", ", prevScores)}");
            }

            Rounds.Add(round);
        }