Hello guys I'm new to android development and java programming. 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.
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.
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
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.
So store them into a ArrayList .So you can use Collections.shuffle(list);
It will shuffle the items in the ArrayList. So ,no head ache regarding repetition.
Related
I'm making a quiz app I have string array and I want to load them on button click, strings should be loaded randomly into Text View. Strings should not be repeated.
Thanks..
Because it's your quiz, I'm gonna give you hints not the whole soltion
Random rnd = new Random;
while(array.size()>0){
// 1. use rng to get a index between 0 to current array.size()
// 2. remove the string by array.remove(index) so that it won't duplicated.
// 3. setText(string)
}
Try this, you can add comments if you need more details.
One way to do it is to convert the array to a list and shuffling it.
List myList = Arrays.asList(yourArray);
Collections.shuffle(myList);
After that you can just iterate over the list.
I am trying to create a card game for an android app (for practice) and I was wondering how would I upload random cards into the players hand? I have the GUI implemented and I think I would need something along the lines of a typedarray and
((ImageView)findViewById(R.id.textView18)).setImageResource(R.drawable.1c);
Am I moving along the right tracks with this?
You can get random images like this:
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();
imageView.setImageResource(images[rand.nextInt(images.length)]);
I have is how to generate random images without repeating , as an example driving in an array 15 images and 7 images to send imageview but that are not repeated .
int[] img={ R.drawable.ima1,R.drawable.ima2,R.drawable.ima3,R.drawable.ima4,R.drawable.ima5,R.drawable.ima6,R.drawable.ima7,R.drawable.ima8,R.drawable.ima9,R.drawable.ima10,R.drawable.ima11,R.drawable.ima12,R.drawable.ima13,R.drawable.ima14,R.drawable.ima15};
int [] game= new int[7];
Random numerRan = new Random();
for (int i=0;i<game.length ;i++)
{
int num= numerRan.nextInt(17);
int x= img[num];
game[i]=img[num];
}
img1.setImageResource(juego[0]);
img2.setImageResource(juego[1]);
img3.setImageResource(juego[2]);
img4.setImageResource(juego[3]);
img5.setImageResource(juego[4]);
img6.setImageResource(juego[5]);
img7.setImageResource(juego[6]);
img8.setImageResource(juego[7]);
When I press the button control images to the score rando the position that was generated . The problem I have is that images are repeated.
I am using 2 arrangements the first is to save all the images and the second keep the random images that will assign imageview .
Instead of using an int[] array, use a List<Integer> instead, such as an ArrayList<Integer>.
That way, you'll be able to use Collections#shuffle(), which will have the desired effect. It'll randomly permute your list, in a non-repeating way. All you'll have to do later is to iterate on that List.
For more info, please refer to the Collections documentation.
You can also convert that array to a List, like the following:
List<Integer> myList = Arrays.asList(img);
For more info about that, take a look at this question.
HI,
I want to display 20 random images at a time over Activity screen in android. I'm using this snippet:-
ImageView imageArr[] = new ImageView[25];
int id[]={ R.drawable.letter1,R.drawable.letter2,R.drawable.letter3,R.drawable.letter4,
R.drawable.letter5,R.drawable.letter6,R.drawable.letter7,R.drawable.letter8,
R.drawable.letter9,R.drawable.letter10,R.drawable.letter11,R.drawable.letter12,
R.drawable.letter13,R.drawable.letter14,R.drawable.letter15,R.drawable.letter16,
R.drawable.letter17,R.drawable.letter18,R.drawable.letter19,R.drawable.letter20};
ArrayList<Integer> randomArr = new ArrayList<Integer>();
for(int i=0; i<20;i++) {
randomArr.add(i);
}
Collections.shuffle(randomArr);
for(int i=0; i<20; i++){
//ImageView mImageplay = (ImageView)findViewById(R.id.image_play);
imageArr[i]=(ImageView)findViewById(id[randomArr.get(i)]);
//mImageplay.setImageResource(id[randomArr.get(i)]);
}
Here I'm having 20 custom images of size 49x49 pixels(small sizes)in drawable folder which i want to dispaly over UI at a time in random fashion with no alignment.
The problem is in imageArr[i] is showing NULL value for all the 20 images,whereas "randomArr" is having the correct shuffled data.
Also, Is there any way to display it in randomized look in Layout Area as i was not able to find out solution for this.
I am struck in this and not able to resolve it. please help me out for this.
thanks in advance.
-pk
Your array id[] contains R.drawable. values (id of Drawables in your app) instead of R.id. (id of your ImageViews in your layout).
Set the android:id attributes in your layout, and use them in your id[] array.
I am developing an Android game application.I have implemented the all the basic functionality.Designed all the screens.Now the main problem i am struck is I have to select 25 images randomly from 40 images.And display all the 25 images in the application.Can anyone guide me,,,, Thanks in advance
Regards
Tushar Sahni
I am assuming that you have all drawables in your drawable folder for imageview.
See the below implementation I have done in my app. First take Imageview array and int array with length of number of images you have.
ImageView i[]=new ImageView[49];
int id[]={R.drawable.pic1,......,R.drawable.pic40};
Then take arraylist like below and add no 1 to 40 to it.
ArrayList<Integer> ranArr = new ArrayList<Integer>();
for(int i=0;i<(40);i++)
{
ranArr.add(i);
}
Then shuffle it so your randomness will generate.
Collections.shuffle(ranArr);
Now your random no logic is over so you can now use that to imageview like below
for(int i=0;i<25;i++)
{
i[i]=(ImageView)findViewById(id[ranArr.get[i]);
}
So it will generate random images from your 40 drawables.
use Random class to generate 25 numbers, which are less than 40. Then get the images with generated numbers.