문제

I have a the following function:

int isXOfAKind(card *hand, int x, enum pips pip) {
    //... do something
}

I would like the third argument to be the pips inside the following struct:

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

My header file:

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

#define DECKSZ 52
#define HAND_SIZE 5

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;

extern card deck[];

void initDeck(card[]);
void labelCards(card[]);
void shuffleDeck(card[]);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
int isFlush(card*);
int isStraight(card*);
int isXOfAKind(card*, int, enum pips);
int isStraightFlush(card*);
int isFullHouse(card*);
int isTwoPair(card*);

My source file:

#include "Poker.h"

int main(void) {
    card deck[DECKSZ];
    card *pDeck = &deck[0];
    initDeck(deck);
    labelCards(deck);
    shuffleDeck(deck);
    displayHand(deck);
    return EXIT_SUCCESS;
}

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

void labelCards(card deck[]) {
    static const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
    static const char *suitNames[] = {"Spades","Hearts","Diamonds","Clubs"};
    int i;
    for (i = 0; i < DECKSZ; i++) {
        sprintf(deck[i].cardName, "%s of %s\n", pipNames[i % 13], suitNames[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;
    for (i = 0; i < HAND_SIZE; i++) {
        printf("%s", 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;
}

int isFlush(card *hand) {
    int i, result = 0;
    for (i = 0; i < HAND_SIZE - 1; i++) {
        if ((hand+i)->suits != (hand+i+1)->suits) {
            result = 1;
            break;
        }
    }
    return result;
}

int isStraight(card *hand) {
    int i, result = 0;
    for (i = 0; i < HAND_SIZE - 1; i++) {
        if ((hand+i)->pips != (hand+i+1)->pips + 1) {
            result = 1;
            break;
        }
    }
    return result;
}

int isXOfAKind(card *hand, int x, enum pips pip) {
    int result = 0;
    return result;
}

int isStraightFlush(card *hand) {
    int result = 0;
    result += isFlush(hand);
    result += isStraight(hand);
    return result;
}

int isFullHouse(card *hand) {
    int result = 0;
    result += isXOfAKind(hand, 3, hand->pips);
    result += isXOfAKind(hand, 2, hand->pips);
    return result;
}

int isTwoPair(card *hand) {
    int result = 0;
    result += isXOfAKind(hand, 2, hand->pips);
    result += isXOfAKind(hand, 2, hand->pips);
    return result;
}

How would I use the pips enum as a parameter for my function? I'm using GCC in Ubuntu and coding in ANSI C. Thanks!

도움이 되었습니까?

해결책

update

You made the fixes to you code correctly based on the initial answer but you just have a typo which once you fix will give you clean code:

int isXOfAKind(card*, int, enum pips);
                                ^^^^

should be:

int isXOfAKind(card*, int, enum pip);
                                ^^^

here as well:

int isXOfAKind(card *hand, int x, enum pips pip) {
                                       ^^^^^^^^

should be:

int isXOfAKind(card *hand, int x, enum pip pips) {
                                       ^^^^^^^^  

Original answer

The problem is that pips is a variable not a type here:

enum {ACE=1, TWO, ... } pips;
                        ^^^^

This would create a type pips:

enum pips {ACE=1, TWO, } myPip;
     ^^^^

See the following sample live:

#include <stdio.h>

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

void test( enum pips pip) {
   printf( "pip = %d\n", pip ) ;
}

int main()
{
  test( TWO ) ;
  test( KING ) ;
}

If we go to the draft C99 standard we see in section 6.7.2.3 Tags paragraph 6 says (emphasis mine):

type specifier of the form

struct-or-union identifieropt { struct-declaration-list }

or

 enum identifieropt { enumerator-list }

or

  enum identifieropt { enumerator-list , }

declares a structure, union, or enumerated type. The list defines the structure content, union content, or enumeration content. If an identifier is provided,130) the type specifier also declares the identifier to be the tag of that type.

where footnote 130 says:

If there is no identifier, the type can, within the translation unit, only be referred to by the declaration of which it is a part. Of course, when the declaration is of a typedef name, subsequent declarations can make use of that typedef name to declare objects having the specified structure, union, or enumerated type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top