public void ChangeQuestion(string set) { UserQuestions userQuestions = new UserDatabase().getUserQuestions(username); Console.WriteLine(set); List<string[]> questions = userQuestions.getQuestions(set); Random random = new Random(); int questionIndex = -1; do{ questionIndex = random.Next(questions.Count); }while(questions.Count > 1 && questions[questionIndex][0].Equals(QuestionLabel.Text)); QuestionLabel.Text = questions[questionIndex][0]; int button1TextIndex, button2TextIndex, button3TextIndex; button1TextIndex = random.Next(3) + 1; do { button2TextIndex = random.Next(3) + 1; } while (button2TextIndex == button1TextIndex); do { button3TextIndex = random.Next(3) + 1; } while (button3TextIndex == button1TextIndex || button3TextIndex == button2TextIndex); if (button1TextIndex == 1) { correctAnswerButton = "Answer1"; } else if (button2TextIndex == 1) { correctAnswerButton = "Answer2"; } else if (button3TextIndex == 1) { correctAnswerButton = "Answer3"; } Answer1Button.BackColor = Color.Gainsboro; Answer2Button.BackColor = Color.Gainsboro; Answer3Button.BackColor = Color.Gainsboro; Answer1Button.Text = questions[questionIndex][button1TextIndex]; Answer2Button.Text = questions[questionIndex][button2TextIndex]; Answer3Button.Text = questions[questionIndex][button3TextIndex]; }
// Used to change the question that the player is answering using a subject / tier public void ChangeQuestion(string set) { UserQuestions userQuestions = new UserDatabase().getUserQuestions(username); // Create a new UserDatabase and extract and store the User Questions of the active user List<string[]> questions = userQuestions.getQuestions(set); // Save the questions from the appropriate set in a list Random random = new Random(); // Create a new Random number generator int questionIndex = -1; // Default the question index to -1 do { // keep trying to find a new question questionIndex = random.Next(questions.Count); // Find a random index to check } while (questions.Count > 1 && questions[questionIndex][0].Equals(QuestionLabel.Text)); // Check the index QuestionLabel.Text = questions[questionIndex][0]; // Set the text of the question label to our newly found question int[] buttonTextIndexes = { 1, 2, 3 }; // Create integers to store the index of the text that will be displayed by each button // Shuffle the indexes of the text to be displayed on the buttons for (int i = 0; i < 3; i++) // For each button text index { int changeIndex = random.Next(i, 3); // Find another index to be swapped with our current one (that is higher than our current one) int tempIndex = buttonTextIndexes[i]; // Temporarily store the first value which is to be overwritten in the swap buttonTextIndexes[i] = buttonTextIndexes[changeIndex]; // Overwrite the first value buttonTextIndexes[changeIndex] = tempIndex; // Overwrite the second value } if (buttonTextIndexes[0] == 1) // If button1 has the index of the correct answer { correctAnswerButton = "Answer1"; // Save the answer which is the correct one } else if (buttonTextIndexes[1] == 1) // If button2 has the index of the correct answer { correctAnswerButton = "Answer2"; // Save the answer which is the correct one } else if (buttonTextIndexes[2] == 1) // If button3 has the index of the correct answer { correctAnswerButton = "Answer3"; // Save the answer which is the correct one } Answer1Button.BackColor = Color.Gainsboro; // Reset the colour of all buttons (They become red when the player makes a mistake) Answer2Button.BackColor = Color.Gainsboro; Answer3Button.BackColor = Color.Gainsboro; Answer1Button.Text = questions[questionIndex][buttonTextIndexes[0]]; //Assign the text of the buttons to their randomly chosen answers Answer2Button.Text = questions[questionIndex][buttonTextIndexes[1]]; Answer3Button.Text = questions[questionIndex][buttonTextIndexes[2]]; }