Moving responsibility off from main file.

This commit is contained in:
Murilo Pereira 2011-02-06 00:32:21 -02:00
parent 8bd7147a8e
commit 196a11d1fe
4 changed files with 42 additions and 35 deletions

View File

@ -217,6 +217,7 @@ void initialize_game() {
}
void end_game(struct deck *deck) {
print_deck(deck); // debugging purposes
delete_deck(deck);
return;

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}
}