我有 3 个相同类型的链表。我试图在调用 newGame() 时将牌(具有值(value)、颜色和花色)添加到这两个链接列表中,或者在 2 个玩家之间分配牌。 我试图创建一个函数来为我做到这一点。但是,我不确定调用 insertCardAtFront() 时如何处理不兼容的指针类型警告。 有时我对指针感到非常困惑,我想将游戏的状态(玩家1,2的手牌)存储在我的游戏结构中。
任何帮助将不胜感激。
typedef struct _card {
color color; //enum of RED, BLACK
suit suit; //enum of HEARTS,DIAMONS etc..
value value; //enum of ONE,TWO,THREE etc.
}card;
typedef struct _deck{ //linked list of deck of cards
card *cards;
struct _deck *next;
}deck;
typedef struct _player1Hand{ //linked list of player1's hand
card *card;
struct _player1Hand *next;
}player1Hand;
typedef struct _player2Hand{//linked list of player2's hand
card *card;
struct _player2Hand *next;
}player2Hand;
typedef struct _game{ //game struct to store the state of my game
player1Hand *game_player1Hand;
player2Hand *game_player2Hand;
}*Game;
void insertCardAtFront(struct _deck *deck,card card){
struct _deck *newDeck = malloc(sizeof(struct _deck));
newDeck->cards = malloc(sizeof(card));
newDeck->cards->color = card.color;
newDeck->cards->suit = card.suit;
newDeck->cards->value = card.value;
newDeck->next = deck;
deck = newDeck;
}
Game newGame(int deckSize, value values[], color colors[], suit suits[]){
Game nGame = malloc(sizeof(struct _game));
nGame->game_player1Hand = NULL;
nGame->game_player2Hand = NULL;
for(int i = 0;i < deckSize; i++){
insertCardAtFront(nGame->game_player1Hand,newCard(values[i],colors[i],suits[i]));
i++;
insertCardAtFront(nGame->game_player2Hand,newCard(values[i],colors[i],suits[i]));
}
return nGame;
}
Card newCard(value value, color color, suit suit) {
Card new = calloc(1, sizeof(card));
if (new == NULL) {
err(EXIT_FAILURE, "Could not allocate memory for Card");
}
new->value = value;
new->suit = suit;
new->color = color;
return new;
}
主要功能示例:
int main (void){
init_deck();
}
static void init_deck(void){
int deck_size = 4;
value values[] = {ONE, TWO, THREE, FOUR};
color colors[] = {RED, BLACK, RED, BLACK};
suit suits[] = {HEARTS, DIAMONDS, CLUBS, SPADES};
Game game = newGame(deck_size, values, colors, suits);
}
最佳答案
您在标识符中使用了
_
。避免这种情况。虽然没有绝对禁止,但这不是好的做法
类型insertCardAtFront
将接受struct Deck
类型的输入,但您给出的输入为player2Hand
void insertCardAtFront(struct _deck *deck,card card) .... insertCardAtFront(nGame->game_player1Hand,newCard(values[i],colors[i],suits[i]));
您可以拥有单一类型的牌组
,并且您的游戏
可以包含该类型的两个变量。
typedef struct _deck{ //linked list of deck of cards
card *cards;
struct _deck *next;
}deck;
....
typedef struct _game{ //game struct to store the state of my game
deck *game_player1Hand;
deck *game_player2Hand;
}*Game;
- 在
newcard
中,您使用的是calloc
,但将new
定义为card
类型的值。您需要一个指向card
的指针来存储calloc 的返回值。
此外,在当前代码中您将返回一个局部变量。
newcard
的修复如下。
card* newCard(value value, color color, suit suit) {
card *new;
new = calloc(1, sizeof(card));
if (new == NULL) {
err(EXIT_FAILURE, "Could not allocate memory for Card");
}
new->value = value;
new->suit = suit;
new->color = color;
return new;
}
您需要修改insertCardAtFront
函数以获取指向deck
的指针和指向card
的指针的参数
void insertCardAtFront(struct _deck *deck,card *card){
关于C - 链表不兼容的指针类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003998/