From e65e4e2875eabdf76302975dd8edff3d1addbcfa Mon Sep 17 00:00:00 2001 From: Murilo Soares Pereira Date: Mon, 12 Apr 2010 23:42:21 -0300 Subject: [PATCH] Moved code into 'game' and 'util', removed unnecessary headers. --- lib/card.c | 1 - lib/deck.c | 185 ----------------------------------- lib/deck.h | 33 ------- lib/display.c | 127 ++++++------------------ lib/display.h | 9 +- lib/game.c | 229 ++++++++++++++++++++++++++++++++++++++++++++ lib/game.h | 44 +++++++++ lib/util.c | 25 +++++ lib/util.h | 7 ++ src/tty-solitaire.c | 6 +- 10 files changed, 339 insertions(+), 327 deletions(-) create mode 100644 lib/game.c create mode 100644 lib/game.h create mode 100644 lib/util.c create mode 100644 lib/util.h diff --git a/lib/card.c b/lib/card.c index 573e23e..61bfb69 100644 --- a/lib/card.c +++ b/lib/card.c @@ -1,4 +1,3 @@ -#include #include #include "card.h" diff --git a/lib/deck.c b/lib/deck.c index ad76ff4..0016b61 100644 --- a/lib/deck.c +++ b/lib/deck.c @@ -1,6 +1,4 @@ -#include #include -#include #include "deck.h" void allocate_deck(struct deck **deck) { @@ -66,186 +64,3 @@ void delete_deck(struct deck *deck) { return; } - -void set_deck_stacks_coordinates(struct deck *deck) { - set_frame(deck->stock->card->frame, - STOCK_STARTING_Y, - STOCK_STARTING_X); - set_frame(deck->waste_pile->card->frame, - WASTE_PILE_STARTING_Y, - WASTE_PILE_STARTING_X); - set_frame(deck->foundation_0->card->frame, - FOUNDATION_0_STARTING_Y, - FOUNDATION_0_STARTING_X); - set_frame(deck->foundation_1->card->frame, - FOUNDATION_1_STARTING_Y, - FOUNDATION_1_STARTING_X); - set_frame(deck->foundation_2->card->frame, - FOUNDATION_2_STARTING_Y, - FOUNDATION_2_STARTING_X); - set_frame(deck->foundation_3->card->frame, - FOUNDATION_3_STARTING_Y, - FOUNDATION_3_STARTING_X); - set_frame(deck->maneuvre_0->card->frame, - MANEUVRE_0_STARTING_Y, - MANEUVRE_0_STARTING_X); - set_frame(deck->maneuvre_1->card->frame, - MANEUVRE_1_STARTING_Y, - MANEUVRE_1_STARTING_X); - set_frame(deck->maneuvre_2->card->frame, - MANEUVRE_2_STARTING_Y, - MANEUVRE_2_STARTING_X); - set_frame(deck->maneuvre_3->card->frame, - MANEUVRE_3_STARTING_Y, - MANEUVRE_3_STARTING_X); - set_frame(deck->maneuvre_4->card->frame, - MANEUVRE_4_STARTING_Y, - MANEUVRE_4_STARTING_X); - set_frame(deck->maneuvre_5->card->frame, - MANEUVRE_5_STARTING_Y, - MANEUVRE_5_STARTING_X); - set_frame(deck->maneuvre_6->card->frame, - MANEUVRE_6_STARTING_Y, - MANEUVRE_6_STARTING_X); - - return; -} - -void fill_deck(struct deck *deck) { - struct card *card[NUMBER_OF_CARDS]; - - for (int i = 0; i < NUMBER_OF_CARDS; i++) { - allocate_card(&(card[i])); - } - set_card(card[0], TWO, DIAMONDS, COVERED, 1, 1); - set_card(card[1], TWO, SPADES, COVERED, 1, 1); - set_card(card[2], TWO, HEARTS, COVERED, 1, 1); - set_card(card[3], TWO, CLUBS, COVERED, 1, 1); - set_card(card[4], THREE, DIAMONDS, COVERED, 1, 1); - set_card(card[5], THREE, SPADES, COVERED, 1, 1); - set_card(card[6], THREE, HEARTS, COVERED, 1, 1); - set_card(card[7], THREE, CLUBS, COVERED, 1, 1); - set_card(card[8], FOUR, DIAMONDS, COVERED, 1, 1); - set_card(card[9], FOUR, SPADES, COVERED, 1, 1); - set_card(card[10], FOUR, HEARTS, COVERED, 1, 1); - set_card(card[11], FOUR, CLUBS, COVERED, 1, 1); - set_card(card[12], FIVE, DIAMONDS, COVERED, 1, 1); - set_card(card[13], FIVE, SPADES, COVERED, 1, 1); - set_card(card[14], FIVE, HEARTS, COVERED, 1, 1); - set_card(card[15], FIVE, CLUBS, COVERED, 1, 1); - set_card(card[16], SIX, DIAMONDS, COVERED, 1, 1); - set_card(card[17], SIX, SPADES, COVERED, 1, 1); - set_card(card[18], SIX, HEARTS, COVERED, 1, 1); - set_card(card[19], SIX, CLUBS, COVERED, 1, 1); - set_card(card[20], SEVEN, DIAMONDS, COVERED, 1, 1); - set_card(card[21], SEVEN, SPADES, COVERED, 1, 1); - set_card(card[22], SEVEN, HEARTS, COVERED, 1, 1); - set_card(card[23], SEVEN, CLUBS, COVERED, 1, 1); - set_card(card[24], EIGHT, DIAMONDS, COVERED, 1, 1); - set_card(card[25], EIGHT, SPADES, COVERED, 1, 1); - set_card(card[26], EIGHT, HEARTS, COVERED, 1, 1); - set_card(card[27], EIGHT, CLUBS, COVERED, 1, 1); - set_card(card[28], NINE, DIAMONDS, COVERED, 1, 1); - set_card(card[29], NINE, SPADES, COVERED, 1, 1); - set_card(card[30], NINE, HEARTS, COVERED, 1, 1); - set_card(card[31], NINE, CLUBS, COVERED, 1, 1); - set_card(card[32], TEN, DIAMONDS, COVERED, 1, 1); - set_card(card[33], TEN, SPADES, COVERED, 1, 1); - set_card(card[34], TEN, HEARTS, COVERED, 1, 1); - set_card(card[35], TEN, CLUBS, COVERED, 1, 1); - set_card(card[36], JACK, DIAMONDS, COVERED, 1, 1); - set_card(card[37], JACK, SPADES, COVERED, 1, 1); - set_card(card[38], JACK, HEARTS, COVERED, 1, 1); - set_card(card[39], JACK, CLUBS, COVERED, 1, 1); - set_card(card[40], QUEEN, DIAMONDS, COVERED, 1, 1); - set_card(card[41], QUEEN, SPADES, COVERED, 1, 1); - set_card(card[42], QUEEN, HEARTS, COVERED, 1, 1); - set_card(card[43], QUEEN, CLUBS, COVERED, 1, 1); - set_card(card[44], KING, DIAMONDS, COVERED, 1, 1); - set_card(card[45], KING, SPADES, COVERED, 1, 1); - set_card(card[46], KING, HEARTS, COVERED, 1, 1); - set_card(card[47], KING, CLUBS, COVERED, 1, 1); - set_card(card[48], ACE, DIAMONDS, COVERED, 1, 1); - set_card(card[49], ACE, SPADES, COVERED, 1, 1); - set_card(card[50], ACE, HEARTS, COVERED, 1, 1); - set_card(card[51], ACE, CLUBS, COVERED, 1, 1); - - for (int i = 0; i < NUMBER_OF_CARDS; i++) { - push(&(deck->stock), card[i]); - } - - return; -} - -void shuffle_deck(struct deck *deck) { - struct stack **stack = NULL; - struct stack tmp; - int random; - - stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)); - - for (int i = 0; i < NUMBER_OF_CARDS; i++) { - stack[i] = pop(&(deck->stock)); - } - - srand(time(NULL)); - - for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) { - random = i + (rand() % (NUMBER_OF_CARDS) - i); - tmp = (*stack[i]); - (*stack[i]) = (*stack[random]); - (*stack[random]) = tmp; - } - - for (int i = 0; i < NUMBER_OF_CARDS; i++) { - push(&(deck->stock), stack[i]->card); - } - - return; -} - -void deal_cards(struct deck *deck) { - move_card(&(deck->stock), &(deck->maneuvre_0)); - expose_card(deck->maneuvre_0->card); - move_card(&(deck->stock), &(deck->maneuvre_1)); - move_card(&(deck->stock), &(deck->maneuvre_2)); - move_card(&(deck->stock), &(deck->maneuvre_3)); - move_card(&(deck->stock), &(deck->maneuvre_4)); - move_card(&(deck->stock), &(deck->maneuvre_5)); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_1)); - expose_card(deck->maneuvre_1->card); - move_card(&(deck->stock), &(deck->maneuvre_2)); - move_card(&(deck->stock), &(deck->maneuvre_3)); - move_card(&(deck->stock), &(deck->maneuvre_4)); - move_card(&(deck->stock), &(deck->maneuvre_5)); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_2)); - expose_card(deck->maneuvre_2->card); - move_card(&(deck->stock), &(deck->maneuvre_3)); - move_card(&(deck->stock), &(deck->maneuvre_4)); - move_card(&(deck->stock), &(deck->maneuvre_5)); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_3)); - expose_card(deck->maneuvre_3->card); - move_card(&(deck->stock), &(deck->maneuvre_4)); - move_card(&(deck->stock), &(deck->maneuvre_5)); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_4)); - expose_card(deck->maneuvre_4->card); - move_card(&(deck->stock), &(deck->maneuvre_5)); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_5)); - expose_card(deck->maneuvre_5->card); - move_card(&(deck->stock), &(deck->maneuvre_6)); - - move_card(&(deck->stock), &(deck->maneuvre_6)); - expose_card(deck->maneuvre_6->card); - - return; -} diff --git a/lib/deck.h b/lib/deck.h index ba0e55c..76dd3cc 100644 --- a/lib/deck.h +++ b/lib/deck.h @@ -3,35 +3,6 @@ #include "stack.h" -#define NUMBER_OF_CARDS 52 - -#define STOCK_STARTING_X 1 -#define STOCK_STARTING_Y 1 -#define WASTE_PILE_STARTING_X 9 -#define WASTE_PILE_STARTING_Y 1 -#define FOUNDATION_0_STARTING_X 25 -#define FOUNDATION_0_STARTING_Y 1 -#define FOUNDATION_1_STARTING_X 33 -#define FOUNDATION_1_STARTING_Y 1 -#define FOUNDATION_2_STARTING_X 41 -#define FOUNDATION_2_STARTING_Y 1 -#define FOUNDATION_3_STARTING_X 49 -#define FOUNDATION_3_STARTING_Y 1 -#define MANEUVRE_0_STARTING_X 1 -#define MANEUVRE_0_STARTING_Y 7 -#define MANEUVRE_1_STARTING_X 9 -#define MANEUVRE_1_STARTING_Y 7 -#define MANEUVRE_2_STARTING_X 17 -#define MANEUVRE_2_STARTING_Y 7 -#define MANEUVRE_3_STARTING_X 25 -#define MANEUVRE_3_STARTING_Y 7 -#define MANEUVRE_4_STARTING_X 33 -#define MANEUVRE_4_STARTING_Y 7 -#define MANEUVRE_5_STARTING_X 41 -#define MANEUVRE_5_STARTING_Y 7 -#define MANEUVRE_6_STARTING_X 49 -#define MANEUVRE_6_STARTING_Y 7 - struct deck { struct stack *stock; struct stack *waste_pile; @@ -53,9 +24,5 @@ struct deck { void allocate_deck(struct deck **); void initialize_deck(struct deck *); void delete_deck(struct deck *); -void set_deck_stacks_coordinates(struct deck *); -void fill_deck(struct deck *); -void shuffle_deck(struct deck *); -void deal_cards(struct deck *); #endif diff --git a/lib/display.c b/lib/display.c index ff2bd90..fd74ea8 100644 --- a/lib/display.c +++ b/lib/display.c @@ -1,103 +1,8 @@ -#include #include #include #include -#include -#include "deck.h" #include "display.h" - -void initialize_curses() { - setlocale(LC_ALL, ""); /* supporting unicode characters */ - initscr(); /* initialize the terminal in curses mode */ - raw(); /* disable line buffers */ - noecho(); /* character echo is unnecessary */ - keypad(stdscr, TRUE); /* enable F and arrow keys */ - start_color(); /* I want colors */ - curs_set(FALSE); /* invisible cursor */ - - init_pair(1, COLOR_BLACK, COLOR_WHITE); - init_pair(2, COLOR_RED, COLOR_WHITE); - init_pair(3, COLOR_WHITE, COLOR_BLUE); - - return; -} - -void end_curses() { - endwin(); - puts("Game finished."); - - return; -} - -void initialize_game() { - struct deck *deck = NULL; - int pressed_key; - - mvprintw(11, 27, "Welcome to tty-solitaire."); - mvprintw(12, 19, "Press the space bar to play or q to quit."); - while (1) { - switch (pressed_key = getch()) { - case KEY_SPACEBAR: - clear(); - refresh(); - assume_default_colors(COLOR_WHITE, COLOR_GREEN); - draw_empty_stacks(); - allocate_deck(&deck); - initialize_deck(deck); - set_deck_stacks_coordinates(deck); - fill_deck(deck); - shuffle_deck(deck); - deal_cards(deck); - draw_game(deck); - getchar(); - end_curses(); - end_game(deck); - return; - case 'q': - case 'Q': - end_curses(); - return; - } - } - - return; -} - -void end_game(struct deck *deck) { - delete_deck(deck); - - return; -} - -void draw_empty_stacks() { - WINDOW **empty_stack; - - empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack)); - - empty_stack[0] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 1); - empty_stack[1] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 9); - empty_stack[2] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 25); - empty_stack[3] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 33); - empty_stack[4] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 41); - empty_stack[5] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 49); - empty_stack[6] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 1); - empty_stack[7] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 9); - empty_stack[8] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 17); - empty_stack[9] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 25); - empty_stack[10] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 33); - empty_stack[11] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 41); - empty_stack[12] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 49); - - for (int i = 0; i < EMPTY_STACKS_NUMBER; i++) { - box(empty_stack[i], 0, 0); - wrefresh(empty_stack[i]); - delwin(empty_stack[i]); - } - - free(empty_stack); - - return; -} +#include "game.h" char *card_suit(enum suit suit) { char *card_suit; @@ -140,6 +45,36 @@ char *card_value(enum value value) { return(card_value); } +void draw_empty_stacks() { + WINDOW **empty_stack; + + empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack)); + + empty_stack[0] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 1); + empty_stack[1] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 9); + empty_stack[2] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 25); + empty_stack[3] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 33); + empty_stack[4] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 41); + empty_stack[5] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 49); + empty_stack[6] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 1); + empty_stack[7] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 9); + empty_stack[8] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 17); + empty_stack[9] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 25); + empty_stack[10] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 33); + empty_stack[11] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 41); + empty_stack[12] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 49); + + for (int i = 0; i < EMPTY_STACKS_NUMBER; i++) { + box(empty_stack[i], 0, 0); + wrefresh(empty_stack[i]); + delwin(empty_stack[i]); + } + + free(empty_stack); + + return; +} + void draw_value(struct card *card) { mvwprintw(card->frame->shape, 0, 0, card_value(card->value)); mvwprintw(card->frame->shape, 4, diff --git a/lib/display.h b/lib/display.h index 2f8deb8..1a73859 100644 --- a/lib/display.h +++ b/lib/display.h @@ -1,11 +1,8 @@ #ifndef DISPLAY_H #define DISPLAY_H -#include "card.h" #include "deck.h" -#define KEY_SPACEBAR ' ' - #define EMPTY_STACKS_NUMBER 13 #define DIAMONDS_SYMBOL "\u2666" @@ -17,13 +14,9 @@ #define RED_ON_WHITE 2 #define WHITE_ON_BLUE 3 -void initialize_curses(); -void end_curses(); -void initialize_game(); -void end_game(); -void draw_empty_stacks(); char *card_suit(enum suit); char *card_value(enum value); +void draw_empty_stacks(); void draw_value(struct card *); void draw_suit(struct card *); void draw_front(struct card *); diff --git a/lib/game.c b/lib/game.c new file mode 100644 index 0000000..6056db7 --- /dev/null +++ b/lib/game.c @@ -0,0 +1,229 @@ +#include +#include +#include +#include "display.h" +#include "util.h" +#include "game.h" + +void set_stacks_coordinates(struct deck *deck) { + set_frame(deck->stock->card->frame, + STOCK_STARTING_Y, + STOCK_STARTING_X); + set_frame(deck->waste_pile->card->frame, + WASTE_PILE_STARTING_Y, + WASTE_PILE_STARTING_X); + set_frame(deck->foundation_0->card->frame, + FOUNDATION_0_STARTING_Y, + FOUNDATION_0_STARTING_X); + set_frame(deck->foundation_1->card->frame, + FOUNDATION_1_STARTING_Y, + FOUNDATION_1_STARTING_X); + set_frame(deck->foundation_2->card->frame, + FOUNDATION_2_STARTING_Y, + FOUNDATION_2_STARTING_X); + set_frame(deck->foundation_3->card->frame, + FOUNDATION_3_STARTING_Y, + FOUNDATION_3_STARTING_X); + set_frame(deck->maneuvre_0->card->frame, + MANEUVRE_0_STARTING_Y, + MANEUVRE_0_STARTING_X); + set_frame(deck->maneuvre_1->card->frame, + MANEUVRE_1_STARTING_Y, + MANEUVRE_1_STARTING_X); + set_frame(deck->maneuvre_2->card->frame, + MANEUVRE_2_STARTING_Y, + MANEUVRE_2_STARTING_X); + set_frame(deck->maneuvre_3->card->frame, + MANEUVRE_3_STARTING_Y, + MANEUVRE_3_STARTING_X); + set_frame(deck->maneuvre_4->card->frame, + MANEUVRE_4_STARTING_Y, + MANEUVRE_4_STARTING_X); + set_frame(deck->maneuvre_5->card->frame, + MANEUVRE_5_STARTING_Y, + MANEUVRE_5_STARTING_X); + set_frame(deck->maneuvre_6->card->frame, + MANEUVRE_6_STARTING_Y, + MANEUVRE_6_STARTING_X); + + return; +} + +void fill_deck(struct deck *deck) { + struct card *card[NUMBER_OF_CARDS]; + + for (int i = 0; i < NUMBER_OF_CARDS; i++) { + allocate_card(&(card[i])); + } + set_card(card[0], TWO, DIAMONDS, COVERED, 1, 1); + set_card(card[1], TWO, SPADES, COVERED, 1, 1); + set_card(card[2], TWO, HEARTS, COVERED, 1, 1); + set_card(card[3], TWO, CLUBS, COVERED, 1, 1); + set_card(card[4], THREE, DIAMONDS, COVERED, 1, 1); + set_card(card[5], THREE, SPADES, COVERED, 1, 1); + set_card(card[6], THREE, HEARTS, COVERED, 1, 1); + set_card(card[7], THREE, CLUBS, COVERED, 1, 1); + set_card(card[8], FOUR, DIAMONDS, COVERED, 1, 1); + set_card(card[9], FOUR, SPADES, COVERED, 1, 1); + set_card(card[10], FOUR, HEARTS, COVERED, 1, 1); + set_card(card[11], FOUR, CLUBS, COVERED, 1, 1); + set_card(card[12], FIVE, DIAMONDS, COVERED, 1, 1); + set_card(card[13], FIVE, SPADES, COVERED, 1, 1); + set_card(card[14], FIVE, HEARTS, COVERED, 1, 1); + set_card(card[15], FIVE, CLUBS, COVERED, 1, 1); + set_card(card[16], SIX, DIAMONDS, COVERED, 1, 1); + set_card(card[17], SIX, SPADES, COVERED, 1, 1); + set_card(card[18], SIX, HEARTS, COVERED, 1, 1); + set_card(card[19], SIX, CLUBS, COVERED, 1, 1); + set_card(card[20], SEVEN, DIAMONDS, COVERED, 1, 1); + set_card(card[21], SEVEN, SPADES, COVERED, 1, 1); + set_card(card[22], SEVEN, HEARTS, COVERED, 1, 1); + set_card(card[23], SEVEN, CLUBS, COVERED, 1, 1); + set_card(card[24], EIGHT, DIAMONDS, COVERED, 1, 1); + set_card(card[25], EIGHT, SPADES, COVERED, 1, 1); + set_card(card[26], EIGHT, HEARTS, COVERED, 1, 1); + set_card(card[27], EIGHT, CLUBS, COVERED, 1, 1); + set_card(card[28], NINE, DIAMONDS, COVERED, 1, 1); + set_card(card[29], NINE, SPADES, COVERED, 1, 1); + set_card(card[30], NINE, HEARTS, COVERED, 1, 1); + set_card(card[31], NINE, CLUBS, COVERED, 1, 1); + set_card(card[32], TEN, DIAMONDS, COVERED, 1, 1); + set_card(card[33], TEN, SPADES, COVERED, 1, 1); + set_card(card[34], TEN, HEARTS, COVERED, 1, 1); + set_card(card[35], TEN, CLUBS, COVERED, 1, 1); + set_card(card[36], JACK, DIAMONDS, COVERED, 1, 1); + set_card(card[37], JACK, SPADES, COVERED, 1, 1); + set_card(card[38], JACK, HEARTS, COVERED, 1, 1); + set_card(card[39], JACK, CLUBS, COVERED, 1, 1); + set_card(card[40], QUEEN, DIAMONDS, COVERED, 1, 1); + set_card(card[41], QUEEN, SPADES, COVERED, 1, 1); + set_card(card[42], QUEEN, HEARTS, COVERED, 1, 1); + set_card(card[43], QUEEN, CLUBS, COVERED, 1, 1); + set_card(card[44], KING, DIAMONDS, COVERED, 1, 1); + set_card(card[45], KING, SPADES, COVERED, 1, 1); + set_card(card[46], KING, HEARTS, COVERED, 1, 1); + set_card(card[47], KING, CLUBS, COVERED, 1, 1); + set_card(card[48], ACE, DIAMONDS, COVERED, 1, 1); + set_card(card[49], ACE, SPADES, COVERED, 1, 1); + set_card(card[50], ACE, HEARTS, COVERED, 1, 1); + set_card(card[51], ACE, CLUBS, COVERED, 1, 1); + + for (int i = 0; i < NUMBER_OF_CARDS; i++) { + push(&(deck->stock), card[i]); + } + + return; +} + +void shuffle_deck(struct deck *deck) { + struct stack **stack = NULL; + struct stack tmp; + int random; + + stack = malloc(NUMBER_OF_CARDS * sizeof(*stack)); + + for (int i = 0; i < NUMBER_OF_CARDS; i++) { + stack[i] = pop(&(deck->stock)); + } + + srand(time(NULL)); + + for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) { + random = i + (rand() % (NUMBER_OF_CARDS) - i); + tmp = (*stack[i]); + (*stack[i]) = (*stack[random]); + (*stack[random]) = tmp; + } + + for (int i = 0; i < NUMBER_OF_CARDS; i++) { + push(&(deck->stock), stack[i]->card); + } + + return; +} + +void deal_cards(struct deck *deck) { + move_card(&(deck->stock), &(deck->maneuvre_0)); + expose_card(deck->maneuvre_0->card); + move_card(&(deck->stock), &(deck->maneuvre_1)); + move_card(&(deck->stock), &(deck->maneuvre_2)); + move_card(&(deck->stock), &(deck->maneuvre_3)); + move_card(&(deck->stock), &(deck->maneuvre_4)); + move_card(&(deck->stock), &(deck->maneuvre_5)); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_1)); + expose_card(deck->maneuvre_1->card); + move_card(&(deck->stock), &(deck->maneuvre_2)); + move_card(&(deck->stock), &(deck->maneuvre_3)); + move_card(&(deck->stock), &(deck->maneuvre_4)); + move_card(&(deck->stock), &(deck->maneuvre_5)); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_2)); + expose_card(deck->maneuvre_2->card); + move_card(&(deck->stock), &(deck->maneuvre_3)); + move_card(&(deck->stock), &(deck->maneuvre_4)); + move_card(&(deck->stock), &(deck->maneuvre_5)); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_3)); + expose_card(deck->maneuvre_3->card); + move_card(&(deck->stock), &(deck->maneuvre_4)); + move_card(&(deck->stock), &(deck->maneuvre_5)); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_4)); + expose_card(deck->maneuvre_4->card); + move_card(&(deck->stock), &(deck->maneuvre_5)); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_5)); + expose_card(deck->maneuvre_5->card); + move_card(&(deck->stock), &(deck->maneuvre_6)); + + move_card(&(deck->stock), &(deck->maneuvre_6)); + expose_card(deck->maneuvre_6->card); + + return; +} + +void initialize_game() { + struct deck *deck = NULL; + int pressed_key; + + mvprintw(11, 27, "Welcome to tty-solitaire."); + mvprintw(12, 19, "Press the space bar to play or q to quit."); + while (1) { + switch (pressed_key = getch()) { + case KEY_SPACEBAR: + clear(); + refresh(); + assume_default_colors(COLOR_WHITE, COLOR_GREEN); + draw_empty_stacks(); + allocate_deck(&deck); + initialize_deck(deck); + set_stacks_coordinates(deck); + fill_deck(deck); + shuffle_deck(deck); + deal_cards(deck); + draw_game(deck); + getchar(); + end_curses(); + end_game(deck); + return; + case 'q': + case 'Q': + end_curses(); + return; + } + } + + return; +} + +void end_game(struct deck *deck) { + delete_deck(deck); + + return; +} diff --git a/lib/game.h b/lib/game.h new file mode 100644 index 0000000..f74325c --- /dev/null +++ b/lib/game.h @@ -0,0 +1,44 @@ +#ifndef GAME_H +#define GAME_H + +#include "deck.h" + +#define KEY_SPACEBAR ' ' + +#define NUMBER_OF_CARDS 52 + +#define STOCK_STARTING_X 1 +#define STOCK_STARTING_Y 1 +#define WASTE_PILE_STARTING_X 9 +#define WASTE_PILE_STARTING_Y 1 +#define FOUNDATION_0_STARTING_X 25 +#define FOUNDATION_0_STARTING_Y 1 +#define FOUNDATION_1_STARTING_X 33 +#define FOUNDATION_1_STARTING_Y 1 +#define FOUNDATION_2_STARTING_X 41 +#define FOUNDATION_2_STARTING_Y 1 +#define FOUNDATION_3_STARTING_X 49 +#define FOUNDATION_3_STARTING_Y 1 +#define MANEUVRE_0_STARTING_X 1 +#define MANEUVRE_0_STARTING_Y 7 +#define MANEUVRE_1_STARTING_X 9 +#define MANEUVRE_1_STARTING_Y 7 +#define MANEUVRE_2_STARTING_X 17 +#define MANEUVRE_2_STARTING_Y 7 +#define MANEUVRE_3_STARTING_X 25 +#define MANEUVRE_3_STARTING_Y 7 +#define MANEUVRE_4_STARTING_X 33 +#define MANEUVRE_4_STARTING_Y 7 +#define MANEUVRE_5_STARTING_X 41 +#define MANEUVRE_5_STARTING_Y 7 +#define MANEUVRE_6_STARTING_X 49 +#define MANEUVRE_6_STARTING_Y 7 + +void set_stacks_coordinates(struct deck *); +void fill_deck(struct deck *); +void shuffle_deck(struct deck *); +void deal_cards(struct deck *); +void initialize_game(); +void end_game(); + +#endif diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..959faaa --- /dev/null +++ b/lib/util.c @@ -0,0 +1,25 @@ +#include +#include + +void initialize_curses() { + setlocale(LC_ALL, ""); /* supporting unicode characters */ + initscr(); /* initialize the terminal in curses mode */ + raw(); /* disable line buffers */ + noecho(); /* character echo is unnecessary */ + keypad(stdscr, TRUE); /* enable F and arrow keys */ + start_color(); /* I want colors */ + curs_set(FALSE); /* invisible cursor */ + + init_pair(1, COLOR_BLACK, COLOR_WHITE); + init_pair(2, COLOR_RED, COLOR_WHITE); + init_pair(3, COLOR_WHITE, COLOR_BLUE); + + return; +} + +void end_curses() { + endwin(); + puts("Game finished."); + + return; +} diff --git a/lib/util.h b/lib/util.h new file mode 100644 index 0000000..6159cb3 --- /dev/null +++ b/lib/util.h @@ -0,0 +1,7 @@ +#ifndef UTIL_H +#define UTIL_H + +void initialize_curses(); +void end_curses(); + +#endif diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index f8bc148..71af084 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -1,7 +1,5 @@ -#include -#include -#include -#include "../lib/display.h" +#include "../lib/util.h" +#include "../lib/game.h" int main(int argc, const char *argv[]) { initialize_curses();