# Random running continously (Card Game, deck of cards)

**URL:** https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784
**Category:** Coding Questions
**Created:** [November 19, 2022, 7:06am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784 "2022-11-19T07:06:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![BrokenSpace](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@BrokenSpace](https://discourse.processing.org/u/BrokenSpace)
#### Post date: [November 19, 2022, 7:06am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/1 "2022-11-19T07:06:26Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 19, 2022, 7:46am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/2 "2022-11-19T07:46:36Z")

</div>

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**

```auto
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):

```auto
  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

---

<div class="post-metadata">

### Author: ![BrokenSpace](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@BrokenSpace](https://discourse.processing.org/u/BrokenSpace)
#### Post date: [November 20, 2022, 4:10am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/3 "2022-11-20T04:10:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 20, 2022, 5:09am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/4 "2022-11-20T05:09:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![BrokenSpace](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@BrokenSpace](https://discourse.processing.org/u/BrokenSpace)
#### Post date: [November 20, 2022, 5:42am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/5 "2022-11-20T05:42:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 21, 2022, 10:24am UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/6 "2022-11-21T10:24:23Z")

</div>

> [@Chrisir](#):
>
> should contain a 2D grid of class Card

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 21, 2022, 12:22pm UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/7 "2022-11-21T12:22:16Z")

</div>

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

See here:

> [@BrokenSpace](#):
>
> ```auto
>   
> 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);
> }
> 
> ```

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)

```auto

  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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 21, 2022, 1:18pm UTC](https://discourse.processing.org/t/random-running-continously-card-game-deck-of-cards/39784/8 "2022-11-21T13:18:03Z")

</div>

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

> [@The operator \> is undefined for the argument - HOW TO RESOLVE ERROR (OOP)](https://discourse.processing.org/t/the-operator-is-undefined-for-the-argument-how-to-resolve-error-oop/38705/6):
>
> If you wish you can take a look at a very old deck of cards implementation I did a long time ago: [Picking cards at random, then excluding those from further picking. - Processing 2.x and 3.x Forum](https://forum.Processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-#Item_2) It’s split into 2 sketches: 1 creates & saves the “.csv” file, while the other loads it and makes a deck of cards out of it. For convenience I’ve merged them now into 1 sketch w/ 4 “.pde” tabs: “Table\_CSV\_Card\_Pack.pde”: /\*\* \* Table CSV Card Pack (v1.0.1) \* by GoToLoop (2022/Sep/13) \* \* htt…
