# randomize array blackjack c program

• 09-01-2005
bazzano
randomize array blackjack c program
Hi guys i have the following code... The char * deckOfCards[DECK_SIZE] is an array which holds a deck of 52 cards.

How would i use my shuffle deck function to randomize the strings in the array. So then it randomly picks 3s or 3h (3 hearts) intead of just randomizing an int from 1-11. How would i randomize 52 strings of cards in that function.

Code:

```                  void blackjackGame() {   /* Spades, Clubs, Diamonds, Hearts. */   char* deckOfCards[DECK_SIZE] = {"2S",  "2C",  "2D",  "2H",                                   "3S",  "3C",  "3D",  "3H",                                   "4S",  "4C",  "4D",  "4H",                                   "5S",  "5C",  "5D",  "5H",                                   "6S",  "6C",  "6D",  "6H",                                   "7S",  "7C",  "7D",  "7H",                                   "8S",  "8C",  "8D",  "8H",                                   "9S",  "9C",  "9D",  "9H",                                   "10S",  "10C",  "10D",  "10H",                                   "JS",  "JC",  "JD",  "JH",                                   "QS",  "QC",  "QD",  "QH",                                 "KS",  "KC",  "KD",  "KH",                                 "AS",  "AC",  "AD",  "AH"};* int player1Total, int  player2Total;      /* Hold totals per hand */ int  playerLost;        /* Used as flag    */    char reply;          /* Holds user input    */  do   {                      /* Begin Hand */       player1Total=0;       player2Total=0;       playerLost = NO;       printf("\nBlackJack21\n\n");   }   while((reply != 's') && (playerLost == NO)) } /* this is a function used to randomize the array of cards char* deckofCards(DECK_SIZE) */ shuffledeck() {       for(i=0;i<1;++i)  /* Deal ONE  card */       {         player1Card = (1+rand()%11);         printf("Your card is %d\n",player1Card);         playerTotal += player1Card;     } }```
• 09-01-2005
Salem
A very simple shuffle swaps two cards over

Code:

```temp = deckOfCards[2]; deckOfCards[2] = deckOfCards[20]; deckOfCards[20] = temp;```
Now do that a number of times, picking two random spots in the array.
• 09-01-2005
bazzano
random strings
But the project guidlines are to generate a random string of that array. How could that possible be done?
• 09-01-2005
Salem
You shuffle the deck.

Then to draw 5 cards you look at deckOfCards[0] to deckOfCards[4]
To draw another 5 cards, you look at deckOfCards[5] to deckOfCards[9]

A simple counter
int numCardsDrawnFromDeck;
will keep a count of which cards you dealt.
• 09-01-2005
dwks
Code:

```  char* deckOfCards[DECK_SIZE] = {"2S",  "2C",  "2D",  "2H",                                   "3S",  "3C",  "3D",  "3H",                                   "4S",  "4C",  "4D",  "4H",                                   "5S",  "5C",  "5D",  "5H",                                   "6S",  "6C",  "6D",  "6H",                                   "7S",  "7C",  "7D",  "7H",                                   "8S",  "8C",  "8D",  "8H",                                   "9S",  "9C",  "9D",  "9H",                                   "10S",  "10C",  "10D",  "10H",                                   "JS",  "JC",  "JD",  "JH",                                   "QS",  "QC",  "QD",  "QH",                                 "KS",  "KC",  "KD",  "KH",                                 "AS",  "AC",  "AD",  "AH"};*```
You don't need DECK_SIZE, although you can leave it in if you want to. And what's that asterick doing there?

Quote:

A simple counter
int numCardsDrawnFromDeck;
will keep a count of which cards you dealt.
Adding to this, if numCardsDrawnFromDeck is the same as DECK_SIZE, the deck needs to be re-dealt.