public AllQuizzez() { AllQuizzezList = new List <Quiz>(); XmlDocument xDoc = new XmlDocument(); xDoc.Load(Path); XmlElement xRoot = xDoc.DocumentElement; foreach (XmlNode quiz in xRoot) { XmlNode quizAttr = quiz.Attributes.GetNamedItem("id"); int quizAttrId = Convert.ToInt32(quizAttr.Value); // получили id викторины quizAttr = quiz.Attributes.GetNamedItem("name"); string quizAttrName = quizAttr.Value; // получили название викторины List <XmlQuestionData> questionList = new List <XmlQuestionData>(); foreach (XmlNode item in quiz) { XmlNode itemAttr = item.Attributes.GetNamedItem("id"); int itemAttrId = Convert.ToInt32(itemAttr.Value); // получили id вопроса string question = default; List <string> options = new List <string>(); List <int> answers = new List <int>(); foreach (XmlNode childItem in item.ChildNodes) { if (childItem.Name == "question") { question = childItem.InnerText; // получили вопрос } if (childItem.Name == "possible") { options.Add(childItem.InnerText); // получили вариант ответа и добавили в массив } if (childItem.Name == "answer") { answers.Add(Convert.ToInt32(childItem.InnerText)); // получили номер правильного ответа и добавили в массив } } XmlQuestionData currentQuestion = new XmlQuestionData(itemAttrId, question, options, answers); questionList.Add(currentQuestion); } Quiz currentQuiz = new Quiz(quizAttrId, quizAttrName, questionList); AllQuizzezList.Add(currentQuiz); // добавили все викторины со всем содержимым в массив } }
public void StartMixedQuiz(AllQuizzez myAllQuizzez, Dictionary <KeyValuePair <string, string>, int> statistics) { Console.Clear(); Console.WriteLine($"Начинаем смешанную викторину!"); Console.WriteLine("\nДля продолжения нажмите любую кнопку..."); Console.ReadKey(); List <XmlQuestionData> allQuestions = new List <XmlQuestionData>(); foreach (Quiz quiz in myAllQuizzez.AllQuizzezList) { foreach (XmlQuestionData question in quiz.QuizList) { allQuestions.Add(question); } } Random rnd = new Random(); for (int l = 0; l < allQuestions.Count; l++) { int k = rnd.Next(0, l); XmlQuestionData value = allQuestions[k]; allQuestions[k] = allQuestions[l]; allQuestions[l] = value; } allQuestions.RemoveRange(20, allQuestions.Count - 20); int countRightQuestions = 0; int i = 0; while (i < allQuestions.Count) { Console.Clear(); Console.WriteLine(allQuestions[i].Question); Console.WriteLine(); for (int j = 0; j < allQuestions[i].PossibleOptionsList.Count; j++) { Console.WriteLine($"{j} - {allQuestions[i].PossibleOptionsList[j]}"); } Console.WriteLine("\nВведите номер правильного ответа."); Console.WriteLine("Если их несколько, введите номера подряд без пробелов и запятых и нажмите Enter, иначе ответ не засчитается."); Console.Write("\nВвод: "); string userAnswers = Console.ReadLine(); int countRightAnswers = 0; string tmp = ""; foreach (char answer in userAnswers) { if (userAnswers.Length != allQuestions[i].AnswersList.Count) { break; } foreach (int answ in allQuestions[i].AnswersList) { tmp += answ.ToString(); } if (tmp.Contains(answer)) { ++countRightAnswers; } } if (countRightAnswers == allQuestions[i].AnswersList.Count) { ++countRightQuestions; } i++; } KeyValuePair <string, string> del = new KeyValuePair <string, string>("Смешанная викторина", Login); statistics.Remove(del); statistics.Add(new KeyValuePair <string, string>("Смешанная викторина", Login), countRightQuestions); Menu.SaveStatisticsInFile(statistics); Console.Clear(); Console.WriteLine("Викторина окончена!"); Console.WriteLine($"Количество правильных ответов: {countRightQuestions} из {allQuestions.Count}."); Console.WriteLine(); ShowMyPlaceInCurrentQuiz(statistics, "Смешанная викторина"); }