Thread: dealing cards

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    dealing cards

    Hi ,
    I want to make a cards that distribute cards for 4 players. deck[5][20] is arrays of stings that have the name of the cards.

    the first card (from deck)goes to the first player, the second card (from deck) to the second player, the third card to the third player... until each player has 5 cards

    this is my approach:



    Code:
    for(j=0;j<5;j++)
                     { for(i=0;i<19;i=i+5)
                       { hand1[j][20]=deck[i][20];
                         hand2[j][20]=deck[i+1][20];
                         hand3[j][20]=deck[i+2][20];
                         hand4[j][20]=deck[i+3][20];
                         }
                      }
    then I have to print each hand.
    The code compiles well but it prints gibberish.
    Please, do you know how to fix it?
    thank you
    B

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    foo[x][20] is not a valid index. It is one past the end of your array, which can only ever be referenced as a comparison to its address, not its contents. That is to say, your array would only have the following valid indexes:
    Code:
    foo[x][0]
    ...
    foo[x][19]
    Anything less than 0 and greater than 19 cannot be used. Also if it 'prints gibberish', and that is in theory what you're trying to debug, you'd want to test/post your printing segment also, because there's a good chance you're doing that wrong too.


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

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Also, you said your deck was declared deck[5][20], but your i goes up to 19.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by SlyMaelstrom
    Also, you said your deck was declared deck[5][20], but your i goes up to 19.
    It is a typo it is
    char deck[52][20]..

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's really odd about the whole thing, is why your deck is a 2D array instead of a single dimension. Also, why is the hand a 2D array, instead of just an array of five cards?


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

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, I'm also curious to know what the 20 is meant to represent. Is it perhaps 20 games?
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by SlyMaelstrom
    Yes, I'm also curious to know what the 20 is meant to represent. Is it perhaps 20 games?
    when it is said char deck [52][20] ,this means that there is 52 arrays of strings whose length(max) of 20 characters.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    MMmm... I see. You should be using structs.

    You'll find that work with a deck of strings can get quite confusing when you start working out the kinks. How exactly do you plan on checking for pairs or flushes? You're going to parse every card in a persons hand? Consider trying something like the following:
    Code:
    typedef struct {
       char suit;
       int faceVal;
       char fullName[20];
    } card;
    
    /* Somewhere in main... */
    card *deck = malloc(sizeof(card) * 52);
    card *myHand = malloc(sizeof(card) * 5);
    
    /* Somewhere later in main... */
    if (myHand[i].faceVal == myHand[i+1].faceVal)
       printf("Yay! A pair!");
    Of course this is just a simple example.
    Last edited by SlyMaelstrom; 03-26-2006 at 11:22 PM.
    Sent from my iPadŽ

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by SlyMaelstrom
    MMmm... I see. You should be using structs.

    You'll find that work with a deck of strings can get quite confusing when you start working out the kinks. How exactly do you plan on checking for pairs or flushes? You're going to parse every card in a persons hand? Consider trying something like the following:
    Code:
    typedef struct {
       char suit;
       int faceVal;
       char fullName[20];
    } card;
    
    /* Somewhere in main... */
    card *deck = malloc(sizeof(card) * 52);
    card *myHand = malloc(sizeof(card) * 5);
    
    /* Somewhere later in main... */
    if (myHand[ i].faceVal == myHand[i+1].faceVal)
       printf("Yay! A pair!");
    Of course this is just a simple example.
    Why mess with malloc()? A deck is not dynamic.
    Code:
    card deck[52];
    card myHand[5];
    would suffice.

    Something to consider:
    Each deck has 52 cards, 4 suits, 13 cards each suit.
    Number the cards 0-51.
    CardNumber / 13 = 0-3 for the suit
    CardNumber % 13 = 0-12 for the rank (face value)

    Now all you have to deal with is a simple integer and small functions to return the suit or rank of any card.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by WaltP
    Why mess with malloc()? A deck is not dynamic.
    That's not true, consider if he wanted jokers, or perhaps wanted to play mutliple deck blackjack. I know you're not going to tell me all card games use 5 card hands. Sure, he could fix the side, but dynamic allocation could really make things easier if he wanted to add more functionality.

    Plus, it never hurts to learn a new trick.
    Sent from my iPadŽ

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A card deck can be represented by a 52 element array. Each card can be represented by a simple two digit number. This boils the problem down to sticking numbers in arrays and removing them from others.

    Look at a card deck
    2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH - 0 1 2 3 4 5 6 7 8 9 10 11 12
    2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD - 13 14 15 16 17 18 19 20 21 22 23 24 25
    2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS - 26 27 28 29 30 31 32 33 34 35 36 37 38
    2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC - 39 40 41 42 43 44 45 46 47 48 49 50 51
    See an algorithm here?

    Divide the card number by 13 to get the suite.

    Use this algo to translate numbers to strings, no need to store the strings at all.
    So each players hand becomes a simple array and the deck is just an array of 52 elements.

    Or you could store this information in a structure which has already been discussed. I see the need for only at max 1+(number_of_players) number of 1 dimensional arrays for the entire game. Each array would have at max 52 elements, but the players would not need 52 since they will never hold the entire deck in their hands. Also jokers can be represented by numbers as well. The actual face point value of the card can also be obtained using this system.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Indeed, it could be done like that. But, I've found decks are quite easier to maintain with structures. It'd be more difficult to make an efficient random shuffle for an array of integers and suppose you wanted to keep track of what's already been dealt so you don't have two people with the 3 of hearts.

    Personally, and this may be my C++ OOP side talking, but I'd rather use a card structure inside of a stack implementation. It's more realistic to how a deck actually works, and when you work with real life logic, I find it's easier to come to solutions.
    Sent from my iPadŽ

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    More difficult for a random shuffle? How when each card is a unique integer? I fail to see the issue there.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by Bubba
    More difficult for a random shuffle? How when each card is a unique integer? I fail to see the issue there.
    Thank you your your help.

    I will try to work on it.
    I had also some issue with shuffling but one thing at a time.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm going to agree with Bubba. It doesn't get any easier than using a set of integers and simple division/modulus. You can:

    1 - Use a sorted array, and shuffle it. Deal the first N cards.
    2 - Copy from random locations in a sorted array, to the hand.
    -2a- Skip-count from the start N places, where N is less than cards remaining; skipping count for 'empty' squares.
    -2b- Similar to 2a, use memmove to simply move the tail segment up one. Just keep track of remaining card count.

    There are more ways to do this, but those are a few variations, and they're all pretty easy. Some are more interesting than others.


    Quzah.
    Last edited by quzah; 03-27-2006 at 08:11 AM.
    Hope is the first step on the road to disappointment.

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, 03: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. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM
  5. I need some help with dealing cards.
    By collegegal in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 10:13 PM