我不确定如何解决问题。所以我正在编程blackJack,我有我的函数声明(如图所示),对于我的add_card_to_hand函数,我不知道该怎么做。我有两个参数,一个我通过参考传递的手,因为我要改变它的内部值和一张卡;我正在添加到手中的卡片。那么我该如何将卡片添加到手中呢?我也不知道类型,我只是把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) {
}
答案 0 :(得分:0)
#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;
}
对黑杰克不太了解,但这应该有用。