public Question GetNextQuestion() { Question returnQuestion = null; if (Questions.Any() && Strikes > 0) { int index = 0; while (returnQuestion == null) { if (Randomizer.IsSelected(1.0 / (double)Questions.Count)) { returnQuestion = Questions.ElementAt(index); Questions.RemoveAt(index); } index++; if (index >= Questions.Count) { index = 0; } } } return(returnQuestion); }
public TenQuestionQuiz(QuizContext context, Category category) { _context = context; QuizCategory = category; Questions = new List <Question>(); double questionsToChoose = 10; double totalQuestions = _context.Questions.Count(); CurrentQuestionIndex = 1; while (Questions.Count < 10) { Question currentQuestion = _context.Questions.Where(c => c.CategoryId == QuizCategory.CategoryId).FirstOrDefault(q => q.QuestionId == CurrentQuestionIndex); if (currentQuestion == null) { if (CurrentQuestionIndex > _context.Questions.Max(q => q.QuestionId)) { CurrentQuestionIndex = 0; } } else { if (!Questions.Contains(currentQuestion)) { if (Randomizer.IsSelected(questionsToChoose / totalQuestions)) { Questions.Add(currentQuestion); questionsToChoose -= 1; } } } CurrentQuestionIndex++; } List <int> chosenQuestionIds = Questions.Select(q => q.QuestionId).ToList(); Answers = _context.Answers.Where(a => chosenQuestionIds.Contains(a.QuestionId)).ToList(); CurrentQuestionIndex = 0; }