微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将卡片颜色分配给 C 中的一副牌

如何解决将卡片颜色分配给 C 中的一副牌

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

#define SUITS 4
#define RANKS 13
#define DECK_SIZE 52
#define MAX 9
    typedef struct
    {
        char *rank;
        char *suit;
        char *colour;

    }Card;

    Card* make_deck();
    void print(Card*);
    void shuffle(Card*);
    void print_to_file(FILE *,Card *);

    int main()
    {
        Card *deck = make_deck();

        printf(" ***************Original Deck ***************\n");

        print(deck); /*print original deck */

        printf("\n\n ***************Shuffled Deck ***************\n");

        shuffle(deck);

        print(deck); /*print shuffled deck */

        printf("\n");

        return 0;

    }

    Card* make_deck()
    {
        /*You may want to use these arrays to point rank and suit to,or to strcpy from. Your choice which
         *you choose */
        char *ranks[] = { "King","Queen","Jack","10","9","8","7","6","5","4","3","2","Ace" };

        char *suits[] = { "Spades","Clubs","Hearts","Diamonds" };
        char *colours[] = { "Red","Black" };

        /*allocate space for 52 cards on the heap */
        Card *deck = (Card*) malloc(DECK_SIZE *(sizeof(Card)));

        for (int i = 0; i < DECK_SIZE; i++)
        {
            /*Set the ranks,suits and colours of the cards  */
            deck[i].rank = ranks[i % RANKS];

            deck[i].suit = suits[i / RANKS];
            deck[i].colour = colours[i];

        }

        return deck;

    }

    /*print the deck to the screen (see sample output on assignment sheet). */
    void
    print(Card *deck)
    {
        int i = 0;

        for (i = 0; i < DECK_SIZE; i++)
        {
            printf("%5s %-12s",deck[i].suit,deck[i].rank);

            if (0 == ((i + 1) % 3))
            {
                printf("\n");

            }
        }
    }

    /*traverse the deck,one card at a time,swapping the current card
     *with a randomly chosen card from the deck
     */
    void
    shuffle(Card *deck)
    {
        int swap = 0;   //index of card to be swapped
        int i = 0;  //counter
        Card temp = { "","" }; //temp holding place for swap
        srand(time(NULL));  //seed the random numbers with current time
        for (i = 0; i < DECK_SIZE; i++)
        {
            //generate a pseudo-random number from 0 to 51
            swap = rand() % DECK_SIZE;

            //swap current card with da swapper
            temp = deck[i];

            deck[i] = deck[swap];

            deck[swap] = temp;

        }
    }

    /*print the deck to a file */
    void
    print_to_file(FILE *filename,Card *deck) {}

在上面的代码中,我存储了一副纸牌的值并在终端中打印它们。但我有一个问题。我也想打印卡片的颜色。我做了一个颜色数组

char *colors = {"Red","Black"}

并将其存储在内存中。 当我运行应用程序时,只有前两张卡获得颜色,其余的则没有。 有什么建议吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。