Thread: randomize array blackjack c program

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    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; 
         }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    random strings

    But the project guidlines are to generate a random string of that array. How could that possible be done?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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?

    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Debugging Array Program
    By dcwang3 in forum C Programming
    Replies: 9
    Last Post: 10-01-2008, 08:56 PM
  2. somebody help me with this array program
    By hieugene in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 05:53 AM
  3. Need some help with a C program...
    By pityocamptes in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 06:43 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM