Skip to content
Advertisement

Assign random class object (image) to ImageView

I’m developing a card game, based on higher wins. From my first question here assign int value to image for compare I’ve rewritten my code but now I’m stuck on randomization of cards and assignment to ImageView.

I have a class card.

public class Card{
private int cardValue;
@DrawableRes
private int image;

public Card(int cardValue, @DrawableRes int image){
    this.cardValue = cardValue;
    this.image = image;
}

public int getCardValue()
{
    return cardValue;
}
@DrawableRes
public int getCardImage()
{
    return image;
}

//Cards
Card ash = new Card(20, R.drawable.ash_card);
Card atlas = new Card(23, R.drawable.atlas_card);
Card banshee = new Card(14, R.drawable.banshee_card);
and so on....

In MainActivity: I have array of this cards:

//Creating array of the cards
ArrayList<Card> cards = new ArrayList<>();

Now I need to randomize four cards from this array and assign each one to IV.

 final Random random1 = new Random();
 //Generate random indexes for cards
 int RandomCard1 = random1.nextInt(cards.size());
 int RandomCard2 = random1.nextInt(cards.size());
 int RandomCard3 = random1.nextInt(cards.size());
 int RandomCard4 = random1.nextInt(cards.size());
 //Generate the card from RandomCard
 Card drawableCard1 = cards.get(RandomCard1);
 Card drawableCard2 = cards.get(RandomCard2);
 Card drawableCard3 = cards.get(RandomCard3);
 Card drawableCard4 = cards.get(RandomCard4);

I’m using this, because I have flip animation for each card (maybe there is a better way to do it, but it works for me). So now I just need to assign a card to IV, but this won’t work.

//Deal my cards
card1.setImageResource(drawableCard1);
card2.setImageResource(drawableCard2);
card3.setImageResource(drawableCard3);
card4.setImageResource(drawableCard4);

I can’t use setImageResource for class object. I’ve been stuck here for days, trying to figure it out but with no luck. Should I do randomizing differently (f.e. with Collections.shuffle?). Or is there any way how to do it? Thank you.

Advertisement

Answer

You actually have two different problems:
the simplest is that your cards are not showing since setImageResource requires the int representing the resource id as parameter, you’ll just need to call:

//Deal my cards
card1.setImageResource(drawableCard1.getCardImage());
card2.setImageResource(drawableCard2.getCardImage());
card3.setImageResource(drawableCard3.getCardImage());
card4.setImageResource(drawableCard4.getCardImage());

Now if the UI is correctly set up you’ll see the images.

The second problem (I don’t know if it’s intended behavior) is that some cards could be given repeatedly, since the algorithm just generates a number within the bound and repetitions could occur.
A possible solution is, as you suggested, using Collections.shuffle() on the deck at the beginning so that it would simulate the initial deck shuffling and start dealing the cards in the exact order in which they are contained in your shuffled deck.

How to deal cards
Currently, if we consider our deck to be the entire List of Card objects, the way in which the dealer is picking the next card to give is by choosing a random card in any position of the deck (list) and put it on the table (for example). This idea could be extended by removing the picked card from the deck (which is something that at the moment is not implemented), but the dealing style wouldn’t represent what happens in reality.
Solution
The solution would be shuffling the cards at the beginning of the game (Collections.shuffle()) and start picking the card in the exact order in which they are contained in the list. In fact, since it’s already shuffled, the dealer can pick the 0-th, 1-st, 2-nd etc. card and put them on the table (or give them to the players). The Random object, in this case, wouldn’t be needed. A simple cycle could be implemented to select the next card the dealer will pick.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement