为什么我的变量没有递增?

时间:2014-03-29 20:48:50

标签: c loops stack poker

我在C中写了一个小型扑克应用程序,我有冲洗,直道等数量的计数器。

主要功能:

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;
}

我的冲洗次数函数:

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;
}

当我运行程序时,do ... while循环中的代码应该无限循环。对于从堆栈中弹出的每一只手,我想使用像我的isFlush()函数这样的函数计算它是否是同花顺,直线等。问题是这些计数器(例如numFlushes)保持为零值。有人知道为什么计数器保持为零,以及如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

for函数中的isFlush循环最多可以增加HAND_SIZE-1次计数。由于count始于0,因此永远不会超过HAND_SIZE-1

一种选择是在count开始1,因为第一张卡总是计为1,无论它适合什么。 Anther选项是在count语句中将HAND_SIZE-1if进行比较。然后,您实际上可以从isFlush返回非零值。

顺便说一句,如果你使用调试器单步执行代码,你应该很容易看到你的if条件永远不会满足。

相关问题