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

This is a discussion on Explanation for analyze_hand() to detrmine whether the hand contains flush/straight/. within the C Programming forums, part of the General Programming Boards category; This is a part of the code. I am confused as to what the foll code works: /* check for ...

1. ## 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. 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.