diff --git a/lib/game.c b/lib/game.c index 6056db7..9a36e17 100644 --- a/lib/game.c +++ b/lib/game.c @@ -188,36 +188,31 @@ void deal_cards(struct deck *deck) { return; } -void initialize_game() { - struct deck *deck = NULL; - int pressed_key; - +void greet_player() { 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 prepare_game(struct deck **deck) { + draw_empty_stacks(); + allocate_deck(deck); + initialize_deck(*deck); + set_stacks_coordinates(*deck); + fill_deck(*deck); + shuffle_deck(*deck); + deal_cards(*deck); + + return; +} + +void initialize_game() { + struct deck *deck = NULL; + + clear_screen(); + prepare_game(&deck); + draw_game(deck); return; } diff --git a/lib/game.h b/lib/game.h index f74325c..241ae0f 100644 --- a/lib/game.h +++ b/lib/game.h @@ -3,8 +3,6 @@ #include "deck.h" -#define KEY_SPACEBAR ' ' - #define NUMBER_OF_CARDS 52 #define STOCK_STARTING_X 1 @@ -38,7 +36,9 @@ void set_stacks_coordinates(struct deck *); void fill_deck(struct deck *); void shuffle_deck(struct deck *); void deal_cards(struct deck *); +void greet_player(); void initialize_game(); +void prepare_game(struct deck **); void end_game(); #endif diff --git a/lib/keyboard.c b/lib/keyboard.c new file mode 100644 index 0000000..5466c42 --- /dev/null +++ b/lib/keyboard.c @@ -0,0 +1,10 @@ +#include +#include "keyboard.h" + +int key_event() { + int pressed_key; + + pressed_key = getchar(); + + return(pressed_key); +} diff --git a/lib/keyboard.h b/lib/keyboard.h new file mode 100644 index 0000000..72660e3 --- /dev/null +++ b/lib/keyboard.h @@ -0,0 +1,8 @@ +#ifndef KEYBOARD_H +#define KEYBOARD_H + +#define KEY_SPACEBAR ' ' + +int key_event(); + +#endif diff --git a/lib/util.c b/lib/util.c index 959faaa..e59d492 100644 --- a/lib/util.c +++ b/lib/util.c @@ -9,6 +9,7 @@ void initialize_curses() { keypad(stdscr, TRUE); /* enable F and arrow keys */ start_color(); /* I want colors */ curs_set(FALSE); /* invisible cursor */ + assume_default_colors(COLOR_WHITE, COLOR_GREEN); init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_RED, COLOR_WHITE); @@ -23,3 +24,10 @@ void end_curses() { return; } + +void clear_screen() { + clear(); + refresh(); + + return; +} diff --git a/lib/util.h b/lib/util.h index 6159cb3..e02c4fc 100644 --- a/lib/util.h +++ b/lib/util.h @@ -3,5 +3,6 @@ void initialize_curses(); void end_curses(); +void clear_screen(); #endif diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index 71af084..12d3ef7 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -1,10 +1,30 @@ +#include #include "../lib/util.h" #include "../lib/game.h" +#include "../lib/keyboard.h" int main(int argc, const char *argv[]) { + int option; + initialize_curses(); - initialize_game(); + greet_player(); + + while (1) { + switch (option = getch()) { + case KEY_SPACEBAR: + initialize_game(); + break; + case 'q': + case 'Q': + end_curses(); + exit(0); + } + } + + while (1) { + key_event(); + } return(0); }