程序卡在 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 张牌。我不知道它当前在做什么以及卡住了什么。
最佳答案
这个:
for (i = 0; i < 10; i += 1) {
current = *deck;
应该是:
current = *deck;
for (i = 0; i < 10; i++, current++) {
关于c - 我正在为纸牌游戏编写代码。在我的交易卡功能中,程序卡住了,我无法确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857841/