diff --git a/lib/card.c b/lib/card.c new file mode 100644 index 0000000..9c7b456 --- /dev/null +++ b/lib/card.c @@ -0,0 +1,44 @@ +#include +#include +#include "card.h" +#include "frame.h" +#include "../src/common.h" + +struct card *initialize_card() { + struct card *card = NULL; + + card = malloc(sizeof(card)); + + card->frame = initialize_frame(); + card->value = NONE; + card->suit = NONE; + card->exposed = FALSE; + + return(card); +} + +void delete_card(struct card *card) { + delete_frame(card->frame); + free(card); + + return; +} + +void set_card(struct card *card, + enum value value, + enum suit suit, + char exposed, + int start_y, + int start_x) { + set_frame(card->frame, start_y, start_x); + card->value = value; + card->suit = suit; + card->exposed = exposed; + + return; +} + +void refresh_card(struct card *card) { + box(card->frame->shape, 0, 0); + wrefresh(card->frame->shape); +} diff --git a/lib/card.h b/lib/card.h new file mode 100644 index 0000000..325e036 --- /dev/null +++ b/lib/card.h @@ -0,0 +1,41 @@ +#ifndef CARD_H +#define CARD_H + +enum suit { + BLANK = -1, + DIAMONDS = 0, + SPADES = 1, + HEARTS = 2, + CLUBS = 3 +}; + +enum value { + NONE = -1, + TWO = 2, + THREE = 3, + FOUR = 4, + FIVE = 5, + SIX = 6, + SEVEN = 7, + EIGHT = 8, + NINE = 9, + TEN = 10, + JACK = 11, + QUEEN = 12, + KING = 13, + ACE = 14 +}; + +struct card { + struct frame *frame; + enum value value; + enum suit suit; + char exposed; +}; + +struct card *initialize_card(); +void delete_card(struct card *); +void set_card(struct card *, enum value, enum suit, char, int, int); +void refresh_card(struct card *); + +#endif diff --git a/lib/frame.c b/lib/frame.c new file mode 100644 index 0000000..57bce6d --- /dev/null +++ b/lib/frame.c @@ -0,0 +1,43 @@ +#include +#include +#include "frame.h" + +WINDOW *initialize_shape() { + WINDOW *shape; + + shape = malloc(sizeof(shape)); + + return(shape); +} + +struct frame *initialize_frame() { + struct frame *frame = NULL; + + frame = malloc(sizeof(frame)); + + frame->shape = initialize_shape(); + frame->height = FRAME_HEIGHT; + frame->width = FRAME_WIDTH; + frame->start_y = 0; + frame->start_x = 0; + + return(frame); +} + +void delete_frame(struct frame *frame) { + free(frame->shape); + free(frame); + + return; +} + +void set_frame(struct frame *frame, int start_y, int start_x) { + frame->start_y = start_y; + frame->start_x = start_x; + frame->shape = newwin(frame->height, + frame->width, + frame->start_y, + frame->start_x); + + return; +} diff --git a/lib/frame.h b/lib/frame.h new file mode 100644 index 0000000..e4529e8 --- /dev/null +++ b/lib/frame.h @@ -0,0 +1,20 @@ +#ifndef FRAME_H +#define FRAME_H + +#define FRAME_WIDTH 7 +#define FRAME_HEIGHT 5 + +struct frame { + WINDOW *shape; + int height; + int width; + int start_y; + int start_x; +}; + +WINDOW *initialize_shape(); +struct frame *initialize_frame(); +void delete_frame(struct frame *); +void set_frame(struct frame *, int, int); + +#endif diff --git a/makefile b/makefile index 6a7a305..a2904a2 100644 --- a/makefile +++ b/makefile @@ -3,10 +3,12 @@ SRC_DIR = src SRC = ${SRC_DIR}/tty-solitaire.c CFLAGS = -W -Wall -pedantic -ansi -std=c99 OUTPUT = bin/tty-solitaire +LIB_DIR = lib +OBJECTS = ${LIB_DIR}/card.o ${LIB_DIR}/frame.o LDFLAGS = -lncurses -tty-solitaire: ${SRC} - ${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${OUTPUT} +tty-solitaire: ${OBJECTS} + ${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${OUTPUT} ${OBJECTS} clean: - rm -rf ${SRC_DIR}/*.o ${OUTPUT} + rm -rf ${LIB_DIR}/*.o ${OUTPUT} diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index 6cf0e4a..da16351 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -2,58 +2,7 @@ #include #include #include "common.h" - -#define CARD_FRAME_WIDTH 7 -#define CARD_FRAME_HEIGHT 5 - -enum suit { - BLANK = -1, - DIAMONDS = 0, - SPADES = 1, - HEARTS = 2, - CLUBS = 3 -}; - -enum value { - NONE = -1, - TWO = 2, - THREE = 3, - FOUR = 4, - FIVE = 5, - SIX = 6, - SEVEN = 7, - EIGHT = 8, - NINE = 9, - TEN = 10, - JACK = 11, - QUEEN = 12, - KING = 13, - ACE = 14 -}; - -struct card_frame { - WINDOW *shape; - int height; - int width; - int start_y; - int start_x; -}; - -struct card { - struct card_frame *frame; - enum value value; - enum suit suit; - char exposed; -}; - -WINDOW *initialize_shape(); -struct card_frame *initialize_card_frame(); -struct card *initialize_card(); -void draw_card(struct card *); -void set_card_frame(struct card_frame *, int, int); -void set_card_attributes(struct card *, enum value, enum suit, char); -void set_card(struct card *, enum value, enum suit, char, int, int); -void delete_card(struct card *); +#include "../lib/card.h" int main(int argc, const char *argv[]) { char message[] = "Welcome to tty-solitaire."; @@ -76,25 +25,25 @@ int main(int argc, const char *argv[]) { set_card(card, KING, CLUBS, TRUE, 1, 1); - draw_card(card); + refresh_card(card); while ((ch = getch()) != KEY_F(1)) { switch(ch) { case KEY_LEFT: case 'h': - draw_card(card); + refresh_card(card); break; case KEY_RIGHT: case 'l': - draw_card(card); + refresh_card(card); break; case KEY_UP: case 'k': - draw_card(card); + refresh_card(card); break; case KEY_DOWN: case 'j': - draw_card(card); + refresh_card(card); break; } } @@ -104,87 +53,3 @@ int main(int argc, const char *argv[]) { return 0; } - -WINDOW *initialize_shape() { - WINDOW *shape; - - shape = malloc(sizeof(shape)); - - return(shape); -} - -struct card_frame *initialize_card_frame() { - struct card_frame *card_frame = NULL; - - card_frame = malloc(sizeof(card_frame)); - - card_frame->shape = initialize_shape(); - card_frame->height = CARD_FRAME_HEIGHT; - card_frame->width = CARD_FRAME_WIDTH; - card_frame->start_y = 0; - card_frame->start_x = 0; - - return(card_frame); -} - -struct card *initialize_card() { - struct card *card = NULL; - - card = malloc(sizeof(card)); - - card->frame = initialize_card_frame(); - card->value = NONE; - card->suit = NONE; - card->exposed = FALSE; - - return(card); -} - -void draw_card(struct card *card) { - box(card->frame->shape, 0, 0); - wrefresh(card->frame->shape); -} - -void set_card_frame(struct card_frame *card_frame, - int start_y, - int start_x) { - card_frame->start_y = start_y; - card_frame->start_x = start_x; - card_frame->shape = newwin(card_frame->height, - card_frame->width, - card_frame->start_y, - card_frame->start_x); - - return; -} - -void set_card_attributes(struct card *card, - enum value value, - enum suit suit, - char exposed) { - card->value = value; - card->suit = suit; - card->exposed = exposed; - - return; -} - -void set_card(struct card *card, - enum value value, - enum suit suit, - char exposed, - int start_y, - int start_x) { - set_card_attributes(card, value, suit, exposed); - set_card_frame(card->frame, start_y, start_x); - - return; -} - -void delete_card(struct card *card) { - free(card->frame->shape); - free(card->frame); - free(card); - - return; -}