From e7706dfa4b5a391a20b8c23e6b6a807453bfda1b Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sat, 7 May 2011 23:09:39 -0300 Subject: [PATCH] Implemented game#valid_move. --- lib/display.c | 13 +- lib/game.c | 71 +++++++- lib/game.h | 2 + lib/keyboard.c | 15 +- test/card_test.c | 1 + test/frame_test.c | 1 + test/game_test.c | 404 +++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 488 insertions(+), 19 deletions(-) diff --git a/lib/display.c b/lib/display.c index 2851349..4bf13f8 100644 --- a/lib/display.c +++ b/lib/display.c @@ -66,8 +66,6 @@ void erase_stack(struct stack *stack) { box(empty_stack, 0, 0); wrefresh(empty_stack); delwin(empty_stack); - - return; } void draw_empty_stacks() { @@ -198,7 +196,16 @@ void draw_card(struct card *card) { } void draw_stack(struct stack *stack) { - if (!empty(stack)) { + erase_stack(stack); + if (empty(stack)) { + WINDOW *empty_stack = newwin(FRAME_HEIGHT, + FRAME_WIDTH, + stack->card->frame->start_y, + stack->card->frame->start_x); + box(empty_stack, 0, 0); + wrefresh(empty_stack); + delwin(empty_stack); + } else { if (maneuvre_stack(stack)) { struct stack *reversed_stack = reverse(stack); diff --git a/lib/game.c b/lib/game.c index c2d9d2a..73ce95f 100644 --- a/lib/game.c +++ b/lib/game.c @@ -8,23 +8,82 @@ #include "util.h" #include "game.h" +bool stock_stack(struct stack *stack) { + return(stack && stack->card && stack->card->frame && + (stack->card->frame->start_y == STOCK_STARTING_Y) && + (stack->card->frame->start_x == STOCK_STARTING_X)); +} + +bool waste_pile_stack(struct stack *stack) { + return(stack && stack->card && stack->card->frame && + (stack->card->frame->start_y == WASTE_PILE_STARTING_Y) && + (stack->card->frame->start_x == WASTE_PILE_STARTING_X)); +} + +bool foundation_stack(struct stack *stack) { + return(stack && stack->card && stack->card->frame && + stack->card->frame->start_y == FOUNDATION_STARTING_Y && + (stack->card->frame->start_x == FOUNDATION_0_STARTING_X || + stack->card->frame->start_x == FOUNDATION_1_STARTING_X || + stack->card->frame->start_x == FOUNDATION_2_STARTING_X || + stack->card->frame->start_x == FOUNDATION_3_STARTING_X)); +} + bool maneuvre_stack(struct stack *stack) { - return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y); + return(stack && stack->card && stack->card->frame && + stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y && + (stack->card->frame->start_x == MANEUVRE_0_STARTING_X || + stack->card->frame->start_x == MANEUVRE_1_STARTING_X || + stack->card->frame->start_x == MANEUVRE_2_STARTING_X || + stack->card->frame->start_x == MANEUVRE_3_STARTING_X || + stack->card->frame->start_x == MANEUVRE_4_STARTING_X || + stack->card->frame->start_x == MANEUVRE_5_STARTING_X || + stack->card->frame->start_x == MANEUVRE_6_STARTING_X)); +} + +bool valid_move(struct stack *origin, struct stack *destination) { + if (stock_stack(origin)) { + if (waste_pile_stack(destination)) { + return(true); + } else { + return(false); + } + } else if (waste_pile_stack(origin)) { + if (foundation_stack(destination) || maneuvre_stack(destination)) { + return(true); + } else { + return(false); + } + } else if (foundation_stack(origin)) { + if ((foundation_stack(destination) && origin != destination) || maneuvre_stack(destination)) { + return(true); + } else { + return(false); + } + } else if (maneuvre_stack(origin)) { + if ((maneuvre_stack(destination) && origin != destination) || foundation_stack(destination)) { + return(true); + } else { + return(false); + } + } else { + return(false); + } } void move_card(struct stack **origin, struct stack **destination) { - struct stack *stack = NULL; + struct stack *stack; - (*origin)->card->frame->start_x = (*destination)->card->frame->start_x; - (*origin)->card->frame->start_y = (*destination)->card->frame->start_y; + if (!empty(*origin)) { + (*origin)->card->frame->start_x = (*destination)->card->frame->start_x; + (*origin)->card->frame->start_y = (*destination)->card->frame->start_y; + } if (!empty(*destination) && maneuvre_stack(*destination)) { (*origin)->card->frame->start_y++; } if ((stack = pop(origin))) { push(destination, stack->card); } - - return; } static void set_stacks_initial_coordinates(struct deck *deck) { diff --git a/lib/game.h b/lib/game.h index 77be4d1..cda2071 100644 --- a/lib/game.h +++ b/lib/game.h @@ -34,6 +34,8 @@ extern const char *program_name; struct deck *deck; struct cursor *cursor; +bool valid_move(struct stack *, struct stack *); +bool maneuvre_stack(struct stack *); void move_card(struct stack **, struct stack **); void greet_player(); void initialize_game(); diff --git a/lib/keyboard.c b/lib/keyboard.c index 5f38845..aaf32e5 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -1,3 +1,4 @@ +#include #include "card.h" #include "game.h" #include "display.h" @@ -90,19 +91,19 @@ static void handle_card_movement(struct cursor *cursor) { move_cursor(cursor, RIGHT); break; case KEY_SPACEBAR: - if (!cursor_on_invalid_spot(cursor) && - !cursor_on_stock(cursor) && - !cursor_on_stack(cursor, origin)) { - destination = cursor_stack(cursor); + destination = cursor_stack(cursor); + if (valid_move(origin, destination)) { move_card(&origin, &destination); draw_stack(origin); draw_stack(destination); } return; + case 'q': + case 'Q': + end_curses(); + exit(0); } } - - return; } void handle_keyboard_event(int key) { @@ -131,6 +132,4 @@ void handle_keyboard_event(int key) { } break; } - - return; } diff --git a/test/card_test.c b/test/card_test.c index 38235d7..3831d40 100644 --- a/test/card_test.c +++ b/test/card_test.c @@ -1,4 +1,5 @@ #include +#include "test_helper.h" #include "../lib/card.h" void test_initialize_card() { diff --git a/test/frame_test.c b/test/frame_test.c index 53b9b6b..71bc9df 100644 --- a/test/frame_test.c +++ b/test/frame_test.c @@ -1,4 +1,5 @@ #include +#include "test_helper.h" #include "../lib/frame.h" void test_initialize_frame() { diff --git a/test/game_test.c b/test/game_test.c index afb08ef..b2bc87d 100644 --- a/test/game_test.c +++ b/test/game_test.c @@ -94,8 +94,8 @@ void test_move_card_from_non_empty_stack_to_non_empty_stack() { allocate_card(&card[1]); initialize_card(card[0]); initialize_card(card[1]); - set_card(card[0], ACE, SPADES, EXPOSED, 1, 1); - set_card(card[1], KING, HEARTS, EXPOSED, 1, 1); + set_card(card[0], ACE, SPADES, EXPOSED, 99, 99); + set_card(card[1], KING, HEARTS, EXPOSED, 99, 99); allocate_stack(&origin); allocate_stack(&destination); @@ -116,7 +116,407 @@ void test_move_card_from_non_empty_stack_to_non_empty_stack() { return; } +void test_valid_move_from_stock_to_stock() { + struct stack *stock_0, *stock_1; + + allocate_stack(&stock_0); + allocate_stack(&stock_1); + initialize_stack(stock_0); + initialize_stack(stock_1); + set_card(stock_0->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + set_card(stock_1->card, KING, HEARTS, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + assert(!valid_move(stock_0, stock_0)); + assert(!valid_move(stock_0, stock_1)); + assert(!valid_move(stock_1, stock_0)); + assert(!valid_move(stock_1, stock_1)); + free_stack(stock_0); + free_stack(stock_1); +} + +void test_valid_move_from_stock_to_waste_pile() { + struct stack *stock, *waste_pile; + + allocate_stack(&stock); + allocate_stack(&waste_pile); + initialize_stack(stock); + initialize_stack(waste_pile); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + assert(valid_move(stock, waste_pile)); + free_stack(stock); + free_stack(waste_pile); +} + +void test_valid_move_from_stock_to_foundation_stacks() { + struct stack *stock, *foundation_stacks[4]; + + allocate_stack(&stock); + initialize_stack(stock); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 4; i++) { + assert(!valid_move(stock, foundation_stacks[i])); + } + free_stack(stock); + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } +} + +void test_valid_move_from_stock_to_maneuvre_stacks() { + struct stack *stock, *maneuvre_stacks[7]; + + allocate_stack(&stock); + initialize_stack(stock); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + assert(!valid_move(stock, maneuvre_stacks[i])); + } + free_stack(stock); + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_waste_pile_to_stock() { + struct stack *stock, *waste_pile; + + allocate_stack(&stock); + allocate_stack(&waste_pile); + initialize_stack(stock); + initialize_stack(waste_pile); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + set_card(waste_pile->card, KING, HEARTS, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + assert(!valid_move(waste_pile, stock)); + free_stack(stock); + free_stack(waste_pile); +} + +void test_valid_move_from_waste_pile_to_waste_pile() { + struct stack *waste_pile_0, *waste_pile_1; + + allocate_stack(&waste_pile_0); + allocate_stack(&waste_pile_1); + initialize_stack(waste_pile_0); + initialize_stack(waste_pile_1); + set_card(waste_pile_0->card, ACE, SPADES, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + set_card(waste_pile_1->card, KING, HEARTS, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + assert(!valid_move(waste_pile_0, waste_pile_0)); + 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_1)); + free_stack(waste_pile_0); + free_stack(waste_pile_1); +} + +void test_valid_move_from_waste_pile_to_foundation_stacks() { + struct stack *waste_pile, *foundation_stacks[4]; + + allocate_stack(&waste_pile); + initialize_stack(waste_pile); + set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 4; i++) { + assert(valid_move(waste_pile, foundation_stacks[i])); + } + free_stack(waste_pile); + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } +} + +void test_valid_move_from_waste_pile_to_maneuvre_stacks() { + struct stack *waste_pile, *maneuvre_stacks[7]; + + allocate_stack(&waste_pile); + initialize_stack(waste_pile); + set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + assert(valid_move(waste_pile, maneuvre_stacks[i])); + } + free_stack(waste_pile); + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_foundation_stack_to_stock() { + struct stack *stock, *foundation_stacks[4]; + + allocate_stack(&stock); + initialize_stack(stock); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 4; i++) { + assert(!valid_move(foundation_stacks[i], stock)); + } + free_stack(stock); + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } +} + +void test_valid_move_from_foundation_stack_to_waste_pile() { + struct stack *waste_pile, *foundation_stacks[4]; + + allocate_stack(&waste_pile); + initialize_stack(waste_pile); + set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 4; i++) { + assert(!valid_move(foundation_stacks[i], waste_pile)); + } + free_stack(waste_pile); + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } +} + +void test_valid_move_from_foundation_stack_to_foundation_stacks() { + struct stack *foundation_stacks[4]; + + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (i == j) { + assert(!valid_move(foundation_stacks[i], foundation_stacks[j])); + } else { + assert(valid_move(foundation_stacks[i], foundation_stacks[j])); + } + } + } + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } +} + +void test_valid_move_from_foundation_stack_to_maneuvre_stacks() { + struct stack *foundation_stacks[4]; + struct stack *maneuvre_stacks[7]; + + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 7; j++) { + assert(valid_move(foundation_stacks[i], maneuvre_stacks[j])); + } + } + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_maneuvre_stack_to_stock() { + struct stack *stock, *maneuvre_stacks[7]; + + allocate_stack(&stock); + initialize_stack(stock); + set_card(stock->card, ACE, SPADES, EXPOSED, STOCK_STARTING_Y, STOCK_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + assert(!valid_move(maneuvre_stacks[i], stock)); + } + free_stack(stock); + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_maneuvre_stack_to_waste_pile() { + struct stack *waste_pile, *maneuvre_stacks[7]; + + allocate_stack(&waste_pile); + initialize_stack(waste_pile); + set_card(waste_pile->card, ACE, SPADES, EXPOSED, WASTE_PILE_STARTING_Y, WASTE_PILE_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + assert(!valid_move(maneuvre_stacks[i], waste_pile)); + } + free_stack(waste_pile); + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_maneuvre_stack_to_foundation_stacks() { + struct stack *foundation_stacks[4]; + struct stack *maneuvre_stacks[7]; + + for (int i = 0; i < 4; i++) { + allocate_stack(&foundation_stacks[i]); + initialize_stack(foundation_stacks[i]); + } + set_card(foundation_stacks[0]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_0_STARTING_X); + set_card(foundation_stacks[1]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_1_STARTING_X); + set_card(foundation_stacks[2]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_2_STARTING_X); + set_card(foundation_stacks[3]->card, ACE, SPADES, EXPOSED, FOUNDATION_STARTING_Y, FOUNDATION_3_STARTING_X); + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + for (int j = 0; j < 4; j++) { + assert(valid_move(maneuvre_stacks[i], foundation_stacks[j])); + } + } + for (int i = 0; i < 4; i++) { + free_stack(foundation_stacks[i]); + } + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + +void test_valid_move_from_maneuvre_stack_to_maneuvre_stacks() { + struct stack *maneuvre_stacks[7]; + + for (int i = 0; i < 7; i++) { + allocate_stack(&maneuvre_stacks[i]); + initialize_stack(maneuvre_stacks[i]); + } + set_card(maneuvre_stacks[0]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_0_STARTING_X); + set_card(maneuvre_stacks[1]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_1_STARTING_X); + set_card(maneuvre_stacks[2]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_2_STARTING_X); + set_card(maneuvre_stacks[3]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_3_STARTING_X); + set_card(maneuvre_stacks[4]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_4_STARTING_X); + set_card(maneuvre_stacks[5]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_5_STARTING_X); + set_card(maneuvre_stacks[6]->card, ACE, SPADES, EXPOSED, MANEUVRE_STARTING_Y, MANEUVRE_6_STARTING_X); + for (int i = 0; i < 7; i++) { + for (int j = 0; j < 7; j++) { + if (i == j) { + assert(!valid_move(maneuvre_stacks[i], maneuvre_stacks[j])); + } else { + assert(valid_move(maneuvre_stacks[i], maneuvre_stacks[j])); + } + } + } + for (int i = 0; i < 7; i++) { + free_stack(maneuvre_stacks[i]); + } +} + void test_game() { + test_valid_move_from_stock_to_stock(); + test_valid_move_from_stock_to_waste_pile(); + test_valid_move_from_stock_to_foundation_stacks(); + test_valid_move_from_stock_to_maneuvre_stacks(); + test_valid_move_from_waste_pile_to_stock(); + test_valid_move_from_waste_pile_to_waste_pile(); + test_valid_move_from_waste_pile_to_foundation_stacks(); + test_valid_move_from_waste_pile_to_maneuvre_stacks(); + test_valid_move_from_foundation_stack_to_stock(); + test_valid_move_from_foundation_stack_to_waste_pile(); + test_valid_move_from_foundation_stack_to_foundation_stacks(); + test_valid_move_from_foundation_stack_to_maneuvre_stacks(); + test_valid_move_from_maneuvre_stack_to_stock(); + test_valid_move_from_maneuvre_stack_to_waste_pile(); + test_valid_move_from_maneuvre_stack_to_foundation_stacks(); + test_valid_move_from_maneuvre_stack_to_maneuvre_stacks(); + test_move_card_from_empty_stack_to_empty_stack(); test_move_card_from_empty_stack_to_non_empty_stack(); test_move_card_from_non_empty_stack_to_empty_stack();