0

I am making a quiz game. I need to shuffle the arrays so that the questions, options, and answer arrays don't appear in order.

Here are the three arrays.

public string[] EasyQuestions = 
{
    "What is the capital of Australia?",
    "Who was Steve Jobs?",
    "What number Apollo took Neil Armstrong to the moon?",
    "Which metal is heavier, silver or gold?",
    "What country has the largest population?"
};

public string[] EasyOptions = 
{
    "Brisbane |Canberra |Adelaide |Australian Capital Territory",
    "CEO of Microsoft |Co-founder of Apple |Creator of IBM |Australian Politician",
    "10|11|12|1",
    "Silver|Gold|Gold|Silver",
    "'Murica|China|India|Australia"
};

public string[] EasyAnswers = 
{
    "Canberra",
    "Apple",
    "11",
    "Gold",
    "China"
};

I want all of the arrays to be shuffled identically so I don't get wrong answers with different options and different questions?

9
  • 6
    Perhaps you should look at creating classes for these things to make it easier on yourself. Commented Sep 20, 2013 at 2:49
  • 1
    I agree, make a QuizEntry class that has Question, Options, and Answer properties. Instantiate instances of this class, assign the values, throw them into a collection, then it becomes trivial to shuffle them. Commented Sep 20, 2013 at 2:52
  • How could I achieve this, any links. I thought of doing this, but had no idea how and what it is exactly called, I've only been programming for 6 months xD Commented Sep 20, 2013 at 2:55
  • @Trontor: Creating classes is a fundamental, basic part of C#. google.ca/#q=C%23+class+tutorial Commented Sep 20, 2013 at 2:57
  • 2
    @Trontor: If you want random order, then yes, you still have to shuffle. But you don't have to shuffle three arrays -- just one -- so keeping stuff in sync becomes a non-issue. Commented Sep 20, 2013 at 4:15

3 Answers 3

4

As I suggested in the comment, if you group everything together then you don't need to worry about keeping things in sync:

public class Question {

    private List<string> answers;

    public Question(string text, IEnumerable<string> answers, int answer) {
        this.Text = text;
        this.Answer = answer;

        this.answers = new List<string>(answers);
    }

    public string Text {
        get;
        private set;
    }

    public int Answer {
        get;
        private set;
    }

    public IEnumerable<string> Answers {
        get {
            return this.answers;
        }
    }

    public string GetAnswer() {
        return this.answers[this.Answer];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh, so this is how you do properties and such. Even though this doesn't directly answer my question, it is greatly appreciated.Thanks
Could you mind explaining how this eliminates the need of shuffling?
It doesn't, if you want to randomize it you'd still need to shuffle the collection, but you only have to shuffle a single collection. Then you also have the added benefit of having a logical data structure, instead of having a few disparate arrays, one with pipe separated values (gross).
3

Use Fisher-Yates shuffle. It shuffles the elements in a single pass.

Random rnd = new Random();

for (int i = 0; i < EasyQuestions.Length - 1; i++) {
    int j = rnd.Next(i, EasyQuestions.Length);

    string temp = EasyQuestions[j];
    EasyQuestions[j] = EasyQuestions[i];
    EasyQuestions[i] = temp;

    temp = EasyOptions[j];
    EasyOptions[j] = EasyOptions[i];
    EasyOptions[i] = temp;

    temp = EasyAnswers[j];
    EasyAnswers[j] = EasyAnswers[i];
    EasyAnswers[i] = temp;
}

3 Comments

Thanks, this answered my question, so I'll mark it as the answer.
What is the images array...?
Sorry, should be any of your arrays.
0

Don't shuffle the arrays themselves. Instead, shuffle a list of integers containing 0 through 4. Since this appears to be homework I'll let you figure out how to then use that shuffled list.

1 Comment

It isn't homework, I self-teach myself programming (Year 9), programming isn't part of our curriculum 8)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.