2010-04-04 00:44:59 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "stack.h"
|
2010-04-21 07:14:39 +00:00
|
|
|
#include "game.h"
|
2010-04-04 00:44:59 +00:00
|
|
|
|
2010-04-05 07:33:10 +00:00
|
|
|
void allocate_stack(struct stack **stack) {
|
2010-04-04 23:52:02 +00:00
|
|
|
*stack = malloc(sizeof(**stack));
|
2010-04-04 00:44:59 +00:00
|
|
|
|
2010-04-05 07:33:10 +00:00
|
|
|
allocate_card(&((*stack)->card));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_stack(struct stack *stack) {
|
2010-04-09 03:37:57 +00:00
|
|
|
initialize_card(stack->card);
|
2010-04-05 07:33:10 +00:00
|
|
|
stack->next = NULL;
|
2010-04-04 00:44:59 +00:00
|
|
|
|
2010-04-04 23:52:02 +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) {
|
2010-04-09 04:23:10 +00:00
|
|
|
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 {
|
2010-04-05 07:33:10 +00:00
|
|
|
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
|
|
|
|
2010-04-10 03:32:25 +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;
|
2010-04-04 23:52:02 +00:00
|
|
|
if (length(*stack) == 1) {
|
2010-04-10 03:32:25 +00:00
|
|
|
int start_y, start_x;
|
|
|
|
start_y = (*stack)->card->frame->start_y;
|
|
|
|
start_x = (*stack)->card->frame->start_x;
|
2010-04-05 07:33:10 +00:00
|
|
|
allocate_stack(stack);
|
|
|
|
initialize_stack(*stack);
|
2010-04-10 03:32:25 +00:00
|
|
|
set_frame((*stack)->card->frame, start_y, start_x);
|
2010-04-04 23:52:02 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2010-04-09 03:49:17 +00:00
|
|
|
|
2010-04-10 03:32:25 +00:00
|
|
|
bool maneuvre_stack(struct stack *stack) {
|
2010-04-21 07:14:39 +00:00
|
|
|
return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2010-04-10 03:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-04-09 03:49:17 +00:00
|
|
|
void move_card(struct stack **origin, struct stack **destination) {
|
|
|
|
struct stack *stack = NULL;
|
|
|
|
|
2010-04-10 03:32:25 +00:00
|
|
|
refresh_card_coordinates(*origin, *destination);
|
2010-04-09 03:49:17 +00:00
|
|
|
stack = pop(origin);
|
|
|
|
push(destination, stack->card);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|