2

I am trying to create a deck of cards in drawPile when newGame is called (with specific cards given as parameters)

I have 4 linked lists to store cards (drawPile,discardPile,player1's hand, and player2's hand)

My _game struct would store the state of my game at a given time (e.g. the cards in drawPile, discardPile, and player1,2's hands)

I am having trouble trying to understand how I should go about using drawPile linked list inside struct _game. How should I allocation memory for drawPile,discardPile..etc when _game is created? As I believe what I currently have in newGame is totally wrong.

Any advice or tips would be greatly appreciated.

typedef enum {RED,BLACK} color;
typedef enum {HEARTS,DIAMONDS,CLUBS,SPADES} suit;
typedef enum {ONE,TWO,THREE,FOUR} value;

typedef struct drawPile{
    enum color color;
    enum suit suit;
    enum value value;
    struct drawPile *next;
}*drawPile; 

I'm a bit confused what is the difference between putting * before drawPile?

typedef struct discardPile{
    enum color color;
    enum suit suit;
    enum value value;
    struct discardPile *next;
};

typedef struct player1Hand{
    enum color color;
    enum suit suit;
    enum value value;
    struct player1Hand *next;
};

typedef struct player2Hand{
    enum color color;
    enum suit suit;
    enum value value;
    struct player2Hand *next;
};

typedef struct _game{
    drawPile game_drawPile;
    discardPile game_discardPile;
    player1Hand game_player1Hand;
    player2Hand game_player2Hand;
}Game;

Game newGame(int deckSize, value values[], color colors[], suit suits[]){
    Game nGame;
    for(int i = 0; i < deckSize; i++){
        nGame->drawPile.value = value[i];
        nGame->drawPile.color = colors[i];
        nGame->drawPile.suit = suits[i];

    }

}

below is an example main function of how the newGame function is going to run with only 4 cards.

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);
 }

I have only pasted segments of my code, please tell me if you need more information.

Frankieeee
  • 71
  • 1
  • 9
  • `discardPile game_discardPile` There is no type defined as `discardPile`. You have a `struct discardPile` But not the type. – Rishikesh Raje Oct 22 '18 at 09:56
  • @RishikeshRaje but doesn't typedef define it as a type? I am new to C – Frankieeee Oct 22 '18 at 09:58
  • @franklinhe - No, you need to define the type. Refer https://stackoverflow.com/questions/17720223/c-typedef-struct-name-vs-typedef-struct-name or good C book for more information. – Rishikesh Raje Oct 22 '18 at 10:01
  • Also, it is a bad idea to define the type as `*drawPile`. See https://stackoverflow.com/questions/10768947/typedef-struct-pointer-definition. What you are doing is defining `drawpile` as a pointer to a struct, rather than a struct. This causes issues later on in the code. – Rishikesh Raje Oct 22 '18 at 10:03
  • @RishikeshRaje ahh that makes sense thank you – Frankieeee Oct 22 '18 at 10:04

1 Answers1

1

First of all, you can use the same Struct for all the linked lists.

struct Card {
    enum color color;
    enum suit suit;
    enum value value;
};

struct CardList {
    struct Card *card;
    struct CardList *next;
};

Then you can write functions to add/remove card to/from the list:

CardList *add(CardList **list, Card *card);

In newGame:

Game *newGame(int deckSize, value values[], color colors[], suit suits[]){
    Game *nGame = (Game*) malloc(sizeof(Game));
    for(int i = 0; i < deckSize; i++){
        Card* card = (Card*) malloc(sizeof(Card));
        ... assign variable
        add(&game->drawPile, card);
    }

    return nGame;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
Daniel Tran
  • 6,083
  • 12
  • 25
  • Potentially relevant: [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/1025391) – moooeeeep Oct 22 '18 at 10:05
  • For c you dont need – Daniel Tran Oct 22 '18 at 10:10
  • I got a bit confused because OP mixed -> things from c++ – Daniel Tran Oct 22 '18 at 10:12
  • is there a way to not have create a new add function? Because I'm asked to implement an abstract data type. How do I assign variables in this case? Do I allocation memory for struct card then directly assign values of card to nGame->drawPile->card.color(.value, .suit etc)? – Frankieeee Oct 22 '18 at 11:13