summaryrefslogtreecommitdiff
path: root/c3prj1_deck/deck.c
blob: 2757a3c6a07a491272eb93dcafe069777536a30f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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) {

}