Random running continously (Card Game, deck of cards)

I’m making a deck of cards. I have each image of the cards in a 2D array with each value of the array representing suit[4] and rank[13]. I’m trying to use random to randomly pull an image from it but it just cycles through numerous cards at once.

class DeckCards { 
  int randoSuit;
  int randoRank;
  
  PImage cardBack;
  PImage newCard;
  
  CardImages card = new CardImages();
  
  void pickCard(){
    randoSuit = (floor(random(4)));
    randoRank = (floor(random(13)));
    newCard = card.cardFaces[randoSuit][randoRank];
  }

  void display(int xCardPos, int yCardPos){
    pickCard();
    image(newCard, xCardPos, yCardPos);
  }
  
  void stationaryDeck(int xDeckPos, int yDeckPos){
    image(cardBack, xDeckPos, yDeckPos);
  }
  
  class CardImages{
    PImage cardFront;

    PImage[][] cardFaces = new PImage[4][13];

There’s a large chunk of code beneath this to fill in the .png’s to each PImage variable and assigns them all to the array.

you use your function pickCard
in a way it picks again and again

I think what you want is fill your 2D array using pickCard and display the array (not using pickCard anymore)

Pseudo-Code

for randoSuit....
    for randoRank....
        card.cardFaces[randoSuit][randoRank] = pickCard();
   }
}

this needs a changed function pickCard() which gives now an PImage back (or whatever you need):

  PImage pickCard(){
    randoSuit   = (floor(random(4)));
    randoRank = (floor(random(13)));
    newCard = card.cardFaces[randoSuit][randoRank];

    return newCard; 
  }

The structure

Not sure about you structure of classes.
I would do it like this:

class DeckCards

  • should contain a 2D grid of class Card

class Card

  • should contain one card
  • should contain PImage cardFront - one variable for the card

Class does not need inside a class, you can separate them and have a new tab for each class

Chrisir

4 Likes

Could you elaborate on both the chunk of code with the for loops and the structure of the class, mainly the DeckCards class. I can definitely separate the two if that’s a better idea as well

It was a suggestion only

Best if you save your sketch with a new version and try to implement what you need

Show your attempt then please

1 Like

Thank you, I think you definitely set me on the right path

1 Like

Your data structure will depend on your goal.
The game you want to implement.
We didn’t talk about it.

You can also make a 1D array of cards instead and store in each card its properties like color

My main point is that you need
to store the cards instead of selecting them again and again

2 Likes

I found time to look into this.
Forget what I said.

See here:

these lines leads to the card changing which is inconvenient.

Instead you can store the new Card and set a flag (and pick a new Card only once)


  PImage newCard;
  boolean newCardHasBeenPicked=false;   // flag 

// not changed : 
  void  pickCard() {
    randoSuit = (floor(random(4)));
    randoRank = (floor(random(13)));
    newCard = card.cardFaces[randoSuit][randoRank];
  }

  // changed: 
  void display(int xCardPos, int yCardPos) {
    if (! newCardHasBeenPicked) {   // flag  -  the "!" means "not" or == false
      pickCard();
      newCardHasBeenPicked = true;
    }
    image(newCard, xCardPos, yCardPos); // display 
  }

now, when the game continues and we need to display a new card, just say newCardHasBeenPicked=false;

Chrisir

2 Likes

There’s this deck of cards example if you wanna take a look: :black_joker:

3 Likes