Created the 'deck' structure, allocate() and initialize() for all structures.
This commit is contained in:
		
							parent
							
								
									7e47ca3ccf
								
							
						
					
					
						commit
						ecaa2a4c68
					
				
							
								
								
									
										14
									
								
								lib/card.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								lib/card.c
									
									
									
									
									
								
							| @ -2,17 +2,21 @@ | |||||||
| #include <malloc.h> | #include <malloc.h> | ||||||
| #include "card.h" | #include "card.h" | ||||||
| 
 | 
 | ||||||
| struct card *initialize_card() { | void allocate_card(struct card **card) { | ||||||
|   struct card *card = NULL; |   *card = malloc(sizeof(**card)); | ||||||
| 
 | 
 | ||||||
|   card = malloc(sizeof(*card)); |   allocate_frame(&((*card)->frame)); | ||||||
| 
 | 
 | ||||||
|   card->frame   = initialize_frame(); |   return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void initialize_card(struct card *card) { | ||||||
|  |   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; | ||||||
| 
 | 
 | ||||||
|   return(card); |   return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void delete_card(struct card *card) { | void delete_card(struct card *card) { | ||||||
|  | |||||||
| @ -41,7 +41,8 @@ struct card { | |||||||
|   enum face    face; |   enum face    face; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct card *initialize_card(); | void allocate_card(struct card **); | ||||||
|  | void initialize_card(struct card *); | ||||||
| void delete_card(struct card *); | void delete_card(struct card *); | ||||||
| void set_card(struct card *, enum value, enum suit, enum face, int, int); | void set_card(struct card *, enum value, enum suit, enum face, int, int); | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										42
									
								
								lib/deck.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								lib/deck.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,42 @@ | |||||||
|  | #include <malloc.h> | ||||||
|  | #include "deck.h" | ||||||
|  | 
 | ||||||
|  | void allocate_deck(struct deck **deck) { | ||||||
|  |   *deck = malloc(sizeof(**deck)); | ||||||
|  | 
 | ||||||
|  |   allocate_stack(&((*deck)->stock)); | ||||||
|  | 
 | ||||||
|  |   allocate_stack(&((*deck)->foundation_0)); | ||||||
|  |   allocate_stack(&((*deck)->foundation_1)); | ||||||
|  |   allocate_stack(&((*deck)->foundation_2)); | ||||||
|  |   allocate_stack(&((*deck)->foundation_3)); | ||||||
|  | 
 | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_0)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_1)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_2)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_3)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_4)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_5)); | ||||||
|  |   allocate_stack(&((*deck)->maneuvre_6)); | ||||||
|  | 
 | ||||||
|  |   return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void initialize_deck(struct deck *deck) { | ||||||
|  |   initialize_stack(deck->stock); | ||||||
|  | 
 | ||||||
|  |   initialize_stack(deck->foundation_0); | ||||||
|  |   initialize_stack(deck->foundation_1); | ||||||
|  |   initialize_stack(deck->foundation_2); | ||||||
|  |   initialize_stack(deck->foundation_3); | ||||||
|  | 
 | ||||||
|  |   initialize_stack(deck->maneuvre_0); | ||||||
|  |   initialize_stack(deck->maneuvre_1); | ||||||
|  |   initialize_stack(deck->maneuvre_2); | ||||||
|  |   initialize_stack(deck->maneuvre_3); | ||||||
|  |   initialize_stack(deck->maneuvre_4); | ||||||
|  |   initialize_stack(deck->maneuvre_5); | ||||||
|  |   initialize_stack(deck->maneuvre_6); | ||||||
|  | 
 | ||||||
|  |   return; | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								lib/deck.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								lib/deck.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | |||||||
|  | #ifndef DECK_H | ||||||
|  | #define DECK_H | ||||||
|  | 
 | ||||||
|  | #include "stack.h" | ||||||
|  | 
 | ||||||
|  | struct deck { | ||||||
|  |   struct stack *stock; | ||||||
|  | 
 | ||||||
|  |   struct stack *foundation_0; | ||||||
|  |   struct stack *foundation_1; | ||||||
|  |   struct stack *foundation_2; | ||||||
|  |   struct stack *foundation_3; | ||||||
|  | 
 | ||||||
|  |   struct stack *maneuvre_0; | ||||||
|  |   struct stack *maneuvre_1; | ||||||
|  |   struct stack *maneuvre_2; | ||||||
|  |   struct stack *maneuvre_3; | ||||||
|  |   struct stack *maneuvre_4; | ||||||
|  |   struct stack *maneuvre_5; | ||||||
|  |   struct stack *maneuvre_6; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | void allocate_deck(struct deck **); | ||||||
|  | void initialize_deck(struct deck *); | ||||||
|  | //void delete_deck(struct deck *);
 | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
| @ -45,7 +45,9 @@ void draw_empty_stacks() { | |||||||
|   return; |   return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void init_game() { | void initialize_game() { | ||||||
|  |   struct deck *deck = NULL; | ||||||
|  | 
 | ||||||
|   draw_empty_stacks(); |   draw_empty_stacks(); | ||||||
| 
 | 
 | ||||||
|   return; |   return; | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ | |||||||
| 
 | 
 | ||||||
| void init_curses(); | void init_curses(); | ||||||
| void draw_empty_stacks(); | void draw_empty_stacks(); | ||||||
| void init_game(); | void initialize_game(); | ||||||
| char *card_suit(enum suit); | char *card_suit(enum suit); | ||||||
| char *card_value(enum value); | char *card_value(enum value); | ||||||
| void draw_value(struct card *); | void draw_value(struct card *); | ||||||
|  | |||||||
							
								
								
									
										10
									
								
								lib/frame.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								lib/frame.c
									
									
									
									
									
								
							| @ -1,18 +1,20 @@ | |||||||
| #include <malloc.h> | #include <malloc.h> | ||||||
| #include "frame.h" | #include "frame.h" | ||||||
| 
 | 
 | ||||||
| struct frame *initialize_frame() { | void allocate_frame(struct frame **frame) { | ||||||
|   struct frame *frame = NULL; |   *frame = malloc(sizeof(**frame)); | ||||||
| 
 | 
 | ||||||
|   frame = malloc(sizeof(*frame)); |   return; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|  | void initialize_frame(struct frame *frame) { | ||||||
|   frame->shape   = NULL; |   frame->shape   = NULL; | ||||||
|   frame->height  = FRAME_HEIGHT; |   frame->height  = FRAME_HEIGHT; | ||||||
|   frame->width   = FRAME_WIDTH; |   frame->width   = FRAME_WIDTH; | ||||||
|   frame->start_y = 0; |   frame->start_y = 0; | ||||||
|   frame->start_x = 0; |   frame->start_x = 0; | ||||||
| 
 | 
 | ||||||
|   return(frame); |   return; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void delete_frame(struct frame *frame) { | void delete_frame(struct frame *frame) { | ||||||
|  | |||||||
| @ -14,7 +14,8 @@ struct frame { | |||||||
|   int    start_x; |   int    start_x; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct frame *initialize_frame(); | void allocate_frame(struct frame **); | ||||||
|  | void initialize_frame(struct frame *); | ||||||
| void delete_frame(struct frame *); | void delete_frame(struct frame *); | ||||||
| void set_frame(struct frame *, int, int); | void set_frame(struct frame *, int, int); | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										17
									
								
								lib/stack.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								lib/stack.c
									
									
									
									
									
								
							| @ -2,11 +2,17 @@ | |||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| #include "stack.h" | #include "stack.h" | ||||||
| 
 | 
 | ||||||
| void initialize_stack(struct stack **stack) { | void allocate_stack(struct stack **stack) { | ||||||
|   *stack = malloc(sizeof(**stack)); |   *stack = malloc(sizeof(**stack)); | ||||||
| 
 | 
 | ||||||
|   (*stack)->card = NULL; |   allocate_card(&((*stack)->card)); | ||||||
|   (*stack)->next = NULL; | 
 | ||||||
|  |   return; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void initialize_stack(struct stack *stack) { | ||||||
|  |   stack->card = NULL; | ||||||
|  |   stack->next = NULL; | ||||||
| 
 | 
 | ||||||
|   return; |   return; | ||||||
| } | } | ||||||
| @ -36,7 +42,7 @@ void push(struct stack **stack, struct card *card) { | |||||||
|   if (empty(*stack)) { |   if (empty(*stack)) { | ||||||
|     (*stack)->card = card; |     (*stack)->card = card; | ||||||
|   } else { |   } else { | ||||||
|     initialize_stack(&new_stack); |     allocate_stack(&new_stack); | ||||||
|     new_stack->card = card; |     new_stack->card = card; | ||||||
|     new_stack->next = (*stack); |     new_stack->next = (*stack); | ||||||
|     *stack = new_stack; |     *stack = new_stack; | ||||||
| @ -50,7 +56,8 @@ struct stack *pop(struct stack **stack) { | |||||||
|   if(!empty(*stack)) { |   if(!empty(*stack)) { | ||||||
|     popped_entry = *stack; |     popped_entry = *stack; | ||||||
|     if (length(*stack) == 1) { |     if (length(*stack) == 1) { | ||||||
|       initialize_stack(stack); |       allocate_stack(stack); | ||||||
|  |       initialize_stack(*stack); | ||||||
|     } else { |     } else { | ||||||
|       *stack = (*stack)->next; |       *stack = (*stack)->next; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -8,7 +8,8 @@ struct stack { | |||||||
|   struct stack *next; |   struct stack *next; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void initialize_stack(struct stack **); | void allocate_stack(struct stack **); | ||||||
|  | void initialize_stack(struct stack *); | ||||||
| bool empty(struct stack *); | bool empty(struct stack *); | ||||||
| int length(struct stack *); | int length(struct stack *); | ||||||
| void push(struct stack **, struct card *); | void push(struct stack **, struct card *); | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ int main(int argc, const char *argv[]) { | |||||||
|            message); |            message); | ||||||
|   getch(); |   getch(); | ||||||
| 
 | 
 | ||||||
|   endwin(); |   initialize_game(); | ||||||
| 
 | 
 | ||||||
|   puts("Game finished."); |   puts("Game finished."); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user