Question

I'm writing a small poker application in C and I have counters for the number of flushes, straights, etc.

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 function for the number of flushes:

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

When I run the program, the code in the do...while loop is supposed to infinitely loop. For every hand popped off the stack, I want to calculate if it is a flush, straight, etc using functions like my isFlush() function here. The issue is these counters, such as numFlushes, remain at a value of zero. Does anybody know why the counters remain at zero, and how to fix that? Thanks!

Was it helpful?

Solution

The for loop in your isFlush function can at most increment the count HAND_SIZE-1 times. Since count starts at 0, it is never more than HAND_SIZE-1.

One option is to start your count at 1, since the first card always counts as 1 towards your flush no matter what suit it is. Anther option is to compare count to HAND_SIZE-1 in your if statement. Then you can actually return a non-zero value from isFlush.

BTW, if you step through the code with a debugger, you should easily see that your if condition is never met.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top