I need some help with dealing cards.

This is a discussion on I need some help with dealing cards. within the C Programming forums, part of the General Programming Boards category; struct card { const char *face; const char *suit; }; typedef struct card Card; void fillDeck( Card * const, const ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    13

    I need some help with dealing cards.

    struct card {
    const char *face;
    const char *suit;
    };

    typedef struct card Card;

    void fillDeck( Card * const, const char*[], const char*[] );
    void shuffle( Card * const );
    void Deal( const Card * const );
    int main()
    {
    Card deck[312];
    const char *face[] = { "Ace", "Duece", "Three", "Four",
    "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen", "King"};
    const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades" };

    srand(time(NULL ) );

    fillDeck( deck, face, suit );
    shuffle( deck);
    Deal( deck );
    return 0;

    }
    void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[])
    {
    int i;

    for( i = 0; i <= 51; i++) {
    wDeck[ i ].face = wFace[ i % 13 ];
    wDeck[ i ].suit = wSuit[ i/ 13 ];
    }
    }
    void shuffle( Card * const wDeck )
    {
    int i, j;
    Card temp;

    for ( i = 0; i<= 51; i++ ) {
    j = rand() % 52;
    temp =wDeck[ i ];
    wDeck[ i ] = wDeck[ j ];
    wDeck[ j ] = temp;
    }
    }

    void Deal( const Card * const wDeck)
    {
    This is where I am having a problem. I want to use this shufflling program code, but how do I get it to print out just one card everytime I call this function. I tried

    int i;
    i = 0;
    while ( i++ && i<= 300)
    printf("%5s of %-8%s", wDeck[ i ].face, wDeck[ i ].suit, '\n' );
    The entire code compiled but wouldn't print out one card liked I wanted it to. Help!!

    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    312 cards in a deck? That'll be hell to shuffle...

    If you only want one card, simply print ONE card. Don't loop through them all.

    printf("%5s of %-8%s", wDeck[ i ].face, wDeck[ i ].suit, '\n' );

    This prints ONE CARD. The problem is you're using it in a loop, which causes it to print a bunch of cards.

    Do you want to know the easiest way to shuffel cards?

    Card deck[52];
    InitDeck( deck );

    Init deck is basicly this:
    Code:
    i = 0;
    for( x = 0; x < 4; x++ )
    {
        for( y = 0; y < 13; y++ )
        {
            deck[i].suit = x;
            deck[i].facevalue = y;
            i++;
        }
    }
    Ok, now just use the following to pop a card from the deck at random:

    Code:
    Card getCard( Card deck[] )
    {
        static int cards = 52;
        int count = 0;
        int cardnumber = 0;
        Card c;
    
        cardnumber = rand()% cards;
        for( count = 0; count < cardnumber; count++ )
        {
            if ( deck[count].facevalue > -1 )
            {
                cardnumber--;
                if( cardnumber <= count )
                {
                    c = deck[count];
                    deck[count].facevalue = -1;
                    cards--;
                    return c;
                }
            }
        }
    }
    That should do the trick. Basicly what that does, is picks a random number from the remaining cards, and then count that many "unused" cards deep into the deck, and grab that card, setting that card as "used", so that it cannot be selected again.

    Quzah.
    Last edited by quzah; 03-12-2002 at 09:08 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    I know 312 is a lot of cards. But I have to use 6 decks for my game. Hey you got any ideas for a function for Split. I thought about using pointers but I'm not really good at that. Any ideas?Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Please use code tags, as it makes your code much more readable...

    I would like to point out that fillDeck() and shuffle() only do the first 52 cards.

    To handle dealing cards, I would actually suggest representing your deck of cards as a stack, which is basically an array with an index to it's top.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct card { 
     const char *face; 
     const char *suit; 
    }; 
    
    typedef struct card Card; 
    
    // A more sophisticated implementation of Deck might use 
    //  Card * myCards and memory allocation to create a deck of
    //  any size, instead of just 312
    typedef struct {
     Card myCards[312];
     unsigned int topCard;
    } Deck;
    
    void fillDeck( Card * const, const char*[], const char*[] ); 
    void shuffle( Card * const ); 
    
    void initDeck (Deck * const initMe, const char *face[], const char * suit[]);
    void Deal(Deck * const dealMe); 
    // These are very simple, they just save typing really.
    char const * getFace (Deck const * const getMyFace);
    char const * getSuit (Deck const * const getMySuit);
    // Card * topCard (Deck * getMyTop);
    
    int main() 
    { 
     const char *face[] = { "Ace", "Duece", "Three", "Four", 
     "Five", "Six", "Seven", "Eight", 
     "Nine", "Ten", "Jack", "Queen", "King"}; 
     const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades" }; 
    
     Deck myDeck;
    
     srand(time(NULL ) ); 
    
     initDeck (&myDeck, face, suit);
     Deal(&myDeck); 
    
     return 0; 
    } 
    
    void initDeck (Deck * const initMe, const char * face[], const char * suit[])
    {
     // Whether you start at 0 and count up, or start at 311 and count
     //  down is arbitrary.
     initMe -> topCard = 0;
    
     fillDeck (initMe -> myCards, face, suit);
     shuffle (initMe -> myCards);
    
     return;
    }
    
    void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[]) 
    { 
     int i; 
    
     for( i = 0; i <= 51; i++) { 
      wDeck[ i ].face = wFace[ i % 13 ]; 
      wDeck[ i ].suit = wSuit[ i/ 13 ]; 
     } 
    } 
    
    void shuffle( Card * const wDeck ) 
    { 
     int i, j; 
     Card temp; 
    
     for ( i = 0; i<= 51; i++ ) { 
      j = rand() % 52; 
      temp =wDeck[ i ]; 
      wDeck[ i ] = wDeck[ j ]; 
      wDeck[ j ] = temp; 
     } 
    } 
    
    // Takes the top card off the deck and prints it
    void Deal(Deck * const dealMe) 
    { 
     printf ("%5s of %-8s", getFace (dealMe), getSuit (dealMe));
     dealMe -> topCard++;
     return;
    }
    
    // These could be implemented as macros
    char const * getFace (Deck const * const getMyFace)
    {
     return getMyFace->myCards[getMyFace->topCard].face;
    }
    
    char const * getSuit (Deck const * const getMySuit)
    {
     return getMySuit->myCards[getMySuit->topCard].suit;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>312 cards in a deck? That'll be hell to shuffle...

    The casino I worked at used 8 decks (416) cards. Rotated teh type of shuffle each day. If you p........ed the casino off, I would be asked to do a 'chemi' shuffle.

    Make all players get up and stand away from the table. Spread all 800+ cards flat on the table and mix round and round for 5mins. Used to do it to the card counters all the time.

    Spit function shold be fairly simple. As is just a new hand with the first card in both determined.
    Code:
    //make sure has a pair
    if(card1==card2) 
       may_spilt=TRUE;
    else may_split=FALSE;
    
    //ask user if wants to spilt and set flag
    if((may_split==TRUE)&&(wants_split==TRUE))
       Split(card1);
    
    //now have two hands (hand1_1, hand2_1)
    //deal first hand
    card1_1=card1;
    card1_2=DealCard();
    //continue with normal sequence (allow splits / doubles??)
    //until hand1_1 resolved
    //pay / take / push
    
    //repeat with second hand.
    All depends on how you see the program working. I can see a recursive function here.

    What are the rules for BJ you are using. (hard is no aces, soft is ace counting as 11 ie [Ace+2] = 3 / 13 -> SOFT)
    Local casino's rules
    Dealer sits on >16 (hard or soft)
    players have to draw to >8
    Split any pair but split aces only get one card
    May split an already split hand, ie up to 4 hands total on one player
    Double on hard 9, 10 or 11 (one card only to doubles), may double on splits.
    Blackjack pays 1.5, can only get Blackjack on original two cards (split tens or aces can not get a blackjack)
    Insurance is 2:1
    dealer gets 1 card to start face up (continental BJ as opposed to Nevada style [dealer get 1 up, 1 down])
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  3. Graphics: Dealing with multi resolutions?
    By Kurisu33 in forum Windows Programming
    Replies: 3
    Last Post: 10-02-2006, 12:05 AM
  4. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 02:46 AM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21