Thread: Theoretical question about dealing cards like an ACTUAL dealer

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    11

    Theoretical question about dealing cards like an ACTUAL dealer

    i'm working on a simple card game and I'm having trouble envisioning how a loop that deals cards like a real dealer would look.

    I don't have code that needs fixing yet, I'm just having trouble picturing what needs to happen in the loop control - before I've even started writing it.

    I figure there will be 3 separate variables that are looping at different rates, but I'm not sure how to go about doing that.

    I'll try to explain what I mean with quick examples.

    So in theory, i'd have my deck of cards as an array:
    deck_card[52]

    Then my players (lets say there are 3 of them) would be setup as a 2d array. And if they could hold a maximum of 5 cards I'd use:
    player[3][5]

    So the simplest "deal" would probably be pulling the first 5 cards from deck_card and assigning them to player[0], the next 5 cards to player[1] and so on...
    And since the deck has been shuffled, it's still random so that works fine.

    But an actual dealer sends the first card to player one, the second card to player two... etc... going around the table.
    Code:
    deck_card[0] goes to player[0][0]
    deck_card[1] goes to player[1][0]
    deck_card[2] goes to player[2][0]
    deck_card[3] goes to player[0][1]
    deck_card[4] goes to player[1][1]
    deck_card[5] goes to player[2][1]
    deck_card[6] goes to player[0][2]
    etc...
    Looking at that, I'm not immediately seeing a pattern that could easily be plugged into a "for" loop. Is this a multiple loop problem, or is there a math algorithm that i'm not seeing?

    If the number of players and the number of cards in each hand were dependent on the function arguments it gets even more confusing to me.

    So what would that loop look like? 3 different variables incrementing at different rates, to different maximum values.


    Also, am I wasting my time thinking about this? I mean, I have no idea how to start doing it, so maybe that's a sign. Should I just stick to "1st 5 cards to player one, 2nd 5 cards to player two"? I realize in general, simpler is better... this idea was just nagging me.

    Plus, I'm a beginner to C so my knowledge is pretty low level.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Something has occurred to me. Why are questions on this forum almost always grouped into similar or the same questions? Are you all the same programming class?

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Hodor View Post
    Something has occurred to me. Why are questions on this forum almost always grouped into similar or the same questions? Are you all the same programming class?
    Gotta be.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    11
    Quote Originally Posted by Hodor View Post
    ...Are you all the same programming class?
    I have posted 3 questions today dealing with a card game project I'm working on. They were 3 different problems I was having so I asked in 3 separate posts.

    Is that bad form?
    Should I have kept all my questions in a single thread?
    My apologies if that's the case.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by unsolicited View Post
    So what would that loop look like?
    It's just a double loop, possibly with a separate counter for the deck. You want to deal 5 cards each to 3 people, or, more generally, HAND_SIZE cards each to NUM_PLAYERS players:
    Code:
    int top = 0;
    for (card = 0; card < HAND_SIZE; card++)
        for (int pnum = 0; pnum < NUM_PLAYERS; pnum++)
            player[pnum][card] = deck[top++];
    Last edited by oogabooga; 12-18-2013 at 04:41 AM. Reason: changed duplicate variable name
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by unsolicited View Post
    I have posted 3 questions today dealing with a card game project I'm working on. They were 3 different problems I was having so I asked in 3 separate posts.

    Is that bad form?
    Should I have kept all my questions in a single thread?
    My apologies if that's the case.
    I don't think it's a problem.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by unsolicited View Post
    I figure there will be 3 separate variables that are looping at different rates, but I'm not sure how to go about doing that.
    Three nested loops is one way. It is not the only way.

    Quote Originally Posted by unsolicited View Post
    Code:
    deck_card[0] goes to player[0][0]
    deck_card[1] goes to player[1][0]
    deck_card[2] goes to player[2][0]
    deck_card[3] goes to player[0][1]
    deck_card[4] goes to player[1][1]
    deck_card[5] goes to player[2][1]
    deck_card[6] goes to player[0][2]
    etc...
    Looking at that, I'm not immediately seeing a pattern that could easily be plugged into a "for" loop. Is this a multiple loop problem, or is there a math algorithm that i'm not seeing?
    There's a mathematical relationship between indices that you're not seeing.

    The only hint I'll give you is to think about what integer division does and the modulus operator does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Two nested loops is the easy to understand way.

    But as grumpy says, it can also be done with one loop, looping through the total number of cards to be dealt, and using modulus and integer division for the player indices.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Dec 2013
    Posts
    11
    Quote Originally Posted by oogabooga View Post
    Code:
    int top = 0;
    for (card = 0; card < HAND_SIZE; card++)
        for (int pnum = 0; pnum < NUM_PLAYERS; pnum++)
            player[pnum][card] = deck[top++];
    Wow! That was more than I was looking for. I was trying to get a sense for how to begin, but you... you went and did it all!
    i guess the problem wasn't as complex as i thought it was?
    Thank you so much! Very generous.

    Can i ask how you can see that? (I know that's a weird question)
    Like, i was trying to draw out nested loops on paper to get a mental idea of how they might interact. But I still can't see it.

    I'm gonna keep re-reading and digesting your loop in the meantime until it hopefully makes sense.
    Thank you.

    And thank you grumpy for the math hint.
    But that's not happening in my lifetime. My math skills are utter garbage.
    I understand what both of those operators do*, but applying them creatively is beyond me.
    I envy both of you for being able to see this stuff.
    Thanks again!

    *Actually that's not exactly true. i have to revisit modulus every couple of days to confirm to myself how it works! I think I get it one day and then... it kind of slips out.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by unsolicited View Post
    Wow! That was more than I was looking for. I was trying to get a sense for how to begin, but you... you went and did it all!
    i guess the problem wasn't as complex as i thought it was?
    Thank you so much! Very generous.
    Can i ask how you can see that? (I know that's a weird question)
    Like, i was trying to draw out nested loops on paper to get a mental idea of how they might interact. But I still can't see it.
    It may have been too generous. It's actually more work to write an answer like grumpy's than to blurt out code, which becomes a kind of a reflex.

    Anyway, about the algorithm, I think the insight is to see that the problem is one of copying (some of) a one-dimensional array into a two-dimensional array. But the process can be looked at as either one- or two-dimensional.

    You can use a 2d-loop to loop through the player elements, setting them to consecutive cards from deck. The deck index can either be kept explicitly or can be calculated from the player indices.

    Or you can use a 1d-loop through deck, setting the player elements to each card. The player indices could be kept explicitly or can be calculated from the deck index.

    EDIT:
    Actually, as long as you declare your player array with the HANDSIZE first and NUMPLAYERS second you can deal the cards in an interleaved manner with a single memcpy.
    Code:
      int player[HANDSIZE][NUMPLAYERS];
      memcpy(player, deck, HANDSIZE*NUMPLAYERS*sizeof(int));
    Last edited by oogabooga; 12-18-2013 at 05:51 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by unsolicited View Post
    Can i ask how you can see that? (I know that's a weird question)
    Like, i was trying to draw out nested loops on paper to get a mental idea of how they might interact. But I still can't see it.
    For me, it's all about looking for patterns and relationships between values.
    Given your example ....
    Code:
    deck_card[0] goes to player[0][0]
    deck_card[1] goes to player[1][0]
    deck_card[2] goes to player[2][0]
    deck_card[3] goes to player[0][1]
    deck_card[4] goes to player[1][1]
    deck_card[5] goes to player[2][1]
    deck_card[6] goes to player[0][2]
    etc...
    I picture it as a mapping of the form card_deck[i] -> player[j][k]. This means, for every value if i there must be a j and a k.

    For mapping i to j, the examples are
    Code:
       0->0
       1->1
       2->2
       3->0
       4->1
       5->2
       6->0
    The values on the right show a pattern i.e. 0,1,2,0,1,2,.... I then ask what sort of thing produces a sequence like that. It cycles every 3, and ramps up one step at a time from 0 to 2. In other words, j is always the remainder when dividing something (which is related to i somehow) by 3. j = i %3 does the trick.

    For mapping i to k, it is a similar thing.
    Code:
       0->0
       1->0
       2->0
       3->1
       4->1
       5->1
       6->2
    This time, however, the pattern is 0,0,0,1,1,1,2 In other words three values the same, then increment, then repeat. What gives that sort of pattern: integer division, that's what. 0/3 = 0, 1/3 = 0, 2/3 = 0, 3/3 = 1, etc/ So k = i/3.

    Quote Originally Posted by unsolicited View Post
    And thank you grumpy for the math hint.
    But that's not happening in my lifetime. My math skills are utter garbage.
    I understand what both of those operators do*, but applying them creatively is beyond me.
    Frankly, you're going to need to increase your basic math skills if you want to be able to do this sort of stuff - this particular problem is primary school stuff. If you insist on treating mathematics as something you recite by rote, rather than as something to understand and think about, you'll never get it. For people who look at mathematics as something to understand rather than to recite, it is easy. The basics concepts for answering your question, in this case, are integer arithmetic.

    The reason I gave you an elementary math answer is that you asked a theoretical question (that is your subject line on this thread). Mathematics is a basic language for articulating the theory you asked about.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Deck of Cards: Dealing Hands
    By Gabo Szto in forum C Programming
    Replies: 6
    Last Post: 10-20-2012, 07:58 PM
  2. Dealing cards to players
    By trums1992 in forum C Programming
    Replies: 8
    Last Post: 06-27-2012, 09:21 PM
  3. C++ without fear Card Dealer question
    By BISHDP in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2011, 04:19 AM
  4. dealing cards
    By braddy in forum C Programming
    Replies: 15
    Last Post: 03-29-2006, 03:46 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

Tags for this Thread