Exemple #1
0
        // Display score and incorrect answers
        void FinaliseQuiz()
        {
            List <QuestionCard> incorrect = (from x in listOfQuestionCards where x.AnswerCorrect == false select x).ToList();

            // New string array to hold lines for the question box
            string[] strArr = new string[incorrect.Count + 2];

            int score = listOfQuestionCards.Count - incorrect.Count;

            strArr[0] = $"Your score = {score}/{listOfQuestionCards.Count}\n";

            if (incorrect.Count != 0)
            {
                strArr[1] = "Questions you got wrong:\n";

                // Display each incorrect answer
                for (int i = 2; i < incorrect.Count + 2; i++)
                {
                    strArr[i] = incorrect[i - 2].Question + " -> Your answer was: " + incorrect[i - 2].PlayerAnswer + "\n";
                }
            }


            if (currentUser.StoreResultsInDatabase(topic, score, difficulty) == -1)
            {
                MessageBox.Show("Database connection error, could not store results", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            qBox.Lines = strArr;
            BtnAnswer.Hide();
            rbAnswer1.Hide();
            rbAnswer2.Hide();
            rbAnswer3.Hide();
            rbAnswer4.Hide();
        }
Exemple #2
0
        void StartQuiz()
        {
            // Create a new list of QuestionCards
            listOfQuestionCards = new List <QuestionCard>();
            index = 0;

            // Create new XmlSerializer to deserialize the xml files for questions
            XmlSerializer serializer = new XmlSerializer(typeof(List <QuestionCard>));

            topic = drpdwnSubject.Text;

            switch (topic)
            {
            case "Computer Science":
                using (FileStream fs = File.OpenRead("Data/csQuestions.xml"))
                {
                    // Deserialize xml file to get list of question cards, and order randomly, then select 5
                    listOfQuestionCards = ((List <QuestionCard>)serializer.Deserialize(fs)).OrderBy(x => rnd.Next()).Take(5).ToList();
                }
                break;

            case "History":
                using (FileStream fs = File.OpenRead("Data/histQuestions.xml"))
                {
                    // Deserialize xml file to get list of question cards, and order randomly, then select 5
                    listOfQuestionCards = ((List <QuestionCard>)serializer.Deserialize(fs)).OrderBy(x => rnd.Next()).Take(5).ToList();
                }
                break;

            case "Music":
                using (FileStream fs = File.OpenRead("Data/musicQuestions.xml"))
                {
                    // Deserialize xml file to get list of question cards, and order randomly, then select 5
                    listOfQuestionCards = ((List <QuestionCard>)serializer.Deserialize(fs)).OrderBy(x => rnd.Next()).Take(5).ToList();
                }
                break;
            }

            if (drpdwnDifficulty.Text != "" && drpdwnSubject.Text != "")
            {
                difficulty = drpdwnDifficulty.Text;

                BtnAnswer.Show();
                rbAnswer1.Show();
                rbAnswer2.Show();

                SetScreen(listOfQuestionCards[0]);
            }
        }