Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent repeat in shuffle mode

How do I random out my already asked question (so that it doesn't repeat) in a quiz? What's the best possible way to achieve this without crashing the app?

Problem: I wanted to keep only one Java class with all the questions, don't know if that's a good approach when progressing towards other Level activities. So I tried it with 10 questions to start with, but I keep getting already asked questions. Then I break that class into two (5 questions each) defining each by new level (Easy Level 1 and 2) but the problem is same.

I guess, this is the line I'm struggling with the logic part:

updateQuestion(rand.nextInt(mQuestionsLength)); 

This is what I tried, till now:

public class Quiz extends AppCompatActivity {
    Button answer1, answer2;
    TextView score, question, timer;
    private Question mQuestions = new Question();
    private String mExplanation;
    private String mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = 0;
    private String mQuestionsIndex;
    private int mAns = 0;
    public int mQuestionsLength = mQuestions.mQuestions.length;
    Random rand;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy_level1);

        rand = new Random();

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);

        score = (TextView) findViewById(R.id.score);
        question = (TextView) findViewById(R.id.question);
        timer = (TextView) findViewById(R.id.timer);

        score.setText("Overall Score: " + mScore);

        updateQuestion(rand.nextInt(mQuestionsLength));

        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer1.getText() == mAnswer){
                    bingoAlert();
                } else {
                    oopsAlert();
                }
            }
        });
        answer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer2.getText() == mAnswer){
                    bingoAlert();
                } else {
                    oopsAlert();
                }
            }
        });
    }
    private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
        mQuestionsIndex = mQuestions.getQuestionIndex(num);
        mQuestionNumber++;
    }

    private void bingoAlert(){
        mScore+=5;
        score.setText("Score: " + mScore);
        if(mQuestionNumber == mQuestions.mQuestions.length){
            // Do something...
        }else{
            updateQuestion(rand.nextInt(mQuestionsLength));
        }
    }
    private void oopsAlert(){
        mScore-=2;
        score.setText("Score: " + mScore);
        if(mQuestionNumber == mQuestions.mQuestions.length){
            // Do something...
        }else{
            updateQuestion(rand.nextInt(mQuestionsLength));
        }
    }

Question Class

public class Questions {

    public static String[] mQuestions = new String[]{
            "Question 1",
            "Question 2",
            "Question 3",
            "Question 4",
            "Question 5"

    };
    public static String[] mQuestionsIndex = new String[]{
            "1", "2", "3", "4", "5"
    };
    private static String mChoices[][] = {
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"}
    };
    private String mCorrectAnswers[] =
            {"Choice 1", "Choice 2", "Choice 1", "Choice 2", "Choice 1"};

    public String getQuestion(int a) {
        String question = mQuestions[a];
        return question;
    }

    public String getChoice1(int a) {
        String choice = mChoices[a][0];
        return choice;
    }

    public String getChoice2(int a) {
        String choice = mChoices[a][1];
        return choice;
    }

    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }

    public String getQuestionIndex(int a) {
        String questionsIndex = mQuestionsIndex[a];
        return questionsIndex;
    }

This is my Random Class

class RandomClass {
    private Random randNum;

    public RandomClass() {
        randNum = new Random();
    }

    public int[] generateRandomArray(int arraySize){
        int[] theArray = new int[arraySize];
        for(int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++){
            theArray[mQuestionNumber] = randNum.nextInt(6);
        }
        return theArray;
    }
}

And this is my recent attempt:

private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
        mExplanation = mQuestions.getExplanation(num);
        mQuestionsIndex = mQuestions.getQuestionIndex(num);

        for (int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++) {
            int[] ints = new RandomClass().generateRandomArray(5);
        }
    }

Thanks!

like image 558
Rohit Sharma Avatar asked Aug 21 '26 20:08

Rohit Sharma


1 Answers

Tip: Maintain an ArrayList

  1. Create an ArrayList.
  2. Add the indexes of the questions. eg: 1,2,3,4.....n in list.
  3. Generate a random number within the range of size of List.
  4. Display the question corresponding to the random number generated(preferably the index of the question number).
  5. After displaying the question remove that item from the list.
  6. As you delete the item from the list, the list gets updated and all the elements after the deleted elements move up by one position and size of the list gets updated (reduced by 1).
  7. Again generate a random number within the range of size of the list and repeat the process.

    list.get(random_number); will give you the index of the number stored in that position so that you can display your position accordingly.

Let me know if any point is not clear or ambiguous. I'll update it.

like image 79
nimi0112 Avatar answered Aug 24 '26 11:08

nimi0112



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!