C-链表不兼容的指针类型

时间:2018-10-26 07:53:44

标签: c

我有3个相同类型的链表。我试图在调用newGame()时将纸牌(具有值,颜色和花色)添加到这2个链接列表中,或在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);
 }

2 个答案:

答案 0 :(得分:5)

  1. 您在标识符中使用_。避免那样。虽然不是绝对禁止的,但这不是一个好习惯

  2. insertCardAtFront将接受类型为struct deck的输入,但是您输入的类型为player2Hand

    void insertCardAtFront(struct _deck *deck,card card)
    ....
    insertCardAtFront(nGame->game_player1Hand,newCard(values[i],colors[i],suits[i]));
    

您可以使用单一类型的deck,而您的game可以包含该类型的两个变量。

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;
  1. 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){

答案 1 :(得分:1)

我认为主要的问题已经解决了。

我只想为这种问题提出一个递归解决方案:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define N_CARDS 52

enum suits {
    HEARTS,
    DIAMONDS,
    CLUBS,
    SPADES
};

typedef struct _card {
    char suit; //enum of HEARTS,DIAMONS etc..
    char value; //enum of ONE,TWO,THREE etc.
}card_t;

typedef struct _deck{ //linked list of deck of cards
    card_t *card;
    struct _deck *next;
}deck_t;

typedef struct _playerHand{ //linked list of player's hand 
    card_t *card;
    struct _playerHand *nextCard;
}playerHand_t;

typedef struct _game{ //game struct to store the state of my game       
    playerHand_t *game_player1Hand;
    playerHand_t *game_player2Hand;
}game_t;

const char *suitsStr[4];

void printDeck(deck_t *deck);
void createDeck(deck_t *deck, char suit, char value);
void newGame(playerHand_t *player1, playerHand_t *player2);
void dealCards(playerHand_t *player1, playerHand_t *player2, deck_t *deck, int turn);
void printPlayerCards(playerHand_t* player);

int main() {
    suitsStr[0] = "HEARTS";
    suitsStr[1] = "DIAMONDS";
    suitsStr[2] = "CLUBS";
    suitsStr[3] = "SPADES";

    game_t game;
    game.game_player1Hand = NULL;
    game.game_player2Hand = NULL;
    newGame(game.game_player1Hand, game.game_player2Hand);
    //need to free the allocated memory!
    return 0;
}

void newGame(playerHand_t *player1, playerHand_t *player2) {
    deck_t *deck = NULL;
    deck = malloc(sizeof(deck_t));
    deck->card = NULL;
    createDeck(deck, HEARTS, 1);
    printf("The deck to deal is:\n\r");
    printDeck(deck);
    printf("\n\r");
    player1 = malloc(sizeof(playerHand_t));
    player2 = malloc(sizeof(playerHand_t));
    player1->card = NULL;
    player1->nextCard = NULL;
    player2->card = NULL;
    player2->nextCard = NULL;
    dealCards(player1, player2, deck, 0);
    printf("Player 1 cards:\n\r");
    printPlayerCards(player1);
    printf("\n\r");
    printf("Player 2 cards:\n\r");
    printPlayerCards(player2);
}

void printPlayerCards(playerHand_t* player) {
    printf("%d %s\n", player->card->value, suitsStr[player->card->suit]);
    if (player->nextCard != NULL) {
        printPlayerCards(player->nextCard);
    }
    return;
}

void printDeck(deck_t *deck) {
    printf("%d %s\n", deck->card->value, suitsStr[deck->card->suit]);
    if (deck->next != NULL) {
        printDeck(deck->next);
    }
    return;
}

void createDeck(deck_t *deck, char suit, char value) {
    //need to implement here the algorithm to fill the deck (now just fill in order)
    card_t card;
    card.suit = suit;
    card.value = value;
    deck->card = malloc(sizeof(card_t));
    memcpy(deck->card, &card, sizeof(card_t));
    deck->next = NULL;
    if (value == 13) {
        if (suit == SPADES) {
            return;
        } else {
            deck->next = malloc(sizeof(deck_t));
            createDeck(deck->next, ++suit, 1);
        }
    } else {
        deck->next = malloc(sizeof(deck_t));
        createDeck(deck->next, suit, ++value);
    }
}

void dealCards(playerHand_t *player1, playerHand_t *player2, deck_t *deck, int turn) {
    if (turn % 2 == 0) {
        player1->card = malloc(sizeof(card_t));
        memcpy(player1->card, deck->card, sizeof(card_t));
        if (++turn < N_CARDS) {
            if (turn != N_CARDS - 1) {
                player1->nextCard = malloc(sizeof(playerHand_t));
                player1->nextCard->card = NULL;
                player1->nextCard->nextCard = NULL;
                dealCards(player1->nextCard, player2, deck->next, turn);
            } else {
                dealCards(player1, player2, deck->next, turn);
            }
        }
    } else {
        player2->card = malloc(sizeof(card_t));
        memcpy(player2->card, deck->card, sizeof(card_t));
        if (++turn < N_CARDS) {
            if (turn != N_CARDS - 1) {
                player2->nextCard = malloc(sizeof(playerHand_t));
                player2->nextCard->card = NULL;
                player2->nextCard->nextCard = NULL;
                dealCards(player1, player2->nextCard, deck->next, turn);
            } else {
                dealCards(player1, player2, deck->next, turn);
            }
        }
    }

    return;
}

请注意,已经使用了极其简单的算法来生成套牌,因此,牌是按值(从1到13,即国王)和套装(红心,钻石,棍棒和黑桃)的顺序生成的。因此,需要一种改组算法,因此您可以像这样生成卡片组,然后找到一种适用于卡片组的改组算法。

这里的内存也已分配但未释放!当前程序遭受内存泄漏,我在其中应该释放内存的地方留下了一个注释,但没有这样做。