我正在用c编写纸牌游戏War。我的代码有一些困难。我得到以下错误:
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张牌&/;部分。
我对编码非常陌生,我试图遵循讲座中给出的代码,所以我不确定是什么导致了我的错误。任何帮助或反馈都是感激的。
整个代码如下:
#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("Clubsn"); break;
case 1: printf("Diamondsn"); break;
case 2: printf("Heartsn"); break;
case 3: printf("Spadesn"); 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 An");
showpile(Atop);
printf("nPLAYER Bn");
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)&;但这造成了一个未声明的"洗牌";我把它改成了shufflepile。
不清楚为什么要在这个练习中使用链表。您不需要从列表中添加或删除元素,使用malloc
分配52张卡更容易。
例如,如果你发的是这副牌组中的牌,那么你确实需要使用,比如说两个链表,将牌组中的牌添加到这些链表中,或者你想从主牌组中删除牌组。
要做到这一点,链表必须具有添加/删除函数。参考链表的例子来熟悉这个
注意,typedef
在某些情况下可能是有用的,例如与c++兼容,但您不必担心。如果必须使用typedef
,可以将其声明为
typedef struct node link;
然后在代码中使用声明link *ptr;
。否则坚持使用struct node *ptr;
,您必须将malloc
分配的所有内存都free
。这个例子展示了如何生成一副牌并洗牌。
#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 %sn", 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;
}