From ecaa2a4c68681ce90d3a1ede6367f0e2f2038234 Mon Sep 17 00:00:00 2001 From: Murilo Soares Pereira Date: Mon, 5 Apr 2010 04:33:10 -0300 Subject: [PATCH] Created the 'deck' structure, allocate() and initialize() for all structures. --- lib/card.c | 14 +++++++++----- lib/card.h | 3 ++- lib/deck.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/deck.h | 27 +++++++++++++++++++++++++++ lib/display.c | 4 +++- lib/display.h | 2 +- lib/frame.c | 12 +++++++----- lib/frame.h | 3 ++- lib/stack.c | 17 ++++++++++++----- lib/stack.h | 3 ++- src/tty-solitaire.c | 2 +- 11 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 lib/deck.c create mode 100644 lib/deck.h diff --git a/lib/card.c b/lib/card.c index 9a2803d..d44c1ca 100644 --- a/lib/card.c +++ b/lib/card.c @@ -2,17 +2,21 @@ #include #include "card.h" -struct card *initialize_card() { - struct card *card = NULL; +void allocate_card(struct card **card) { + *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->suit = NO_SUIT; card->face = NO_FACE; - return(card); + return; } void delete_card(struct card *card) { diff --git a/lib/card.h b/lib/card.h index d79db06..4819665 100644 --- a/lib/card.h +++ b/lib/card.h @@ -41,7 +41,8 @@ struct card { enum face face; }; -struct card *initialize_card(); +void allocate_card(struct card **); +void initialize_card(struct card *); void delete_card(struct card *); void set_card(struct card *, enum value, enum suit, enum face, int, int); diff --git a/lib/deck.c b/lib/deck.c new file mode 100644 index 0000000..df1f359 --- /dev/null +++ b/lib/deck.c @@ -0,0 +1,42 @@ +#include +#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; +} diff --git a/lib/deck.h b/lib/deck.h new file mode 100644 index 0000000..4fd2fef --- /dev/null +++ b/lib/deck.h @@ -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 diff --git a/lib/display.c b/lib/display.c index 055816e..6a113dc 100644 --- a/lib/display.c +++ b/lib/display.c @@ -45,7 +45,9 @@ void draw_empty_stacks() { return; } -void init_game() { +void initialize_game() { + struct deck *deck = NULL; + draw_empty_stacks(); return; diff --git a/lib/display.h b/lib/display.h index 7038c37..4100c41 100644 --- a/lib/display.h +++ b/lib/display.h @@ -16,7 +16,7 @@ void init_curses(); void draw_empty_stacks(); -void init_game(); +void initialize_game(); char *card_suit(enum suit); char *card_value(enum value); void draw_value(struct card *); diff --git a/lib/frame.c b/lib/frame.c index 2df8159..9343eb4 100644 --- a/lib/frame.c +++ b/lib/frame.c @@ -1,18 +1,20 @@ #include #include "frame.h" -struct frame *initialize_frame() { - struct frame *frame = NULL; +void allocate_frame(struct frame **frame) { + *frame = malloc(sizeof(**frame)); - frame = malloc(sizeof(*frame)); + return; +} - frame->shape = NULL; +void initialize_frame(struct frame *frame) { + frame->shape = NULL; frame->height = FRAME_HEIGHT; frame->width = FRAME_WIDTH; frame->start_y = 0; frame->start_x = 0; - return(frame); + return; } void delete_frame(struct frame *frame) { diff --git a/lib/frame.h b/lib/frame.h index d01f98a..b9b10fa 100644 --- a/lib/frame.h +++ b/lib/frame.h @@ -14,7 +14,8 @@ struct frame { int start_x; }; -struct frame *initialize_frame(); +void allocate_frame(struct frame **); +void initialize_frame(struct frame *); void delete_frame(struct frame *); void set_frame(struct frame *, int, int); diff --git a/lib/stack.c b/lib/stack.c index 5c5c69b..cf0df7c 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -2,11 +2,17 @@ #include #include "stack.h" -void initialize_stack(struct stack **stack) { +void allocate_stack(struct stack **stack) { *stack = malloc(sizeof(**stack)); - (*stack)->card = NULL; - (*stack)->next = NULL; + allocate_card(&((*stack)->card)); + + return; +} + +void initialize_stack(struct stack *stack) { + stack->card = NULL; + stack->next = NULL; return; } @@ -36,7 +42,7 @@ void push(struct stack **stack, struct card *card) { if (empty(*stack)) { (*stack)->card = card; } else { - initialize_stack(&new_stack); + allocate_stack(&new_stack); new_stack->card = card; new_stack->next = (*stack); *stack = new_stack; @@ -50,7 +56,8 @@ struct stack *pop(struct stack **stack) { if(!empty(*stack)) { popped_entry = *stack; if (length(*stack) == 1) { - initialize_stack(stack); + allocate_stack(stack); + initialize_stack(*stack); } else { *stack = (*stack)->next; } diff --git a/lib/stack.h b/lib/stack.h index 03d6f34..505ccf4 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -8,7 +8,8 @@ struct stack { struct stack *next; }; -void initialize_stack(struct stack **); +void allocate_stack(struct stack **); +void initialize_stack(struct stack *); bool empty(struct stack *); int length(struct stack *); void push(struct stack **, struct card *); diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index bb28d32..6163c79 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -16,7 +16,7 @@ int main(int argc, const char *argv[]) { message); getch(); - endwin(); + initialize_game(); puts("Game finished.");