我不太确定该如何解决。因此,我正在编程blackJack,并且具有这样的函数声明(如图所示),对于我的add_card_to_hand函数,我不知道该怎么做。我有2个论点,由于我将要更改其内部值和一张牌,因此已通过引用传递给我。我要添加到手上的卡片。那么,如何实际将卡片添加到手上呢?我也不知道类型,我只输入了hand_t,它可能是空的。
typedef enum { HEARTS, DIAMONDS, SPADES, CLUBS } suit_t;
typedef enum { ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING } value_t;
typedef struct {
suit_t suit;
value_t value;
} card_t;
typedef struct {
card_t* deck;
int num_decks;
int nextcard;
} shoe_t;
typedef struct {
card_t playercards[21];
int num_cards_in_array;
} hand_t;
hand_t add_card_to_hand(hand_t* hand, card_t card) {
}
最佳答案
#define MAX_HAND_CARDS 21
typedef struct {
card_t playercards[MAX_HAND_CARDS];
int num_cards_in_array;
} hand_t;
int add_card_to_hand(hand_t* hand, const card_t card) {
if (hand->num_cards_in_array >= MAX_HAND_CARDS) {
return -1;
}
hand->playercards[hand->num_cards_in_array] = card;
hand->num_cards_in_array++;
return 0;
}
int main(int argc, char *argv[]) {
hand_t *hand = calloc(1, sizeof(hand_t));
card_t *card = malloc(sizeof(card_t));
card->suit = HEARTS;
card->value = ACE;
if (add_card_to_hand(hand, *card)) {
printf("failed to add Card\n");
} else {
printf("card added\n");
}
return 0;
}
对黑杰克了解不多,但这应该可行。