Make stack#pop return a card and don't duplicate stuff on push/pop.
This commit is contained in:
@@ -107,7 +107,6 @@ void draw_back(struct card *card) {
|
||||
}
|
||||
|
||||
void draw_card(struct card *card) {
|
||||
mvwin(card->frame->window, card->frame->begin_y, card->frame->begin_x);
|
||||
if (card->face == EXPOSED) {
|
||||
draw_front(card);
|
||||
} else {
|
||||
@@ -124,7 +123,6 @@ void draw_stack(struct stack *stack) {
|
||||
} else {
|
||||
if (maneuvre_stack(stack)) {
|
||||
struct stack *reversed_stack = reverse(stack);
|
||||
|
||||
for (struct stack *i = reversed_stack; i; i = i->next) {
|
||||
draw_card(i->card);
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ void allocate_frame(struct frame **frame) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
(*frame)->window = newwin(FRAME_HEIGHT, FRAME_WIDTH, 0, 0);
|
||||
}
|
||||
|
||||
void initialize_frame(struct frame *frame) {
|
||||
frame->window = NULL;
|
||||
frame->begin_y = 0;
|
||||
frame->begin_x = 0;
|
||||
}
|
||||
@@ -38,8 +38,5 @@ void free_frame(struct frame *frame) {
|
||||
void set_frame(struct frame *frame, int begin_y, int begin_x) {
|
||||
frame->begin_y = begin_y;
|
||||
frame->begin_x = begin_x;
|
||||
frame->window = newwin(FRAME_HEIGHT,
|
||||
FRAME_WIDTH,
|
||||
frame->begin_y,
|
||||
frame->begin_x);
|
||||
mvwin(frame->window, begin_y, begin_x);
|
||||
}
|
||||
|
||||
87
lib/game.c
87
lib/game.c
@@ -73,60 +73,32 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
||||
}
|
||||
|
||||
void move_card(struct stack **origin, struct stack **destination) {
|
||||
struct stack *stack;
|
||||
|
||||
if (!empty(*origin)) {
|
||||
(*origin)->card->frame->begin_x = (*destination)->card->frame->begin_x;
|
||||
(*origin)->card->frame->begin_y = (*destination)->card->frame->begin_y;
|
||||
}
|
||||
if (!empty(*destination) && maneuvre_stack(*destination)) {
|
||||
(*origin)->card->frame->begin_y++;
|
||||
}
|
||||
if ((stack = pop(origin))) {
|
||||
push(destination, stack->card);
|
||||
struct card *tmp;
|
||||
if ((tmp = pop(origin))) {
|
||||
int destination_y = (*destination)->card->frame->begin_y;
|
||||
int destination_x = (*destination)->card->frame->begin_x;
|
||||
if (!empty(*destination) && maneuvre_stack(*destination)) {
|
||||
destination_y++;
|
||||
}
|
||||
push(destination, tmp);
|
||||
set_frame((*destination)->card->frame, destination_y, destination_x);
|
||||
}
|
||||
}
|
||||
|
||||
static void set_stacks_initial_coordinates(struct deck *deck) {
|
||||
set_frame(deck->stock->card->frame,
|
||||
STOCK_BEGIN_Y,
|
||||
STOCK_BEGIN_X);
|
||||
set_frame(deck->waste_pile->card->frame,
|
||||
WASTE_PILE_BEGIN_Y,
|
||||
WASTE_PILE_BEGIN_X);
|
||||
set_frame(deck->foundation_0->card->frame,
|
||||
FOUNDATION_BEGIN_Y,
|
||||
FOUNDATION_0_BEGIN_X);
|
||||
set_frame(deck->foundation_1->card->frame,
|
||||
FOUNDATION_BEGIN_Y,
|
||||
FOUNDATION_1_BEGIN_X);
|
||||
set_frame(deck->foundation_2->card->frame,
|
||||
FOUNDATION_BEGIN_Y,
|
||||
FOUNDATION_2_BEGIN_X);
|
||||
set_frame(deck->foundation_3->card->frame,
|
||||
FOUNDATION_BEGIN_Y,
|
||||
FOUNDATION_3_BEGIN_X);
|
||||
set_frame(deck->maneuvre_0->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_0_BEGIN_X);
|
||||
set_frame(deck->maneuvre_1->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_1_BEGIN_X);
|
||||
set_frame(deck->maneuvre_2->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_2_BEGIN_X);
|
||||
set_frame(deck->maneuvre_3->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_3_BEGIN_X);
|
||||
set_frame(deck->maneuvre_4->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_4_BEGIN_X);
|
||||
set_frame(deck->maneuvre_5->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_5_BEGIN_X);
|
||||
set_frame(deck->maneuvre_6->card->frame,
|
||||
MANEUVRE_BEGIN_Y,
|
||||
MANEUVRE_6_BEGIN_X);
|
||||
set_frame(deck->stock->card->frame, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||
set_frame(deck->waste_pile->card->frame, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||
set_frame(deck->foundation_0->card->frame, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||
set_frame(deck->foundation_1->card->frame, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||
set_frame(deck->foundation_2->card->frame, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||
set_frame(deck->foundation_3->card->frame, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||
set_frame(deck->maneuvre_0->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||
set_frame(deck->maneuvre_1->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||
set_frame(deck->maneuvre_2->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||
set_frame(deck->maneuvre_3->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||
set_frame(deck->maneuvre_4->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||
set_frame(deck->maneuvre_5->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||
set_frame(deck->maneuvre_6->card->frame, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||
}
|
||||
|
||||
static void fill_deck(struct deck *deck) {
|
||||
@@ -194,30 +166,29 @@ static void fill_deck(struct deck *deck) {
|
||||
}
|
||||
|
||||
static void shuffle_deck(struct deck *deck) {
|
||||
struct stack **stack = NULL;
|
||||
struct stack tmp;
|
||||
struct card **card, tmp;
|
||||
int random;
|
||||
|
||||
if (!(stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)))) {
|
||||
if (!(card = malloc(NUMBER_OF_CARDS * sizeof(*card)))) {
|
||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
stack[i] = pop(&(deck->stock));
|
||||
card[i] = pop(&(deck->stock));
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||
random = i + (rand() % (NUMBER_OF_CARDS) - i);
|
||||
tmp = (*stack[i]);
|
||||
(*stack[i]) = (*stack[random]);
|
||||
(*stack[random]) = tmp;
|
||||
tmp = *card[i];
|
||||
*card[i] = (*card[random]);
|
||||
*card[random] = tmp;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), stack[i]->card);
|
||||
push(&(deck->stock), card[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
38
lib/stack.c
38
lib/stack.c
@@ -33,7 +33,7 @@ struct stack *duplicate_stack(struct stack *stack) {
|
||||
push(&tmp_stack, duplicate_card(iterator->card));
|
||||
}
|
||||
while (!empty(tmp_stack)) {
|
||||
push(&new_stack, (pop(&tmp_stack))->card);
|
||||
push(&new_stack, (pop(&tmp_stack)));
|
||||
}
|
||||
free_stack(tmp_stack);
|
||||
|
||||
@@ -69,44 +69,40 @@ int length(struct stack *stack) {
|
||||
}
|
||||
|
||||
void push(struct stack **stack, struct card *card) {
|
||||
struct stack *new_stack;
|
||||
|
||||
if (card) {
|
||||
if (empty(*stack)) {
|
||||
(*stack)->card = duplicate_card(card);
|
||||
(*stack)->card = card;
|
||||
} else {
|
||||
struct stack *new_stack;
|
||||
allocate_stack(&new_stack);
|
||||
new_stack->card = duplicate_card(card);
|
||||
new_stack->card = card;
|
||||
new_stack->next = (*stack);
|
||||
*stack = new_stack;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct stack *pop(struct stack **stack) {
|
||||
struct stack *popped_entry = NULL;
|
||||
|
||||
if(!empty(*stack)) {
|
||||
allocate_stack(&popped_entry);
|
||||
initialize_stack(popped_entry);
|
||||
popped_entry->card = duplicate_card((*stack)->card);
|
||||
popped_entry->next = NULL;
|
||||
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,
|
||||
(*stack)->card->frame->begin_y,
|
||||
(*stack)->card->frame->begin_x);
|
||||
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);
|
||||
}
|
||||
|
||||
return(popped_entry);
|
||||
}
|
||||
|
||||
struct stack *reverse(struct stack *stack) {
|
||||
|
||||
@@ -15,7 +15,7 @@ void free_stack(struct stack *);
|
||||
bool empty(struct stack *);
|
||||
int length(struct stack *);
|
||||
void push(struct stack **, struct card *);
|
||||
struct stack *pop(struct stack **);
|
||||
struct card *pop(struct stack **);
|
||||
struct stack *reverse(struct stack *);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user