c - 增加扑克牌的值(value) - 使用指针和结构

标签 c

我正在经历一些 c 编程问题,目前我被困在一个与指针相关的问题上

问:编写一个函数,将两张牌的二十一点手牌的值作为输入,并返回这手牌的总点数。值(value) 从“2”到“9”的牌数等于它们的面值,牌“T”、“K”、“Q”、“J”值 10 分,A(“A”)值 11 分 除非它带有另一张 A,否则第二张 A 值 1 分。该程序应该能够捕获不正确的输入。

例子: 输入牌:A Q 分数是21

输入卡片:A A 分数是12

我以前解决过这个问题,但这次我不得不使用指针,我对它还是很陌生。获取卡片值和计算卡片必须在一个函数中完成。这是我到目前为止所拥有的:

#include <stdio.h>
#define HAND 2
struct player_hand
{
     char card1;
     char card2;
};


void getHandValue(struct player_hand * hnd_ptr, char size, char size2)
{
    int first_card;
    int second_card;

    //get cards from user
    scanf("%c %c",&hnd_ptr->card1, &hnd_ptr->card2);
    printf("Enter Cards: %c %c", &hnd_ptr->card1, &hnd_ptr->card2);

    //check value of first card in hand 
    if(hnd_ptr->card1<='9' && hnd_ptr->card1>='2')
    {
        first_card=(int)hnd_ptr->card1 -48;

    }
    //check for special cards: king, queen, jack, ten
    else if(hnd_ptr->card1=='T'||hnd_ptr->card1=='K'||hnd_ptr->card1=='Q'||hnd_ptr->card1=='J')
    {
        first_card=10;
    }
    //if first card is Ace
    else if(hnd_ptr->card1=='A')
    {
        first_card=11;
    }
    else
    {
        //card not valid
        printf("Not a valid card: %c",hnd_ptr->card1);
        return;
    }

    //check value of 2nd card
    if(hnd_ptr->card2<='9' && hnd_ptr->card2>='2')
    {
        second_card=(int)hnd_ptr->card2 -48;

    }
    //if 2nd card is a special kind
    else if(hnd_ptr->card2=='T'||hnd_ptr->card2=='K'||hnd_ptr->card2=='Q'||hnd_ptr->card2=='J')
    {
        second_card=10;
    }
    //if 2nd card is Ace
    else if(hnd_ptr->card2=='A')
    {
        if(hnd_ptr->card1=='A')
        second_card=1;
        else
        second_card=11;
    }
    else
    {
        //if 2nd card not valid
        printf("Not a valid card: %c",hnd_ptr->card2);
        return;
    }

    add cards
    printf("\nThe total card value is: %d",first_card+second_card);

}

//call function, test if works
//calling it wrong?
int main(void) 
{
    struct player_hand hnd [HAND]  =  { {'A', 'A'}};
    getHandValue (hnd, HAND);
    return;
}

最佳答案

你有一些错误。

main 中的错误调用。

该函数不需要大小参数,如果需要,它们应该是 int

main 的错误返回

在函数中,printf是错误的。

事情比他们需要的要复杂得多,因为 struct 使用两个标量而不是数组。

我已经为您的程序创建了两个版本。一个带有错误注释的。还有一个可以清理东西。

这是注释版本:

#include <stdio.h>

#define HAND 2

struct player_hand {
    char card1;
    char card2;
};

// NOTE/BUG: use 'int' for size and size2
void
getHandValue(struct player_hand *hnd_ptr, char size, char size2)
{
    int first_card;
    int second_card;

    // get cards from user
    scanf("%c %c", &hnd_ptr->card1, &hnd_ptr->card2);

// NOTE/BUG: this would print the _address_ of the values vs. the values
    printf("Enter Cards: %c %c", &hnd_ptr->card1, &hnd_ptr->card2);

// NOTE/BUG [sort of]: the code below is cut-n-paste replication because you
// have separate card1 and card2 in the struct -- this "cries out" for an
// array and a loop. Consider the general case where you have 5 cards in the
// hand (e.g. five card charlie). The code would be easier even with an array
// of only two

    // check value of first card in hand
    if (hnd_ptr->card1 <= '9' && hnd_ptr->card1 >= '2') {
        first_card = (int) hnd_ptr->card1 - 48;

    }
    // check for special cards: king, queen, jack, ten
    else if (hnd_ptr->card1 == 'T' || hnd_ptr->card1 == 'K' || hnd_ptr->card1 == 'Q' || hnd_ptr->card1 == 'J') {
        first_card = 10;
    }
    // if first card is Ace
    else if (hnd_ptr->card1 == 'A') {
        first_card = 11;
    }
    else {
        // card not valid
        printf("Not a valid card: %c", hnd_ptr->card1);
        return;
    }

    // check value of 2nd card
    if (hnd_ptr->card2 <= '9' && hnd_ptr->card2 >= '2') {
        second_card = (int) hnd_ptr->card2 - 48;

    }
    // if 2nd card is a special kind
    else if (hnd_ptr->card2 == 'T' || hnd_ptr->card2 == 'K' || hnd_ptr->card2 == 'Q' || hnd_ptr->card2 == 'J') {
        second_card = 10;
    }
    // if 2nd card is Ace
    else if (hnd_ptr->card2 == 'A') {
        if (hnd_ptr->card1 == 'A')
            second_card = 1;
        else
            second_card = 11;
    }
    else {
        // if 2nd card not valid
        printf("Not a valid card: %c", hnd_ptr->card2);
        return;
    }

    printf("\nThe total card value is: %d", first_card + second_card);
}

//call function, test if works
//calling it wrong?
int
main(void)
{

// NOTE: based on usage, this is only an array because you're not using &hnd
// below
    struct player_hand hnd[HAND] = {
        {'A', 'A'}
    };

// NOTE/BUG: too few arguments to function, but why pass count at all?
    getHandValue(hnd, HAND);

// NOTE/BUG: need to return value (e.g. return 0)
    return;
}

这是清理后的版本:

#include <stdio.h>

#define CARDS_PER_HAND      2

struct player_hand {
    char card[CARDS_PER_HAND];
};

void
getHandValue(struct player_hand *hnd_ptr)
{
    int idx;
    int card;
    int sum;
    int count[CARDS_PER_HAND];

    // get cards from user
    printf("Enter Cards:");
    fflush(stdout);
    for (idx = 0;  idx < CARDS_PER_HAND;  ++idx)
        scanf(" %c", &hnd_ptr->card[idx]);

    // print cards
    printf("Cards entered:");
    for (idx = 0;  idx < CARDS_PER_HAND;  ++idx)
        printf(" %c", hnd_ptr->card[idx]);
    printf("\n");

    for (idx = 0;  idx < CARDS_PER_HAND;  ++idx) {
        card = hnd_ptr->card[idx];

        // simple cards
        if (card <= '9' && card >= '2') {
            count[idx] = (card - '2') + 2;
            continue;
        }

        switch (card) {
        case 'A':
            count[idx] = 11;
            if ((idx == 1) && (count[0] == 11))
                count[idx] = 1;
            break;

        case 'T':
        case 'K':
        case 'Q':
        case 'J':
            count[idx] = 10;
            break;

        default:
            printf("Not a valid card: %c", card);
            return;
            break;
        }
    }

    sum = 0;
    for (idx = 0;  idx < CARDS_PER_HAND;  ++idx)
        sum += count[idx];

    printf("The total card value is: %d\n", sum);
}

int
main(void)
{
    struct player_hand hnd;

    getHandValue(&hnd);

    return 0;
}

关于c - 增加扑克牌的值(value) - 使用指针和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165276/

相关文章:

c - 无法将结构传递给结构内的指针

c - 为什么使用 & 符号来检索整数的内存地址,而不是函数的地址?

c - 尽管文件已被另一个程序更改,但 fread 没有读取更新的值

python - 使用 PLY 用 Python 编写的语言会很慢吗?

c - 如何检查用户是否没有在C中输入所有空白字符

c# - 保证CPU响应?

c - 需要帮助解释 wiss 代码中使用/和 % 的位操作

c - 使用空指针交换数组中的元素

c - 迁移到实验性 gradle Android Studio 2.0+ 时包含的问题

c - 在 C 程序中嵌套 while 循环时遇到问题