0

我正在为学校解决一个问题,并且在我的 suitsInHand 或 facesInHand 数组的每一行中都得到了“未声明的标识符”。上一次发生这种情况是因为我没有在块的顶部声明我的变量,但据我所知,这并不是出于同样的原因。谁能告诉我我做错了什么?

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

#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1

void dealACard(char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void analyzeHand(char *suits[], char *faces[]);

int main(void){
    int suitsInHand[4] = {0};
    int facesInHand[13] = {0};
    char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    int deck[4][13] = {AVAILABLE};

    srand(time(NULL));
    dealAHand(suits, faces, deck);

    system("pause");
    return 0;
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES]){
    int i;
    for(i=0;i<5;i++)
        dealACard(suits,faces,deck);
}

void dealACard(char *suits[], char *faces[], int deck[][FACES]){
    int suitIndex, faceIndex;
    memset(suitsInHand, 0, sizeof(suitsInHand));
    memset(facesInHand, 0, sizeof(facesInHand));

    suitIndex = rand() % 4;
    faceIndex = rand() % 13;
    while (deck[suitIndex][faceIndex]==TAKEN){
        suitIndex = rand() % 4;
        faceIndex = rand() % 13;
    }
    deck[suitIndex][faceIndex]=TAKEN;
    suitsInHand[suitIndex]++;
    facesInHand[faceIndex]++;

    printf("%s of %s.\n", faces[faceIndex], suits[suitIndex]);
}

void analyzeHand(char *suits[], char *faces[]){
    typedef enum {false, true} bool;
    int num_consec = 0;
    int rank, suit;
    bool straight = false;
    bool flush = false;
    bool four = false;
    bool three = false;
    int pairs = 0;

    for(suit=0;suit<SUITS;suit++)
        if(suitsInHand[suit]==5)
            flush = true;

    rank = 0;
    while (facesInHand[rank]==0)
        rank++;

    //for(;rank<FACES && facesInHand[rank]; rank++)
    //  num_consec++;

    if(num_consec == 5){
        straight = true;
        return;
    }

    for(rank = 0;rank<NUM_RANKS; rank++){
        if(facesInHand[rank]==4)
            four = true;
        if(facesInHand[rank]==3)
            three = true;
        if(facesInHand[rank]==2)
            pairs++;
    }

    if(straight == true && flush == true)
        printf("Straight flush.\n\n");
    else if(four == true)
        printf("Four of a kind.\n\n");
    else if(three == true && pairs == 1)
        printf("Full house.\n\n");
    else if(flush == true)
        printf("Flush.\n\n");
    else if(straight == true)
        printf("Straight.\n\n");
    else if(three == true)
        printf("Three of a kind.\n\n");
    else if(pairs == 2)
        printf("Two pairs.\n\n");
    else if(pairs == 1)
        printf("Pair.\n\n");
    else
        printf("High card.\n\n");


}
4

2 回答 2

2

这里:

void dealACard(char *suits[], char *faces[], int deck[][FACES]){
    int suitIndex, faceIndex;
    memset(suitsInHand, 0, sizeof(suitsInHand));

suitsInHand未在此范围内声明。suitsInHand在函数范围内声明,main不在dealACard范围内。

要解决此问题,您可以向函数添加参数以在文件范围内传递suitsInHand或声明。suitsInHand

这同样适用于facesInHand

于 2013-03-17T22:00:43.100 回答
1

您已经在 main 中声明suitsInHand,但这并不意味着它可用于其他函数,您需要将其传递给其他函数以便他们可以访问它。

您已经到了要传递很多东西的地步,您应该将它们放入一个结构中,然后传递该结构。

此外,一般来说,您的 dealACard 函数可能非常非常讨厌。特别是如果一副牌中只有一张牌,可能需要很长时间才能获得正确的随机数来处理一副牌中的最后一张牌

于 2013-03-17T22:01:23.167 回答