Question

I am creating a small application in GCC using the ANSI C C90 standard.

My header file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DECKSZ 52
#define HAND_SIZE 5

#define STACKMAX 52
#define EMPTY -1
#define FULL (STACKMAX-1)

typedef enum boolean {false, true} boolean;

typedef struct card {
    enum pip {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
    enum suit {SPADES, CLUBS, HEARTS, DIAMONDS} suits;
    char cardName[20];
} card;

typedef struct stack {
    card s[STACKMAX];
    int top;
} stack;

extern card deck[];

void initDeck(card[]);
void shuffleDeck(card[]);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
boolean isFlush(card[]);
boolean isStraight(card[]);
boolean isXOfAKind(card[], int, enum pip);
boolean isStraightFlush(card[]);
boolean isFullHouse(card[]);
boolean isTwoPair(card[]);
boolean isEmpty(stack*);
boolean isFull(stack*);
void push(card, stack*);
card pop(stack*);
void reset(stack*);

My main file:

#include "Poker.h"

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 {
        numHands += 1;
        reset(&handStack);
        for (i = 0; i < HAND_SIZE; i++) {
            push(pop(&deckStack), &handStack);
            if (isEmpty(&deckStack)) {
                reset(&handStack);
                i = HAND_SIZE;
                shuffleDeck(deck);
                reset(&deckStack);
                numHands -= 1;
                for (j = 0; j < DECKSZ; j++) {
                    push(deck[j], &deckStack);
                }
            }
            hand[i] = handStack.s[i];
        }

        arrangeHand(hand);
        /*displayHand(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;
}

void initDeck(card deck[]) {
    int i;
    for (i = 0; i < DECKSZ; i++) {
        deck[i].pips = (const)((i % 13) + 1);
        deck[i].suits = (const)(i / 13);
    }
}

void shuffleDeck(card deck[]) {
    int i, j;
    for (i = 0; i < DECKSZ; i++) {
        j = rand() % DECKSZ;
        swap(&deck[i], &deck[j]);
    }
}

void displayHand(card hand[]) {
    int i, tmpPip = 0, tmpSuit = 0;
    const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    const char *suitNames[] = {" of Spades"," of Hearts"," of Diamonds"," of Clubs"};
    for (i = 0; i < HAND_SIZE; i++) {
        tmpPip = (hand[i].pips) - 1;
        tmpSuit = (hand[i].suits);
        strcpy(hand[i].cardName, pipNames[tmpPip]);
        strcat(hand[i].cardName, suitNames[tmpSuit]);
        printf("%s\n", hand[i].cardName);
    }
}

void arrangeHand(card *hand) {
    int i, j;
    for (i = HAND_SIZE-1; i >= 0; i--) {
        for (j = 0; j < i; j++) {
            if ((hand+j)->pips > (hand+j+1)->pips)
                swap(hand+j, hand+j+1);
        }
    }
}

void swap(card *c1, card *c2) {
    card temp;
    temp = *c1;
    *c1 = *c2;
    *c2 = temp;
}

boolean 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 ((boolean) (result));
}

boolean isStraight(card hand[]) {
    int i, count = 0, 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 ((boolean) (result));
}

boolean isXOfAKind(card hand[], int x, enum pip pipsIgnored) {
    int i, count = 0, result = 0;
    for (i = 0; i < HAND_SIZE - 1; i++) {
        if (hand[i].pips == hand[i+1].pips) {
            if (hand[i].pips != pipsIgnored) {
                count++;
            }
        }
    }
    if (count == (x - 1))
        result = 1;
    return count;
}

boolean isStraightFlush(card hand[]) {
    int result = 0;
    result = isFlush(hand);
    result = isStraight(hand);
    return ((boolean) (result));
}

boolean isFullHouse(card hand[]) {
    int result = 0;
    result = isXOfAKind(hand, 3, 0);
    result = isXOfAKind(hand, 2, 0);
    return ((boolean) (result));
}

boolean isTwoPair(card hand[]) {
    int result = 0;
    result = isXOfAKind(hand, 2, hand->pips);
    result = isXOfAKind(hand, 2, hand->pips);
    return ((boolean) (result));
}

boolean isEmpty(stack *stk) {
    return ((boolean) (stk->top == EMPTY));
}

boolean isFull(stack *stk) {
    return ((boolean) (stk->top == FULL));
}

void push(card c, stack *stk) {
    stk->top++;
    stk->s[stk -> top] = c;
}

card pop(stack *stk) {
    return (stk->s[stk->top--]);
}

void reset(stack *stk) {
    stk->top = EMPTY;
}

My question is about my variable numHands inside my main() function. It seems to reset itself to zero every time I call reset() on my deck stack. I do not want it reset at all. Does anybody know why it's resetting to zero, and how I can get it to not reset? Thanks!

Was it helpful?

Solution

here you trigger Undefined Behaviour by accessing an out of bounds element of hand

        for (i = 0; i < HAND_SIZE; i++) {
            push(pop(&deckStack), &handStack);
            if (isEmpty(&deckStack)) {
                reset(&handStack);
                i = HAND_SIZE;                     /* <== set i to 5 */
                shuffleDeck(deck);
                reset(&deckStack);
                numHands -= 1;
                for (j = 0; j < DECKSZ; j++) {
                    push(deck[j], &deckStack);
                }
            }
            hand[i] = handStack.s[i];             /* <== illegal hand[5] */
        }

I suspect the Undefined Behaviour triggered by the invalid access changed the value of numHands when you run your program earlier. It might do some thing different today.

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