2011-02-06 05:44:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-04-04 00:44:59 +00:00
|
|
|
#include <stdbool.h>
|
2011-02-06 05:44:45 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-04-04 00:44:59 +00:00
|
|
|
#include "stack.h"
|
2011-05-08 23:38:36 +00:00
|
|
|
#include "common.h"
|
2010-04-04 00:44:59 +00:00
|
|
|
|
2010-04-05 07:33:10 +00:00
|
|
|
void allocate_stack(struct stack **stack) {
|
2011-02-06 05:44:45 +00:00
|
|
|
if (!(*stack = malloc(sizeof(**stack)))) {
|
2011-05-08 23:20:22 +00:00
|
|
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
2011-02-06 05:44:45 +00:00
|
|
|
exit(errno);
|
|
|
|
}
|
2010-04-04 00:44:59 +00:00
|
|
|
|
2010-04-05 07:33:10 +00:00
|
|
|
allocate_card(&((*stack)->card));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)) {
|
2011-05-29 18:19:00 +00:00
|
|
|
push(&new_stack, (pop(&tmp_stack)));
|
2011-05-01 06:06:43 +00:00
|
|
|
}
|
|
|
|
free_stack(tmp_stack);
|
|
|
|
|
|
|
|
return(new_stack);
|
|
|
|
}
|
|
|
|
|
2011-02-14 02:10:47 +00:00
|
|
|
void free_stack(struct stack *stack) {
|
2011-05-08 04:22:26 +00:00
|
|
|
struct stack *tmp;
|
2011-02-14 02:04:26 +00:00
|
|
|
|
2011-05-08 04:22:26 +00:00
|
|
|
for (; stack; stack = tmp) {
|
|
|
|
tmp = stack->next;
|
2011-02-14 02:10:47 +00:00
|
|
|
free_card(stack->card);
|
2011-02-14 02:05:54 +00:00
|
|
|
free(stack);
|
2011-02-12 03:26:03 +00:00
|
|
|
}
|
2010-04-07 00:59:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 00:44:59 +00:00
|
|
|
bool empty(struct stack *stack) {
|
2011-02-14 01:13:00 +00:00
|
|
|
return(stack->card->value == NO_VALUE &&
|
|
|
|
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)) {
|
2011-02-17 00:42:49 +00:00
|
|
|
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) {
|
2011-02-14 01:13:00 +00:00
|
|
|
if (card) {
|
|
|
|
if (empty(*stack)) {
|
2011-05-29 18:19:00 +00:00
|
|
|
(*stack)->card = card;
|
2011-02-14 01:13:00 +00:00
|
|
|
} else {
|
2011-05-29 18:19:00 +00:00
|
|
|
struct stack *new_stack;
|
2011-02-14 01:13:00 +00:00
|
|
|
allocate_stack(&new_stack);
|
2011-05-29 18:19:00 +00:00
|
|
|
new_stack->card = card;
|
2011-02-14 01:13:00 +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
|
|
|
|
2011-05-29 18:19:00 +00:00
|
|
|
struct card *pop(struct stack **stack) {
|
|
|
|
if(empty(*stack)) {
|
|
|
|
return(NULL);
|
|
|
|
} else {
|
|
|
|
struct card *popped_card = (*stack)->card;
|
2010-04-04 23:52:02 +00:00
|
|
|
if (length(*stack) == 1) {
|
2011-05-29 18:19:00 +00:00
|
|
|
/* 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));
|
2011-05-01 08:36:46 +00:00
|
|
|
/* An empty stack is a stack with a blank top card
|
|
|
|
* and with stack->next == NULL. */
|
2011-05-29 18:19:00 +00:00
|
|
|
set_card((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
|
2011-05-01 08:36:46 +00:00
|
|
|
(*stack)->next = NULL;
|
2010-04-04 23:52:02 +00:00
|
|
|
} else {
|
2011-05-29 18:19:00 +00:00
|
|
|
struct stack *tmp = *stack;
|
2010-04-04 23:52:02 +00:00
|
|
|
*stack = (*stack)->next;
|
2011-05-29 18:19:00 +00:00
|
|
|
free(tmp);
|
2010-04-04 23:52:02 +00:00
|
|
|
}
|
2011-05-29 18:19:00 +00:00
|
|
|
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) {
|
|
|
|
if (length(stack) > 1) {
|
2011-05-01 05:04:16 +00:00
|
|
|
struct stack *tmp_stack, *iterator;
|
2011-05-01 01:13:41 +00:00
|
|
|
|
|
|
|
allocate_stack(&tmp_stack);
|
|
|
|
initialize_stack(tmp_stack);
|
2011-05-01 05:04:16 +00:00
|
|
|
for (iterator = stack; iterator; iterator = iterator->next) {
|
|
|
|
push(&tmp_stack, iterator->card);
|
2011-05-01 01:13:41 +00:00
|
|
|
}
|
|
|
|
return(tmp_stack);
|
|
|
|
} else {
|
2011-05-01 04:13:15 +00:00
|
|
|
return(stack);
|
2011-05-01 01:13:41 +00:00
|
|
|
}
|
|
|
|
}
|