Encapsulated function to move blocks and removed compiler warnings.
This commit is contained in:
		
							parent
							
								
									656f8e6190
								
							
						
					
					
						commit
						4d3892101d
					
				| @ -36,5 +36,7 @@ void set_card(struct card *, enum value, enum suit, enum face, int, int); | |||||||
| void expose_card(struct card *); | void expose_card(struct card *); | ||||||
| void cover_card(struct card *); | void cover_card(struct card *); | ||||||
| struct card *duplicate_card(struct card *); | struct card *duplicate_card(struct card *); | ||||||
|  | void mark_card(struct card *); | ||||||
|  | void unmark_card(struct card *); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										17
									
								
								src/game.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								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) { | static void fill_deck(struct deck *deck) { | ||||||
|   struct card *card[NUMBER_OF_CARDS]; |   struct card *card[NUMBER_OF_CARDS]; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -34,7 +34,7 @@ struct cursor *cursor; | |||||||
| bool maneuvre_stack(struct stack *); | bool maneuvre_stack(struct stack *); | ||||||
| bool valid_move(struct stack *, struct stack *); | bool valid_move(struct stack *, struct stack *); | ||||||
| void move_card(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_init(); | ||||||
| void game_end(); | void game_end(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -77,8 +77,8 @@ 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 = marked_cards_count(stack); |   int _marked_cards_count = marked_cards_count(stack); | ||||||
|   for (int i = 0; i < marked_cards; stack = stack->next, i++) { |   for (int i = 0; i < _marked_cards_count; stack = stack->next, i++) { | ||||||
|     unmark_card(stack->card); |     unmark_card(stack->card); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @ -164,50 +164,34 @@ static void handle_card_movement(struct cursor *cursor) { | |||||||
|       break; |       break; | ||||||
|     case KEY_SPACEBAR: |     case KEY_SPACEBAR: | ||||||
|       destination = cursor_stack(cursor); |       destination = cursor_stack(cursor); | ||||||
|       int marked_cards = marked_cards_count(*origin); |       int _marked_cards_count = marked_cards_count(*origin); | ||||||
|       if (maneuvre_stack(*origin) && marked_cards > 0) { |       if (maneuvre_stack(*origin) && _marked_cards_count > 0) { | ||||||
|         erase_stack(*origin); |         erase_stack(*origin); | ||||||
|         unmark_cards(*origin); |         unmark_cards(*origin); | ||||||
|         draw_stack(*origin); |         draw_stack(*origin); | ||||||
|       } |       } | ||||||
|       if (destination) { |       if (destination) { | ||||||
|         if (marked_cards > 1 && |         erase_stack(*origin); | ||||||
|  |         erase_cursor(cursor); | ||||||
|  |         if (_marked_cards_count > 1 && | ||||||
|               maneuvre_stack(*origin) && |               maneuvre_stack(*origin) && | ||||||
|               maneuvre_stack(*destination)) { |               maneuvre_stack(*destination)) { | ||||||
|           struct stack *block = *origin; |           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)) { |           if (valid_move(block, *destination)) { | ||||||
|             erase_stack(*origin); |             move_block(origin, destination, _marked_cards_count); | ||||||
|             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); |  | ||||||
|           } |           } | ||||||
|         } else { |         } else { | ||||||
|           if (valid_move(*origin, *destination)) { |           if (valid_move(*origin, *destination)) { | ||||||
|             erase_stack(*origin); |  | ||||||
|             move_card(origin, destination); |  | ||||||
|             if (maneuvre_stack(*destination) && length(*destination) > 1) { |             if (maneuvre_stack(*destination) && length(*destination) > 1) { | ||||||
|               erase_cursor(cursor); |  | ||||||
|               cursor->y++; |               cursor->y++; | ||||||
|             } |             } | ||||||
|             draw_stack(*origin); |             move_card(origin, destination); | ||||||
|             draw_stack(*destination); |  | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|  |         draw_stack(*origin); | ||||||
|  |         draw_stack(*destination); | ||||||
|         if (maneuvre_stack(*origin) && *origin == *destination) { |         if (maneuvre_stack(*origin) && *origin == *destination) { | ||||||
|           erase_cursor(cursor); |           erase_cursor(cursor); | ||||||
|           cursor->y--; |           cursor->y--; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user