1

So i'm currently attempting to create a quiz game to help with my revision at School. It displays a question and you need to choose which button answers the question correctly to get the "point". At the moment, the answer is always on the leftmost button, and i can't figure out a way to randomise the position of the answer. Any help would be appreciated. https://i.stack.imgur.com/e1PUr.jpg

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        QuestionLbl.Text = GetQuestion();
        Answer1Btn.Text = GetAnswer();
        Answer2Btn.Text = GetFalse1();
        Answer3Btn.Text = GetFalse2();
    }

    public static class Globals
    {
        public static int QNum = GetRandomNumber();
        public static int FalseNum1 = GetRandomNumber();
        public static int FalseNum2 = GetRandomNumber();
    }

    private void Answer1Btn_Click(object sender, EventArgs e)
    {

    }


    private static readonly Random getrandom = new Random();


    public static int GetRandomNumber()
    {
        lock (getrandom) // synchronize
        {
            return getrandom.Next(0, 6);
        }
    }


    public static string GetQuestion()
    {
        string[] Questions = {
            "What Is The Formula For Speed?",
            "What Is The Formula For Energy Transfered?",
            "What Is The Formula For Efficiency?",
            "What Is The Formula For Work Done?",
            "What Is The Formula For Gravitational Potential Energy?",
            "What Is The Formula For Elastic Potential Energy?",
            "What Is The Formula For Kinetic Energy"
        };

        string QText = Questions[Globals.QNum];
        return (QText);
    }


    public static string GetAnswer()
    {
        string[] Answers = {
            "Distance / Time",
            "Power * Time",
            "(useful power output ÷ actual power input) × 100 %",
            "Force * Distance",
            "mass × gravitational field strength × height",
            "1/2 × spring constant × extension2",
            "1/2 × mass × speed2"

        };

        string AnswerTxt = Answers[Globals.QNum];
        return (AnswerTxt);
    }
    public static string GetFalse1()
    {
        string[] Answers = {
            "Distance / Time",
            "Power * Time",
            "(useful power output ÷ actual power input) × 100 %",
            "Force * Distance",
            "mass × gravitational field strength × height",
            "1/2 × spring constant × extension2",
            "1/2 × mass × speed2"

        };
        int FalseNum;
        if (Globals.FalseNum1 == Globals.QNum)
        {
            FalseNum = GetRandomNumber();
        }
        else
        {
            FalseNum = Globals.FalseNum1;
        }
        string FalseAnswer = Answers[FalseNum];
        return (FalseAnswer);
    }

    public static string GetFalse2()
    {
        string[] Answers = {
            "Distance / Time",
            "Power * Time",
            "(useful power output ÷ actual power input) × 100 %",
            "Force * Distance",
            "mass × gravitational field strength × height",
            "1/2 × spring constant × extension2",
            "1/2 × mass × speed2"

        };


        Validation1:
        if (Globals.FalseNum2 == Globals.QNum)
        {
            Globals.FalseNum2 = GetRandomNumber();
            goto Validation1;
        }
        else
        { }

        Validation2:
        if (Globals.FalseNum2 == Globals.FalseNum1)
        {
            Globals.FalseNum2 = GetRandomNumber();
            goto Validation2;
        }
        else
        { }

        string FalseAnswer = Answers[Globals.FalseNum2];
        return (FalseAnswer);
    }
}

3 Answers3

1

If you have a List of references to the buttons, you can shuffle that list and then use the references in the list in order.

So, you'll need some way of shuffling a list. Fortunately, we can find a suitable method with a quick search and base the code on, say, Randomize a List. Step 1: add a new class to your project; name it "ListShuffle" (or whatever you want). Step 2: use this code in it:

using System;
using System.Collections.Generic;

namespace WindowsFormsApp1
{
    static class ListShuffle
    {
        private static Random rand = new Random();

        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count - 1;
            while (n > 1)
            {
                int k = rand.Next(n);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
                n--;
            }
        }

    }
}

Then, in your Form1.cs, take out the code in the constructor related to the answer buttons and use:

private void Form1_Load(object sender, EventArgs e)
{
    var buttons = new List<Button> { Answer1Btn, Answer2Btn, Answer3Btn };
    buttons.Shuffle();
    buttons[0].Text = "A";
    buttons[1].Text = "B";
    buttons[2].Text = "C";
}

and see how the letters ABC are randomly assigned each time the form is loaded.

After confirming that it works for that, adjust it to be:

buttons[0].Text = GetAnswer();
buttons[1].Text = GetFalse1();
buttons[2].Text = GetFalse2();

(I have to guess that it will work with the way you are checking the answer.)

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

One option would be to create a collection of your answer controls, create a second random number then use that get the index of the answer control in the collection.

You could have a switch statement after that sets the other answer control values. Here is some Pseudocode that give a general idea what I mean.

var answerControls = new List<Control>{answer1Btn, answer2Btn, answer3Btn};
var answerIndex = 0;
var answerRandom = new Random();
private void setAnswer(){
  answerIndex = answerRandom.Next(0, 2);
  var correctAnswer = answerControls.ElementAt(answerIndex);
  correctAnswer.Text = //get correct answer
  switch(answerIndex){
     case 0:
      answer2Btn.Text = //get false answer
      answer3Btn.Text = //get false answer
     break;
     ....
  }
}
Sean
  • 1,359
  • 11
  • 15
-1

I think it will be better, to use a database. For example, you can have A table for questions. In this table there must be a columns named QuestionID, QuestionText, Answer1, Answer2, Answer3, CorrectAnswer. So, when you call "GetQuestion()" get a random row of table and write Answer1 to buttonLeft(name is not important :D ), Answer2 to buttonMiddle, Answer3 to butonRight. Also, get correct answer from "CorrectAnswer" column and declare a variable to save it. At last, when user clicks button, call "CheckAnswer(char answer)" with given parameter. For example:

Given method names are not same as yours.

char CorrAnswer = GetCorrectAnswer(int QuestionID);
public bool CheckAnswer(char answer){
  if(answer.Equals(CorrAnswer)){
    return true;
  }
  else
    return false;
}

public void GetQuestion(){

}

public void ButtonClickForAnswer1(*some arguments here :D *){
  CheckAnswer('A')
}
public void ButtonClickForAnswer2(*some arguments here :D *){
  CheckAnswer('B')
}
public void ButtonClickForAnswer3(*some arguments here :D *){
  CheckAnswer('C')
}

By the way, it is easy to create database with Entity Framework Code-First.

Oh, also use switch-case to handle returned variable from CheckAnswer('A/B/C'); If it is true, load new question. Otherwise, execute MessageBox.Show("Wrong Answer!").

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alvan Rahimli
  • 348
  • 4
  • 10