【问题标题】:I am writing code for a card game. In my deal cards function the program gets stuck and I can't determine why我正在为纸牌游戏编写代码。在我的交易卡功能中,程序卡住了,我无法确定原因
【发布时间】:2019-09-15 09:23:17
【问题描述】:

程序卡在deal函数中,调用后不打印main中的下一条语句。发牌功能应该在玩家之间交替,接下来使用链表向每个人发 5 张牌。每张卡片都有一张脸和一套西装。

typedef struct card_s {
    char suit[20];
    int face;
    struct card_s *next;
}card;

void Dealcards(card** deck,card** p1,card** p2,card** Tail,card** Tail2 ) {

    int i;

    card* temp = NULL;
    card* prev = NULL;
    card* current = NULL;
    card* temp2 = NULL;
    card* prev2 = NULL;

    for (i = 0; i < 10; i += 1) {   

        current = *deck;

        if (i % 2 == 0) {   //alternate who card is deal to

            temp = (card*)malloc(sizeof(card));
            temp->face = current->face;     //give p1 card value
            strcpy(temp->face, current->face);      //give p1 card suit
            temp->next = NULL;

            if (prev == NULL){

                *p1 = temp;

            }
            else {

                prev->next = temp;      //next card
            }
        prev = temp;
        prev->next = NULL;

        }
        else {  //cpu dealt hand

            temp2 = (card*)malloc(sizeof(card));
            temp->face = current->face;     //give p2 card value
            strcpy(temp2->face, current->face);     //give p2 card suit
            temp2->next = NULL;

            if (prev2 == NULL) {

                *p2 = temp2;

            }
            else {
                prev2->next = temp2;    //next card
            }
            prev2 = temp2;
            prev->next == NULL;
        }
    }
    *Tail = prev;
    *Tail2 = prev2;
    return;
}

int main() {

    char YorN = 'Y';

    int playerCoins=100;
    int p1handlength;

    card *cards = NULL;
    card *player1 = NULL;
    card *player2 = NULL;
    card *Tail1 = NULL;
    card *Tail2 = NULL;

    while (YorN == 'Y') {

        printf("********Now playing Jacks Or Better********\n");

        makeDeck(&cards);   //create deck

        int decklength = findLength(cards); //gets length of deck used in shuffle function

        //shuffleDeck(&cards,decklength);   //shuffle deck

        printf("Dealing cards...\n\n");
        Dealcards(&cards, &player1, &player2, &Tail1, &Tail2);  //deal cards to cpu and player 2 (cpu)

        printf("P1 firstcard:\n");
        printf("Player 1's cards:\n");
        p1handlength = findLength(player1);
        PrintHand(player1,p1handlength);


    }

    return 0;
}

该函数应该向 p1 和 p2 的每手牌发 5 张牌。我无法弄清楚它当前在做什么以及卡住了什么。

【问题讨论】:

  • strcpy(temp-&gt;face, current-&gt;face); //give p1 card suit 中应该是strcpy(temp-&gt;suit, current-&gt;suit);,因为temp-&gt;faceint 并且无论如何你只在上面的那一行写信给它。 ?请注意编译器警告。
  • 好吧,我不知道我怎么没听懂,但它帮了很多忙,现在代码通过了 deal 函数并打印了玩家的手。然而,玩家的手牌都是同一张牌,所以我认为这个功能不会增加电流。我该怎么做?

标签: c linked-list


【解决方案1】:

这个:

for (i = 0; i < 10; i += 1) {   

    current = *deck;

应该是:

current = *deck;
for (i = 0; i < 10; i++, current++) {   

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 2021-07-24
    • 2020-07-16
    • 2014-09-17
    • 2013-03-01
    • 2023-03-30
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多