Get random images - android

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)]);

Related

Gerator Image Random

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.

Random images with no repetition (image guessing)

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.

Android - Randomly setting a button in a grid

this is an extension to this question: Android - Handling a grid
Basically, I am wondering how to make a grid of 4x4 buttons randomly change their features(text, color, etc.) I don't need help setting the actual change in text or color, I just need a way to go about something like this. I don't know if I should write an array and choose from there, or use the GridView. Just a little start to something is all I need, I'm not asking for lots of code. Thanks in advance, I really appreciate it.
If your 4 ids are sequential as is
btnId1 = 8006;
btnId2 = 8007;
btnId3 = 8008;
btnId4 = 8009;
Random rn = new Random();
int chooseId = btnId1 + rn.nextInt(btnId4 - btnId1 + 1);
You can generate a random number like so:
Random r=new Random();
r.nextInt(4);
this will give you a random number in the range [0,4).
Based on this random number, you change the properties of the appropriate tile/square which you can enumerate yourself from 0 to 3 since you have 4 tiles in total.
From both your questions it seems that you lack some basic programming knowledge of algorithms/approaches to problems. You should try to fill that gap, which will make you progress MUCH MUCH faster in your programming endeavors!

Shuffling cards and assigning them to user and computer in android development

I am creating a basic top trumps app in android development. I only need 6 top trumps card which have been made by using a database. I already have the code for this and have successfully created the cards.
I am stuck on this bit
When the user selects Play you should randomly assign half of the cards in the database to the human player, and half of the cards to the computer.
And was wondering if anyone had any ideas on how to do this? Thanks everyone.
UPDATE
I have tried doing this
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i=1;i<=6;i++)
{
this.cards.add(i);
}
Collections.shuffle(this.cards);
To shuffle the cards but I'm unsure on how to assign the computer and user the cards.
I'm not sure what you are aiming for here, but would something like this do (after you have shuffled):
ArrayList<Integer> playerCards = new ArrayList<Integer>(cards.subList(0,2));
ArrayList<Integer> computerCards = new ArrayList<Integer>(cards.subList(3,5));
Of course, you should use variables instead of constants everywhere, but you get the idea.
I am not an android programmer, but once long ago I realized the way to deal shuffled cards is to store the cards in an array or whatever in their natural order rather than a shuffled order -- for regular poker cards, 2-spades to A-spades, then 2-clubs to A-clubs, etc. Then, instead of dealing cards in-order from the shuffled deck, deal the cards by random draw from the unshuffled deck. You card elements need a "used" setting or value so they don't get reused or used twice. Saves LOADS of time doing the shuffle. In your case you on;y need to draw three cards, then the other guy gets the rest.

Randomly Generating Sentences for Display

I have a few questions concerning the application I'm designing. I have many different routes I could try in order to get what I wanted, but I thought I would get suggestions instead of doing trial and error.
I'm developing a simple app that has one Game screen (activity) and a main menu to start the game. The game screen will have two buttons, "Next" and "Repeat". Every time "Next" is hit, a new sentence in a different language (with the english translation below it) will appear, audio will pronounce the sentence, and hopefully I can get a highlighter to highlight the part of the sentence being spoken. You can guess what the Repeat button does.
My question is, what do you guys think would be the best way to store these sentences so they can be randomly picked out? I thought about making an array of structures or classes with the English definition, audio, and sentence in each structure. Then using a random iterator to pick one out. However, it would take a long time to do this approach and I wanted to get some ideas before I tried it.
Also, I'm not sure how I would print the sentence and definition on the screen.
Thanks!
Using an array of structs/classes seems like that would be the normal way to go.
Not really sure what you mean by a random iterator, but when picking out random sentences from the array of sentences, you might want to avoid repeats until you've gone through all the elements. To do that, you can make a second array of indices, select one at random from those, use the element that corresponds to that index, and remove that number from the array of indices.
In Java, that would look something like
ArrayList<Sentence> sentences;
ArrayList<Integer> indices;
Random rand;
private void populateIndices() {
for(int i = 0; i < sentences.size(); i++)
indices.add(i);
}
public Sentence getNextSentence() {
if(indices.isEmpty())
populateIndices();
int idx = rand.nextInt(indices.size());
int val = indices.get(idx);
indices.remove(idx);
return sentences.get(val);
}
Quite frankly I would load out of copyright books from Project Gutenberg and randomly pull sentences from them. I would then pass the sentences into Google APIs to translate and pronounce the sentences. Relying on external services is at the very heart of what a connected OS like Android is made for. It would be a much more compelling use of the platform than a canned Rosetta Stone like CD solution and your ability to tap into a broader amount of content would be increased exponentially.

Categories

Resources