-1

I am making a program in the C90 standard using GCC in Ubuntu 10.04, that randomly generates a hand of 5 card structs and calculates if the hand is a flush, straight, etc.

My function to calculate straights is:

int isStraight(card hand[]) {
    int i, count = 1, result = 0;
    for (i = 0; i < HAND_SIZE-1; i++) {
        if (hand[i].pips == ((hand[i+1].pips) + 1)) {
            count++;
        }
    }
    if (count == HAND_SIZE)
        result = 1;
    return result;
}

My main function:

int main(void) {

    int i, j;
    int numHands = 0;
    int flushCount = 0;
    int straightCount = 0;
    int xOfAKindCount = 0;
    int straightFlushCount = 0;
    int fullHouseCount = 0;
    int isTwoPairCount = 0;

    card deck[DECKSZ] = {0};
    card hand[HAND_SIZE] = {0};

    stack deckStack = {0};
    stack handStack = {0};

    initDeck(deck);
    shuffleDeck(deck);
    reset(&deckStack);

    for (i = 0; i < DECKSZ; i++) {
        push(deck[i], &deckStack);
    }

    do {
        reset(&handStack);
        for (i = 0; i < HAND_SIZE; i++) {
            push(pop(&deckStack), &handStack);
            if (isEmpty(&deckStack)) {
                reset(&handStack);
                shuffleDeck(deck);
                reset(&deckStack);
                for (j = 0; j < DECKSZ; j++) {
                    push(deck[j], &deckStack);
                }
            }
                hand[i] = handStack.s[i];
            }

        numHands += 1;
        arrangeHand(hand);

        flushCount += isFlush(hand);
        straightCount += isStraight(hand);
        xOfAKindCount += isXOfAKind(hand, 2, 0);
        straightFlushCount += isStraightFlush(hand);
        fullHouseCount += isFullHouse(hand);
        isTwoPairCount += isTwoPair(hand);

        printf("Flushes:%d Straights:%d SF's:%d Number of Hands:%d\r",
            flushCount, straightCount, straightFlushCount, numHands);
    } while (1);

    printf("\n");

    return EXIT_SUCCESS;
}

My issue is my variable declared inside my function, result, is never set to 1 to indicate whether or not the hand is a straight, which therefore means my straightCount variable always remains at a value of zero. I do not have access to a debugger and in my mind the code I have makes sense. I'm new to programming in C, so if anybody could help me point out what is wrong with my function, I'd appreciate it. Thanks!

Benjamin
  • 345
  • 2
  • 3
  • 15

4 Answers4

0

Right, after reading the code again, there are not enogh cards...

 for (i = 0; i < HAND_SIZE-1; ++i)

Then you care counting pairs, not just individual cards, so

  If (count == HAND_SIZE-1)
Gábor Buella
  • 1,840
  • 14
  • 22
0

for (i = 0; i < HAND_SIZE-1; i++) { means that you are testing HAND_SIZE-1 pairs (which is correct), with i from from 0 to HAND_SIZE-2, so count will never be HAND_SIZE.

You just need to change your test to if (count == HAND_SIZE-1)

CMoi
  • 846
  • 4
  • 9
  • What do you mean? If there are 2 cards, you want count of cards followed by the next one to be 1. – CMoi Mar 30 '14 at 17:22
  • In the loop body, the OP always tests the indexed card vs the following card. Which card follows card `HAND_SIZE-1`? – Deduplicator Mar 30 '14 at 17:26
  • None, it will not be tested, the loop is about "i < HAND_SIZE-1" so HAND_SIZE-1 cards will be tested, from 0 to HAND_SIZE-2. – CMoi Mar 30 '14 at 17:29
  • Ah, he sets the first card as properly matched. So your code is still wrong. – Deduplicator Mar 30 '14 at 17:32
  • Can you explain how? If cards are [1 2 3], he will compare [1 2] with i=0 and set count to 1, then compare [2 3] with i=1 and set count to 2. What is wrong with that? – CMoi Mar 30 '14 at 18:30
  • That's just it. `count`starts at `1`, because the first card is always right. It's just a slight shift in view, but you only shifted it back halfway, so your code is a mess. – Deduplicator Mar 30 '14 at 18:37
  • I didn't shift anything, and didn't write any code. I think I will stop using this website to not read your aggressions. – CMoi Mar 30 '14 at 18:57
  • I'm sorrry if i offended you. I didn't want to sound in any way aggressive. – Deduplicator Mar 30 '14 at 19:00
  • Well, after repeating my code is wrong, you say it is a mess. I didn't write any code, I just explained what is wrong (expected count is wrong by one). – CMoi Mar 30 '14 at 19:03
0
int isStraight(card hand[]) {
  int step = 0;
  for(int i = 1;i < HAND_SIZE; i++)
    if(hand[i].pip != hand[i-1].pip+1)
      /* Substitute step with i!=1 if over-edge invalid */
      if(step || hand->pip != 1 || hand[i].pip != hand[i-1].pip+13-HAND_SIZE)
        return 0;
      else
        step = 1;
  return 1;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

Assuming that (a) pip values are 1=Ace, 2=Deuce, ... and (b) the hand is sorted before being passed to the function, and (c) hands are exactly five cards, here's a quick one:

int isStraight(card hand[]) {
    int i;
    // Handle Broadway special case
    if (hand[0].pips == 13 && hand[1].pips == 12 && hand[2].pips == 11 &&
        hand[3].pips == 10 && hand[4].pips == 1) return 1;

    // This will handle the rest
    for (i = 0; i < (HAND_SIZE-1); i += 1) {
        if (hand[i].pips != hand[i+1].pips) return 0;
    }
    return 1;
}

Also, I wouldn't use a structure for cards. Using a single integer is much faster and more versatile. Check out http://etceterology.com/blog/2013/5/23/representing-playing-cards-in-software

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55