tty-solitaire/src/stack.c

128 lines
3.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2010-04-04 00:44:59 +00:00
#include <stdbool.h>
#include <malloc.h>
#include <errno.h>
2011-06-03 05:48:26 +00:00
2010-04-04 00:44:59 +00:00
#include "stack.h"
2011-06-03 05:48:26 +00:00
#include "card.h"
2011-05-08 23:38:36 +00:00
#include "common.h"
2010-04-04 00:44:59 +00:00
void allocate_stack(struct stack **stack) {
if (!(*stack = malloc(sizeof(**stack)))) {
2011-05-08 23:20:22 +00:00
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
exit(errno);
}
allocate_card(&((*stack)->card));
}
void initialize_stack(struct stack *stack) {
initialize_card(stack->card);
stack->next = NULL;
2010-04-04 00:44:59 +00:00
}
2011-06-03 05:48:26 +00:00
void free_stack(struct stack *stack) {
struct stack *tmp;
for (; stack; stack = tmp) {
tmp = stack->next;
free_card(stack->card);
free(stack);
}
}
2011-05-01 06:06:43 +00:00
struct stack *duplicate_stack(struct stack *stack) {
struct stack *iterator = stack;
struct stack *tmp_stack, *new_stack;
allocate_stack(&new_stack);
allocate_stack(&tmp_stack);
initialize_stack(new_stack);
initialize_stack(tmp_stack);
for (iterator = stack; iterator; iterator = iterator->next) {
push(&tmp_stack, duplicate_card(iterator->card));
}
while (!empty(tmp_stack)) {
push(&new_stack, (pop(&tmp_stack)));
2011-05-01 06:06:43 +00:00
}
free_stack(tmp_stack);
return(new_stack);
}
2010-04-04 00:44:59 +00:00
bool empty(struct stack *stack) {
return(stack->card->value == NO_VALUE &&
2011-05-31 05:45:11 +00:00
stack->card->suit == NO_SUIT &&
stack->card->face == NO_FACE &&
!stack->next);
2010-04-04 00:44:59 +00:00
}
int length(struct stack *stack) {
2011-05-09 03:04:03 +00:00
int length = 0;
2010-04-04 00:44:59 +00:00
2011-05-09 03:04:03 +00:00
if (!empty(stack)) {
for (length = 1; stack->next; stack = stack->next, length++)
;
2010-04-04 00:44:59 +00:00
}
return(length);
}
2010-04-04 06:20:56 +00:00
void push(struct stack **stack, struct card *card) {
if (card) {
if (empty(*stack)) {
(*stack)->card = card;
} else {
2011-05-31 05:45:11 +00:00
/* Allocating by hand because stack#allocate_stack would
* have allocated an unwanted card object. */
struct stack *new_stack = malloc(sizeof(*new_stack));
new_stack->card = card;
new_stack->next = (*stack);
*stack = new_stack;
}
2010-04-04 00:44:59 +00:00
}
}
2010-04-04 01:12:42 +00:00
struct card *pop(struct stack **stack) {
if(empty(*stack)) {
return(NULL);
} else {
struct card *popped_card = (*stack)->card;
if (length(*stack) == 1) {
/* Remembering the stack position before clearing it. */
int begin_y = (*stack)->card->frame->begin_y;
int begin_x = (*stack)->card->frame->begin_x;
allocate_card(&((*stack)->card));
/* An empty stack is a stack with a blank top card
* and with stack->next == NULL. */
set_card((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
(*stack)->next = NULL;
} else {
struct stack *tmp = *stack;
*stack = (*stack)->next;
free(tmp);
}
return(popped_card);
2010-04-04 01:12:42 +00:00
}
}
2011-05-01 01:13:41 +00:00
2011-05-01 04:13:15 +00:00
struct stack *reverse(struct stack *stack) {
2011-06-03 05:48:26 +00:00
struct stack *tmp_stack, *iterator;
2011-05-01 01:13:41 +00:00
2011-06-03 05:48:26 +00:00
allocate_stack(&tmp_stack);
initialize_stack(tmp_stack);
if (length(stack) > 1) {
for (iterator = stack; iterator; iterator = iterator->next) {
2011-06-03 05:48:26 +00:00
push(&tmp_stack, duplicate_card(iterator->card));
2011-05-01 01:13:41 +00:00
}
} else {
2011-06-03 05:48:26 +00:00
set_card(tmp_stack->card,
stack->card->value,
stack->card->suit,
stack->card->face,
stack->card->frame->begin_y,
stack->card->frame->begin_x);
2011-05-01 01:13:41 +00:00
}
2011-06-03 05:48:26 +00:00
return(tmp_stack);
2011-05-01 01:13:41 +00:00
}