Thread: Explanation for analyze_hand() to detrmine whether the hand contains flush/straight/.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Explanation for analyze_hand() to detrmine whether the hand contains flush/straight/.

    This is a part of the code. I am confused as to what the foll code works:
    /* check for straight */
    rank = 0;
    while (num_in_rank[rank] == 0) rank++;
    for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
    num_consec++;
    if (num_consec == NUM_CARDS) {
    straight = true;
    return;

    say if the hands are 2d,3d,4d,5d,6d how wld the code work?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The while loop increments rank until it finds that a rank that you have a card in. In your example I'm guessing it would stop at 2 since you say the hand begins with a 2 of diamonds.

    The for loop then increments num_consec for each rank that has cards in it until it comes across a gap in your hand - at which point the for loop would abort early. Since your hand contains 2d, 3d, 4d, 5d, 6d. It would likely increment num_consec from 0 until it was 5. If your hand was 2d, 3d, 4d, 6d, 7d then the loop would stop with num_consec at 3.

    The if statement checks that num_consec is equal to NUM_CARDS (5 I'm guessing? Number of cards in a players hand?) If these numbers are equal then it means that a sequence of 5 cards of consecutive ranks was found indicating a straight. Again, if there were any gap in your hand, then num_consec would not be equal to NUM_CARDS and there would therefore be no straight.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. so let me get this straight...
    By Sea Monster in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-11-2005, 02:48 AM
  2. Trying to get a few things straight
    By homeyg in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2004, 11:23 PM
  3. what are some other 'life skills' that can go hand in hand with programming
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-17-2003, 02:34 PM
  4. Let's Get Something Straight
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-18-2002, 02:09 AM