diff --git a/lib/cursor.h b/lib/cursor.h index 28af2b7..3485c95 100644 --- a/lib/cursor.h +++ b/lib/cursor.h @@ -7,6 +7,20 @@ #define CURSOR_STARTING_X 4 #define CURSOR_STARTING_Y 7 +#define CURSOR_STOCK_X 4 +#define CURSOR_WASTE_PILE_X 12 +#define CURSOR_FOUNDATION_0_X 28 +#define CURSOR_FOUNDATION_1_X 36 +#define CURSOR_FOUNDATION_2_X 44 +#define CURSOR_FOUNDATION_3_X 52 +#define CURSOR_MANEUVRE_0_X 4 +#define CURSOR_MANEUVRE_1_X 12 +#define CURSOR_MANEUVRE_2_X 20 +#define CURSOR_MANEUVRE_3_X 28 +#define CURSOR_MANEUVRE_4_X 36 +#define CURSOR_MANEUVRE_5_X 44 +#define CURSOR_MANEUVRE_6_X 52 + struct cursor { int x; int y; diff --git a/lib/deck.h b/lib/deck.h index 76dd3cc..1a00cc0 100644 --- a/lib/deck.h +++ b/lib/deck.h @@ -3,6 +3,22 @@ #include "stack.h" +enum stack_name { + STOCK, + WASTE_PILE, + FOUNDATION_0, + FOUNDATION_1, + FOUNDATION_2, + FOUNDATION_3, + MANEUVRE_0, + MANEUVRE_1, + MANEUVRE_2, + MANEUVRE_3, + MANEUVRE_4, + MANEUVRE_5, + MANEUVRE_6 +}; + struct deck { struct stack *stock; struct stack *waste_pile; diff --git a/lib/keyboard.c b/lib/keyboard.c index 1ec156d..40c73e1 100644 --- a/lib/keyboard.c +++ b/lib/keyboard.c @@ -2,6 +2,42 @@ #include "display.h" #include "keyboard.h" +void mark_origin(struct cursor *cursor) { + + return; +} + +struct stack *cursor_stack(struct cursor *cursor) { + struct stack *cursor_stack = NULL; + + if (cursor->y == CURSOR_STARTING_Y) { + switch (cursor->x) { + case CURSOR_STOCK_X: cursor_stack = deck->stock; + case CURSOR_WASTE_PILE_X: cursor_stack = deck->waste_pile; + case CURSOR_FOUNDATION_0_X: cursor_stack = deck->foundation_0; + case CURSOR_FOUNDATION_1_X: cursor_stack = deck->foundation_1; + case CURSOR_FOUNDATION_2_X: cursor_stack = deck->foundation_2; + case CURSOR_FOUNDATION_3_X: cursor_stack = deck->foundation_3; + } + } else { + switch (cursor->x) { + case CURSOR_MANEUVRE_0_X: cursor_stack = deck->maneuvre_0; + case CURSOR_MANEUVRE_1_X: cursor_stack = deck->maneuvre_1; + case CURSOR_MANEUVRE_2_X: cursor_stack = deck->maneuvre_2; + case CURSOR_MANEUVRE_3_X: cursor_stack = deck->maneuvre_3; + case CURSOR_MANEUVRE_4_X: cursor_stack = deck->maneuvre_4; + case CURSOR_MANEUVRE_5_X: cursor_stack = deck->maneuvre_5; + case CURSOR_MANEUVRE_6_X: cursor_stack = deck->maneuvre_6; + } + } + + return(cursor_stack); +} + +bool cursor_on_stack(struct cursor *cursor, struct stack *stack) { + return(cursor_stack(cursor) == stack); +} + void handle_stock_event() { if (!empty(deck->stock)) { /* erase the stack before emptying it */ @@ -17,6 +53,46 @@ void handle_stock_event() { return; } +void handle_card_movement(struct cursor *cursor) { + struct stack *origin = NULL; + struct stack *destination = NULL; + int option; + + mark_origin(cursor); + origin = cursor_stack(cursor); + + 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) && !cursor_on_stack(cursor, origin)) { + destination = cursor_stack(cursor); + move_card(&origin, &destination); + draw_stack(origin); + draw_stack(destination); + } + return; + } + } + + return; +} + int key_event() { int pressed_key; diff --git a/lib/keyboard.h b/lib/keyboard.h index 584da02..b29d3e8 100644 --- a/lib/keyboard.h +++ b/lib/keyboard.h @@ -2,12 +2,17 @@ #define KEYBOARD_H #include "deck.h" +#include "cursor.h" #define KEY_SPACEBAR ' ' extern struct deck *deck; +void mark_origin(struct cursor *); +struct stack *cursor_stack(struct cursor *); +bool cursor_on_stack(struct cursor *, struct stack *); void handle_stock_event(); +void handle_card_movement(struct cursor *); int key_event(); #endif diff --git a/src/tty-solitaire.c b/src/tty-solitaire.c index f9f185b..2b713c8 100644 --- a/src/tty-solitaire.c +++ b/src/tty-solitaire.c @@ -50,7 +50,7 @@ int main(int argc, const char *argv[]) { if (cursor_on_stock(cursor)) { handle_stock_event(); } else { - /*handle_card_event();*/ + handle_card_movement(cursor); } break; case 'q':