diff --git a/src/cursor.h b/src/cursor.h index 2782a90..16d9151 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -34,6 +34,7 @@ struct cursor { enum movement { LEFT, DOWN, UP, RIGHT }; extern struct deck *deck; +extern struct game game; void cursor_malloc(struct cursor **); void cursor_init(struct cursor *); diff --git a/src/draw.c b/src/draw.c index 641a223..c84a1cc 100644 --- a/src/draw.c +++ b/src/draw.c @@ -64,6 +64,13 @@ void draw_card(struct card *card) { void draw_stack(struct stack *stack) { if (stack_empty(stack)) { box(stack->card->frame->window, 0, 0); + if (stock_stack(stack)) { + if (game.passes_through_deck_left >= 1) { + mvwprintw(stack->card->frame->window, 2, 3, "O"); + } else { + mvwprintw(stack->card->frame->window, 2, 3, "X"); + } + } wrefresh(stack->card->frame->window); } else { if (maneuvre_stack(stack)) { diff --git a/src/draw.h b/src/draw.h index 9b83484..e79418e 100644 --- a/src/draw.h +++ b/src/draw.h @@ -11,6 +11,8 @@ #define WHITE_ON_BLUE 3 #define WHITE_ON_GREEN 4 +extern struct game game; + void draw_card(struct card *); void draw_stack(struct stack *); void draw_deck(struct deck *); diff --git a/src/game.c b/src/game.c index fc1d4ff..2341439 100644 --- a/src/game.c +++ b/src/game.c @@ -43,11 +43,6 @@ static int maneuvre_begin_x(int x) { } } -static bool stock_stack(struct stack *stack) { - return((stack->card->frame->begin_y == STOCK_BEGIN_Y) && - (stack->card->frame->begin_x == STOCK_BEGIN_X)); -} - static bool waste_pile_stack(struct stack *stack) { return((stack->card->frame->begin_y == WASTE_PILE_BEGIN_Y) && (stack->card->frame->begin_x == WASTE_PILE_BEGIN_X)); @@ -61,6 +56,11 @@ static bool foundation_stack(struct stack *stack) { stack->card->frame->begin_x == FOUNDATION_3_BEGIN_X)); } +bool stock_stack(struct stack *stack) { + return((stack->card->frame->begin_y == STOCK_BEGIN_Y) && + (stack->card->frame->begin_x == STOCK_BEGIN_X)); +} + bool maneuvre_stack(struct stack *stack) { return(stack->card->frame->begin_y >= MANEUVRE_BEGIN_Y && (stack->card->frame->begin_x == MANEUVRE_0_BEGIN_X || @@ -177,7 +177,7 @@ static void deal_cards(struct deck *deck) { } } -void game_init() { +void game_init(struct game *game, int passes_through_deck) { cursor_malloc(&cursor); cursor_init(cursor); deck_malloc(&deck); @@ -199,6 +199,8 @@ void game_init() { draw_cursor(cursor); draw_deck(deck); + + game->passes_through_deck_left = passes_through_deck; } void game_end() { diff --git a/src/game.h b/src/game.h index 5c2f890..266b51f 100644 --- a/src/game.h +++ b/src/game.h @@ -30,14 +30,19 @@ #define MANEUVRE_5_BEGIN_X 41 #define MANEUVRE_6_BEGIN_X 49 +struct game { + int passes_through_deck_left; +}; + struct deck *deck; struct cursor *cursor; bool maneuvre_stack(struct stack *); +bool stock_stack(struct stack *); bool valid_move(struct stack *, struct stack *); void move_card(struct stack **, struct stack **); void move_block(struct stack **, struct stack **, int); -void game_init(); +void game_init(struct game *, int); bool game_won(); void game_end(); diff --git a/src/keyboard.c b/src/keyboard.c index 48d7f4c..55a9380 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -178,10 +178,20 @@ void keyboard_event(int key) { break; case KEY_SPACEBAR: if (cursor_on_stock(cursor)) { - if (!stack_empty(deck->stock)) { + if (stack_empty(deck->stock)) { + if (game.passes_through_deck_left >= 1) { + while (!stack_empty(deck->waste_pile)) { + move_card(&(deck->waste_pile), &(deck->stock)); + } + draw_stack(deck->stock); + draw_stack(deck->waste_pile); + } + } else { move_card(&(deck->stock), &(deck->waste_pile)); + if (stack_empty(deck->stock)) { + game.passes_through_deck_left--; + } card_expose(deck->waste_pile->card); - erase_stack(deck->waste_pile); draw_stack(deck->stock); draw_stack(deck->waste_pile); } diff --git a/src/keyboard.h b/src/keyboard.h index dfbf26d..d3826be 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -9,6 +9,7 @@ extern struct deck *deck; extern struct cursor *cursor; +extern struct game game; void keyboard_event(); diff --git a/src/ttysolitaire.c b/src/ttysolitaire.c index f255999..f355acb 100644 --- a/src/ttysolitaire.c +++ b/src/ttysolitaire.c @@ -8,6 +8,7 @@ #include "keyboard.h" const char *program_name; +struct game game; void draw_greeting() { mvprintw(8, 26, "Welcome to tty-solitaire."); @@ -36,18 +37,23 @@ void version() { int main(int argc, char *argv[]) { int option; int option_index; + int passes_through_deck = 3; static const struct option options[] = { - {"help", no_argument, NULL, 'h'}, - {"version", no_argument, NULL, 'v'} + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'v'}, + {"passes", required_argument, NULL, 'p'} }; program_name = argv[0]; - while ((option = getopt_long(argc, argv, "hv", options, &option_index)) != -1) { + while ((option = getopt_long(argc, argv, "hvp:", options, &option_index)) != -1) { switch (option) { case 'v': version(); exit(0); + case 'p': + passes_through_deck = atoi(optarg); + break; case 'h': default: usage(); @@ -77,7 +83,7 @@ int main(int argc, char *argv[]) { case KEY_SPACEBAR: clear(); refresh(); - game_init(); + game_init(&game, passes_through_deck); break; case 'q': case 'Q':