From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- c3prj1_deck/deck.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 c3prj1_deck/deck.c (limited to 'c3prj1_deck/deck.c') 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 +#include +#include + +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) { + +} -- cgit v1.2.3