Thread: Dealing cards to players

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    15

    Dealing cards to players

    So I am currently creating a game of blackjack and I am found to be stuck. I am trying to deal out the cards between a computer and a user. I then want those cards to be set as part of the players hand until the round is over. Those cards are then removed from the deck until the deck is reshuffled after all 52 cards are used. I currently have a function that allows the deck to print cards. Any helpful methods of going about this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    
    
    /*Prototypes*/
    void intro (void);
    void shuffle(int deck[], int array_size);
    int start_deck(int initial[], int size);
    void deal_card(int deck[], int size);
    void first_deal(int deck[],int size);
    
    
    /*Global Variables*/
    int count=0;
    int playsum=0;
    int compsum=0;
    int plycards[10];
    int compcards[10];
    
    
    int main(void)
        {
            int deck[52];
            int plycards[10];
            int compcards[10];
    
    
            srand(time(NULL));
    
    
            intro();
            shuffle(deck,52);
    
    
            deal_card(deck,52);
            getch();
    
    
        }
    
    
    void intro(void)
        {
            printf("Welcome to the game called 31.\n");
            printf("The object of this game is to get exactly thirty one or as close as possible.\n");
            printf("Whoever gets a higher score that is less than 31 is the winner.\n");
            printf("If you or the computer exceeds 31, the opponent wins by default.\n");
            printf("If there is a tie, the computer wins by default.\n");
            printf("\n\nPress any key to continue...\n");
            getch();
            printf("\n\nThe deck reshuffles after all card have been dealt.\n");
            printf("J,Q,K all count as 10. An 'A' can count as an 11 or a 1.\n");
            printf("The basic premise of the game is that you want to have a hand value that is \ncloser to 31 than that of the dealer, without going over 31.\n");
            printf("\nHave Fun!!\n");
            printf("\nPress any key to play the game.");
    
    
            getch();
            system ("cls");
    
    
        }
    
    
    
    
    int start_deck (int initial[],int size) //initializes the deck
        {
            int i;
    
    
            for (i=0; i<size; i++) //Used to increment 52 cards for deck
                initial[i]=i;
        }
    
    
    void shuffle(int deck[], int array_size) //Shuffles deck
        {
            int i , j , temp ; //temp used to help shuffle
    
    
            start_deck(deck,52);
    
    
            for( i = 0 ; i < array_size ; i ++ )
                {
                    j = rand() % array_size ; //chooses random number for the array swapping
                    temp = deck[i] ;
                    deck[i] = deck[j] ;
                    deck[j] = temp ;
                }
        }
    
    
    void deal_card(int deck[], int size) //Prints deck to window
        {
            int i,card;
            static int end=0;
            static int start=0;
            char suit[4][9]={"Diamonds","Hearts","Clubs","Spades"}; //Suit of the cards in deck
            char rank[13][6]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}; //ranking of cards
    
    
            end=start+1;
    
    
             if (end == 53)
                {
                    start=0;
                    end=1;
                    shuffle(deck,52);
                    printf("\n\nThe deck ran out of cards and was re-shuffled.\n\n\n");
                }
    
    
                for (i=start; i<end; i++) //computes the rank and suit of each card in the deck
                printf("%s of %s\n",rank[deck[i]%13],suit[deck[i]/13]);
    
    
            start++;
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shuffle the desk. Use a counter to keep track of where the "top" of the deck is. Start it at 0. Deal one card by assigning deck[ counter ] to whomever is getting that card. Then increment counter. When you reach the end of the deck, shuffle the deck again and reset the counter.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    My program does this already I believe. I was just wondering if I have to store the dealt card in an array for each player so that it knows which cards are whose.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    My program does this already I believe. I was just wondering if I have to store the dealt card in an array for each player so that it knows which cards are whose.
    Yes, that would be the best way to keep track of hands.
    Last edited by Matticus; 06-27-2012 at 11:09 AM. Reason: misread post

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Ok so how would I go about doing that then? I know I will have to call a function deal_deck and store the dealt card into the plycard, which is the players hand. My deal_deck function deals one card each time it is called. So if I wanted to store the called card into the players hand what would I do?
    I think its something like
    Code:
    plycards=deal_card(deck,52);
    And I would have to do this after declaring plycards[]; I know I am incorrect and could really be confusing some thing up here. Thankful for any help.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm assuming you're assigning a numeric value to each possible card value; ie:

    Code:
      0 = 2 clubs
      1 = 3 clubs
      etc...
    Therefore, after shuffling the deck, your "deck" array contains shuffled values; ie:

    Code:
      deck[0] == 16
      deck[1] == 4
      deck[2] == 32
      etc...
    If this is the case, then you can deal the initial hand as follows:

    Code:
    // generic code
    for(deckCounter=0; deckCounter<maxHandSize; deckCounter+=2)
    {
        playerCard[deckCounter] = deck[deckCounter];
        ComputerCard[deckCounter] = deck[deckCounter+1];
    }
    This example does not assume the use of functions - this will require a little more development work on your part to incorporate the functions you've written. The value of "deckCounter" in this example should not be reset to zero until a shuffle occurs. This way, you're pointing to the next valid (undrawn) card in the "deck" array.

    Is this along the lines of what you were thinking?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    FYI - Just realized there is a large flaw in my "generic code" above, but I'll leave that as an exercise to the OP.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try something like this
    Code:
    int deck[DECKSIZE], player[HANDSIZE], dealer[HANDSIZE];
    int deckTop = 0, playerTop = 0, dealerTop = 0;
    
    // Deal first four cards
    player[playerTop++] = deck[deckTop++];
    dealer[dealerTop++] = deck[deckTop++];
    player[playerTop++] = deck[deckTop++];
    dealer[dealerTop++] = deck[deckTop++];
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    That worked very nicely. Thank you very much, you made my life much easier. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MP3 Players
    By Dark_Phoenix in forum Tech Board
    Replies: 5
    Last Post: 12-03-2006, 03:22 PM
  2. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 03:46 AM
  3. dvd players
    By metsman20 in forum Tech Board
    Replies: 1
    Last Post: 12-26-2003, 09:56 PM
  4. mp3 players vs mp3 cd players
    By Geo-Fry in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-06-2003, 09:22 PM
  5. I need some help with dealing cards.
    By collegegal in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 10:13 PM