-1

I'm making a trivia game app in c# visual studio 2019 (using Windows Forms), but I don't know how to randomize my questions using fisher yates shuffle, I'm currently new in programming I need help thank you.

Here's my code.

I'm not sure how to put questions in an array.

{
    public partial class categoryquestion_form : Form
    {
        // quiz game variables

        int correctAnswer2;
        int questionNumber2 = 1;
        int score2;
        int percentage2;
        int totalQuestions2;

        public categoryquestion_form()
        {
            InitializeComponent();

            askquestionInventors(questionNumber2);

            totalQuestions2 = 5;
        }

        private void categoryquestion_form_Load(object sender, EventArgs e)
        {

        }
        private void checkAnswerEvent2(object sender, EventArgs e)
        {
            var senderObject2 = (Button)sender;

            int buttonTag2 = Convert.ToInt32(senderObject2.Tag);

            if (buttonTag2 == correctAnswer2)
            {
                score2++;
            }

            if (questionNumber2 == totalQuestions2)
            {
                // work out percentage

                percentage2 = (int)Math.Round((double)(score2 * 100) / totalQuestions2);

                MessageBox.Show(
                    "Quiz Ended!" + Environment.NewLine +
                    "You have answered " + score2 + " questions correctly." + Environment.NewLine +
                    "Your total percentage is " + percentage2 + "%" + Environment.NewLine + "Click OK to return menu" + Environment.NewLine
                    );

                score2 = 0;
                questionNumber2 = 0;
                askquestionInventors(questionNumber2);

                this.Close();
                mainmenu_form f1 = new mainmenu_form();
                f1.ShowDialog();
            }

            questionNumber2++;
            askquestionInventors(questionNumber2);
        }
            private void askquestionInventors(int qnum2)
        {
            switch (qnum2)
            {
                case 1:

                    pictureBox_2.Image = Properties.Resources.alberteinstein;

                    question_label2.Text = "Which famous inventor worked at a patent office?";

                    btn1.Text = "Nikola Tesla";
                    btn2.Text = "Thomas Edison";
                    btn3.Text = "Albert Einstein";
                    btn4.Text = "Albert Wesker";

                    correctAnswer2 = 3;

                    break;

                case 2:

                    pictureBox_2.Image = Properties.Resources.niepcecamera;

                    question_label2.Text = "The Niepce Crater on the Moon was named in recognition of the famous inventor Joseph Niepce. What did Niepce invent?";

                    btn1.Text = "A telescope";
                    btn2.Text = "A camera";
                    btn3.Text = "A microscope";
                    btn4.Text = "A nanoscope";

                    correctAnswer2 = 2;

                    break;

                case 3:

                    pictureBox_2.Image = Properties.Resources.inventionofradio;

                    question_label2.Text = "What do inventors, Tesla, Popov and Lodge have in common?";

                    btn1.Text = "They invented engines.";
                    btn2.Text = "They invented telephone technology.";
                    btn3.Text = "They invented electric motors.";
                    btn4.Text = "They invented radio technology.";

                    correctAnswer2 = 4;

                    break;

                case 4:

                    pictureBox_2.Image = Properties.Resources.oliverevans;

                    question_label2.Text = "The invention of the refrigerator made a significant improvement to our standard of living. When it was invented, not many people wanted one. Who invented the refrigerator ?";

                    btn1.Text = "Oliver Evans";
                    btn2.Text = "Michael Faraday";
                    btn3.Text = "Carl Von Linde";
                    btn4.Text = "Benjamin Franklin";

                    correctAnswer2 = 1;

                    break;

                case 5:

                    pictureBox_2.Image = Properties.Resources.farnsworth;

                    question_label2.Text = "Many inventors contributed to the technology that created the television. There were conflicts between inventors over their rights to various aspects of this technology. Who invented the television?";

                    btn1.Text = "John Baird";
                    btn2.Text = "Philo Farnsworth";
                    btn3.Text = "Vladimir Zworykin";
                    btn4.Text = "Carl Von Linde";

                    correctAnswer2 = 2;

                    break;
            }
        }
      }
   }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Joshua
  • 1

1 Answers1

0

In my mind, this is a good time to learn about Object Oriented Programming(OOP). A class that holds all the information you need, allows you to create instances for each question and hold them in an indexed collection(array, List<>, etc.), which can easily be shuffled with the Fisher-Yates algorithm

Here's what a class could look like:

class MultiChoiceQuestion
{
    public string Question{get;set;} = "";
    public List<string> Choices{get;set;} = new List<string>();
    public string Answer{get;set;} = "";

    public MultiChoiceQuestion(){}
    public MultiChoiceQuestion(string question, List<string> choices, string answer)
    {
        Question = question;
        Choices = choices;
        Answer = answer;
    }
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22