代码之家  ›  专栏  ›  技术社区  ›  Mallom

在21点游戏中,我如何将int值分配给const char数组,C〔closed〕

  •  0
  • Mallom  · 技术社区  · 9 年前

    我在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
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   Rainbacon    9 年前

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

    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];
        }
    }
    

    然后,当你决定赢家时,你可以简单地看一看他们手中的所有牌,并总结出它们的价值。

        2
  •  0
  •   Weather Vane    9 年前

    不要将程序的卡值与其等级混淆-明确区分等级 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" };
    

    无论“王牌”是高还是低,排名“三”的值都不为 3 。我建议交易(双关语!)带有卡值 0..51 并根据需要获得西装和军衔

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

    在这两种情况下,套装或等级的值范围为 0 ,适合(双关语!)数组索引。

    至于排名的得分值,您可以定义另一个数组,如下所示

    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]);
    

    正如我所展示的那样,你也可以通过定义花色的顺序来获得花色层次(在一些纸牌游戏中很重要)。