From 196a11d1fecc8a98d64b390d5560c135326cee1b Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Sun, 6 Feb 2011 00:32:21 -0200 Subject: [PATCH] Moving responsibility off from main file. --- lib/game.c | 1 + lib/keyboard.c | 30 ++++++++++++++++++++++++++++++ lib/keyboard.h | 2 ++ src/tty-solitaire.c | 44 +++++++++----------------------------------- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/game.c b/lib/game.c index 19bffbd..f73b275 100644 --- a/lib/game.c +++ b/lib/game.c @@ -217,6 +217,7 @@ void initialize_game() { } void end_game(struct deck *deck) { + print_deck(deck); // debugging purposes delete_deck(deck); return; diff --git a/lib/keyboard.c b/lib/keyboard.c index a174435..a15915d 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -111,3 +111,33 @@ int key_event() { return(pressed_key); } + +void handle_keyboard_event(int key) { + switch (key) { + case 'h': + case KEY_LEFT: + move_cursor(cursor, LEFT); + break; + case 'j': + case KEY_DOWN: + move_cursor(cursor, DOWN); + break; + case 'k': + case KEY_UP: + move_cursor(cursor, UP); + break; + case 'l': + case KEY_RIGHT: + move_cursor(cursor, RIGHT); + break; + case KEY_SPACEBAR: + if (cursor_on_stock(cursor)) { + handle_stock_event(); + } else { + handle_card_movement(cursor); + } + break; + } + + return; +} diff --git a/lib/keyboard.h b/lib/keyboard.h index 7d3cc22..78e0328 100644 --- a/lib/keyboard.h +++ b/lib/keyboard.h @@ -7,6 +7,7 @@ #define KEY_SPACEBAR ' ' extern struct deck *deck; +extern struct cursor *cursor; void mark_origin(struct cursor *); struct stack *cursor_stack(struct cursor *); @@ -15,5 +16,6 @@ bool cursor_on_invalid_spot(struct cursor *); void handle_stock_event(); void handle_card_movement(struct cursor *); int key_event(); +void handle_keyboard_event(); #endif diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index d080183..61b8892 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -4,17 +4,14 @@ #include "../lib/cursor.h" #include "../lib/keyboard.h" -extern struct deck *deck; -extern struct cursor *cursor; - int main(int argc, const char *argv[]) { - int option; + int key; initialize_curses(); greet_player(); - while (option != KEY_SPACEBAR) { - switch (option = getch()) { + while (key != KEY_SPACEBAR) { + switch (key = getch()) { case KEY_SPACEBAR: initialize_game(); break; @@ -27,35 +24,12 @@ int main(int argc, const char *argv[]) { } while (1) { - switch (option = getch()) { - case 'h': - case KEY_LEFT: - move_cursor(cursor, LEFT); - break; - case 'j': - case KEY_DOWN: - move_cursor(cursor, DOWN); - break; - case 'k': - case KEY_UP: - move_cursor(cursor, UP); - break; - case 'l': - case KEY_RIGHT: - move_cursor(cursor, RIGHT); - break; - case KEY_SPACEBAR: - if (cursor_on_stock(cursor)) { - handle_stock_event(); - } else { - handle_card_movement(cursor); - } - break; - case 'q': - case 'Q': - end_curses(); - print_deck(deck); - exit(0); + if ((key = getch()) == 'q' || key == 'Q') { + end_game(); + end_curses(); + exit(0); + } else { + handle_keyboard_event(key); } }