Merge remote-tracking branch 'AZMCODE/tty-solitaire/auto-win-disable'

This commit is contained in:
maride 2024-11-26 22:25:01 +01:00
commit 2024d53e48
3 changed files with 29 additions and 16 deletions

View File

@ -200,7 +200,7 @@ static void deal_cards(struct deck *deck) {
} }
void game_init(struct game *game, int passes_through_deck, void game_init(struct game *game, int passes_through_deck,
int four_color_deck) { int four_color_deck, int auto_win_disable) {
cursor_malloc(&cursor); cursor_malloc(&cursor);
cursor_init(cursor); cursor_init(cursor);
deck_malloc(&deck); deck_malloc(&deck);
@ -220,6 +220,7 @@ void game_init(struct game *game, int passes_through_deck,
} }
game->four_color_deck = four_color_deck; game->four_color_deck = four_color_deck;
game->auto_win_disable = auto_win_disable;
fill_deck(deck); fill_deck(deck);
shuffle_deck(deck); shuffle_deck(deck);
@ -236,20 +237,27 @@ void game_end() {
deck_free(deck); deck_free(deck);
} }
bool game_won() { bool game_won(int auto_win_disable) {
// If any card in the maneuvre stacks is covered, game is not won. if (auto_win_disable) {
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) { return(stack_length(deck->foundation[0]) == 13 &&
for (struct stack *j = deck->maneuvre[i]; j != NULL; j = j->next) { stack_length(deck->foundation[1]) == 13 &&
if (j->card->face == COVERED) { stack_length(deck->foundation[2]) == 13 &&
return (false); stack_length(deck->foundation[3]) == 13);
} else {
// If any card in the maneuvre stacks is covered, game is not won.
for (int i = 0; i < MANEUVRE_STACKS_NUMBER; i++) {
for (struct stack *j = deck->maneuvre[i]; j != NULL; j = j->next) {
if (j->card->face == COVERED) {
return (false);
}
} }
} }
}
// If the stock pile or the waste pile aren't empty, game is not won. // If the stock pile or the waste pile aren't empty, game is not won.
if (!stack_empty(deck->stock) || !stack_empty(deck->waste_pile)) { if (!stack_empty(deck->stock) || !stack_empty(deck->waste_pile)) {
return (false); return (false);
} }
return (true); return (true);
}
} }

View File

@ -33,6 +33,7 @@
struct game { struct game {
int passes_through_deck_left; int passes_through_deck_left;
int four_color_deck; int four_color_deck;
int auto_win_disable;
}; };
extern struct deck *deck; extern struct deck *deck;
@ -44,7 +45,7 @@ bool valid_move(struct stack *, struct stack *);
void move_card(struct stack **, struct stack **); void move_card(struct stack **, struct stack **);
void move_block(struct stack **, struct stack **, int); void move_block(struct stack **, struct stack **, int);
void expose_top(struct stack **); void expose_top(struct stack **);
void game_init(struct game *, int, int); void game_init(struct game *, int, int, int);
bool game_won(); bool game_won();
void game_end(); void game_end();

View File

@ -26,12 +26,14 @@ int main(int argc, char *argv[]) {
int passes_through_deck = 3; int passes_through_deck = 3;
static int four_color_deck; static int four_color_deck;
static int no_background_color; static int no_background_color;
static int auto_win_disable;
static const struct option options[] = { static const struct option options[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'v'},
{"passes", required_argument, NULL, 'p'}, {"passes", required_argument, NULL, 'p'},
{"four-color-deck", no_argument, &four_color_deck, 1}, {"four-color-deck", no_argument, &four_color_deck, 1},
{"no-background-color", no_argument, &no_background_color, 1}, {"no-background-color", no_argument, &no_background_color, 1},
{"auto-win-disable", no_argument, &auto_win_disable, 1},
{0, 0, 0, 0}}; {0, 0, 0, 0}};
program_name = basename(argv[0]); program_name = basename(argv[0]);
@ -108,7 +110,7 @@ int main(int argc, char *argv[]) {
if (key == KEY_SPACEBAR) { if (key == KEY_SPACEBAR) {
clear(); clear();
refresh(); refresh();
game_init(&game, passes_through_deck, four_color_deck); game_init(&game, passes_through_deck, four_color_deck, auto_win_disable);
break; break;
} }
} else if (key == KEY_RESIZE) { } else if (key == KEY_RESIZE) {
@ -120,7 +122,7 @@ int main(int argc, char *argv[]) {
do { do {
keyboard_event(getch()); keyboard_event(getch());
} while (!game_won()); } while (!game_won(game.auto_win_disable));
endwin(); endwin();
game_end(); game_end();
@ -148,6 +150,8 @@ void usage(const char *program_name) {
"(default: false)\n"); "(default: false)\n");
printf(" --no-background-color Don't draw background color " printf(" --no-background-color Don't draw background color "
"(default: false)\n"); "(default: false)\n");
printf(" --auto-win-disable Disable auto-winning the game "
"(default: false)\n");
} }
void version() { printf("%s\n", VERSION); } void version() { printf("%s\n", VERSION); }