c - 为什么我的直接计数器的值始终为零?

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

我的问题是我的函数中声明的变量 result 从未设置为 1 来指示这手牌是否是顺子,因此这意味着我的 StraightCount 变量始终保持为零值。我无法访问调试器,在我看来,我所拥有的代码是有意义的。我是 C 编程新手,所以如果有人能帮助我指出我的函数有什么问题,我将不胜感激。谢谢!

最佳答案

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

关于c - 为什么我的直接计数器的值始终为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747031/

相关文章:

c - 用厘秒修复 FPS

python - 循环遍历包含字典的嵌套列表

返回 C.CString 时出现 CGo 段错误

c - 这个 for 循环中缺少什么

c - 无法使用 fgets 函数读取字符串

python - 当子列表的顺序无关紧要时,从列表列表中获取唯一元素

php - mysql/php 循环有多种形式还是只有一种?

c - 结构指针操作

c - 如何调用以结构体作为参数的函数?

c - 使用 fscanf 从文件读取并保存到 struct c 语言的数组中