2014-03-27 47 views
-1

我正在使用ANSI C C90標準在GCC中創建一個小應用程序。爲什麼我的手計數器重置爲零?

我的頭文件:

#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*); 

我的主文件:

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

我的問題是關於我的我的main()函數內部變量numHands。每次我在我的甲板堆棧上調用reset()時,似乎都會將其重置爲零。我不想讓它重置。有人知道它爲什麼重置爲零,以及如何讓它不能重置?謝謝!

+0

你的代碼中有'numHands - = 1;'語句。該語句似乎是'numHands'可以改變爲零的唯一一個(在一個循環中)。調試你的程序,特別注意那行(或者在減量之前加上'printf(「numHands:%d \ n」,numHands);')。還可以啓用** ALL **警告('clang -Weverything ...')並記住那些警告。 – pmg

+0

我在我的主文件中得到'missing initializer'警告,其代碼如下:'card deck [DECKSZ] = {0};'''card hand [HAND_SIZE] = {0};'',stack deckStack = {0};'和'stack handStack = {0};'。我唯一的警告是因爲我沒有在'struct card ...'中明確地給出'enum pip ...'和'enum suit'。 –

+0

至於'numHands'當我打印到遞減之前它的價值,它的價值是'11',但隨後的下一次我打印'flushCount'和'straightCount'它的值是'0' ... –

回答

1

這裏您當您運行的程序訪問前面的出界元素的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] */ 
     } 

我懷疑被非法訪問引發的未定義行爲改變的numHands值觸發未定義行爲。它今天可能會做一些不同的事情。

相關問題