C 钓鱼卡转帐
我必须用c语言创建一个围棋游戏。除了交换卡片,我的大脑大部分都在工作 我已经重写了很多次了,当我在调试器中一步一步地运行它时,它是有效的,但当我运行程序时,它就不起作用了C 钓鱼卡转帐,c,linked-list,C,Linked List,我必须用c语言创建一个围棋游戏。除了交换卡片,我的大脑大部分都在工作 我已经重写了很多次了,当我在调试器中一步一步地运行它时,它是有效的,但当我运行程序时,它就不起作用了 #include <stdlib.h> #include <stdio.h> struct card { char suit; char rank; }; struct player { struct hand* card_list; char book[7];
#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);
}
正确的输出应该是
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\u player\u cards()->add\u card(),然后检查target->card\u list的值。卡片列表和手牌大小未在此点初始化。在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;
}
这可能是代码中未定义行为的症状。当我运行它(MSVC)时,它会打印除了每手牌中的第一张牌以外的所有牌的垃圾:
玩家1的手-9CEI218Y
和玩家2的手-9HÚR6DÚR5CÚRKS
和玩家1的手-9CEI218Y m
和玩家2的手-9H-[6D]]KS––
我使用gcc,但看不出UB就是这样使用的。使用哪种编译器,或者使用什么调试或优化选项,都不会有任何区别。好吧,我该如何处理UB?好吧,简短而没有帮助的答案是“编写定义良好的代码”。更有用的花絮包括“定义时初始化所有内容”、“清楚代码中堆分配块的概念“所有权”,以及所有者离开时的free
”、“习惯性地检查返回代码”、“如果无法以其他方式证明它们是好的,则在使用它们之前使用空测试指针”,等等该列表应包括“自动存储中优先分配给alloc
family functions”,除非您无法避免使用head进行此分配。不幸的是,处理的手中仍然有垃圾-每次运行都有不同的垃圾。你试过了吗?编辑建议不要。不,我没有。我刚刚向UB致词。printDeck()至少还有一个其他问题。我把它添加到我的答案中。
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;
}
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");
}