tty-solitaire/src/deck.c
2011-06-03 02:48:26 -03:00

46 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <errno.h>
#include "deck.h"
#include "common.h"
void allocate_deck(struct deck **deck) {
if (!(*deck = malloc(sizeof(**deck)))) {
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno);
}
allocate_stack(&((*deck)->stock));
allocate_stack(&((*deck)->waste_pile));
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
allocate_stack(&((*deck)->foundation[i]));
}
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
allocate_stack(&((*deck)->maneuvre[i]));
}
}
void initialize_deck(struct deck *deck) {
initialize_stack(deck->stock);
initialize_stack(deck->waste_pile);
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
initialize_stack(deck->foundation[i]);
}
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
initialize_stack(deck->maneuvre[i]);
}
}
void free_deck(struct deck *deck) {
free_stack(deck->stock);
free_stack(deck->waste_pile);
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
free_stack(deck->foundation[i]);
}
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
free_stack(deck->maneuvre[i]);
}
free(deck);
}