Skip to content

Commit 49a1d8a

Browse files
committed
Add win condition.
1 parent 4cdbd74 commit 49a1d8a

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/game.c

+14
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ GameRule *new_game_rule(GameRuleType type) {
6262
rule->next_rank = RANK_ANY;
6363
rule->move_group = MOVE_ONE;
6464
rule->from = RULE_ANY;
65+
rule->win_rank = RANK_NONE;
6566
switch (type) {
6667
case RULE_FOUNDATION:
6768
rule->first_rank = RANK_ACE;
6869
rule->next_suit = SUIT_SAME;
6970
rule->next_rank = RANK_UP;
71+
rule->win_rank = RANK_KING;
7072
break;
7173
case RULE_TABLEAU:
7274
rule->next_suit = SUIT_ANY;
@@ -536,3 +538,15 @@ int turn_card(Card *card) {
536538
}
537539
return 0;
538540
}
541+
542+
int check_win_condition(Pile *piles) {
543+
for (Pile *p = piles; p; p = p->next) {
544+
if (p->rule->win_rank != RANK_NONE) {
545+
Card *top = get_top(p->stack);
546+
if (!check_first_rank(top, p->rule->win_rank)) {
547+
return 0;
548+
}
549+
}
550+
}
551+
return 1;
552+
}

src/game.h

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct game_rule {
9090
GameRuleRank next_rank;
9191
GameRuleMove move_group;
9292
GameRuleType from;
93+
GameRuleRank win_rank;
9394
};
9495

9596
struct pile {
@@ -117,6 +118,7 @@ int move_to_foundation(Card *src, Pile *src_pile, Pile *piles);
117118
int move_to_free_cell(Card *src, Pile *src_pile, Pile *piles);
118119
int auto_move_to_foundation(Pile *piles);
119120
int turn_card(Card *card);
121+
int check_win_condition(Pile *piles);
120122

121123
void clear_undo_history();
122124
void undo_move();

src/rc.c

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef enum {
5858
K_NEXT_SUIT,
5959
K_MOVE_GROUP,
6060
K_FROM,
61+
K_WIN_RANK,
6162
K_REDEAL,
6263
K_X_SPACING,
6364
K_Y_SPACING,
@@ -141,6 +142,7 @@ struct symbol game_rule_commands[] ={
141142
{"next_suit", K_NEXT_SUIT},
142143
{"move_group", K_MOVE_GROUP},
143144
{"from", K_FROM},
145+
{"win_rank", K_WIN_RANK},
144146
{NULL, K_UNDEFINED}
145147
};
146148

@@ -637,6 +639,9 @@ GameRule *define_game_rule(FILE *file, GameRuleType type, int index) {
637639
case K_FROM:
638640
rule->from = read_from_rule(file);
639641
break;
642+
case K_WIN_RANK:
643+
rule->win_rank = read_rank(file);
644+
break;
640645
default:
641646
break;
642647
}

src/ui.c

+32-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ int print_tableau(int y, int x, Card *bottom, Theme *theme) {
173173
}
174174
}
175175

176-
177176
void print_pile(Pile *pile, Theme *theme) {
178177
int y = pile->rule->y * (theme->height + theme->y_spacing);
179178
if (pile->rule->type == RULE_STOCK) {
@@ -191,8 +190,27 @@ void print_pile(Pile *pile, Theme *theme) {
191190
}
192191
}
193192

193+
int ui_victory(Pile *piles) {
194+
getmaxyx(stdscr, win_h, win_w);
195+
int start_y = win_h / 2 - 3;
196+
int start_x = win_w >= 38 ? win_w / 2 - 19 : 0;
197+
mvprintw(start_y , start_x, "**************************************");
198+
mvprintw(start_y + 1, start_x, "* VICTORY! *");
199+
mvprintw(start_y + 2, start_x, "* Press 'r' to redeal or 'q' to quit *");
200+
mvprintw(start_y + 3, start_x, "**************************************");
201+
while (1) {
202+
switch (getch()) {
203+
case 'r':
204+
return 1;
205+
case 'q':
206+
return 0;
207+
}
208+
}
209+
}
210+
194211
int ui_loop(Game *game, Theme *theme, Pile *piles) {
195212
MEVENT mouse;
213+
int move_made = 0;
196214
int mouse_action = 0;
197215
selection = NULL;
198216
selection_pile = NULL;
@@ -224,6 +242,14 @@ int ui_loop(Game *game, Theme *theme, Pile *piles) {
224242
refresh();
225243

226244
attron(COLOR_PAIR(COLOR_PAIR_BACKGROUND));
245+
246+
if (move_made) {
247+
if (check_win_condition(piles)) {
248+
return ui_victory(piles);
249+
}
250+
move_made = 0;
251+
}
252+
227253
int ch;
228254
if (mouse_action) {
229255
ch = mouse_action;
@@ -291,6 +317,7 @@ int ui_loop(Game *game, Theme *theme, Pile *piles) {
291317
if (cursor_card->up) {
292318
if (selection == cursor_card) {
293319
if (move_to_foundation(cursor_card, cursor_pile, piles) || move_to_free_cell(cursor_card, cursor_pile, piles)) {
320+
move_made = 1;
294321
clear();
295322
selection = NULL;
296323
selection_pile = NULL;
@@ -301,13 +328,15 @@ int ui_loop(Game *game, Theme *theme, Pile *piles) {
301328
}
302329
} else if (cursor_pile->rule->type == RULE_STOCK) {
303330
if (move_to_waste(cursor_card, cursor_pile, piles)) {
331+
move_made = 1;
304332
clear();
305333
}
306334
} else {
307335
turn_card(cursor_card);
308336
}
309337
} else if (cursor_pile->rule->type == RULE_STOCK) {
310338
if (redeal(cursor_pile, piles)) {
339+
move_made = 1;
311340
clear();
312341
} else {
313342
mvprintw(0, 0, "no more redeals");
@@ -319,6 +348,7 @@ int ui_loop(Game *game, Theme *theme, Pile *piles) {
319348
case 10: // enter
320349
if (selection && cursor_pile) {
321350
if (legal_move_stack(cursor_pile, selection, selection_pile)) {
351+
move_made = 1;
322352
clear();
323353
selection = NULL;
324354
selection_pile = NULL;
@@ -327,6 +357,7 @@ int ui_loop(Game *game, Theme *theme, Pile *piles) {
327357
break;
328358
case 'a':
329359
if (auto_move_to_foundation(piles)) {
360+
move_made = 1;
330361
clear();
331362
}
332363
break;

0 commit comments

Comments
 (0)