I'm trying to develop a Memory game for Android. I don't know how to arrange picture pairs randomly in another place. I've already managed to arrange one or two pairs of pictures at random ImageViews, but I have the problem that as soon as I want to arrange several pictures pairs, sometimes only single pictures are displayed and some ImageViews show no picture.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv[] = {(ImageView) findViewById(R.id.pic1), (ImageView) findViewById(R.id.pic2), (ImageView) findViewById(R.id.pic3), (ImageView) findViewById(R.id.pic4),
(ImageView) findViewById(R.id.pic5), (ImageView) findViewById(R.id.pic6),
(ImageView) findViewById(R.id.pic7),
(ImageView) findViewById(R.id.pic8),
(ImageView) findViewById(R.id.pic9),
(ImageView) findViewById(R.id.pic10),
(ImageView) findViewById(R.id.pic11),
(ImageView) findViewById(R.id.pic12),
(ImageView) findViewById(R.id.pic13),
(ImageView) findViewById(R.id.pic14)};
Random random = new Random();
int picture[] = {R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5, R.drawable.i5, R.drawable.i6, R.drawable.i7};
int i = random.nextInt(13);
iv[i].setImageResource(picture[1]);
i = random.nextInt(13);
iv[i].setImageResource(picture[1]);
i = random.nextInt(13);
iv[i].setImageResource(picture[2]);
i = random.nextInt(13);
iv[i].setImageResource(picture[2]);
i = random.nextInt(13);
iv[i].setImageResource(picture[3]);
i = random.nextInt(13);
iv[i].setImageResource(picture[3]);
i = random.nextInt(13);
iv[i].setImageResource(picture[4]);
i = random.nextInt(13);
iv[i].setImageResource(picture[4]);
i = random.nextInt(13);
iv[i].setImageResource(picture[5]);
i = random.nextInt(13);
iv[i].setImageResource(picture[5]);
i = random.nextInt(13);
iv[i].setImageResource(picture[6]);
i = random.nextInt(13);
iv[i].setImageResource(picture[6]);
}
How can I fix that? Is it possible to generate another random number each time?
Aucun commentaire:
Enregistrer un commentaire