我如何在我的二十一点游戏 C 中将 int 值分配给 const char 数组

How can I assign int values to const char array in my blackjack game, C

我正在用 C 编写 21 点游戏。我有三个功能,一个用于填充牌组,一个用于洗牌,一个用于发牌。我的问题是我不知道如何给我的卡片一个整数值,我需要它来查看谁赢了。如果您能就如何解决此问题提供一些意见,我将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"

/*fill deck with 52 cards*/
void fillDeck(Card * const Deck, const char *suit[], const char *deck[]){

int s;
for (s = 0; s < 52; s++){
    Deck[s].suits = deck[s % 13];
    Deck[s].decks = suit[s / 13];
}
return;
}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"

/*shuffle cards*/
void shuffle(Card * const Deck){
int i, j;
Card temp;

for (i = 0; i < 52; i++){
    j = rand() % 52;
    temp = Deck[i];
    Deck[i] = Deck[j];
    Deck[j] = temp;
}
return;
}



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

/*deal cards*/
void deal(const Card * const Deck, int size, int size_1, int size_2){

int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];

printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);

printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();

    printf("%s's card:\n", name1);
    for (i = 0; i < size; i++){
        printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
    }

    printf("\n%s's card:\n", name2);
    for (i = 2; i < size_1; i++){
        printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
    }

    printf("\nDealer card:\n");
    for (i = 4; i < size_2; i++){
        printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
    }
return;
}



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

int main(void){

Card allCards[52];
const char *suits[] = { "spades", "hearts", "diamonds", "clubs" };
char *decks[] = { "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king" };
int *values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };

srand(time(NULL));

fillDeck(allCards, suits, decks, values);
shuffle(allCards);
deal(allCards, 2, 4, 6);

getchar();
return 0;
}



/*func.h*/
struct card{
    const char *suits;
    const char *decks;
};

typedef struct card Card;

void fillDeck(Card * const Deck, char *suit[], char *deck[]);

void shuffle(Card * const Deck);

void deal(const Card * const Deck, int size, int size_1, int size_2);

#endif

我建议您在卡片结构中添加一个字段来保存该值。

typedef struct card {
    const char *suits;
    const char *decks;
    int value;
}

要为卡片分配您要使用的值:

void fillDeck(Card * const Deck, const char *suit[], const char *deck[], const int *value[])
{
    int s;
    for (s = 0; s < 52; s++){
        Deck[s].suits = suit[s / 13];
        Deck[s].decks = deck[s % 13];
        Deck[s].value = value[s % 13];
    }
}

然后当您确定获胜者时,您可以简单地查看他们手中的所有牌并总结价值。

不要将程序的卡片值与其等级混淆 - 明确区分程序使用的等级 0..12 和卡片的描述值。

//char *suitname[] = { "Hearts", "Clubs", "Diamonds", "Spades" };
//char *rankname[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", 
                          "Ten", "Jack", "Queen", "King", "Ace" };

char *suitname[] = { "Hearts", "Diamonds", "Spades", "Clubs" };
char *rankname[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", 
                          "Ten", "Jack", "Queen", "King" };

无论 "Ace" 是高还是低,在任何情况下排名 "Three" 都没有 3 的值。我建议处理(双关语!)卡值 0..51 并根据需要获得花色和等级

int suit = card / 13;       // range 0..3
int rank = card % 13;       // range 0..12

在这两种情况下,花色或等级是 0 到花色(双关语!)数组索引的值。

关于ranks的分数值,你可以这样定义另一个数组

int cardvalue[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };

所以你可以用

得到卡片的价值
int value = cardvalue[card % 13];

并用

打印其描述
printf("%s of %s", rankname[card % 13], suitname[card / 13]);

您还可以按照您定义的顺序获得花色等级(在某些纸牌游戏中很重要),如我所示。