From 4d3892101d8f1f0974c6bafc8f30e0818447149a Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sun, 5 Jun 2011 23:08:08 -0300 Subject: [PATCH] Encapsulated function to move blocks and removed compiler warnings. --- src/card.h | 2 ++ src/game.c | 17 +++++++++++++++++ src/game.h | 2 +- src/keyboard.c | 40 ++++++++++++---------------------------- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/card.h b/src/card.h index 304761e..3d3dda2 100644 --- a/src/card.h +++ b/src/card.h @@ -36,5 +36,7 @@ void set_card(struct card *, enum value, enum suit, enum face, int, int); void expose_card(struct card *); void cover_card(struct card *); struct card *duplicate_card(struct card *); +void mark_card(struct card *); +void unmark_card(struct card *); #endif diff --git a/src/game.c b/src/game.c index b3cf06c..5bf1086 100644 --- a/src/game.c +++ b/src/game.c @@ -113,6 +113,23 @@ void move_card(struct stack **origin, struct stack **destination) { } } +void move_block(struct stack **origin, struct stack **destination, int block_size) { + struct stack *tmp; + allocate_stack(&tmp); + initialize_stack(tmp); + for (int i = 0; i < block_size; i++) { + push(&tmp, pop(origin)); + } + for (int i = 0; i < block_size; i++) { + move_card(&tmp, destination); + } + if (length(*destination) > 1) { + cursor->y += block_size; + } + free_stack(tmp); +} + + static void fill_deck(struct deck *deck) { struct card *card[NUMBER_OF_CARDS]; diff --git a/src/game.h b/src/game.h index 3c524a3..4a0f254 100644 --- a/src/game.h +++ b/src/game.h @@ -34,7 +34,7 @@ struct cursor *cursor; bool maneuvre_stack(struct stack *); bool valid_move(struct stack *, struct stack *); void move_card(struct stack **, struct stack **); -void greet_player(); +void move_block(struct stack **, struct stack **, int); void game_init(); void game_end(); diff --git a/src/keyboard.c b/src/keyboard.c index 56e0f7f..b3b44c5 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -77,8 +77,8 @@ static int marked_cards_count(struct stack *stack) { } static void unmark_cards(struct stack *stack) { - int marked_cards = marked_cards_count(stack); - for (int i = 0; i < marked_cards; stack = stack->next, i++) { + int _marked_cards_count = marked_cards_count(stack); + for (int i = 0; i < _marked_cards_count; stack = stack->next, i++) { unmark_card(stack->card); } } @@ -164,50 +164,34 @@ static void handle_card_movement(struct cursor *cursor) { break; case KEY_SPACEBAR: destination = cursor_stack(cursor); - int marked_cards = marked_cards_count(*origin); - if (maneuvre_stack(*origin) && marked_cards > 0) { + int _marked_cards_count = marked_cards_count(*origin); + if (maneuvre_stack(*origin) && _marked_cards_count > 0) { erase_stack(*origin); unmark_cards(*origin); draw_stack(*origin); } if (destination) { - if (marked_cards > 1 && + erase_stack(*origin); + erase_cursor(cursor); + if (_marked_cards_count > 1 && maneuvre_stack(*origin) && maneuvre_stack(*destination)) { struct stack *block = *origin; - for (int i = 1; i < marked_cards; block = block->next, i++) + for (int i = 1; i < _marked_cards_count; block = block->next, i++) ; if (valid_move(block, *destination)) { - erase_stack(*origin); - struct stack *tmp; - allocate_stack(&tmp); - initialize_stack(tmp); - for (int i = 0; i < marked_cards; i++) { - push(&tmp, pop(origin)); - } - for (int i = 0; i < marked_cards; i++) { - move_card(&tmp, destination); - } - if (length(*destination) > 1) { - erase_cursor(cursor); - cursor->y += marked_cards; - } - free_stack(tmp); - draw_stack(*origin); - draw_stack(*destination); + move_block(origin, destination, _marked_cards_count); } } else { if (valid_move(*origin, *destination)) { - erase_stack(*origin); - move_card(origin, destination); if (maneuvre_stack(*destination) && length(*destination) > 1) { - erase_cursor(cursor); cursor->y++; } - draw_stack(*origin); - draw_stack(*destination); + move_card(origin, destination); } } + draw_stack(*origin); + draw_stack(*destination); if (maneuvre_stack(*origin) && *origin == *destination) { erase_cursor(cursor); cursor->y--;