Name-spaced functions for card and stack.
This commit is contained in:
parent
c3f6e16dc8
commit
95a1c77a25
22
src/card.c
22
src/card.c
@ -6,7 +6,7 @@
|
|||||||
#include "card.h"
|
#include "card.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
void allocate_card(struct card **card) {
|
void card_malloc(struct card **card) {
|
||||||
if (!(*card = malloc(sizeof(**card)))) {
|
if (!(*card = malloc(sizeof(**card)))) {
|
||||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||||
exit(errno);
|
exit(errno);
|
||||||
@ -14,19 +14,19 @@ void allocate_card(struct card **card) {
|
|||||||
allocate_frame(&((*card)->frame));
|
allocate_frame(&((*card)->frame));
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_card(struct card *card) {
|
void card_init(struct card *card) {
|
||||||
initialize_frame(card->frame);
|
initialize_frame(card->frame);
|
||||||
card->value = NO_VALUE;
|
card->value = NO_VALUE;
|
||||||
card->suit = NO_SUIT;
|
card->suit = NO_SUIT;
|
||||||
card->face = NO_FACE;
|
card->face = NO_FACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_card(struct card *card) {
|
void card_free(struct card *card) {
|
||||||
free_frame(card->frame);
|
free_frame(card->frame);
|
||||||
free(card);
|
free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_card(struct card *card,
|
void card_set(struct card *card,
|
||||||
enum value value,
|
enum value value,
|
||||||
enum suit suit,
|
enum suit suit,
|
||||||
enum face face,
|
enum face face,
|
||||||
@ -38,27 +38,27 @@ void set_card(struct card *card,
|
|||||||
card->face = face;
|
card->face = face;
|
||||||
}
|
}
|
||||||
|
|
||||||
void expose_card(struct card *card) {
|
void card_expose(struct card *card) {
|
||||||
card->face = EXPOSED;
|
card->face = EXPOSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cover_card(struct card *card) {
|
void card_cover(struct card *card) {
|
||||||
card->face = COVERED;
|
card->face = COVERED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mark_card(struct card *card) {
|
void card_mark(struct card *card) {
|
||||||
set_frame(card->frame, card->frame->begin_y + 1, card->frame->begin_x);
|
set_frame(card->frame, card->frame->begin_y + 1, card->frame->begin_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmark_card(struct card *card) {
|
void card_unmark(struct card *card) {
|
||||||
set_frame(card->frame, card->frame->begin_y - 1, card->frame->begin_x);
|
set_frame(card->frame, card->frame->begin_y - 1, card->frame->begin_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct card *duplicate_card(struct card *card) {
|
struct card *card_dup(struct card *card) {
|
||||||
struct card *new_card;
|
struct card *new_card;
|
||||||
|
|
||||||
allocate_card(&new_card);
|
card_malloc(&new_card);
|
||||||
set_card(new_card,
|
card_set(new_card,
|
||||||
card->value,
|
card->value,
|
||||||
card->suit,
|
card->suit,
|
||||||
card->face,
|
card->face,
|
||||||
|
18
src/card.h
18
src/card.h
@ -29,14 +29,14 @@ struct card {
|
|||||||
enum face face;
|
enum face face;
|
||||||
};
|
};
|
||||||
|
|
||||||
void allocate_card(struct card **);
|
void card_malloc(struct card **);
|
||||||
void initialize_card(struct card *);
|
void card_init(struct card *);
|
||||||
void free_card(struct card *);
|
void card_free(struct card *);
|
||||||
void set_card(struct card *, enum value, enum suit, enum face, int, int);
|
void card_set(struct card *, enum value, enum suit, enum face, int, int);
|
||||||
void expose_card(struct card *);
|
void card_expose(struct card *);
|
||||||
void cover_card(struct card *);
|
void card_cover(struct card *);
|
||||||
struct card *duplicate_card(struct card *);
|
void card_mark(struct card *);
|
||||||
void mark_card(struct card *);
|
void card_unmark(struct card *);
|
||||||
void unmark_card(struct card *);
|
struct card *card_dup(struct card *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
14
src/cursor.c
14
src/cursor.c
@ -51,25 +51,25 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
|
|||||||
if (cursor->y == CURSOR_BEGIN_Y) {
|
if (cursor->y == CURSOR_BEGIN_Y) {
|
||||||
switch (cursor->x - 3) {
|
switch (cursor->x - 3) {
|
||||||
case MANEUVRE_0_BEGIN_X:
|
case MANEUVRE_0_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[0]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[0]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_1_BEGIN_X:
|
case MANEUVRE_1_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[1]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[1]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_2_BEGIN_X:
|
case MANEUVRE_2_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[2]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[2]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_3_BEGIN_X:
|
case MANEUVRE_3_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[3]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[3]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_4_BEGIN_X:
|
case MANEUVRE_4_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[4]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[4]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_5_BEGIN_X:
|
case MANEUVRE_5_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[5]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[5]);
|
||||||
break;
|
break;
|
||||||
case MANEUVRE_6_BEGIN_X:
|
case MANEUVRE_6_BEGIN_X:
|
||||||
cursor->y = cursor->y + 7 + length(deck->maneuvre[6]);
|
cursor->y = cursor->y + 7 + stack_length(deck->maneuvre[6]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
src/deck.c
24
src/deck.c
@ -11,35 +11,35 @@ void allocate_deck(struct deck **deck) {
|
|||||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
allocate_stack(&((*deck)->stock));
|
stack_malloc(&((*deck)->stock));
|
||||||
allocate_stack(&((*deck)->waste_pile));
|
stack_malloc(&((*deck)->waste_pile));
|
||||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||||
allocate_stack(&((*deck)->foundation[i]));
|
stack_malloc(&((*deck)->foundation[i]));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||||
allocate_stack(&((*deck)->maneuvre[i]));
|
stack_malloc(&((*deck)->maneuvre[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_deck(struct deck *deck) {
|
void initialize_deck(struct deck *deck) {
|
||||||
initialize_stack(deck->stock);
|
stack_init(deck->stock);
|
||||||
initialize_stack(deck->waste_pile);
|
stack_init(deck->waste_pile);
|
||||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||||
initialize_stack(deck->foundation[i]);
|
stack_init(deck->foundation[i]);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||||
initialize_stack(deck->maneuvre[i]);
|
stack_init(deck->maneuvre[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_deck(struct deck *deck) {
|
void free_deck(struct deck *deck) {
|
||||||
free_stack(deck->stock);
|
stack_free(deck->stock);
|
||||||
free_stack(deck->waste_pile);
|
stack_free(deck->waste_pile);
|
||||||
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
for (int i = 0; i < FOUNDATION_STACKS_NUMBER; i++) {
|
||||||
free_stack(deck->foundation[i]);
|
stack_free(deck->foundation[i]);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
|
||||||
free_stack(deck->maneuvre[i]);
|
stack_free(deck->maneuvre[i]);
|
||||||
}
|
}
|
||||||
free(deck);
|
free(deck);
|
||||||
}
|
}
|
||||||
|
@ -62,16 +62,16 @@ void draw_card(struct card *card) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void draw_stack(struct stack *stack) {
|
void draw_stack(struct stack *stack) {
|
||||||
if (empty(stack)) {
|
if (stack_empty(stack)) {
|
||||||
box(stack->card->frame->window, 0, 0);
|
box(stack->card->frame->window, 0, 0);
|
||||||
wrefresh(stack->card->frame->window);
|
wrefresh(stack->card->frame->window);
|
||||||
} else {
|
} else {
|
||||||
if (maneuvre_stack(stack)) {
|
if (maneuvre_stack(stack)) {
|
||||||
struct stack *reversed_stack = reverse(stack);
|
struct stack *stack_reversed_stack = stack_reverse(stack);
|
||||||
for (struct stack *i = reversed_stack; i; i = i->next) {
|
for (struct stack *i = stack_reversed_stack; i; i = i->next) {
|
||||||
draw_card(i->card);
|
draw_card(i->card);
|
||||||
}
|
}
|
||||||
free_stack(reversed_stack);
|
stack_free(stack_reversed_stack);
|
||||||
} else {
|
} else {
|
||||||
draw_card(stack->card);
|
draw_card(stack->card);
|
||||||
}
|
}
|
||||||
|
40
src/game.c
40
src/game.c
@ -77,7 +77,7 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
|||||||
if (stock_stack(origin) && waste_pile_stack(destination)) {
|
if (stock_stack(origin) && waste_pile_stack(destination)) {
|
||||||
return(true);
|
return(true);
|
||||||
} else if (foundation_stack(destination)) {
|
} else if (foundation_stack(destination)) {
|
||||||
if (empty(destination)) {
|
if (stack_empty(destination)) {
|
||||||
if (origin->card->value == ACE) {
|
if (origin->card->value == ACE) {
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
|||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
} else if (maneuvre_stack(destination)) {
|
} else if (maneuvre_stack(destination)) {
|
||||||
if (empty(destination)) {
|
if (stack_empty(destination)) {
|
||||||
if (origin->card->value == KING) {
|
if (origin->card->value == KING) {
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
@ -103,31 +103,31 @@ bool valid_move(struct stack *origin, struct stack *destination) {
|
|||||||
|
|
||||||
void move_card(struct stack **origin, struct stack **destination) {
|
void move_card(struct stack **origin, struct stack **destination) {
|
||||||
struct card *tmp;
|
struct card *tmp;
|
||||||
if ((tmp = pop(origin))) {
|
if ((tmp = stack_pop(origin))) {
|
||||||
int destination_y = (*destination)->card->frame->begin_y;
|
int destination_y = (*destination)->card->frame->begin_y;
|
||||||
int destination_x = (*destination)->card->frame->begin_x;
|
int destination_x = (*destination)->card->frame->begin_x;
|
||||||
if (!empty(*destination) && maneuvre_stack(*destination)) {
|
if (!stack_empty(*destination) && maneuvre_stack(*destination)) {
|
||||||
destination_y++;
|
destination_y++;
|
||||||
}
|
}
|
||||||
push(destination, tmp);
|
stack_push(destination, tmp);
|
||||||
set_frame((*destination)->card->frame, destination_y, destination_x);
|
set_frame((*destination)->card->frame, destination_y, destination_x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_block(struct stack **origin, struct stack **destination, int block_size) {
|
void move_block(struct stack **origin, struct stack **destination, int block_size) {
|
||||||
struct stack *tmp;
|
struct stack *tmp;
|
||||||
allocate_stack(&tmp);
|
stack_malloc(&tmp);
|
||||||
initialize_stack(tmp);
|
stack_init(tmp);
|
||||||
for (int i = 0; i < block_size; i++) {
|
for (int i = 0; i < block_size; i++) {
|
||||||
push(&tmp, pop(origin));
|
stack_push(&tmp, stack_pop(origin));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < block_size; i++) {
|
for (int i = 0; i < block_size; i++) {
|
||||||
move_card(&tmp, destination);
|
move_card(&tmp, destination);
|
||||||
}
|
}
|
||||||
if (length(*destination) > 1) {
|
if (stack_length(*destination) > 1) {
|
||||||
cursor->y += block_size;
|
cursor->y += block_size;
|
||||||
}
|
}
|
||||||
free_stack(tmp);
|
stack_free(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -137,9 +137,9 @@ static void fill_deck(struct deck *deck) {
|
|||||||
for (int i = ACE; i <= KING; i++) {
|
for (int i = ACE; i <= KING; i++) {
|
||||||
for (int j = DIAMONDS; j <= CLUBS; j++) {
|
for (int j = DIAMONDS; j <= CLUBS; j++) {
|
||||||
int index = 4 * (i - ACE) + j;
|
int index = 4 * (i - ACE) + j;
|
||||||
allocate_card(&(card[index]));
|
card_malloc(&(card[index]));
|
||||||
set_card(card[index], i, j, COVERED, 1, 1);
|
card_set(card[index], i, j, COVERED, 1, 1);
|
||||||
push(&(deck->stock), card[index]);
|
stack_push(&(deck->stock), card[index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ static void shuffle_deck(struct deck *deck) {
|
|||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||||
card[i] = pop(&(deck->stock));
|
card[i] = stack_pop(&(deck->stock));
|
||||||
}
|
}
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||||
@ -163,14 +163,14 @@ static void shuffle_deck(struct deck *deck) {
|
|||||||
*card[random] = tmp;
|
*card[random] = tmp;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||||
push(&(deck->stock), card[i]);
|
stack_push(&(deck->stock), card[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deal_cards(struct deck *deck) {
|
static void deal_cards(struct deck *deck) {
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
move_card(&(deck->stock), &(deck->maneuvre[i]));
|
move_card(&(deck->stock), &(deck->maneuvre[i]));
|
||||||
expose_card(deck->maneuvre[i]->card);
|
card_expose(deck->maneuvre[i]->card);
|
||||||
for (int j = i + 1; j < 7; j++) {
|
for (int j = i + 1; j < 7; j++) {
|
||||||
move_card(&(deck->stock), &(deck->maneuvre[j]));
|
move_card(&(deck->stock), &(deck->maneuvre[j]));
|
||||||
}
|
}
|
||||||
@ -207,8 +207,8 @@ void game_end() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool game_won() {
|
bool game_won() {
|
||||||
return(length(deck->foundation[0]) == 13 &&
|
return(stack_length(deck->foundation[0]) == 13 &&
|
||||||
length(deck->foundation[1]) == 13 &&
|
stack_length(deck->foundation[1]) == 13 &&
|
||||||
length(deck->foundation[2]) == 13 &&
|
stack_length(deck->foundation[2]) == 13 &&
|
||||||
length(deck->foundation[3]) == 13);
|
stack_length(deck->foundation[3]) == 13);
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,9 @@ static bool cursor_on_invalid_spot(struct cursor *cursor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void handle_stock_event() {
|
static void handle_stock_event() {
|
||||||
if (!empty(deck->stock)) {
|
if (!stack_empty(deck->stock)) {
|
||||||
move_card(&(deck->stock), &(deck->waste_pile));
|
move_card(&(deck->stock), &(deck->waste_pile));
|
||||||
expose_card(deck->waste_pile->card);
|
card_expose(deck->waste_pile->card);
|
||||||
erase_stack(deck->waste_pile);
|
erase_stack(deck->waste_pile);
|
||||||
draw_stack(deck->stock);
|
draw_stack(deck->stock);
|
||||||
draw_stack(deck->waste_pile);
|
draw_stack(deck->waste_pile);
|
||||||
@ -61,11 +61,11 @@ static void handle_stock_event() {
|
|||||||
/* FIXME: this function does not work on stacks with no marked cards.
|
/* FIXME: this function does not work on stacks with no marked cards.
|
||||||
* In that case it returns the stack's length. */
|
* In that case it returns the stack's length. */
|
||||||
static int marked_cards_count(struct stack *stack) {
|
static int marked_cards_count(struct stack *stack) {
|
||||||
if (length(stack) == 1) {
|
if (stack_length(stack) == 1) {
|
||||||
if (stack->card->frame->begin_y > MANEUVRE_BEGIN_Y) {
|
if (stack->card->frame->begin_y > MANEUVRE_BEGIN_Y) {
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
} else if (length(stack) > 1) {
|
} else if (stack_length(stack) > 1) {
|
||||||
for (int marked_cards_count = 0; stack; stack = stack->next) {
|
for (int marked_cards_count = 0; stack; stack = stack->next) {
|
||||||
marked_cards_count++;
|
marked_cards_count++;
|
||||||
if (!stack->next || (stack->card->frame->begin_y - stack->next->card->frame->begin_y) > 1) {
|
if (!stack->next || (stack->card->frame->begin_y - stack->next->card->frame->begin_y) > 1) {
|
||||||
@ -79,7 +79,7 @@ static int marked_cards_count(struct stack *stack) {
|
|||||||
static void unmark_cards(struct stack *stack) {
|
static void unmark_cards(struct stack *stack) {
|
||||||
int _marked_cards_count = marked_cards_count(stack);
|
int _marked_cards_count = marked_cards_count(stack);
|
||||||
for (int i = 0; i < _marked_cards_count; stack = stack->next, i++) {
|
for (int i = 0; i < _marked_cards_count; stack = stack->next, i++) {
|
||||||
unmark_card(stack->card);
|
card_unmark(stack->card);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,13 +88,13 @@ static void handle_card_movement(struct cursor *cursor) {
|
|||||||
struct stack **destination;
|
struct stack **destination;
|
||||||
int option;
|
int option;
|
||||||
|
|
||||||
if (cursor_on_invalid_spot(cursor) || empty(*origin)) {
|
if (cursor_on_invalid_spot(cursor) || stack_empty(*origin)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maneuvre_stack(*origin)) {
|
if (maneuvre_stack(*origin)) {
|
||||||
erase_stack(*origin);
|
erase_stack(*origin);
|
||||||
mark_card((*origin)->card);
|
card_mark((*origin)->card);
|
||||||
draw_stack(*origin);
|
draw_stack(*origin);
|
||||||
cursor->y++;
|
cursor->y++;
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ static void handle_card_movement(struct cursor *cursor) {
|
|||||||
if (i->next->card->face == EXPOSED &&
|
if (i->next->card->face == EXPOSED &&
|
||||||
(i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
(i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
||||||
erase_stack(*origin);
|
erase_stack(*origin);
|
||||||
mark_card(i->next->card);
|
card_mark(i->next->card);
|
||||||
draw_stack(*origin);
|
draw_stack(*origin);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -147,14 +147,14 @@ static void handle_card_movement(struct cursor *cursor) {
|
|||||||
if (i->next) {
|
if (i->next) {
|
||||||
if ((i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
if ((i->card->frame->begin_y - i->next->card->frame->begin_y) > 1) {
|
||||||
erase_stack(*origin);
|
erase_stack(*origin);
|
||||||
unmark_card(i->card);
|
card_unmark(i->card);
|
||||||
draw_stack(*origin);
|
draw_stack(*origin);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (i->card->frame->begin_y == (MANEUVRE_BEGIN_Y + 1)) {
|
if (i->card->frame->begin_y == (MANEUVRE_BEGIN_Y + 1)) {
|
||||||
erase_stack(*origin);
|
erase_stack(*origin);
|
||||||
unmark_card(i->card);
|
card_unmark(i->card);
|
||||||
draw_stack(*origin);
|
draw_stack(*origin);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -184,7 +184,7 @@ static void handle_card_movement(struct cursor *cursor) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (valid_move(*origin, *destination)) {
|
if (valid_move(*origin, *destination)) {
|
||||||
if (maneuvre_stack(*destination) && length(*destination) > 1) {
|
if (maneuvre_stack(*destination) && stack_length(*destination) > 1) {
|
||||||
cursor->y++;
|
cursor->y++;
|
||||||
}
|
}
|
||||||
move_card(origin, destination);
|
move_card(origin, destination);
|
||||||
@ -257,7 +257,7 @@ void handle_keyboard_event(int key) {
|
|||||||
struct card *card;
|
struct card *card;
|
||||||
if (cursor_stack(cursor) &&
|
if (cursor_stack(cursor) &&
|
||||||
(card = (*cursor_stack(cursor))->card)->face == COVERED) {
|
(card = (*cursor_stack(cursor))->card)->face == COVERED) {
|
||||||
expose_card(card);
|
card_expose(card);
|
||||||
draw_card(card);
|
draw_card(card);
|
||||||
} else {
|
} else {
|
||||||
handle_card_movement(cursor);
|
handle_card_movement(cursor);
|
||||||
|
72
src/stack.c
72
src/stack.c
@ -8,72 +8,72 @@
|
|||||||
#include "card.h"
|
#include "card.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
void allocate_stack(struct stack **stack) {
|
void stack_malloc(struct stack **stack) {
|
||||||
if (!(*stack = malloc(sizeof(**stack)))) {
|
if (!(*stack = malloc(sizeof(**stack)))) {
|
||||||
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
|
||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
allocate_card(&((*stack)->card));
|
card_malloc(&((*stack)->card));
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_stack(struct stack *stack) {
|
void stack_init(struct stack *stack) {
|
||||||
initialize_card(stack->card);
|
card_init(stack->card);
|
||||||
stack->next = NULL;
|
stack->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_stack(struct stack *stack) {
|
void stack_free(struct stack *stack) {
|
||||||
struct stack *tmp;
|
struct stack *tmp;
|
||||||
|
|
||||||
for (; stack; stack = tmp) {
|
for (; stack; stack = tmp) {
|
||||||
tmp = stack->next;
|
tmp = stack->next;
|
||||||
free_card(stack->card);
|
card_free(stack->card);
|
||||||
free(stack);
|
free(stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stack *duplicate_stack(struct stack *stack) {
|
struct stack *stack_dup(struct stack *stack) {
|
||||||
struct stack *iterator = stack;
|
struct stack *iterator = stack;
|
||||||
struct stack *tmp_stack, *new_stack;
|
struct stack *tmp_stack, *new_stack;
|
||||||
|
|
||||||
allocate_stack(&new_stack);
|
stack_malloc(&new_stack);
|
||||||
allocate_stack(&tmp_stack);
|
stack_malloc(&tmp_stack);
|
||||||
initialize_stack(new_stack);
|
stack_init(new_stack);
|
||||||
initialize_stack(tmp_stack);
|
stack_init(tmp_stack);
|
||||||
for (iterator = stack; iterator; iterator = iterator->next) {
|
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||||
push(&tmp_stack, duplicate_card(iterator->card));
|
stack_push(&tmp_stack, card_dup(iterator->card));
|
||||||
}
|
}
|
||||||
while (!empty(tmp_stack)) {
|
while (!stack_empty(tmp_stack)) {
|
||||||
push(&new_stack, (pop(&tmp_stack)));
|
stack_push(&new_stack, (stack_pop(&tmp_stack)));
|
||||||
}
|
}
|
||||||
free_stack(tmp_stack);
|
stack_free(tmp_stack);
|
||||||
|
|
||||||
return(new_stack);
|
return(new_stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty(struct stack *stack) {
|
bool stack_empty(struct stack *stack) {
|
||||||
return(stack->card->value == NO_VALUE &&
|
return(stack->card->value == NO_VALUE &&
|
||||||
stack->card->suit == NO_SUIT &&
|
stack->card->suit == NO_SUIT &&
|
||||||
stack->card->face == NO_FACE &&
|
stack->card->face == NO_FACE &&
|
||||||
!stack->next);
|
!stack->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
int length(struct stack *stack) {
|
int stack_length(struct stack *stack) {
|
||||||
int length = 0;
|
int stack_length = 0;
|
||||||
|
|
||||||
if (!empty(stack)) {
|
if (!stack_empty(stack)) {
|
||||||
for (length = 1; stack->next; stack = stack->next, length++)
|
for (stack_length = 1; stack->next; stack = stack->next, stack_length++)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return(length);
|
return(stack_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(struct stack **stack, struct card *card) {
|
void stack_push(struct stack **stack, struct card *card) {
|
||||||
if (card) {
|
if (card) {
|
||||||
if (empty(*stack)) {
|
if (stack_empty(*stack)) {
|
||||||
(*stack)->card = card;
|
(*stack)->card = card;
|
||||||
} else {
|
} else {
|
||||||
/* Allocating by hand because stack#allocate_stack would
|
/* Allocating by hand because stack_malloc() would
|
||||||
* have allocated an unwanted card object. */
|
* have allocated an unwanted card object. */
|
||||||
struct stack *new_stack = malloc(sizeof(*new_stack));
|
struct stack *new_stack = malloc(sizeof(*new_stack));
|
||||||
new_stack->card = card;
|
new_stack->card = card;
|
||||||
@ -83,19 +83,19 @@ void push(struct stack **stack, struct card *card) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct card *pop(struct stack **stack) {
|
struct card *stack_pop(struct stack **stack) {
|
||||||
if(empty(*stack)) {
|
if(stack_empty(*stack)) {
|
||||||
return(NULL);
|
return(NULL);
|
||||||
} else {
|
} else {
|
||||||
struct card *popped_card = (*stack)->card;
|
struct card *popped_card = (*stack)->card;
|
||||||
if (length(*stack) == 1) {
|
if (stack_length(*stack) == 1) {
|
||||||
/* Remembering the stack position before clearing it. */
|
/* Remembering the stack position before clearing it. */
|
||||||
int begin_y = (*stack)->card->frame->begin_y;
|
int begin_y = (*stack)->card->frame->begin_y;
|
||||||
int begin_x = (*stack)->card->frame->begin_x;
|
int begin_x = (*stack)->card->frame->begin_x;
|
||||||
allocate_card(&((*stack)->card));
|
card_malloc(&((*stack)->card));
|
||||||
/* An empty stack is a stack with a blank top card
|
/* An stack_empty stack is a stack with a blank top card
|
||||||
* and with stack->next == NULL. */
|
* and with stack->next == NULL. */
|
||||||
set_card((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
|
card_set((*stack)->card, NO_VALUE, NO_SUIT, NO_FACE, begin_y, begin_x);
|
||||||
(*stack)->next = NULL;
|
(*stack)->next = NULL;
|
||||||
} else {
|
} else {
|
||||||
struct stack *tmp = *stack;
|
struct stack *tmp = *stack;
|
||||||
@ -106,17 +106,17 @@ struct card *pop(struct stack **stack) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stack *reverse(struct stack *stack) {
|
struct stack *stack_reverse(struct stack *stack) {
|
||||||
struct stack *tmp_stack, *iterator;
|
struct stack *tmp_stack, *iterator;
|
||||||
|
|
||||||
allocate_stack(&tmp_stack);
|
stack_malloc(&tmp_stack);
|
||||||
initialize_stack(tmp_stack);
|
stack_init(tmp_stack);
|
||||||
if (length(stack) > 1) {
|
if (stack_length(stack) > 1) {
|
||||||
for (iterator = stack; iterator; iterator = iterator->next) {
|
for (iterator = stack; iterator; iterator = iterator->next) {
|
||||||
push(&tmp_stack, duplicate_card(iterator->card));
|
stack_push(&tmp_stack, card_dup(iterator->card));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
set_card(tmp_stack->card,
|
card_set(tmp_stack->card,
|
||||||
stack->card->value,
|
stack->card->value,
|
||||||
stack->card->suit,
|
stack->card->suit,
|
||||||
stack->card->face,
|
stack->card->face,
|
||||||
|
18
src/stack.h
18
src/stack.h
@ -8,14 +8,14 @@ struct stack {
|
|||||||
struct stack *next;
|
struct stack *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
void allocate_stack(struct stack **);
|
void stack_malloc(struct stack **);
|
||||||
void initialize_stack(struct stack *);
|
void stack_init(struct stack *);
|
||||||
void free_stack(struct stack *);
|
void stack_free(struct stack *);
|
||||||
struct stack *duplicate_stack(struct stack *);
|
bool stack_empty(struct stack *);
|
||||||
bool empty(struct stack *);
|
int stack_length(struct stack *);
|
||||||
int length(struct stack *);
|
void stack_push(struct stack **, struct card *);
|
||||||
void push(struct stack **, struct card *);
|
struct card *stack_pop(struct stack **);
|
||||||
struct card *pop(struct stack **);
|
struct stack *stack_reverse(struct stack *);
|
||||||
struct stack *reverse(struct stack *);
|
struct stack *stack_dup(struct stack *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,39 +2,39 @@
|
|||||||
#include "test_helper.h"
|
#include "test_helper.h"
|
||||||
#include "../src/card.h"
|
#include "../src/card.h"
|
||||||
|
|
||||||
void test_initialize_card() {
|
void test_card_init() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
|
|
||||||
assert(card->value == NO_VALUE);
|
assert(card->value == NO_VALUE);
|
||||||
assert(card->suit == NO_SUIT);
|
assert(card->suit == NO_SUIT);
|
||||||
assert(card->face == NO_FACE);
|
assert(card->face == NO_FACE);
|
||||||
|
|
||||||
free_card(card);
|
card_free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_duplicate_card() {
|
void test_card_dup() {
|
||||||
struct card *card_0, *card_1;
|
struct card *card_0, *card_1;
|
||||||
const int begin_y = 5, begin_x = 10;
|
const int begin_y = 5, begin_x = 10;
|
||||||
|
|
||||||
allocate_card(&card_0);
|
card_malloc(&card_0);
|
||||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
card_1 = duplicate_card(card_0);
|
card_1 = card_dup(card_0);
|
||||||
|
|
||||||
assert(card_0 != card_1);
|
assert(card_0 != card_1);
|
||||||
assert(cards_equal(card_0, card_1));
|
assert(cards_equal(card_0, card_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_set_card() {
|
void test_card_set() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
int begin_y = 5;
|
int begin_y = 5;
|
||||||
int begin_x = 10;
|
int begin_x = 10;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
|
|
||||||
assert(card->value == ACE);
|
assert(card->value == ACE);
|
||||||
assert(card->suit == SPADES);
|
assert(card->suit == SPADES);
|
||||||
@ -42,37 +42,37 @@ void test_set_card() {
|
|||||||
assert(card->frame->begin_y == begin_y);
|
assert(card->frame->begin_y == begin_y);
|
||||||
assert(card->frame->begin_x == begin_x);
|
assert(card->frame->begin_x == begin_x);
|
||||||
|
|
||||||
free_card(card);
|
card_free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_expose_card() {
|
void test_card_expose() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
expose_card(card);
|
card_expose(card);
|
||||||
|
|
||||||
assert(card->face == EXPOSED);
|
assert(card->face == EXPOSED);
|
||||||
|
|
||||||
free_card(card);
|
card_free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_cover_card() {
|
void test_card_cover() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
cover_card(card);
|
card_cover(card);
|
||||||
|
|
||||||
assert(card->face == COVERED);
|
assert(card->face == COVERED);
|
||||||
|
|
||||||
free_card(card);
|
card_free(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_card() {
|
void test_card() {
|
||||||
test_initialize_card();
|
test_card_init();
|
||||||
test_duplicate_card();
|
test_card_dup();
|
||||||
test_set_card();
|
test_card_set();
|
||||||
test_expose_card();
|
test_card_expose();
|
||||||
test_cover_card();
|
test_card_cover();
|
||||||
}
|
}
|
||||||
|
@ -6,206 +6,206 @@
|
|||||||
void test_valid_move_from_stock_to_stock() {
|
void test_valid_move_from_stock_to_stock() {
|
||||||
struct stack *stock_0, *stock_1;
|
struct stack *stock_0, *stock_1;
|
||||||
|
|
||||||
allocate_stack(&stock_0);
|
stack_malloc(&stock_0);
|
||||||
allocate_stack(&stock_1);
|
stack_malloc(&stock_1);
|
||||||
initialize_stack(stock_0);
|
stack_init(stock_0);
|
||||||
initialize_stack(stock_1);
|
stack_init(stock_1);
|
||||||
set_card(stock_0->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock_0->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
set_card(stock_1->card, KING, HEARTS, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock_1->card, KING, HEARTS, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
assert(!valid_move(stock_0, stock_0));
|
assert(!valid_move(stock_0, stock_0));
|
||||||
assert(!valid_move(stock_0, stock_1));
|
assert(!valid_move(stock_0, stock_1));
|
||||||
assert(!valid_move(stock_1, stock_0));
|
assert(!valid_move(stock_1, stock_0));
|
||||||
assert(!valid_move(stock_1, stock_1));
|
assert(!valid_move(stock_1, stock_1));
|
||||||
free_stack(stock_0);
|
stack_free(stock_0);
|
||||||
free_stack(stock_1);
|
stack_free(stock_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_stock_to_waste_pile() {
|
void test_valid_move_from_stock_to_waste_pile() {
|
||||||
struct stack *stock, *waste_pile;
|
struct stack *stock, *waste_pile;
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
assert(valid_move(stock, waste_pile));
|
assert(valid_move(stock, waste_pile));
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_stock_to_foundation_stacks() {
|
void test_valid_move_from_stock_to_foundation_stacks() {
|
||||||
struct stack *stock, *foundation_stacks[4];
|
struct stack *stock, *foundation_stacks[4];
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
assert(!valid_move(stock, foundation_stacks[i]));
|
assert(!valid_move(stock, foundation_stacks[i]));
|
||||||
}
|
}
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_stock_to_maneuvre_stacks() {
|
void test_valid_move_from_stock_to_maneuvre_stacks() {
|
||||||
struct stack *stock, *maneuvre_stacks[7];
|
struct stack *stock, *maneuvre_stacks[7];
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
assert(!valid_move(stock, maneuvre_stacks[i]));
|
assert(!valid_move(stock, maneuvre_stacks[i]));
|
||||||
}
|
}
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_waste_pile_to_stock() {
|
void test_valid_move_from_waste_pile_to_stock() {
|
||||||
struct stack *stock, *waste_pile;
|
struct stack *stock, *waste_pile;
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
assert(!valid_move(waste_pile, stock));
|
assert(!valid_move(waste_pile, stock));
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_waste_pile_to_waste_pile() {
|
void test_valid_move_from_waste_pile_to_waste_pile() {
|
||||||
struct stack *waste_pile_0, *waste_pile_1;
|
struct stack *waste_pile_0, *waste_pile_1;
|
||||||
|
|
||||||
allocate_stack(&waste_pile_0);
|
stack_malloc(&waste_pile_0);
|
||||||
allocate_stack(&waste_pile_1);
|
stack_malloc(&waste_pile_1);
|
||||||
initialize_stack(waste_pile_0);
|
stack_init(waste_pile_0);
|
||||||
initialize_stack(waste_pile_1);
|
stack_init(waste_pile_1);
|
||||||
set_card(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
set_card(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
assert(!valid_move(waste_pile_0, waste_pile_0));
|
assert(!valid_move(waste_pile_0, waste_pile_0));
|
||||||
assert(!valid_move(waste_pile_0, waste_pile_1));
|
assert(!valid_move(waste_pile_0, waste_pile_1));
|
||||||
assert(!valid_move(waste_pile_1, waste_pile_0));
|
assert(!valid_move(waste_pile_1, waste_pile_0));
|
||||||
assert(!valid_move(waste_pile_1, waste_pile_1));
|
assert(!valid_move(waste_pile_1, waste_pile_1));
|
||||||
free_stack(waste_pile_0);
|
stack_free(waste_pile_0);
|
||||||
free_stack(waste_pile_1);
|
stack_free(waste_pile_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_waste_pile_to_foundation_stacks() {
|
void test_valid_move_from_waste_pile_to_foundation_stacks() {
|
||||||
struct stack *waste_pile, *foundation_stacks[4];
|
struct stack *waste_pile, *foundation_stacks[4];
|
||||||
|
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
assert(valid_move(waste_pile, foundation_stacks[i]));
|
assert(valid_move(waste_pile, foundation_stacks[i]));
|
||||||
}
|
}
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_waste_pile_to_maneuvre_stacks() {
|
void test_valid_move_from_waste_pile_to_maneuvre_stacks() {
|
||||||
struct stack *waste_pile, *maneuvre_stacks[7];
|
struct stack *waste_pile, *maneuvre_stacks[7];
|
||||||
|
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
assert(valid_move(waste_pile, maneuvre_stacks[i]));
|
assert(valid_move(waste_pile, maneuvre_stacks[i]));
|
||||||
}
|
}
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_foundation_stack_to_stock() {
|
void test_valid_move_from_foundation_stack_to_stock() {
|
||||||
struct stack *stock, *foundation_stacks[4];
|
struct stack *stock, *foundation_stacks[4];
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
assert(!valid_move(foundation_stacks[i], stock));
|
assert(!valid_move(foundation_stacks[i], stock));
|
||||||
}
|
}
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_foundation_stack_to_waste_pile() {
|
void test_valid_move_from_foundation_stack_to_waste_pile() {
|
||||||
struct stack *waste_pile, *foundation_stacks[4];
|
struct stack *waste_pile, *foundation_stacks[4];
|
||||||
|
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
assert(!valid_move(foundation_stacks[i], waste_pile));
|
assert(!valid_move(foundation_stacks[i], waste_pile));
|
||||||
}
|
}
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,13 +213,13 @@ void test_valid_move_from_foundation_stack_to_foundation_stacks() {
|
|||||||
struct stack *foundation_stacks[4];
|
struct stack *foundation_stacks[4];
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
for (int j = 0; j < 4; j++) {
|
for (int j = 0; j < 4; j++) {
|
||||||
if (i == j) {
|
if (i == j) {
|
||||||
@ -230,7 +230,7 @@ void test_valid_move_from_foundation_stack_to_foundation_stacks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,86 +239,86 @@ void test_valid_move_from_foundation_stack_to_maneuvre_stacks() {
|
|||||||
struct stack *maneuvre_stacks[7];
|
struct stack *maneuvre_stacks[7];
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
for (int j = 0; j < 7; j++) {
|
for (int j = 0; j < 7; j++) {
|
||||||
assert(valid_move(foundation_stacks[i], maneuvre_stacks[j]));
|
assert(valid_move(foundation_stacks[i], maneuvre_stacks[j]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_maneuvre_stack_to_stock() {
|
void test_valid_move_from_maneuvre_stack_to_stock() {
|
||||||
struct stack *stock, *maneuvre_stacks[7];
|
struct stack *stock, *maneuvre_stacks[7];
|
||||||
|
|
||||||
allocate_stack(&stock);
|
stack_malloc(&stock);
|
||||||
initialize_stack(stock);
|
stack_init(stock);
|
||||||
set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
card_set(stock->card, ACE, SPADES, EXPOSED, STOCK_BEGIN_Y, STOCK_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
assert(!valid_move(maneuvre_stacks[i], stock));
|
assert(!valid_move(maneuvre_stacks[i], stock));
|
||||||
}
|
}
|
||||||
free_stack(stock);
|
stack_free(stock);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_valid_move_from_maneuvre_stack_to_waste_pile() {
|
void test_valid_move_from_maneuvre_stack_to_waste_pile() {
|
||||||
struct stack *waste_pile, *maneuvre_stacks[7];
|
struct stack *waste_pile, *maneuvre_stacks[7];
|
||||||
|
|
||||||
allocate_stack(&waste_pile);
|
stack_malloc(&waste_pile);
|
||||||
initialize_stack(waste_pile);
|
stack_init(waste_pile);
|
||||||
set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
card_set(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_BEGIN_Y, WASTE_PILE_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
assert(!valid_move(maneuvre_stacks[i], waste_pile));
|
assert(!valid_move(maneuvre_stacks[i], waste_pile));
|
||||||
}
|
}
|
||||||
free_stack(waste_pile);
|
stack_free(waste_pile);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,34 +327,34 @@ void test_valid_move_from_maneuvre_stack_to_foundation_stacks() {
|
|||||||
struct stack *maneuvre_stacks[7];
|
struct stack *maneuvre_stacks[7];
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_stack(&foundation_stacks[i]);
|
stack_malloc(&foundation_stacks[i]);
|
||||||
initialize_stack(foundation_stacks[i]);
|
stack_init(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
card_set(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_0_BEGIN_X);
|
||||||
set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
card_set(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_1_BEGIN_X);
|
||||||
set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
card_set(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_2_BEGIN_X);
|
||||||
set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
card_set(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_BEGIN_Y, FOUNDATION_3_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
for (int j = 0; j < 4; j++) {
|
for (int j = 0; j < 4; j++) {
|
||||||
assert(valid_move(maneuvre_stacks[i], foundation_stacks[j]));
|
assert(valid_move(maneuvre_stacks[i], foundation_stacks[j]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
free_stack(foundation_stacks[i]);
|
stack_free(foundation_stacks[i]);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,16 +362,16 @@ void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() {
|
|||||||
struct stack *maneuvre_stacks[7];
|
struct stack *maneuvre_stacks[7];
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
allocate_stack(&maneuvre_stacks[i]);
|
stack_malloc(&maneuvre_stacks[i]);
|
||||||
initialize_stack(maneuvre_stacks[i]);
|
stack_init(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
card_set(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_2_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
card_set(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_3_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
card_set(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_4_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
card_set(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_5_BEGIN_X);
|
||||||
set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
card_set(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_6_BEGIN_X);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
for (int j = 0; j < 7; j++) {
|
for (int j = 0; j < 7; j++) {
|
||||||
if (i == j) {
|
if (i == j) {
|
||||||
@ -382,23 +382,23 @@ void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
free_stack(maneuvre_stacks[i]);
|
stack_free(maneuvre_stacks[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_move_card_from_empty_stack_to_empty_stack() {
|
void test_move_card_from_stack_empty_stack_to_stack_empty_stack() {
|
||||||
struct stack *origin, *destination,
|
struct stack *origin, *destination,
|
||||||
*new_origin, *new_destination,
|
*new_origin, *new_destination,
|
||||||
*origin_duplicate, *destination_duplicate;
|
*origin_duplicate, *destination_duplicate;
|
||||||
|
|
||||||
allocate_stack(&origin);
|
stack_malloc(&origin);
|
||||||
allocate_stack(&destination);
|
stack_malloc(&destination);
|
||||||
initialize_stack(origin);
|
stack_init(origin);
|
||||||
initialize_stack(destination);
|
stack_init(destination);
|
||||||
new_origin = origin;
|
new_origin = origin;
|
||||||
new_destination = destination;
|
new_destination = destination;
|
||||||
origin_duplicate = duplicate_stack(origin);
|
origin_duplicate = stack_dup(origin);
|
||||||
destination_duplicate = duplicate_stack(destination);
|
destination_duplicate = stack_dup(destination);
|
||||||
move_card(&new_origin, &new_destination);
|
move_card(&new_origin, &new_destination);
|
||||||
|
|
||||||
assert(origin == new_origin);
|
assert(origin == new_origin);
|
||||||
@ -406,29 +406,29 @@ void test_move_card_from_empty_stack_to_empty_stack() {
|
|||||||
assert(destination == new_destination);
|
assert(destination == new_destination);
|
||||||
assert(stacks_equal(destination, destination_duplicate));
|
assert(stacks_equal(destination, destination_duplicate));
|
||||||
|
|
||||||
free_stack(origin);
|
stack_free(origin);
|
||||||
free_stack(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_move_card_from_empty_stack_to_non_empty_stack() {
|
void test_move_card_from_stack_empty_stack_to_non_stack_empty_stack() {
|
||||||
struct stack *origin, *destination,
|
struct stack *origin, *destination,
|
||||||
*new_origin, *new_destination,
|
*new_origin, *new_destination,
|
||||||
*origin_duplicate, *destination_duplicate;
|
*origin_duplicate, *destination_duplicate;
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&origin);
|
stack_malloc(&origin);
|
||||||
allocate_stack(&destination);
|
stack_malloc(&destination);
|
||||||
initialize_stack(origin);
|
stack_init(origin);
|
||||||
initialize_stack(destination);
|
stack_init(destination);
|
||||||
new_origin = origin;
|
new_origin = origin;
|
||||||
new_destination = destination;
|
new_destination = destination;
|
||||||
push(&new_destination, card);
|
stack_push(&new_destination, card);
|
||||||
origin_duplicate = duplicate_stack(origin);
|
origin_duplicate = stack_dup(origin);
|
||||||
destination_duplicate = duplicate_stack(destination);
|
destination_duplicate = stack_dup(destination);
|
||||||
move_card(&new_origin, &new_destination);
|
move_card(&new_origin, &new_destination);
|
||||||
|
|
||||||
assert(origin == new_origin);
|
assert(origin == new_origin);
|
||||||
@ -436,104 +436,104 @@ void test_move_card_from_empty_stack_to_non_empty_stack() {
|
|||||||
assert(destination == new_destination);
|
assert(destination == new_destination);
|
||||||
assert(stacks_equal(destination, destination_duplicate));
|
assert(stacks_equal(destination, destination_duplicate));
|
||||||
|
|
||||||
free_stack(origin);
|
stack_free(origin);
|
||||||
free_stack(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_move_card_from_non_empty_stack_to_empty_stack() {
|
void test_move_card_from_non_stack_empty_stack_to_stack_empty_stack() {
|
||||||
struct stack *origin, *destination;
|
struct stack *origin, *destination;
|
||||||
struct card *card[6];
|
struct card *card[6];
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
card_set(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||||
}
|
}
|
||||||
|
|
||||||
allocate_stack(&origin);
|
stack_malloc(&origin);
|
||||||
allocate_stack(&destination);
|
stack_malloc(&destination);
|
||||||
initialize_stack(origin);
|
stack_init(origin);
|
||||||
initialize_stack(destination);
|
stack_init(destination);
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
push(&origin, card[i]);
|
stack_push(&origin, card[i]);
|
||||||
}
|
}
|
||||||
move_card(&origin, &destination);
|
move_card(&origin, &destination);
|
||||||
|
|
||||||
assert(length(origin) == 5);
|
assert(stack_length(origin) == 5);
|
||||||
assert(length(destination) == 1);
|
assert(stack_length(destination) == 1);
|
||||||
assert(cards_equal(destination->card, card[5]));
|
assert(cards_equal(destination->card, card[5]));
|
||||||
|
|
||||||
move_card(&origin, &destination);
|
move_card(&origin, &destination);
|
||||||
|
|
||||||
assert(length(origin) == 4);
|
assert(stack_length(origin) == 4);
|
||||||
assert(length(destination) == 2);
|
assert(stack_length(destination) == 2);
|
||||||
assert(cards_equal(destination->card, card[4]));
|
assert(cards_equal(destination->card, card[4]));
|
||||||
|
|
||||||
move_card(&origin, &destination);
|
move_card(&origin, &destination);
|
||||||
|
|
||||||
assert(length(origin) == 3);
|
assert(stack_length(origin) == 3);
|
||||||
assert(length(destination) == 3);
|
assert(stack_length(destination) == 3);
|
||||||
assert(cards_equal(destination->card, card[3]));
|
assert(cards_equal(destination->card, card[3]));
|
||||||
|
|
||||||
free_stack(origin);
|
stack_free(origin);
|
||||||
free_stack(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_move_card_from_non_empty_stack_to_non_empty_stack() {
|
void test_move_card_from_non_stack_empty_stack_to_non_stack_empty_stack() {
|
||||||
struct stack *origin, *destination;
|
struct stack *origin, *destination;
|
||||||
struct card *card[6];
|
struct card *card[6];
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
card_set(card[i], TWO + i, i % 5, i % 2, 99, 99);
|
||||||
}
|
}
|
||||||
|
|
||||||
allocate_stack(&origin);
|
stack_malloc(&origin);
|
||||||
allocate_stack(&destination);
|
stack_malloc(&destination);
|
||||||
initialize_stack(origin);
|
stack_init(origin);
|
||||||
initialize_stack(destination);
|
stack_init(destination);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
push(&origin, card[i]);
|
stack_push(&origin, card[i]);
|
||||||
}
|
}
|
||||||
for (int i = 3; i < 6; i++) {
|
for (int i = 3; i < 6; i++) {
|
||||||
push(&destination, card[i]);
|
stack_push(&destination, card[i]);
|
||||||
}
|
}
|
||||||
move_card(&origin, &destination);
|
move_card(&origin, &destination);
|
||||||
|
|
||||||
assert(length(origin) == 2);
|
assert(stack_length(origin) == 2);
|
||||||
assert(length(destination) == 4);
|
assert(stack_length(destination) == 4);
|
||||||
assert(cards_equal(destination->card, card[2]));
|
assert(cards_equal(destination->card, card[2]));
|
||||||
assert(cards_equal(destination->next->card, card[5]));
|
assert(cards_equal(destination->next->card, card[5]));
|
||||||
|
|
||||||
free_stack(origin);
|
stack_free(origin);
|
||||||
free_stack(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_move_card_should_not_change_empty_stack_coordinates() {
|
void test_move_card_should_not_change_stack_empty_stack_coordinates() {
|
||||||
struct stack *origin, *destination;
|
struct stack *origin, *destination;
|
||||||
struct card *card[2];
|
struct card *card[2];
|
||||||
|
|
||||||
allocate_card(&card[0]);
|
card_malloc(&card[0]);
|
||||||
allocate_card(&card[1]);
|
card_malloc(&card[1]);
|
||||||
initialize_card(card[0]);
|
card_init(card[0]);
|
||||||
initialize_card(card[1]);
|
card_init(card[1]);
|
||||||
set_card(card[0], ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
card_set(card[0], ACE, SPADES, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_0_BEGIN_X);
|
||||||
set_card(card[1], KING, HEARTS, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
card_set(card[1], KING, HEARTS, EXPOSED, MANEUVRE_BEGIN_Y, MANEUVRE_1_BEGIN_X);
|
||||||
|
|
||||||
allocate_stack(&origin);
|
stack_malloc(&origin);
|
||||||
allocate_stack(&destination);
|
stack_malloc(&destination);
|
||||||
initialize_stack(origin);
|
stack_init(origin);
|
||||||
initialize_stack(destination);
|
stack_init(destination);
|
||||||
push(&origin, card[0]);
|
stack_push(&origin, card[0]);
|
||||||
push(&destination, card[1]);
|
stack_push(&destination, card[1]);
|
||||||
move_card(&origin, &destination);
|
move_card(&origin, &destination);
|
||||||
|
|
||||||
assert(origin->card->frame->begin_y == MANEUVRE_BEGIN_Y);
|
assert(origin->card->frame->begin_y == MANEUVRE_BEGIN_Y);
|
||||||
assert(origin->card->frame->begin_x == MANEUVRE_0_BEGIN_X);
|
assert(origin->card->frame->begin_x == MANEUVRE_0_BEGIN_X);
|
||||||
|
|
||||||
free_stack(origin);
|
stack_free(origin);
|
||||||
free_stack(destination);
|
stack_free(destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_game() {
|
void test_game() {
|
||||||
@ -554,9 +554,9 @@ void test_game() {
|
|||||||
test_valid_move_from_maneuvre_stack_to_foundation_stacks();
|
test_valid_move_from_maneuvre_stack_to_foundation_stacks();
|
||||||
test_valid_move_from_maneuvre_stack_to_maneuvre_stacks();
|
test_valid_move_from_maneuvre_stack_to_maneuvre_stacks();
|
||||||
|
|
||||||
test_move_card_from_empty_stack_to_empty_stack();
|
test_move_card_from_stack_empty_stack_to_stack_empty_stack();
|
||||||
test_move_card_from_empty_stack_to_non_empty_stack();
|
test_move_card_from_stack_empty_stack_to_non_stack_empty_stack();
|
||||||
test_move_card_from_non_empty_stack_to_empty_stack();
|
test_move_card_from_non_stack_empty_stack_to_stack_empty_stack();
|
||||||
test_move_card_from_non_empty_stack_to_non_empty_stack();
|
test_move_card_from_non_stack_empty_stack_to_non_stack_empty_stack();
|
||||||
test_move_card_should_not_change_empty_stack_coordinates();
|
test_move_card_should_not_change_stack_empty_stack_coordinates();
|
||||||
}
|
}
|
||||||
|
@ -2,314 +2,314 @@
|
|||||||
#include "test_helper.h"
|
#include "test_helper.h"
|
||||||
#include "../src/stack.h"
|
#include "../src/stack.h"
|
||||||
|
|
||||||
void test_initialize_stack() {
|
void test_stack_init() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
|
|
||||||
assert(stack->card->value == NO_VALUE);
|
assert(stack->card->value == NO_VALUE);
|
||||||
assert(!stack->next);
|
assert(!stack->next);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_duplicate_stack() {
|
void test_stack_dup() {
|
||||||
struct stack *stack_0, *stack_1;
|
struct stack *stack_0, *stack_1;
|
||||||
struct card *card[5];
|
struct card *card[5];
|
||||||
const int begin_y = 5, begin_x = 10;
|
const int begin_y = 5, begin_x = 10;
|
||||||
|
|
||||||
allocate_stack(&stack_0);
|
stack_malloc(&stack_0);
|
||||||
initialize_stack(stack_0);
|
stack_init(stack_0);
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
set_card(card[i], i, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card[i], i, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
push(&stack_0, card[i]);
|
stack_push(&stack_0, card[i]);
|
||||||
}
|
}
|
||||||
stack_1 = duplicate_stack(stack_0);
|
stack_1 = stack_dup(stack_0);
|
||||||
|
|
||||||
assert(stack_0 != stack_1);
|
assert(stack_0 != stack_1);
|
||||||
assert(stacks_equal(stack_0, stack_1));
|
assert(stacks_equal(stack_0, stack_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_empty_on_empty_stack() {
|
void test_stack_empty_on_stack_empty_stack() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
|
|
||||||
assert(empty(stack));
|
assert(stack_empty(stack));
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_empty_on_non_empty_stack() {
|
void test_stack_empty_on_non_stack_empty_stack() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
push(&stack, card);
|
stack_push(&stack, card);
|
||||||
|
|
||||||
assert(!empty(stack));
|
assert(!stack_empty(stack));
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_length() {
|
void test_stack_length() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *card[4];
|
struct card *card[4];
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
|
|
||||||
assert(length(stack) == 0);
|
assert(stack_length(stack) == 0);
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], i, SPADES, EXPOSED, 0, 0);
|
card_set(card[i], i, SPADES, EXPOSED, 0, 0);
|
||||||
push(&stack, card[i]);
|
stack_push(&stack, card[i]);
|
||||||
assert(length(stack) == i + 1);
|
assert(stack_length(stack) == i + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_push_on_empty_stack() {
|
void test_stack_push_on_stack_empty_stack() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
push(&stack, card);
|
stack_push(&stack, card);
|
||||||
|
|
||||||
assert(cards_equal(stack->card, card));
|
assert(cards_equal(stack->card, card));
|
||||||
assert(!stack->next);
|
assert(!stack->next);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_push_on_non_empty_stack() {
|
void test_stack_push_on_non_stack_empty_stack() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *card_0, *card_1;
|
struct card *card_0, *card_1;
|
||||||
|
|
||||||
allocate_card(&card_0);
|
card_malloc(&card_0);
|
||||||
allocate_card(&card_1);
|
card_malloc(&card_1);
|
||||||
initialize_card(card_0);
|
card_init(card_0);
|
||||||
initialize_card(card_1);
|
card_init(card_1);
|
||||||
set_card(card_0, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card_0, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
set_card(card_1, ACE, HEARTS, EXPOSED, 0, 0);
|
card_set(card_1, ACE, HEARTS, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
push(&stack, card_0);
|
stack_push(&stack, card_0);
|
||||||
push(&stack, card_1);
|
stack_push(&stack, card_1);
|
||||||
|
|
||||||
assert(cards_equal(stack->card, card_1));
|
assert(cards_equal(stack->card, card_1));
|
||||||
assert(cards_equal(stack->next->card, card_0));
|
assert(cards_equal(stack->next->card, card_0));
|
||||||
assert(!stack->next->next);
|
assert(!stack->next->next);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_push_null_on_empty_stack() {
|
void test_stack_push_null_on_stack_empty_stack() {
|
||||||
struct stack *stack, *old_stack;
|
struct stack *stack, *old_stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
push(&stack, NULL);
|
stack_push(&stack, NULL);
|
||||||
|
|
||||||
assert(cards_equal(stack->card, old_stack->card));
|
assert(cards_equal(stack->card, old_stack->card));
|
||||||
assert(!stack->next);
|
assert(!stack->next);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_push_null_on_non_empty_stack() {
|
void test_stack_push_null_on_non_stack_empty_stack() {
|
||||||
struct stack *stack, *old_stack;
|
struct stack *stack, *old_stack;
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
old_stack = duplicate_stack(stack);
|
old_stack = stack_dup(stack);
|
||||||
push(&stack, NULL);
|
stack_push(&stack, NULL);
|
||||||
|
|
||||||
assert(cards_equal(stack->card, old_stack->card));
|
assert(cards_equal(stack->card, old_stack->card));
|
||||||
assert(stacks_equal(stack->next, old_stack->next));
|
assert(stacks_equal(stack->next, old_stack->next));
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pop_on_empty_stack() {
|
void test_stack_pop_on_stack_empty_stack() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *popped_card;
|
struct card *stack_popped_card;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
popped_card = pop(&stack);
|
stack_popped_card = stack_pop(&stack);
|
||||||
|
|
||||||
assert(empty(stack));
|
assert(stack_empty(stack));
|
||||||
assert(!popped_card);
|
assert(!stack_popped_card);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pop_on_stack_with_one_element() {
|
void test_stack_pop_on_stack_with_one_element() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
struct card *card, *popped_card;
|
struct card *card, *stack_popped_card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
push(&stack, card);
|
stack_push(&stack, card);
|
||||||
popped_card = pop(&stack);
|
stack_popped_card = stack_pop(&stack);
|
||||||
|
|
||||||
assert(empty(stack));
|
assert(stack_empty(stack));
|
||||||
assert(popped_card == card);
|
assert(stack_popped_card == card);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pop_on_stack_with_more_than_one_element() {
|
void test_stack_pop_on_stack_with_more_than_one_element() {
|
||||||
struct stack *stack, *old_stack_next;
|
struct stack *stack, *old_stack_next;
|
||||||
struct card *card[3], *popped_card;
|
struct card *card[3], *stack_popped_card;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card[i], ACE, SPADES, EXPOSED, 0, 0);
|
||||||
push(&stack, card[i]);
|
stack_push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack_next = stack->next;
|
old_stack_next = stack->next;
|
||||||
popped_card = pop(&stack);
|
stack_popped_card = stack_pop(&stack);
|
||||||
|
|
||||||
assert(length(stack) == 2);
|
assert(stack_length(stack) == 2);
|
||||||
assert(stack == old_stack_next);
|
assert(stack == old_stack_next);
|
||||||
assert(popped_card == card[2]);
|
assert(stack_popped_card == card[2]);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_reverse_on_empty_stack() {
|
void test_stack_reverse_on_stack_empty_stack() {
|
||||||
struct stack *stack, *old_stack, *reversed_stack;
|
struct stack *stack, *old_stack, *stack_reversed_stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
reversed_stack = reverse(stack);
|
stack_reversed_stack = stack_reverse(stack);
|
||||||
|
|
||||||
assert(reversed_stack == old_stack);
|
assert(stack_reversed_stack == old_stack);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_reverse_on_stack_with_one_element() {
|
void test_stack_reverse_on_stack_with_one_element() {
|
||||||
struct stack *stack, *old_stack, *reversed_stack;
|
struct stack *stack, *old_stack, *stack_reversed_stack;
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
initialize_card(card);
|
card_init(card);
|
||||||
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
card_set(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
push(&stack, card);
|
stack_push(&stack, card);
|
||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
reversed_stack = reverse(stack);
|
stack_reversed_stack = stack_reverse(stack);
|
||||||
|
|
||||||
assert(reversed_stack == old_stack);
|
assert(stack_reversed_stack == old_stack);
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_reverse_on_stack_with_more_than_one_element() {
|
void test_stack_reverse_on_stack_with_more_than_one_element() {
|
||||||
struct stack *stack, *old_stack, *reversed_stack, *unreversed_stack;
|
struct stack *stack, *old_stack, *stack_reversed_stack, *unstack_reversed_stack;
|
||||||
struct card *card[3];
|
struct card *card[3];
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||||
push(&stack, card[i]);
|
stack_push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack = duplicate_stack(stack);
|
old_stack = stack_dup(stack);
|
||||||
reversed_stack = reverse(stack);
|
stack_reversed_stack = stack_reverse(stack);
|
||||||
|
|
||||||
allocate_stack(&unreversed_stack);
|
stack_malloc(&unstack_reversed_stack);
|
||||||
initialize_stack(unreversed_stack);
|
stack_init(unstack_reversed_stack);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
push(&unreversed_stack, pop(&reversed_stack));
|
stack_push(&unstack_reversed_stack, stack_pop(&stack_reversed_stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(stacks_equal(unreversed_stack, old_stack));
|
assert(stacks_equal(unstack_reversed_stack, old_stack));
|
||||||
|
|
||||||
free_stack(reversed_stack);
|
stack_free(stack_reversed_stack);
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_reverse_should_not_change_stack() {
|
void test_stack_reverse_should_not_change_stack() {
|
||||||
struct stack *stack, *old_stack, *old_stack_address;
|
struct stack *stack, *old_stack, *old_stack_address;
|
||||||
struct card *card[3];
|
struct card *card[3];
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
initialize_stack(stack);
|
stack_init(stack);
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
allocate_card(&card[i]);
|
card_malloc(&card[i]);
|
||||||
initialize_card(card[i]);
|
card_init(card[i]);
|
||||||
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
card_set(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||||
push(&stack, card[i]);
|
stack_push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack_address = stack;
|
old_stack_address = stack;
|
||||||
old_stack = duplicate_stack(stack);
|
old_stack = stack_dup(stack);
|
||||||
reverse(stack);
|
stack_reverse(stack);
|
||||||
|
|
||||||
assert(stack == old_stack_address);
|
assert(stack == old_stack_address);
|
||||||
assert(stacks_equal(stack, old_stack));
|
assert(stacks_equal(stack, old_stack));
|
||||||
|
|
||||||
free_stack(stack);
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_stack() {
|
void test_stack() {
|
||||||
test_initialize_stack();
|
test_stack_init();
|
||||||
|
|
||||||
test_duplicate_stack();
|
test_stack_dup();
|
||||||
|
|
||||||
test_empty_on_empty_stack();
|
test_stack_empty_on_stack_empty_stack();
|
||||||
test_empty_on_non_empty_stack();
|
test_stack_empty_on_non_stack_empty_stack();
|
||||||
|
|
||||||
test_length();
|
test_stack_length();
|
||||||
|
|
||||||
test_push_on_empty_stack();
|
test_stack_push_on_stack_empty_stack();
|
||||||
test_push_on_non_empty_stack();
|
test_stack_push_on_non_stack_empty_stack();
|
||||||
test_push_null_on_empty_stack();
|
test_stack_push_null_on_stack_empty_stack();
|
||||||
|
|
||||||
test_pop_on_empty_stack();
|
test_stack_pop_on_stack_empty_stack();
|
||||||
test_pop_on_stack_with_one_element();
|
test_stack_pop_on_stack_with_one_element();
|
||||||
test_pop_on_stack_with_more_than_one_element();
|
test_stack_pop_on_stack_with_more_than_one_element();
|
||||||
|
|
||||||
test_reverse_on_empty_stack();
|
test_stack_reverse_on_stack_empty_stack();
|
||||||
test_reverse_on_stack_with_one_element();
|
test_stack_reverse_on_stack_with_one_element();
|
||||||
test_reverse_on_stack_with_more_than_one_element();
|
test_stack_reverse_on_stack_with_more_than_one_element();
|
||||||
test_reverse_should_not_change_stack();
|
test_stack_reverse_should_not_change_stack();
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ void test_cards_equal_with_two_nulls() {
|
|||||||
void test_cards_equal_with_one_null() {
|
void test_cards_equal_with_one_null() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
assert(!cards_equal(card, NULL));
|
assert(!cards_equal(card, NULL));
|
||||||
assert(!cards_equal(NULL, card));
|
assert(!cards_equal(NULL, card));
|
||||||
}
|
}
|
||||||
@ -49,10 +49,10 @@ void test_cards_equal_with_two_equivalent_cards() {
|
|||||||
struct card *card_0, *card_1;
|
struct card *card_0, *card_1;
|
||||||
const int begin_y = 5, begin_x = 10;
|
const int begin_y = 5, begin_x = 10;
|
||||||
|
|
||||||
allocate_card(&card_0);
|
card_malloc(&card_0);
|
||||||
allocate_card(&card_1);
|
card_malloc(&card_1);
|
||||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
set_card(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
|
|
||||||
assert(cards_equal(card_0, card_1));
|
assert(cards_equal(card_0, card_1));
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ void test_cards_equal_with_two_equivalent_cards() {
|
|||||||
void test_cards_equal_with_two_card_pointers_to_the_same_address() {
|
void test_cards_equal_with_two_card_pointers_to_the_same_address() {
|
||||||
struct card *card;
|
struct card *card;
|
||||||
|
|
||||||
allocate_card(&card);
|
card_malloc(&card);
|
||||||
|
|
||||||
assert(cards_equal(card, card));
|
assert(cards_equal(card, card));
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ void test_stacks_equal_with_two_nulls() {
|
|||||||
void test_stacks_equal_with_one_null() {
|
void test_stacks_equal_with_one_null() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
assert(!stacks_equal(stack, NULL));
|
assert(!stacks_equal(stack, NULL));
|
||||||
assert(!stacks_equal(NULL, stack));
|
assert(!stacks_equal(NULL, stack));
|
||||||
}
|
}
|
||||||
@ -82,14 +82,14 @@ void test_stacks_equal_with_two_equivalent_stacks() {
|
|||||||
struct card *card_0, *card_1;
|
struct card *card_0, *card_1;
|
||||||
const int begin_y = 5, begin_x = 10;
|
const int begin_y = 5, begin_x = 10;
|
||||||
|
|
||||||
allocate_card(&card_0);
|
card_malloc(&card_0);
|
||||||
allocate_card(&card_1);
|
card_malloc(&card_1);
|
||||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
set_card(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_1, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
allocate_stack(&stack_0);
|
stack_malloc(&stack_0);
|
||||||
allocate_stack(&stack_1);
|
stack_malloc(&stack_1);
|
||||||
push(&stack_0, card_0);
|
stack_push(&stack_0, card_0);
|
||||||
push(&stack_1, card_1);
|
stack_push(&stack_1, card_1);
|
||||||
|
|
||||||
assert(stacks_equal(stack_0, stack_1));
|
assert(stacks_equal(stack_0, stack_1));
|
||||||
}
|
}
|
||||||
@ -99,14 +99,14 @@ void test_stacks_equal_with_two_different_stacks() {
|
|||||||
struct card *card_0, *card_1;
|
struct card *card_0, *card_1;
|
||||||
const int begin_y = 5, begin_x = 10;
|
const int begin_y = 5, begin_x = 10;
|
||||||
|
|
||||||
allocate_card(&card_0);
|
card_malloc(&card_0);
|
||||||
allocate_card(&card_1);
|
card_malloc(&card_1);
|
||||||
set_card(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
card_set(card_0, ACE, SPADES, EXPOSED, begin_y, begin_x);
|
||||||
set_card(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
|
card_set(card_1, KING, HEARTS, EXPOSED, begin_y, begin_x);
|
||||||
allocate_stack(&stack_0);
|
stack_malloc(&stack_0);
|
||||||
allocate_stack(&stack_1);
|
stack_malloc(&stack_1);
|
||||||
push(&stack_0, card_0);
|
stack_push(&stack_0, card_0);
|
||||||
push(&stack_1, card_1);
|
stack_push(&stack_1, card_1);
|
||||||
|
|
||||||
assert(!stacks_equal(stack_0, stack_1));
|
assert(!stacks_equal(stack_0, stack_1));
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ void test_stacks_equal_with_two_different_stacks() {
|
|||||||
void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
|
void test_stacks_equal_with_two_stack_pointers_to_the_same_address() {
|
||||||
struct stack *stack;
|
struct stack *stack;
|
||||||
|
|
||||||
allocate_stack(&stack);
|
stack_malloc(&stack);
|
||||||
|
|
||||||
assert(stacks_equal(stack, stack));
|
assert(stacks_equal(stack, stack));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user