tty-solitaire/lib/stack.c

116 lines
2.6 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 <string.h>
#include <errno.h>
2010-04-04 00:44:59 +00:00
#include "stack.h"
2011-02-06 06:45:53 +00:00
static bool maneuvre_stack(struct stack *stack) {
return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y);
}
static bool waste_pile_stack(struct stack *stack) {
return((stack->card->frame->start_x == WASTE_PILE_STARTING_X) &&
(stack->card->frame->start_y == WASTE_PILE_STARTING_Y));
}
static void refresh_card_coordinates(struct stack *origin, struct stack *destination) {
origin->card->frame->start_x = destination->card->frame->start_x;
origin->card->frame->start_y = destination->card->frame->start_y;
if (!empty(destination) && maneuvre_stack(destination)) {
origin->card->frame->start_y++;
}
return;
}
void allocate_stack(struct stack **stack) {
if (!(*stack = malloc(sizeof(**stack)))) {
fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1);
exit(errno);
}
2010-04-04 00:44:59 +00:00
allocate_card(&((*stack)->card));
return;
}
void initialize_stack(struct stack *stack) {
initialize_card(stack->card);
stack->next = NULL;
2010-04-04 00:44:59 +00:00
return;
2010-04-04 00:44:59 +00:00
}
2010-04-07 00:59:21 +00:00
void delete_stack(struct stack *stack) {
delete_card(stack->card);
free(stack);
return;
}
2010-04-04 00:44:59 +00:00
bool empty(struct stack *stack) {
return(stack->card->value == NO_VALUE);
2010-04-04 00:44:59 +00:00
}
int length(struct stack *stack) {
2010-04-04 03:51:14 +00:00
struct stack *iterator = stack;
2010-04-04 00:44:59 +00:00
int length = 0;
if (!empty(stack)) {
length = 1;
2010-04-04 03:51:14 +00:00
while (iterator->next != NULL) {
2010-04-04 00:44:59 +00:00
length++;
2010-04-04 03:51:14 +00:00
iterator = iterator->next;
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) {
2010-04-04 00:44:59 +00:00
struct stack *new_stack = NULL;
2010-04-04 06:20:56 +00:00
if (empty(*stack)) {
(*stack)->card = card;
2010-04-04 00:44:59 +00:00
} else {
allocate_stack(&new_stack);
2010-04-04 00:44:59 +00:00
new_stack->card = card;
2010-04-04 06:20:56 +00:00
new_stack->next = (*stack);
*stack = new_stack;
2010-04-04 00:44:59 +00:00
}
}
2010-04-04 01:12:42 +00:00
/* FIXME: hack hack hack to get the old coordinates after clearing the structure */
2010-04-04 07:15:43 +00:00
struct stack *pop(struct stack **stack) {
2010-04-04 01:12:42 +00:00
struct stack *popped_entry = NULL;
2010-04-04 07:15:43 +00:00
if(!empty(*stack)) {
popped_entry = *stack;
if (length(*stack) == 1) {
int start_y, start_x;
start_y = (*stack)->card->frame->start_y;
start_x = (*stack)->card->frame->start_x;
allocate_stack(stack);
initialize_stack(*stack);
set_frame((*stack)->card->frame, start_y, start_x);
} else {
*stack = (*stack)->next;
}
2010-04-04 07:15:43 +00:00
popped_entry->next = NULL;
2010-04-04 01:12:42 +00:00
}
return(popped_entry);
}
void move_card(struct stack **origin, struct stack **destination) {
struct stack *stack = NULL;
refresh_card_coordinates(*origin, *destination);
stack = pop(origin);
push(destination, stack->card);
return;
}