Home > Software design >  Assign int value to image for compare with higher value wins (card game)
Assign int value to image for compare with higher value wins (card game)

Time:12-22

I'm a rookie programmer and I'm trying to develop a card game based on higher wins. The problem is, I don't know how to assign int values to cards to compare. Let's say there are 46 cards, each one has a different value, something like:

int peter = 19; 
int jack = 26; 
int martin = 22; 
//...and so on

There are two players, each one has four randomly generated cards (I did it like this, so I can cast flip animation on each one, it's a bit clumsy but it works)

    //Assign my cards
    TypedArray my_cards = res.obtainTypedArray(R.array.mycards);
    final Random random5 = new Random();
    final Random random6 = new Random();
    final Random random7 = new Random();
    final Random random8 = new Random();

    //Generate random indexes for my cards
    int RandomCard5 = random5.nextInt(my_cards.length());
    int RandomCard6 = random6.nextInt(my_cards.length());
    int RandomCard7 = random7.nextInt(my_cards.length());
    int RandomCard8 = random8.nextInt(my_cards.length());
    //Generate my cards from RandomCard
    int drawableCard5 = my_cards.getResourceId(RandomCard5,-1);
    int drawableCard6 = my_cards.getResourceId(RandomCard6,-1);
    int drawableCard7 = my_cards.getResourceId(RandomCard7,-1);
    int drawableCard8 = my_cards.getResourceId(RandomCard8,-1);
    //Deal my cards
    card1.setImageResource(drawableCard5);
    card2.setImageResource(drawableCard6);
    card3.setImageResource(drawableCard7);
    card4.setImageResource(drawableCard8);

As you can see, I'm using typedArray (Not sure if it's a good idea for this). For the enemy cards it's the same. After choosing a card for a fight (confirm button), it's transfered to other ImageView (fightcard1) and an enemy card is randomly choosen from those four random cards and transfered to ImageView (fightcard2).

Now I need to compare those cards (let's say those cards will have values 19 and 24). The compare function shouldn't be a problem, but I have no idea how to assign those specific int values to specific cards. Is there a way I could do this with my current "garbage" code, or is there a better and easier way with some/complete rewriting?

Thanks.

CodePudding user response:

It would be best i think for you to create an object called Card

public class Card{
    private int cardValue;

    public Card(int cardValue){

        this.cardValue = cardValue;
    }

    public int GetCardValue(){
        return cardValue;
    }
}

I am not sure what other information you want to store on this card but it can easily be added by adding it to the constructor as was done with cardValue on the Card method. This will then allow you to store the Card object and when you want to compare them, you would call card1.GetCardValue() which will return the value in the cardValue variable. This will allow you to do the comparison which you are trying to do.

you would create a Card object with

Card card = new Card(5); // this would set the cards value to 5.

If you have any more questions feel free to comment an ill help you out as i am not entirely sure what other information about the card you need to store.

  • Related