Moving game logic to the game object.
This commit is contained in:
		
							parent
							
								
									3495e08490
								
							
						
					
					
						commit
						b1000cfb55
					
				
							
								
								
									
										19
									
								
								lib/game.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								lib/game.c
									
									
									
									
									
								
							| @ -9,6 +9,25 @@ | ||||
| #include "util.h" | ||||
| #include "game.h" | ||||
| 
 | ||||
| static bool maneuvre_stack(struct stack *stack) { | ||||
|   return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y); | ||||
| } | ||||
| 
 | ||||
| void move_card(struct stack **origin, struct stack **destination) { | ||||
|   struct stack *stack = NULL; | ||||
| 
 | ||||
|   (*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) { | ||||
|   set_frame(deck->stock->card->frame, | ||||
|             STOCK_STARTING_Y, | ||||
|  | ||||
| @ -10,6 +10,7 @@ extern const char *program_name; | ||||
| struct deck *deck; | ||||
| struct cursor *cursor; | ||||
| 
 | ||||
| void move_card(struct stack **, struct stack **); | ||||
| void greet_player(); | ||||
| void initialize_game(); | ||||
| void end_game(); | ||||
|  | ||||
							
								
								
									
										19
									
								
								lib/stack.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								lib/stack.c
									
									
									
									
									
								
							| @ -6,10 +6,6 @@ | ||||
| #include <errno.h> | ||||
| #include "stack.h" | ||||
| 
 | ||||
| static bool maneuvre_stack(struct stack *stack) { | ||||
|   return(stack->card->frame->start_y >= MANEUVRE_STACKS_STARTING_Y); | ||||
| } | ||||
| 
 | ||||
| void allocate_stack(struct stack **stack) { | ||||
|   if (!(*stack = malloc(sizeof(**stack)))) { | ||||
|     fprintf(stderr, "%s: %s (%s:%d)\n", program_name, strerror(errno), __FILE__, __LINE__ - 1); | ||||
| @ -100,18 +96,3 @@ struct stack *pop(struct stack **stack) { | ||||
| 
 | ||||
|   return(popped_entry); | ||||
| } | ||||
| 
 | ||||
| void move_card(struct stack **origin, struct stack **destination) { | ||||
|   struct stack *stack = NULL; | ||||
| 
 | ||||
|   (*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; | ||||
| } | ||||
|  | ||||
| @ -40,6 +40,5 @@ bool empty(struct stack *); | ||||
| int length(struct stack *); | ||||
| void push(struct stack **, struct card *); | ||||
| struct stack *pop(struct stack **); | ||||
| void move_card(struct stack **, struct stack **); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										123
									
								
								test/game_test.c
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								test/game_test.c
									
									
									
									
									
								
							| @ -1,9 +1,126 @@ | ||||
| #include <assert.h> | ||||
| #include <stdbool.h> | ||||
| #include "../lib/stack.h" | ||||
| #include "../lib/game.h" | ||||
| #include "test_helper.h" | ||||
| 
 | ||||
| void test_game() { | ||||
|   assert(true); | ||||
| void test_move_card_from_empty_stack_to_empty_stack() { | ||||
|   struct stack *origin, *destination, | ||||
|                *new_origin, *new_destination, | ||||
|                *origin_duplicate, *destination_duplicate; | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   new_origin = origin; | ||||
|   new_destination = destination; | ||||
|   origin_duplicate = duplicate_stack(origin); | ||||
|   destination_duplicate = duplicate_stack(destination); | ||||
|   move_card(&new_origin, &new_destination); | ||||
| 
 | ||||
|   assert(origin == new_origin); | ||||
|   assert(stacks_equal(origin, origin_duplicate)); | ||||
|   assert(destination == new_destination); | ||||
|   assert(stacks_equal(destination, destination_duplicate)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_empty_stack_to_non_empty_stack() { | ||||
|   struct stack *origin, *destination, | ||||
|                *new_origin, *new_destination, | ||||
|                *origin_duplicate, *destination_duplicate; | ||||
|   struct card *card; | ||||
| 
 | ||||
|   allocate_card(&card); | ||||
|   initialize_card(card); | ||||
|   set_card(card, ACE, SPADES, EXPOSED, 0, 0); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   new_origin = origin; | ||||
|   new_destination = destination; | ||||
|   push(&new_destination, card); | ||||
|   origin_duplicate = duplicate_stack(origin); | ||||
|   destination_duplicate = duplicate_stack(destination); | ||||
|   move_card(&new_origin, &new_destination); | ||||
| 
 | ||||
|   assert(origin == new_origin); | ||||
|   assert(stacks_equal(origin, origin_duplicate)); | ||||
|   assert(destination == new_destination); | ||||
|   assert(stacks_equal(destination, destination_duplicate)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_non_empty_stack_to_empty_stack() { | ||||
|   struct stack *origin, *destination; | ||||
|   struct card *card; | ||||
| 
 | ||||
|   allocate_card(&card); | ||||
|   initialize_card(card); | ||||
|   set_card(card, ACE, SPADES, EXPOSED, 0, 0); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   push(&origin, card); | ||||
|   move_card(&origin, &destination); | ||||
| 
 | ||||
|   assert(empty(origin)); | ||||
|   assert(length(destination) == 1); | ||||
|   assert(cards_equal(destination->card, card)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_non_empty_stack_to_non_empty_stack() { | ||||
|   struct stack *origin, *destination; | ||||
|   struct card *card[2]; | ||||
| 
 | ||||
|   allocate_card(&card[0]); | ||||
|   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); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   push(&origin, card[0]); | ||||
|   push(&destination, card[1]); | ||||
|   move_card(&origin, &destination); | ||||
| 
 | ||||
|   assert(empty(origin)); | ||||
|   assert(length(destination) == 2); | ||||
|   assert(cards_equal(destination->card, card[0])); | ||||
|   assert(cards_equal(destination->next->card, card[1])); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_game() { | ||||
|   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(); | ||||
|   test_move_card_from_non_empty_stack_to_non_empty_stack(); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| #include <assert.h> | ||||
| #include "../lib/stack.h" | ||||
| #include "test_helper.h" | ||||
| 
 | ||||
| void test_initialize_stack() { | ||||
|   struct stack *stack; | ||||
| @ -194,119 +193,6 @@ void test_pop_on_stack_with_more_than_one_element() { | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_empty_stack_to_empty_stack() { | ||||
|   struct stack *origin, *destination, | ||||
|                *new_origin, *new_destination, | ||||
|                *origin_duplicate, *destination_duplicate; | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   new_origin = origin; | ||||
|   new_destination = destination; | ||||
|   origin_duplicate = duplicate_stack(origin); | ||||
|   destination_duplicate = duplicate_stack(destination); | ||||
|   move_card(&new_origin, &new_destination); | ||||
| 
 | ||||
|   assert(origin == new_origin); | ||||
|   assert(stacks_equal(origin, origin_duplicate)); | ||||
|   assert(destination == new_destination); | ||||
|   assert(stacks_equal(destination, destination_duplicate)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_empty_stack_to_non_empty_stack() { | ||||
|   struct stack *origin, *destination, | ||||
|                *new_origin, *new_destination, | ||||
|                *origin_duplicate, *destination_duplicate; | ||||
|   struct card *card; | ||||
| 
 | ||||
|   allocate_card(&card); | ||||
|   initialize_card(card); | ||||
|   set_card(card, ACE, SPADES, EXPOSED, 0, 0); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   new_origin = origin; | ||||
|   new_destination = destination; | ||||
|   push(&new_destination, card); | ||||
|   origin_duplicate = duplicate_stack(origin); | ||||
|   destination_duplicate = duplicate_stack(destination); | ||||
|   move_card(&new_origin, &new_destination); | ||||
| 
 | ||||
|   assert(origin == new_origin); | ||||
|   assert(stacks_equal(origin, origin_duplicate)); | ||||
|   assert(destination == new_destination); | ||||
|   assert(stacks_equal(destination, destination_duplicate)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_non_empty_stack_to_empty_stack() { | ||||
|   struct stack *origin, *destination; | ||||
|   struct card *card; | ||||
| 
 | ||||
|   allocate_card(&card); | ||||
|   initialize_card(card); | ||||
|   set_card(card, ACE, SPADES, EXPOSED, 0, 0); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   push(&origin, card); | ||||
|   move_card(&origin, &destination); | ||||
| 
 | ||||
|   assert(empty(origin)); | ||||
|   assert(length(destination) == 1); | ||||
|   assert(cards_equal(destination->card, card)); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_move_card_from_non_empty_stack_to_non_empty_stack() { | ||||
|   struct stack *origin, *destination; | ||||
|   struct card *card[2]; | ||||
| 
 | ||||
|   allocate_card(&card[0]); | ||||
|   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); | ||||
| 
 | ||||
|   allocate_stack(&origin); | ||||
|   allocate_stack(&destination); | ||||
|   initialize_stack(origin); | ||||
|   initialize_stack(destination); | ||||
|   push(&origin, card[0]); | ||||
|   push(&destination, card[1]); | ||||
|   move_card(&origin, &destination); | ||||
| 
 | ||||
|   assert(empty(origin)); | ||||
|   assert(length(destination) == 2); | ||||
|   assert(cards_equal(destination->card, card[0])); | ||||
|   assert(cards_equal(destination->next->card, card[1])); | ||||
| 
 | ||||
|   free_stack(origin); | ||||
|   free_stack(destination); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
| void test_stack() { | ||||
|   test_initialize_stack(); | ||||
| 
 | ||||
| @ -321,10 +207,5 @@ void test_stack() { | ||||
|   test_pop_on_stack_with_one_element(); | ||||
|   test_pop_on_stack_with_more_than_one_element(); | ||||
| 
 | ||||
|   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(); | ||||
|   test_move_card_from_non_empty_stack_to_non_empty_stack(); | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user