0

I have a simple educational kids game with 7 questions. One imageView and four buttons for each question, the user must match the correct answer(button) with what is shown in the image view. I want the images(questions) to be random every game but never repeat a question during a game until all 7 have been asked. I am only using 3 images as of now just to get it working.

Option 1

int[] res = {R.drawable.img1, R.drawable.img2, R.drawable.img3};

Method

    private void randomImage() {
    Random rand = new Random();
    int rndInt = rand.nextInt(res .length);
    imgView.setImageDrawable(getResources().getDrawable(res[rndInt]));
    }

Option 2

private ArrayList<Integer> res1 = new ArrayList<Integer>();
res1.add(R.drawable.img1);
res1.add(R.drawable.img2);
res1.add(R.drawable.img3);

Method

    private void randomImage1() {
    Collections.shuffle(res1);
    for(int i=0;i<res1.size();i++){
    imgView.setImageResource(res1.get(i));
    }
}

Both of these work for randomizing, but I am having a little trouble figuring out how to check if an image has already appeared and to correct it if it had. In fact I'm not really quite sure where to start. Any help would be appreciated.

jeff darnell
  • 65
  • 1
  • 3

1 Answers1

1

If you dont want to see repeated items from array then use shuffleArray() like this example and for a list use shuffle(list) like this example2

Community
  • 1
  • 1
Roon13
  • 387
  • 2
  • 23