结构帮助。将卡结构分配给卡组结构中保存的卡阵列

时间:2014-04-11 05:53:46

标签: c arrays struct playing-cards

这是我的两个结构:

typedef struct _card {
    int suit;
    int value;
} card;

typedef struct _deck {
    int num_cards;
    card *cards;
} deck;

这是我的卡片功能:

card *make_card(int suit, int value)
{
  card *newCard = malloc(sizeof(card));
  newCard->suit = suit;
  newCard->value = value;

  return newCard;
}

现在是我陷入困境的地方。我必须制作一副纸牌。我知道如何将每个值分配给卡,但我很难理解如何在内存中分配它。我知道它与甲板结构中的一系列卡有关,但我无法弄清楚如何。

deck *make_standard_deck()
{
  for (int i = 0; i <= 3; i++)
  {
    for (int j = 1; j <= 13; j++)
    {
      deck.cards[i] = make_card(int suit, int value);
    }
  }
}

4 个答案:

答案 0 :(得分:1)

您应先分配整个卡片组,然后再分配各张卡片。 Alt键。你也可以使用realloc使数组越来越长,但这里似乎没必要。

首先更改make_card(也许重命名)

card *init_card(card* newCard, int suit, int value)
{
  newCard->suit = suit;
  newCard->value = value;
  return newCard;
}

然后更改make_standard_deck

deck *make_standard_deck()
{
  const int numberOfValues = 13; // use constants when you can
  const int numberOfSuits = 4;

  deck* d = malloc(sizeof(deck)); // normally check return value
  d->num_cards = numberOfValues*numberOfSuits;
  d->cards = malloc(sizeof(card) * (d->num_cards));

  for (int i = 0; i < numberOfSuits; i++)
  {
    for (int j = 0; j < numberOfValues; j++)
    {
      // pass the card to initialized to the function
      // same as &(d->cards[i])
      init_card(d->cards + i, i + 1, j + 1);
    }
  }
  return d;
}

当你释放它时

void freeDeck( deck *d )
{
  if ( d != NULL )
  {
    free( d->cards );
  }
  free( d );
}

答案 1 :(得分:1)

要创建一个卡组,首先为卡组结构分配内存,然后(假设您需要一个指向卡的指针数组)为指针数组分配内存。这是一个创建52张牌的标准套牌的例子。

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

typedef struct _card {
    int suit;
    int value;
} card;

typedef struct _deck {
    int num_cards;
    card **cards;
} deck;

card *make_card(int suit, int value)
{
    card *newCard = malloc(sizeof(card));
    newCard->suit = suit;
    newCard->value = value;

    return newCard;
}

deck *make_standard_deck( void )
{
    deck *newDeck = malloc( sizeof(deck) );

    newDeck->num_cards = 52;
    newDeck->cards = malloc( 52 * sizeof(card *) );

    int index = 0;
    for ( int suit = 0; suit < 4; suit++ )
        for ( int value = 1; value <= 13; value++ )
            newDeck->cards[index++] = make_card( suit, value );

    return newDeck;
}

int main( void )
{
    int i;

    deck *stdDeck = make_standard_deck();

    for ( i = 0; i < stdDeck->num_cards; i++ )
        printf( "suit=%d value=%2d\n", stdDeck->cards[i]->suit, stdDeck->cards[i]->value );

    /* free the deck when we're done with it */
    for ( i = 0; i < stdDeck->num_cards; i++ )
        free( stdDeck->cards[i] );
    free( stdDeck->cards );
    free( stdDeck );
}

答案 2 :(得分:0)

如果您事先知道牌组中的牌数(例如52),那么您可以这样做:

deck deck_cards[4][13];

for (int i = 0; i < 4; i++)
    {
            for (int j = 0; j < 13; j++)
            {
               deck_cards[i][j].cards = make_card(suit, value);
            }
     }

答案 3 :(得分:0)

我做这样的事情

struct Card
{
    int suit;
    int value;
};

struct Deck
{
    int num_cards;
    Card *cards;
};

//instead of returning a pointer simply return a Card struct
Card make_card(int suit, int value)
{
    Card newCard;
    newCard.suit = suit;
    newCard.value = value;

    return newCard;
}

Deck *deck = (Deck*) malloc(sizeof(Deck));
//So here's the part you're struggling with? Simply malloc num_cards of Card structs.
deck->cards = (Card*) malloc(sizeof(Card) * deck->num_cards);

//Now iterate through each Card struct in the deck
for(int i etc......)
    deck->cards[i] = make_card(suit, value); //make_card returns Card struct

因此,牌组根据牌组num_cards值保存指向一系列牌的指针。当然,请确保事先设置num_cards,与此示例不同。