简体   繁体   English

如何根据这些说明加载一副纸牌? C

[英]How can I load a deck of cards based on these instructions? C

These are the instructions my professor gave me for a Lab in C这些是我的教授给我的关于 C 实验室的说明

--SOLVED-- - 解决了 -

struct singlecard
{
  int cardnum;
  char face;
  char suit;
};

Your program must have the functions named as follows:您的程序必须具有如下命名的函数:

Main() - Calls "LoadDeck()" and "DealCards()." Main() - 调用“LoadDeck()”和“DealCards()”。

LoadDeck() will fill just the "cardnum" of the array with a unique number between 1 and 52. It will do this by selecting a random number and then calling the function "CheckDup()" to see if the number is a duplicate. LoadDeck() 将只用 1 到 52 之间的唯一数字填充数组的“cardnum”。它会通过选择一个随机数然后调用函数“CheckDup()”来查看该数字是否重复。 It will then call "LoadFace()" and "LoadSuit()."然后它将调用“LoadFace()”和“LoadSuit()”。

CheckDup() will receive the trial number and the deck of cards as input, and will return back a Boolean. CheckDup() 将接收试用号和一副牌作为输入,并将返回一个布尔值。

LoadFace() will go through the deck and put the appropriate value in "face" of the array by using just the "cardnum" the modulus operator to extract the value from the string "A23456789TJQK." LoadFace() 将遍历整个卡片组,并通过仅使用“cardnum”取模运算符从字符串“A23456789TJQK”中提取值,将适当的值放入数组的“face”中。

LoadSuit() will go through the deck and put the appropriate suit value in "suit" by using a method similar to "LoadFace()" where the suit string is "HDCS." LoadSuit() 将遍历牌组并使用类似于“LoadFace()”的方法将合适的西装值放入“suit”中,其中西装字符串是“HDCS”。

DealCards() will display the cards. DealCards() 将显示卡片。

**My question is, how can I check that there are no duplicate faces in each suit without a ton of if statements? **我的问题是,如何在没有大量 if 语句的情况下检查每套西装中没有重复的面孔? Please disregard the fact that I'm not sending the "cardnum" as a parameter to the functions LoadFace and LoadSuit, Also I'm using srand(1) for debugging purposes.请忽略我没有将“cardnum”作为参数发送给 LoadFace 和 LoadSuit 函数的事实,而且我正在使用 srand(1) 进行调试。 So far the output has unique card number and the correct amount of faces and suits (13, 4), but I'm not sure how I can insert a nonduplicate element from here.Any advice would be helpful.到目前为止,输出具有唯一的卡号以及正确数量的面孔和西装(13、4),但我不确定如何从此处插入非重复元素。任何建议都会有所帮助。 Also, a simple assign and shuffle of an array isn't allowed :( **此外,不允许对数组进行简单的分配和洗牌 :( **

EDIT checkDup and LoadDeck currently load the deck with a unique card number but not a unique card face and suit. EDIT checkDup 和 LoadDeck 当前加载具有唯一卡号但不是唯一卡面和花色的牌组。 It just counts the number of A faces 2 faces 3 faces King faces etc. It also makes sure that there are 13 cards in each suit.它只是计算 A 面 2 面 3 面国王面等的数量。它还确保每个花色中有 13 张牌。 I want to insert unique faces and suits into the stuct array so that I don't have say two 7 of spades.我想在 stuct 数组中插入独特的面孔和套装,这样我就不必说两个黑桃 7。

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


struct singlecard {
    int cardnum;
    char face;
    char suit;
};


int i, deckSize = 52;
char suits[] = {"HDCS"};
char faces[] = {"A23456789TJQK"};


void DealCards(struct singlecard Deck[]) {

    //SortCards();
    printf("\n\n");
    for (i = 0; i < deckSize; i++) {
        if ((i + 1) % 4 == 0) {
            printf("[%d %c-%c]  %d\n", Deck[i].cardnum, Deck[i].face, Deck[i].suit, i + 1);
        } else {
            printf("[%d %c-%c] ", Deck[i].cardnum, Deck[i].face, Deck[i].suit);
        }
    }

}

int CheckDupe(struct singlecard Deck[],int n) {

    int check = 0, j;
    for (j = 0; j < deckSize; j++) {
        if (n == Deck[j].cardnum || n == 0) {
           return check = 1;
        }

    }
    return check;

}

void LoadSuit(struct singlecard Deck[],int n) 
{  
        Deck[i].suit = suits[(n-1) % 4];
}

void LoadFace(struct singlecard Deck[],int n) {

        Deck[i].face = faces[(n-1) % 13];

} 
void LoadDeck(struct singlecard Deck[]){

    srand(time(NULL));

    for (i = 0; i < deckSize;) {
        int random_number = rand() % 53;
        if (CheckDupe(Deck,random_number) == 0) {

            Deck[i].cardnum = random_number;
            LoadFace(Deck,Deck[i].cardnum);
            LoadSuit(Deck,Deck[i].cardnum);
            i++;
        }
    }   
}

int main(){
    struct singlecard Deck[52];
    LoadDeck(Deck);
    DealCards(Deck);
    return 0;
}

Current Output电流输出

[5 5-H] [36 T-S] [6 6-D] [29 3-H]  4
[12 Q-S] [19 6-C] [25 Q-H] [13 K-H]  8
[42 3-D] [38 Q-D] [14 A-D] [22 9-D]  12
[16 3-S] [40 A-S] [51 Q-C] [35 9-C]  16
[24 J-S] [4 4-S] [20 7-S] [43 4-C]  20
[31 5-C] [9 9-H] [11 J-C] [48 9-S]  24
[49 T-H] [18 5-D] [41 2-H] [21 8-H]  28
[50 J-D] [52 K-S] [3 3-C] [27 A-C]  32
[39 K-C] [8 8-S] [33 7-H] [23 T-C]  36
[44 5-S] [17 4-H] [32 6-S] [45 6-H]  40
[30 4-D] [28 2-S] [2 2-D] [7 7-C]  44
[26 K-D] [34 8-D] [15 2-C] [47 8-C]  48
[10 T-D] [37 J-H] [1 A-H] [46 7-D]  52

The problems you are facing is because you've not read and followed your homework instructions properly, notably this bit:您面临的问题是因为您没有正确阅读和遵循家庭作业说明,尤其是这一点:

LoadFace() will go through the deck and put the appropriate value in "face" of the array by using just the "cardnum" the modulus operator to extract the value from the string "A23456789TJQK." LoadFace() 将遍历整个卡片组,并通过仅使用“cardnum”取模运算符从字符串“A23456789TJQK”中提取值,将适当的值放入数组的“face”中。

Imagine you've listed out each card in order like so with the card number.想象一下,您已经按照卡号的顺序列出了每张卡。

 1. Ace of Hearts
 2. Two of Hearts
 ....
 14. Ace of Diamonds
 15. Two of Diamonds

Do you notice a pattern?你注意到一个模式吗? Every 13th card has the same face.每 13 张牌都有相同的面孔。 The modulus operator (aka % ) can be used to find out which of those 13 faces a specific value of cardnum relates to like this.模数运算符(又名% )可用于找出这 13 个面中的哪一个与cardnum的特定值相关。 cardnum % 13 will always be between 0 and 12. Because your first card starts at 1, you need to subtract 1 first before doing getting modulus. cardnum % 13总是在 0 到 12 之间。因为你的第一张卡从 1 开始,所以你需要先减去 1,然后才能得到模数。 Your LoadFace function then becomes this.你的LoadFace函数就变成了这个。

void LoadFace() {
    for (i = 0; i < deckSize;) {
        Deck[i].face = faces[(Deck[i].cardnum-1) % 13];
    }
}

If you want to know if two cards have the same face based on their cardnum you can just compare Deck[a].cardnum % 13 and Deck[b].cardnum % 13 .如果你想根据它们的cardnum知道两张卡片是否有相同的面孔,你可以比较Deck[a].cardnum % 13Deck[b].cardnum % 13

So long as you don't put the same cardnum in twice, you know your deck will always contain unique cards.只要您不将相同的cardnum放入两次,您就知道您的套牌将始终包含独特的卡片。

CheckDup will receive the trial number and the deck of cards as input, and will return back a Boolean. CheckDup将接收试用号和一副牌作为输入,并将返回一个布尔值。

which would translate to the following declaration这将转化为以下声明

bool CheckDup( const int trial, const struct singlecard deck[], const unsigned int deckSize );

And should check the deck if the trial card do not exists yet ( probably true if a duplicate is found ), probably by iterating through the deck .如果trial卡尚不存在,则应检查deck (如果发现重复,则可能为true ),可能是通过迭代deck

This is what I understood from your question.这是我从你的问题中了解到的。

you want to find out, using checkDup() if that card has already been dealt您想checkDup() ,使用checkDup()是否已经处理该卡

you could maintain an array (or list whatever) of the card numbers that have been dealt.您可以维护已处理的卡号的数组(或列出任何内容)。 Then simply search through that array every time checkDup is called.然后在每次调用checkDup简单地搜索该数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM