-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.c
64 lines (58 loc) · 1.79 KB
/
deck.c
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
#include <deck.h>
Deck deck_create(Position size, int max) {
Deck deck = {.length = size.row, .width = size.col};
deck.stack = (Stack***) malloc(sizeof(Stack**)*deck.length);
if (!deck.stack) {
GENERROR(MALLOC_FAILED, "Could not allocate memory for the deck");
exit(MALLOC_FAILED);
}
for (int i = 0; i < size.row; ++i) {
deck.stack[i] = (Stack**) malloc(sizeof(Stack*)*deck.width);
if (!deck.stack[i]) {
GENERROR(MALLOC_FAILED, "Could not allocate memory for the deck[%d]", i);
exit(MALLOC_FAILED);
}
for (int j = 0; j < size.col; ++j) {
deck.stack[i][j] = stack_Init(max);
}
}
return deck;
}
Stack* deck_get_At(Deck deck, Position pos) {
return deck.stack[pos.row][pos.col];
}
DeckStackStatus deck_set_At(Deck deck, Position pos, Stack* stack) {
Stack* tmp = deck_get_At(deck, pos);
deck.stack[pos.row][pos.col] = stack;
if (deck.stack[pos.row][pos.col] == tmp) {
return STACK_SAME_SET;
}
if (!deck.stack[pos.row][pos.col]) {
return STACK_EMPTY;
}
return STACK_ALREADY_SET;
}
void print_Deck(Deck deck) {
printf("Deck size: %hhu x %hhu\n", deck.length, deck.width);
printf("Deck stack:\n");
for (int i = 0; i < deck.length; ++i) {
for (int j = 0; j < deck.width; ++j) {
if (deck.stack[i][j]) {
printf("%p ", (void*) deck.stack[i][j]);
} else {
printf("0x0 ");
}
}
printf("\n");
}
}
void free_Deck(Deck *deck) {
for (int i = 0; i < deck->length; ++i) {
for (int j = 0; j < deck->width; ++j) {
if (deck->stack[i][j]) {
free_Stack(deck->stack[i][j]);
}
}
free(deck->stack[i]);
}
}