tty-solitaire/src/deck.c

49 lines
1.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include "deck.h"
2011-05-08 23:20:22 +00:00
#include "common.h"
void allocate_deck(struct deck **deck) {
if (!(*deck = malloc(sizeof(**deck)))) {
2011-05-08 23:20:22 +00:00
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno);
}
allocate_stack(&((*deck)->stock));
2010-04-09 03:12:06 +00:00
allocate_stack(&((*deck)->waste_pile));
for (int i = 0; i < 4; i++) {
allocate_stack(&((*deck)->foundation[i]));
}
for (int i = 0; i < 7; i++) {
allocate_stack(&((*deck)->maneuvre[i]));
}
}
void initialize_deck(struct deck *deck) {
initialize_stack(deck->stock);
2010-04-09 03:12:06 +00:00
initialize_stack(deck->waste_pile);
for (int i = 0; i < 4; i++) {
initialize_stack(deck->foundation[i]);
}
for (int i = 0; i < 7; i++) {
initialize_stack(deck->maneuvre[i]);
}
}
2010-04-07 00:59:21 +00:00
2011-02-14 02:10:47 +00:00
void free_deck(struct deck *deck) {
2011-02-12 03:26:03 +00:00
if (deck) {
free_stack(deck->stock);
2011-02-14 02:10:47 +00:00
free_stack(deck->waste_pile);
for (int i = 0; i < 4; i++) {
free_stack(deck->foundation[i]);
}
for (int i = 0; i < 7; i++) {
free_stack(deck->maneuvre[i]);
}
2011-02-12 03:26:03 +00:00
}
2010-04-07 00:59:21 +00:00
free(deck);
}