简体   繁体   中英

Shuffle elements in array in array (2D array - answers in quiz)

I'm working on a quiz app where I have a working logic except one major thing - shuffling of the answers. The quiz should be replayable, so I need to shuffle the answers each time the user plays it, so they don't remember the order and correct answer. How it works now: I have a class where is all defined. I have also other classes based on difficulty (medium, hard), but that's not relevant now.

Class Questions:

public class Question {


//Questions
public String easyQuestions[] = {
        App.getAppResources().getString(R.string.eqwf1),
        App.getAppResources().getString(R.string.eqwf2),
...

private String easyChoices[][] = {
        {App.getAppResources().getString(R.string.eqwf1_a1),App.getAppResources().getString(R.string.eqwf1_a2),App.getAppResources().getString(R.string.eqwf1_a3),App.getAppResources().getString(R.string.eqwf1_a4)},
        {App.getAppResources().getString(R.string.eqwf2_a1),App.getAppResources().getString(R.string.eqwf2_a2),App.getAppResources().getString(R.string.eqwf2_a3),App.getAppResources().getString(R.string.eqwf2_a4)},
...

//Correct answers
private String easyCorrectAnswers [] = {
        App.getAppResources().getString(R.string.eqwf1_ac),
        App.getAppResources().getString(R.string.eqwf2_ac),
...

//Get question
public String getEasyQuestion(int que){
    String question = easyQuestions[que];
    return question;
}
//Get first answer
public String getAnswer1(int a){
  String choice = easyChoices[a][0];
  return choice;
}
//Get second answer
public String getAnswer2(int a){
    String choice = easyChoices[a][1];
    return choice;
}
//Get third answer
public String getAnswer3(int a){
    String choice = easyChoices[a][2];
    return choice;
}
//Get fourth answer
public String getAnswer4(int a){
    String choice = easyChoices[a][3];
    return choice;
}
//Get correct answer
public String getCorrectAnswer(int a){
    String answer = easyCorrectAnswers[a];
    return answer;
}

In my activity, I have these functions, which handle shuffling questions and updating:

MainActivity:

private void generateQuestions(List<Integer> rQList, int lengthOfWholeArray){
    Random rand = new Random();
    while (rQList.size() != lengthOfWholeArray) {
        int r = rand.nextInt(lengthOfWholeArray);
        if (!rQList.contains(r))
            rQList.add(r);
    }
}

private void generateQuestionsBasedOnDifficulty(List<Integer> list, int difficulty){
    switch (difficulty){
        case 1:
            generateQuestions(list, easyQuestionsLength);
        break;
        case 2:
            generateQuestions(list, mediumQuestionsLength);
        break;
        case 3:
            generateQuestions(list, hardQuestionsLength);
        break;
    }
}
    private void updateEasyQuestions(int num) {
    question.setText(easyQuestions.getEasyQuestion(num));
    answer1.setText(easyQuestions.getAnswer1(num));
    answer2.setText(easyQuestions.getAnswer2(num));
    answer3.setText(easyQuestions.getAnswer3(num));
    answer4.setText(easyQuestions.getAnswer4(num));
    mAnswer = easyQuestions.getCorrectAnswer(num);
}

I think generate functions with difficulty are not important here, but I put them here, so you have an idea of how it works. Then, after the press of the Next button, the question is updated:

  next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (index == questionsLength) {
                gameOver();
            } else {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        switch (difficulty) {
                            case 1:
                                updateEasyQuestions(questionList.get(index));
                                index++;
                                break;
                            case 2:
                                updateMediumQuestions(questionList.get(index));
                                index++;
                                break;
                            case 3:
                                updateHardQuestions(questionList.get(index));
                                index++;
                                break;
                        }
                    }
                },700);
                refreshUI();
            }
        }
    });

The timer is here because of animations, the most relevant is the call of the function. I need to rewrite updateEasyQuestions somehow, so that the answers are shuffled for each question. Or create new function.

answer1 , answer2 ,... are Buttons. refreshUI is just a simple function which handles graphics, animations and UI (fe unable to click on buttons when you answered).

Hope it make sense somehow. There is so much code now, but as I said, the most important part is the class itself and the function which updates the questions. Every idea (and suggestion to improve my code) are welcomed. Thanks.

I am not sure if I understand your question correctly but I would create a Question class that would have a List of Answer Objects, each would have a boolean if this is the correct answer and an integer with an assigned random number, during a presentation they would be sorted by integers, also use ThreadLocalRandom for randomization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM