gpt4 book ai didi

c - 去鱼卡转账

转载 作者:行者123 更新时间:2023-11-30 14:36:15 25 4
gpt4 key购买 nike

我必须用c语言创建一个钓鱼游戏。除了交换卡片之外,我的大部分大脑都在工作。

我已经重写了很多次,当我在调试器中逐步进行时它可以工作,但当我运行程序时它不起作用

#include <stdlib.h>
#include <stdio.h>

struct card
{
char suit;
char rank;
};

struct player {
struct hand* card_list;
char book[7];
size_t hand_size;
};

struct deck
{
struct card list[52];
int top_card;
};

struct hand
{
struct card top;
struct hand *next;
};

struct player user;
struct player computer;
struct deck deck_instance;

int add_card(struct player *target, struct card *new_card) {
struct hand *end = (struct hand *) malloc(sizeof(struct hand));
struct hand *tmp = target->card_list;
end->top.rank = new_card->rank;
end->top.suit = new_card->suit;
end->next = NULL;

if (target->card_list == NULL) {
target->card_list = end;
target->hand_size++;
return 0;
} else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
}
tmp->next = end;
target->hand_size++;
return 0;
}

int remove_card(struct player* target, struct card* old_card) {
struct hand *new_hand = malloc(sizeof(struct hand));
int new_count = 0;
struct hand *tmp = target->card_list;
struct hand *nextTmp = new_hand;
while (tmp != NULL) {
if (!(tmp->top.suit == old_card->suit && tmp->top.rank == old_card->rank)) {
nextTmp->top.rank = tmp->top.rank;
nextTmp->top.suit = tmp->top.suit;
nextTmp->next = malloc(sizeof(struct hand));
nextTmp = nextTmp->next;
new_count++;
}
tmp = tmp->next;
}
target->card_list = new_hand;
target->hand_size = new_count;
return 0;
}

struct card *next_card()
{
return &deck_instance.list[deck_instance.top_card++];
}

size_t deck_size()
{
return 52 - deck_instance.top_card;
}

int deal_player_cards(struct player *target)
{
if (deck_size() < 7)
{ // We don't have enough cards to deal
return 1;
}
for (int i = 0; i < 7; i++)
{
add_card(target, next_card());
}
return 0;
}

int shuffle()
{
const char *suits[4] = {"S", "H", "D", "C"};
const char *ranks[13] = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"};
int suit = 0;
int rank = 0;
for (int i = 0; i < 52; i++)
{
deck_instance.list[i].rank = *ranks[rank];
rank = (rank + 1) % 13;
deck_instance.list[i].suit = *suits[suit];
suit = (suit + 1) % 4;
}
for (int i = 0; i < 1000; i++)
{
int j = rand() % 52;
int k = rand() % 52;
struct card c = deck_instance.list[j];
deck_instance.list[j] = deck_instance.list[k];
deck_instance.list[k] = c;
}
return 0;
}

void printDeck(struct player user) {
for (int i = 0; i < user.hand_size; i++) {
printf("%c%c ", user.card_list[i].top.rank, user.card_list[i].top.suit);
}
printf("\n");
}

int transfer_cards(struct player* src, struct player* dest, char rank) {
int result = 0;
int hand_size = src->hand_size;
for (int i = 0; i < hand_size; i++) {
if (src->card_list[i].top.rank == rank) {
result++;
add_card(dest, &src->card_list[i].top);
remove_card(src, &src->card_list[i].top);

}
}
return result;
}

int main(int args, char* argv[])
{
printf("Shuffling deck....\n");
shuffle();
deal_player_cards(&user);
deal_player_cards(&computer);
printf("Players 1's Hand - ");
printDeck(user);
printf("Players 2's Hand - ");
printDeck(computer);
char rank = '5';
transfer_cards(&computer, &user, rank);
printf("Players 1's Hand - ");
printDeck(user);
printf("Players 2's Hand - ");
printDeck(computer);

}


我相信问题出在 add_card 上。其余代码只是为了让c程序运行

现在我得到这个输出

Shuffling deck....
Players 1's Hand - JC 5H 9H AH TD 3C AS
Players 2's Hand - 4D 5D TS QC 6H TC 5S
Players 1's Hand - JC 5H 9H AH TD 3C AS 4D 5D
Players 2's Hand - 4D TS QC 6H TC

正确的输出应该是

Shuffling deck....
Players 1's Hand - JC 5H 9H AH TD 3C AS
Players 2's Hand - 4D 5D TS QC 6H TC 5S
Players 1's Hand - JC 5H 9H AH TD 3C AS 5D 5S
Players 2's Hand - 4D TS QC 6H TC

最佳答案

你调用deal_player_cards() -> add_card(),然后检查target->card_list的值。此时card_list 和hand_size 尚未初始化。在 deal_player_cards() 中,您需要在调用 add_card() 之前初始化玩家结构。这会导致您遇到未定义的行为。

int deal_player_cards(struct player *target)
{
target->card_list = NULL;
target->hand_size = 0;

if (deck_size() < 7)
{ // We don't have enough cards to deal
return 1;
}
for (int i = 0; i < 7; i++)
{
add_card(target, next_card());
}
return 0;
}

还有其他问题。 printDeck() 假设卡片列表是一个数组。但是,add_card() 将其设置为链表。您不能假设所有条目在内存中都是连续的。打印它们时,您需要像向其中添加卡片一样遍历链接列表。

void printDeck(struct player user) {
struct hand *temp_hand;

temp_hand = user.card_list;
while (temp_hand != NULL)
{
printf("%c%c ", temp_hand->top.rank, temp_hand->top.suit);
temp_hand = temp_hand->next;
}
printf("\n");
}

关于c - 去鱼卡转账,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com