diff options
author | Haidong Ji | 2022-04-15 15:51:30 -0500 |
---|---|---|
committer | Haidong Ji | 2022-04-15 15:51:30 -0500 |
commit | 442a49ad5a48d417345959b903ae6a6d32d55759 (patch) | |
tree | c7127bb497e5e439018b1915e0136eec2c9cb124 /c3prj1_deck/deck.c |
Excellent fundamentals and displine training, many tools and techniques
exercises: gdb, emacs, valgrind, git
Diffstat (limited to 'c3prj1_deck/deck.c')
-rw-r--r-- | c3prj1_deck/deck.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/c3prj1_deck/deck.c b/c3prj1_deck/deck.c new file mode 100644 index 0000000..2757a3c --- /dev/null +++ b/c3prj1_deck/deck.c @@ -0,0 +1,75 @@ +#include "deck.h" +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +void print_hand(deck_t *hand) { + for (size_t i = 0; i < hand->n_cards; i++) { + print_card(*hand->cards[i]); + printf(" "); + } +} + +int deck_contains(deck_t *d, card_t c) { + for (size_t i = 0; i < d->n_cards; i++) { + if (d->cards[i]->value == c.value && d->cards[i]->suit == c.suit) { + return 1; + } + } + return 0; +} + +void shuffle(deck_t *d) { + card_t c; + for (size_t i = d->n_cards; i > 0; i--) { + int r = rand() % i; + c = *d->cards[i - 1]; + *d->cards[i - 1] = *d->cards[r]; + // d[i-1] = d[r]; + *d->cards[r] = c; + } +} + +void assert_full_deck(deck_t *d) { + card_t temp; + for (int i = 0; i <= 51; i++) { + temp = card_from_num(i); + assert(deck_contains(d, temp) == 1); + } +} + +void add_card_to(deck_t *deck, card_t c) { + deck->n_cards++; + deck->cards = realloc(deck->cards, deck->n_cards * sizeof(*card_t)); + deck->cards[deck->n_cards - 1] = &c; +} + +card_t *add_empty_card(deck_t *deck) { + card_t *c = malloc(sizeof(card_t)); + c->value = 0; + c->suit = 0; + add_card_to(deck, *c); + return c; +} + +deck_t *make_deck_exclude(deck_t *excluded_cards) { + deck_t *deck = malloc(sizeof(deck_t)); + deck->n_cards = 0; + card_t *c = malloc(sizeof(card_t)); + for (int i = 0; i < 52; i++) { + *c = card_from_num(i); + if (deck_contains(excluded_cards, c) == 1) { + continue; + } else { + deck->n_cards++; + deck->cards = realloc(deck->cards, deck->n_cards * sizeof(*card_t)); + deck->cards[deck->n_cards - 1] = c; + } + } + free(c); + return deck; +} + +deck_t * build_remaining_deck(deck_t ** hands, size_t n_hands) { + +} |