Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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)

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)

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

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