diff --git a/lib/display.c b/lib/display.c index 9040310..ff2bd90 100644 --- a/lib/display.c +++ b/lib/display.c @@ -6,18 +6,67 @@ #include "deck.h" #include "display.h" -void init_curses() { +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(0); /* invisible cursor */ + 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() { @@ -50,24 +99,6 @@ void draw_empty_stacks() { return; } -void initialize_game() { - struct deck *deck = NULL; - - 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); - - return; -} - char *card_suit(enum suit suit) { char *card_suit; diff --git a/lib/display.h b/lib/display.h index 69529ae..2f8deb8 100644 --- a/lib/display.h +++ b/lib/display.h @@ -4,6 +4,8 @@ #include "card.h" #include "deck.h" +#define KEY_SPACEBAR ' ' + #define EMPTY_STACKS_NUMBER 13 #define DIAMONDS_SYMBOL "\u2666" @@ -15,9 +17,11 @@ #define RED_ON_WHITE 2 #define WHITE_ON_BLUE 3 -void init_curses(); -void draw_empty_stacks(); +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_value(struct card *); diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index 6163c79..f8bc148 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -4,21 +4,9 @@ #include "../lib/display.h" int main(int argc, const char *argv[]) { - char message[] = "Welcome to tty-solitaire."; - int row_number, column_number; - - init_curses(); - - getmaxyx(stdscr, row_number, column_number); - mvprintw(row_number / 2 - 1, - (column_number - strlen(message)) / 2, - "%s\n", - message); - getch(); + initialize_curses(); initialize_game(); - puts("Game finished."); - - return 0; + return(0); }