[英]Random images with no repetition (image guessing)
Hello guys I'm new to android development and java programming.大家好,我是 android 开发和 java 编程的新手。 I hope someone can help me.
我希望有一个人可以帮助我。 I'm developing an app which is an image guessing game.
我正在开发一个应用程序,它是一个图像猜谜游戏。 I have 100 images stored in my drawable.
我的 drawable 中存储了 100 张图像。
I currently have this code that will randomly generate an image and it will be displayed in the images view:我目前有这个代码,它将随机生成一个图像,它将显示在图像视图中:
ImageView random_image = (ImageView) findViewById (R.id.random_level);
final int[] images = { R.drawable.img1, R.drawable.img2...R.drawable.img100 };
Random generator = new Random();
random_image.setImageResource(images[generator.nextInt(images.length - 1)]);
My question now is how can I avoid duplication of image in every level?我现在的问题是如何避免每个级别的图像重复? My game composes of 25 levels.
我的游戏由 25 个级别组成。
Maybe create an ArrayList of those Image id's and whenever a user guesses an image, just remove that Image id position from that ArrayList也许创建这些图像 ID 的 ArrayList,每当用户猜测图像时,只需从该 ArrayList 中删除该图像 ID 位置
Example:例子:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(R.drawable.your_images); //keep adding that, maybe using a loop would be better to add
int position = new Random().nextInt(list.size());
imageViewObject.setImageResource(Integer.intValue(list.get(position)));
//while starting next level
list.remove(position);
That's all!就这样!
If you use Random class there is a chance for repetition.如果您使用 Random 类,则有机会重复。
So store them into a ArrayList .So you can use Collections.shuffle(list);
所以将它们存储到一个 ArrayList 中。所以你可以使用
Collections.shuffle(list);
It will shuffle the items in the ArrayList.它将打乱 ArrayList 中的项目。 So ,no head ache regarding repetition.
所以,没有关于重复的头痛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.