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 --- c3prj2_eval/deck.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 c3prj2_eval/deck.c (limited to 'c3prj2_eval/deck.c') diff --git a/c3prj2_eval/deck.c b/c3prj2_eval/deck.c new file mode 100644 index 0000000..c4a3d78 --- /dev/null +++ b/c3prj2_eval/deck.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include "deck.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); + } + +} -- cgit v1.2.3