2014-03-27 3 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으로 재설정하는 것으로 보입니다. 나는 그것이 전혀 재설정 싶지 않아요. 아무도 왜 0으로 재설정되고, 어떻게 재설정하지 못하게 할 수 있는지 알 수 있습니까? 감사!

+0

코드에 'numHands - = 1;'문이 있습니다. 그 진술은 numHands가 0에 도달하기 위해 바뀔 수있는 유일한 것으로 보인다. 해당 줄에 특별한주의를 기울여 프로그램을 디버그하십시오 (또는 numHands : % d \ n ", numHands);). 또한 ** ALL ** 경고를 사용하여 컴파일하고 ('clang -Weverything ...') 경고에주의하십시오. – pmg

+0

메인 파일에 다음과 같은 코드 줄이있는'missing initializer' 경고가 나타납니다 :'card deck [DECKSZ] = {0};','카드 핸드 [HAND_SIZE] = {0};','stack deckStack = {0};과 스택 stackStack = {0};'. 내 유일한 다른 경고는'struct card ... '에 명시 적으로'enum pip ...'와'enum suit'를주지 않았기 때문입니다. –

+0

그리고'numHands'의 값을 줄이기 전에 출력 할 때 그 값은'11'입니다 만, 다음에'flushCount'와'straightCount' 값을 출력 할 때'0'입니다 ... –

답변

1

여기에 당신은 당신이 당신의 프로그램 이전 실행할 때 내가 잘못된 접근에 의해 트리거 정의되지 않은 동작이 numHands의 값을 변경 의심 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] */ 
     } 

의 경계 요소의 부족에 액세스하여 정의되지 않은 동작을 트리거합니다. 오늘은 다른 것을 할 수도 있습니다.

관련 문제