为什么我的直接计数器保持零值?

时间:2014-03-30 17:10:06

标签: c loops struct poker

我在Ubuntu 10.04中使用GCC制作C90标准的程序,随机生成5张卡片结构的手,并计算手是否是同花顺,等等。

我计算直道的功能是:

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

我的主要职能:

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

我的问题是我在我的函数中声明的变量,结果,永远不会设置为1来指示手是否为直,这意味着我的straightCount变量始终保持为零值。我无法访问调试器,在我看来,我的代码是有道理的。我是C语言编程的新手,所以如果有人能帮助我指出我的功能有什么问题,我会很感激。谢谢!

4 个答案:

答案 0 :(得分:0)

是的,再次阅读代码后,卡片数量不足......

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

然后你关心计数对,而不仅仅是个人卡,所以

  If (count == HAND_SIZE-1)

答案 1 :(得分:0)

for (i = 0; i < HAND_SIZE-1; i++) {表示您正在测试HAND_SIZE-1对(这是正确的),i从0到HAND_SIZE-2,因此count永远不会是HAND_SIZE }。

您只需将测试更改为if (count == HAND_SIZE-1)

即可

答案 2 :(得分: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;
}

答案 3 :(得分:0)

假设(a)点值是1 = Ace,2 = Deuce,......和(b)在传递给函数之前对手进行排序,(c)指针正好是五张牌,这里&#39快速的一个:

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

另外,我不会使用卡片结构。使用单个整数更快,更通用。查看http://etceterology.com/blog/2013/5/23/representing-playing-cards-in-software