首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C语言编写纸牌游戏“战争”

用C语言编写纸牌游戏“战争”
EN

Stack Overflow用户
提问于 2021-10-30 17:28:55
回答 1查看 93关注 0票数 0

我正在用C语言编写纸牌游戏“战争”,我的代码有一些困难。我收到以下错误:

代码语言:javascript
复制
main.c:167:17: warning: passing argument 1 of ‘shufflepile’ from incompatible pointer type [-Wincompatible-pointer-types]
  167 |     shufflepile(a, n); //shuffle array elements
      |                 ^
      |                 |
      |                 struct node **
main.c:159:23: note: expected ‘link’ {aka ‘struct node *’} but argument is of type ‘struct node **’
  159 | link shufflepile(link pile) {
      |                  ~~~~~^~~~
main.c:167:5: error: too many arguments to function ‘shufflepile’
  167 |     shufflepile(a, n); //shuffle array elements
      |     ^~~~~~~~~~~
main.c:159:6: note: declared here
  159 | link shufflepile(link pile) {
      |      ^~~~~~~~~~~

代码已经过测试,并在"//创建52卡片组“一节中正常工作。

我对编程非常陌生,并且正在尝试遵循讲座中给出的代码,所以我不确定是什么导致了我的错误。任何帮助或反馈都是非常感谢的。

整个代码如下:

代码语言:javascript
复制
#include <stdio.h>


// Initiate deck of cards, normal 52 card deck (ace is high)

#define DECKSIZE 52

typedef int Card;

int rank(Card c) {
    return c % 13;
}

// allow for multi-deck war
int suit(Card c) {
    return (c % 52) / 13;
}

// representing the cards

void showcard(Card c) {
    switch (rank(c)) {
        case 0: printf("Deuce of "); break;
        case 1: printf("Three of "); break;
        case 2: printf("Four of "); break;
        case 3: printf("Five of "); break;
        case 4: printf("Six of "); break;
        case 5: printf("Seven of "); break;
        case 6: printf("Eight of "); break;
        case 7: printf("Nine of "); break;
        case 8: printf("Ten of "); break;
        case 9: printf("Jack of "); break;
        case 10: printf("Queen of "); break;
        case 11: printf("King of "); break;
        case 12: printf("Ace of "); break;
    }

    switch (suit(c)) {
    case 0: printf("Clubs\n"); break;
    case 1: printf("Diamonds\n"); break;
    case 2: printf("Hearts\n"); break;
    case 3: printf("Spades\n"); break;
    }
}
// testing the code


// representing the deck and hands (with linked lists because need direct access to top and bottom cards, draw cards from top, won cards go to bottom)

typedef struct node* link;
struct node {
    Card card;
    link next;
};

link Atop, Abot;
link Btop, Bbot;

// showing a hand

void showpile(link pile) {
    struct node *p;
    for (p = pile; p != NULL; p = p->next)
        showcard(p->card);
}

int countpile(link pile) {
    struct node *p;
    int cnt = 0;
    for (p = pile; p != NULL; p = p->next)
        cnt++;
    return cnt;
}

// Creating the 52 card Deck

#include <stdlib.h> //for malloc()

link NEWnode(Card card, link next) {
    struct node *p;
    p = malloc(sizeof *p); //allocate memory
    if (p == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    p->next = next;
    p->card = card;
    return p;
}

link makepile(int N) {
    struct node *p = NULL;
    Card c;

    for (c = N - 1; c >= 0; c--)
        p = NEWnode(c, p);

    return p;
}

// dealing the cards

link Atop, Abot, Btop, Bbot;

 void deal(link d) {
     Atop = d; Abot = d; d = d->next;
     Btop = d; Bbot = d; d = d->next;
     while (d != NULL) {
         Abot->next = d; Abot = d; d = d->next;
         Bbot->next = d; Bbot = d; d = d->next;
     }
     Abot->next = NULL; Bbot->next = NULL;
 }

 int main(void) {
     link deck;
    deck = makepile(DECKSIZE);
    deal(deck);
    printf("PLAYER A\n");
    showpile(Atop);
    printf("\nPLAYER B\n");
    showpile(Btop);
    return 0;
}

// shuffling the deck

link shufflepile(link pile) {
    int i, n;
    struct node *p;
    link a[DECKSIZE];

    for (p = pile, n = 0; p != NULL; p = p->next, n++)
        a[n] = p;

    shufflepile(a, n); //shuffle array elements

    for (i = 0; i < n - 1; i++)
        a[i]->next = a[i + 1];
    a[n-1]->next = NULL;

    return a[0];
}

该讲座使用了"shuffle(a,n)“而不是" shufflepile (a,n)”,但这产生了一个未声明的"shuffle“错误,因此我将其更改为shuffle。

EN

回答 1

Stack Overflow用户

发布于 2021-10-31 01:53:19

尚不清楚您为什么要在此练习中使用链表。您不是在列表中添加或删除元素,使用malloc分配52张卡更容易。

例如,如果您正在处理这副牌中的牌,那么您确实需要使用,比方说两个链表,将这副牌中的牌添加到这些链表中,或者您想要从主牌组中删除牌。

为此,链表必须具有添加/删除功能。请参阅链接列表的示例来熟悉这一点。

注意,typedef在某些情况下可能很有用,例如与c++兼容,但您不必担心这一点。如果必须使用typedef,可以将其声明为

代码语言:javascript
复制
typedef struct node link;

然后在代码中使用声明link *ptr;。否则,坚持使用struct node *ptr;,您必须对malloc分配的所有内存执行free操作。这个例子展示了如何生成一副牌并对其进行洗牌。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void print(int* deck, int count)
{
    const char* faces[] = { "Club", "Diamond", "Heart", "Spade" };
    for (int i = 0; i < count; i++)
        printf("%02d of %s\n", 1 + deck[i] % 13, faces[deck[i] / 13]);
}

void shuffle(int* deck, int count)
{
    for (int i = 0; i < count; i++)
    {
        //swap two cards
        int m = rand() % count;
        int n = rand() % count;
        int temp = deck[m];
        deck[m] = deck[n];
        deck[n] = temp;
    }
}

int main()
{
    srand((unsigned)time(NULL));
    int count = 52; //52 cards
    int* deck = malloc(count * sizeof(int));
    if (!deck) { perror("malloc"); return 0; }
    for (int i = 0; i < count; i++)
        deck[i] = i;
    print(deck, count); 
    shuffle(deck, count);
    print(deck, count);
    free(deck); //release memory
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69780539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档