1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
19 /* stores a function pointer for every takeable action; called by game loop */
20 int (*action
[NUM_PLACES
][10])(int,int,int) = {
22 /* 1 2 3 4 5 6 7 stk wst fnd*/
23 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
24 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
25 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
26 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
27 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
28 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
29 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
30 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
31 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
32 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
34 /* 1 2 3 4 5 6 7 8 9 10*/
35 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
36 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
37 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
38 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
39 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
},
40 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
},
41 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
},
42 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
},
43 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
},
44 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
},
45 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
50 // argv parsing, game loops, cleanup {{{
51 int main(int argc
, char** argv
) {
52 /* opinionated defaults: */
53 op
.s
= &unicode_large_color
;
60 opterr
= 0; /* don't print message on unrecognized option */
61 while ((optget
= getopt (argc
, argv
, "+:hd:o:s:")) != -1) {
64 case 'd': /* difficulty */
65 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
66 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
67 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
70 case 'o': /* misc. options */
71 if(!strcmp(optarg
, "consv")) op
.v
= 1;
73 case 's': /* scheme */
74 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
75 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
76 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
79 case ':': //missing optarg
81 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
93 case GAME_NEW
: goto newgame
;
95 print_table(NO_HI
, NO_HI
);
97 if (getchar()=='q') return 0;
99 case GAME_QUIT
: return 0;
104 /* clean undo (from previous game): */
111 switch (get_cmd(&from
, &to
, &opt
)) {
113 switch (action
[from
][to
](from
,to
,opt
)) {
115 case ERR
: visbell(); break;
116 case WON
: return GAME_WON
;
119 case CMD_HINT
: break;//TODO: show a possible (and sensible) move
120 case CMD_JOIN
: break;//TODO: join any pile to here (longest if possible)
121 case CMD_UNDO
: undo_pop(f
.u
); break;
122 case CMD_INVAL
: visbell(); break;
123 case CMD_NEW
: return GAME_NEW
;
124 case CMD_AGAIN
: //TODO: restart with same seed
125 case CMD_QUIT
: return GAME_QUIT
;
132 /* free undo data structures: */
137 // card games helper functions {{{
138 #define get_suit(card) \
139 ((card-1) % NUM_SUITS)
140 #define get_rank(card) \
141 ((card-1) / NUM_SUITS)
142 #define get_color(card) \
143 ((get_suit(card) ^ get_suit(card)>>1) & 1)
145 #define is_tableu(where) (where <= TAB_MAX)
147 int find_top(card_t
* pile
) {
149 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
152 int first_movable(card_t
* pile
) {
154 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
157 int turn_over(card_t
* pile
) {
158 int top
= find_top(pile
);
164 int check_won(void) {
165 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
166 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
170 int rank_next (card_t a
, card_t b
) {
171 return get_rank(a
) == get_rank(b
)-1;
173 int is_consecutive (card_t
* pile
, int pos
) {
174 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
175 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
178 /* ranks consecutive? */
179 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
180 /* color opposite? */
181 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
183 /* ranks consecutive? */
184 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
186 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
192 int is_movable(card_t
* pile
, int n
) {
194 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
196 int top
= find_top(pile
);
197 for (int i
= top
; i
>= 0; i
--) {
198 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
199 if (!is_consecutive(pile
, i
)) return 0;
200 if (i
== n
) return 1; /* card reached, must be movable */
207 // takeable actions {{{
209 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
210 card_t card
= f
.s
[f
.w
];
211 /* move stack one over, so there are no gaps in it: */
212 for (int i
= f
.w
; i
< f
.z
-1; i
++)
215 f
.w
--; /* make previous card visible again */
218 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
219 (void) to
; (void) opt
; /* don't need */
220 int top_from
= find_top(f
.t
[from
]);
221 to
= get_suit(f
.t
[from
][top_from
]);
222 int top_to
= find_top(f
.f
[to
]);
223 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
224 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
225 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
226 f
.t
[from
][top_from
] = NO_CARD
;
227 undo_push(from
, FOUNDATION
, to
,
228 turn_over(f
.t
[from
]));
229 if (check_won()) return WON
;
233 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
234 (void) from
; (void) to
; (void) opt
; /* don't need */
235 if (f
.w
< 0) return ERR
;
236 to
= get_suit(f
.s
[f
.w
]);
237 int top_to
= find_top(f
.f
[to
]);
238 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
239 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
240 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
241 f
.f
[to
][top_to
+1] = stack_take();
242 if (check_won()) return WON
;
247 int s2w(int from
, int to
, int opt
) { /* stock to waste */
248 (void) from
; (void) to
; (void) opt
; /* don't need */
249 if (f
.z
== 0) return ERR
;
251 if (f
.w
== f
.z
) f
.w
= -1;
254 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
255 (void) from
; (void) to
; (void) opt
; /* don't need */
256 if (f
.z
== 0) return ERR
;
258 if (f
.w
< -1) f
.w
= f
.z
-1;
261 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
262 (void) from
; /* don't need */
263 int top_to
= find_top(f
.t
[to
]);
265 int top_from
= find_top(f
.f
[from
]);
267 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
268 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
269 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
270 f
.f
[from
][top_from
] = NO_CARD
;
271 undo_push(FOUNDATION
, to
, from
, 0);
275 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
276 (void) from
; (void) opt
; /* don't need */
277 int top_to
= find_top(f
.t
[to
]);
278 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
279 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
280 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
281 undo_push(WASTE
, to
, f
.w
, 0);
282 f
.t
[to
][top_to
+1] = stack_take();
286 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
287 (void) opt
; /* don't need */
288 int top_to
= find_top(f
.t
[to
]);
289 int top_from
= find_top(f
.t
[from
]);
290 int count
= 0; //NOTE: could probably be factored out
291 for (int i
= top_from
; i
>=0; i
--) {
292 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
293 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
294 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
295 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
296 /* move cards [i..top_from] to their destination */
297 for (;i
<= top_from
; i
++) {
299 f
.t
[to
][top_to
] = f
.t
[from
][i
];
300 f
.t
[from
][i
] = NO_CARD
;
303 undo_push(from
, to
, count
,
304 turn_over(f
.t
[from
]));
308 return ERR
; /* no such move possible */
311 int remove_if_complete (int pileno
) { //cleanup!
312 card_t
* pile
= f
.t
[pileno
];
313 /* test if K...A complete; move to foundation if so */
314 int top_from
= find_top(pile
);
315 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
316 for (int i
= top_from
; i
>=0; i
--) {
317 if (!is_consecutive (pile
, i
)) return 0;
318 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
319 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
320 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
321 f
.f
[f
.w
][j
] = pile
[i
];
324 undo_push(pileno
, FOUNDATION
, f
.w
,
333 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
334 int top_from
= find_top(f
.t
[from
]);
335 int top_to
= find_top(f
.t
[to
]);
336 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
337 int count
= 0; //NOTE: could probably be factored out
339 for (int i
= top_from
; i
>= 0; i
--) {
340 if (!is_consecutive(f
.t
[from
], i
)) break;
342 /* is consecutive OR to empty pile and rank ok? */
343 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
344 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
345 for (;i
<= top_from
; i
++) {
347 f
.t
[to
][top_to
] = f
.t
[from
][i
];
348 f
.t
[from
][i
] = NO_CARD
;
351 undo_push(from
, to
, count
,
352 turn_over(f
.t
[from
]));
353 remove_if_complete(to
);
354 if (check_won()) return WON
;
359 return ERR
; /* no such move possible */
361 int s2t(int from
, int to
, int opt
) {
362 (void) from
; (void) to
; (void) opt
; /* don't need */
363 if (f
.z
<= 0) return ERR
; /* stack out of cards */
364 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
365 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
366 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
367 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
368 remove_if_complete(pile
);
369 if (check_won()) return WON
;
371 undo_push(STOCK
, TABLEU
, 1, 0); /*NOTE: puts 1 card on each tableu pile*/
374 int t2f(int from
, int to
, int opt
) {
375 (void) to
; (void) opt
; /* don't need */
376 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
377 return remove_if_complete(from
)?OK
:ERR
;
380 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
383 // keyboard input handling {{{
384 // cursor functions{{{
385 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
387 void cursor_left (struct cursor
* cursor
) {
388 if (is_tableu(cursor
->pile
)) {
389 if (cursor
->pile
> 0) cursor
->pile
--;
391 } else { /* stock/waste/foundation*/
392 switch (cursor
->pile
) {
393 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
395 if (cursor
->opt
<= 0)
396 cursor
->pile
= WASTE
;
402 void cursor_down (struct cursor
* cursor
) {
403 if (!is_tableu(cursor
->pile
)) {
404 switch (cursor
->pile
) {
405 case STOCK
: cursor
->pile
= TAB_1
; break;
406 case WASTE
: cursor
->pile
= TAB_2
; break;
408 cursor
->pile
= TAB_4
+ cursor
->opt
;
413 void cursor_up (struct cursor
* cursor
) {
414 if (is_tableu(cursor
->pile
)) {
415 switch (cursor
->pile
) { //ugly :|
416 case TAB_1
: cursor
->pile
= STOCK
; break;
417 case TAB_2
: cursor
->pile
= WASTE
; break;
418 case TAB_3
: cursor
->pile
= WASTE
; break;
419 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
420 cursor
->opt
=cursor
->pile
-TAB_4
;
421 cursor
->pile
= FOUNDATION
;
426 void cursor_right (struct cursor
* cursor
) {
427 if (is_tableu(cursor
->pile
)) {
428 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
430 switch (cursor
->pile
) {
431 case STOCK
: cursor
->pile
= WASTE
; break;
432 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
434 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
440 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
441 void cursor_left (struct cursor
* cursor
) {
442 if (cursor
->pile
> 0) cursor
->pile
--;
445 void cursor_down (struct cursor
* cursor
) {
446 int first
= first_movable(f
.t
[cursor
->pile
]);
447 int top
= find_top(f
.t
[cursor
->pile
]);
448 if (first
+ cursor
->opt
< top
)
451 void cursor_up (struct cursor
* cursor
) {
452 if (cursor
->opt
> 0) cursor
->opt
--;
454 void cursor_right (struct cursor
* cursor
) {
455 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
459 void cursor_to (struct cursor
* cursor
, int pile
) {
463 #pragma GCC diagnostic pop
465 int get_cmd (int* from
, int* to
, int* opt
) {
466 //TODO: escape sequences (mouse, cursor keys)
468 struct cursor inactive
= {-1,-1};
469 static struct cursor active
= {0,0};
470 active
.opt
= 0; /* always reset offset, but keep pile */
473 from_l
: print_table(&active
, &inactive
);
477 /* direct addressing: */
478 case '1': *from
= TAB_1
; break;
479 case '2': *from
= TAB_2
; break;
480 case '3': *from
= TAB_3
; break;
481 case '4': *from
= TAB_4
; break;
482 case '5': *from
= TAB_5
; break;
483 case '6': *from
= TAB_6
; break;
484 case '7': *from
= TAB_7
; break;
486 case '8': *from
= TAB_8
; break;
487 case '9': *from
= TAB_9
; break;
488 case '0': *from
= TAB_10
;break;
489 #elif defined KLONDIKE
490 case '9': *from
= WASTE
; break;
491 case '0': *from
= FOUNDATION
; break;
492 case '8': /* fallthrough */
494 case '\n': /* shortcut for dealing from stock */
498 /* cursor keys addressing: */
499 case 'h': cursor_left (&active
); goto from_l
;
500 case 'j': cursor_down (&active
); goto from_l
;
501 case 'k': cursor_up (&active
); goto from_l
;
502 case 'l': cursor_right(&active
); goto from_l
;
503 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
504 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
505 //TODO: real cursor keys, home/end
506 case ' ': /* continue with second cursor */
508 if (*from
== STOCK
) {
513 *opt
= active
.opt
; /* when FOUNDATION */
520 fprintf (stderr
, ":");
521 raw_mode(0); /* turn on echo */
522 fgets (buf
, 256, stdin
);
525 case 'q': return CMD_QUIT
;
526 case 'n': return CMD_NEW
;
527 case 'r': return CMD_AGAIN
;
528 default: return CMD_INVAL
;
530 case 'J': return CMD_JOIN
;
531 case 'K': /* fallthrough */
532 case '?': return CMD_HINT
;
533 case 'u': return CMD_UNDO
;
534 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
535 default: return CMD_INVAL
;
537 inactive
.pile
= *from
; /* for direct addressing highlighting */
538 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
541 to_l
: print_table(&active
, &inactive
);
545 case 'h': cursor_left (&active
); goto to_l
;
546 case 'j': cursor_down (&active
); goto to_l
;
547 case 'k': cursor_up (&active
); goto to_l
;
548 case 'l': cursor_right(&active
); goto to_l
;
549 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
550 case 'L': cursor_to(&active
,TAB_MAX
);goto to_l
;
551 case 'J': /* fallthrough; key makes no sense on destination */
554 break; /* continues with the foundation/empty tableu check */
555 case 'K': /* fallthrough */
556 case '?': return CMD_HINT
;
557 case 'u': return CMD_NONE
; /* cancel selection */
558 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
560 if (t
< '0' || t
> '9') return CMD_INVAL
;
573 if (*from
== FOUNDATION
) {
574 int top
= find_top(f
.t
[*to
]);
575 if (top
< 0) return CMD_INVAL
;
576 int color
= get_color(f
.t
[*to
][top
]);
577 int choice_1
= 1-color
; /* selects piles of */
578 int choice_2
= 2+color
; /* the opposite color */
579 int top_c1
= find_top(f
.f
[choice_1
]);
580 int top_c2
= find_top(f
.f
[choice_2
]);
582 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
583 && top_c1
>= 0 ) << 0
584 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
585 && top_c2
>= 0 ) << 1) {
586 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
587 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
588 case (1<<1 | 1<<0): /* both, ask user which to pick from */
589 printf ("take from (1-4): "); fflush (stdout
);
590 *opt
= getchar() - '1';
591 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
593 default: return CMD_INVAL
; /* none matched */
595 /* `opt` is the foundation index (0..3) */
598 /* moving to empty tableu? */
599 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
600 int bottom
= first_movable(f
.t
[*from
]);
601 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
602 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
605 int top
= find_top(f
.t
[*from
]);
606 if (top
< 0) return CMD_INVAL
;
607 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
608 *opt
= get_rank(f
.t
[*from
][top
]);
609 } else { /* only ask the user if it's unclear: */
610 printf ("\rup to ([a23456789xjqk] or space/return): ");
613 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
614 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
615 case 'a': case 'A': *opt
= RANK_A
; break;
616 case '0': /* fallthrough */
617 case 'x': case 'X': *opt
= RANK_X
; break;
618 case 'j': case 'J': *opt
= RANK_J
; break;
619 case 'q': case 'Q': *opt
= RANK_Q
; break;
620 case 'k': case 'K': *opt
= RANK_K
; break;
621 default: *opt
-= '1';
623 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
625 /* `opt` is the rank of the highest card to move */
632 // shuffling and dealing {{{
634 f
= (const struct playfield
){0}; /* clear playfield */
635 card_t deck
[DECK_SIZE
*NUM_DECKS
];
636 int avail
= DECK_SIZE
*NUM_DECKS
;
637 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
639 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
640 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
641 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
642 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
645 srandom (time(NULL
));
646 long seed
= time(NULL
);
648 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
649 int j
= random() % (i
+1);
650 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
654 for (int i
= 0; i
< NUM_PILES
; i
++) {
656 int closed
= i
; /* pile n has n closed cards, then 1 open */
658 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
660 /* face down cards are negated: */
661 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
662 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
664 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
665 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
667 f
.w
= -1; /* @start: nothing on waste */
669 f
.w
= 0; /* number of used foundations */
672 f
.u
= &undo_sentinel
;
676 // screen drawing routines {{{
677 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
678 if (bold
&& op
.s
== &unicode_large_color
){//ARGH! awful hack for bold with faint
679 int offset
= str
[3]==033?16:str
[4]==033?17:0;
680 if ((unsigned char)str
[10] == 0x9a) offset
= 0; //facedown card
681 printf ("%s%s%s""%.*s%s%s""%s%s%s",
682 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
683 offset
, str
, bold
?"\033[1m":"", str
+offset
,
684 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
687 printf ("%s%s%s%s%s%s%s",
688 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
690 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
692 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
693 printf("\033[2J\033[H"); /* clear screen, reset cursor */
695 /* print stock, waste and foundation: */
696 for (int line
= 0; line
< op
.s
->height
; line
++) {
698 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
699 (f
.w
< f
.z
-1)?op
.s
->facedown
700 :op
.s
->placeholder
)[line
]);
702 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
703 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
704 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
705 :op
.s
->placeholder
)[line
]);
706 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
708 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
709 int card
= find_top(f
.f
[pile
]);
710 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
711 inactive
->pile
==FOUNDATION
&& (
712 /* cursor addr. || direct addr. */
713 inactive
->opt
==pile
|| inactive
->opt
< 0
715 (card
< 0)?op
.s
->placeholder
[line
]
716 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
722 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
723 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
724 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
725 int spacer_to
= NUM_PILES
*op
.s
->width
-
726 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
727 for (int line
= 0; line
< op
.s
->height
; line
++) {
728 /* available stock: */
729 for (int i
= f
.z
/10; i
; i
--) {
730 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
731 else printf ("%s", op
.s
->halfstack
[line
]);
734 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
735 /* foundation (overlapping): */
736 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
737 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
738 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
739 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
746 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
747 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
750 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
751 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
752 && offset[pile] > cursor->opt)
753 #define TOP_HI(cursor) (cursor->pile == pile && movable \
754 && offset[pile]-1 == cursor->opt)
755 #define INC_OFFSET if (movable) offset[pile]++
757 /* print tableu piles: */
758 int row
[NUM_PILES
] = {0};
759 int line
[NUM_PILES
]= {0};
760 int label
[NUM_PILES
]={0};
762 int did_placeholders
= 0;
765 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
766 card_t card
= f
.t
[pile
][row
[pile
]];
767 card_t next
= f
.t
[pile
][row
[pile
]+1];
768 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
769 int empty
= !card
&& row
[pile
] == 0;
771 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
772 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
773 :(card
<0)?op
.s
->facedown
777 int extreme_overlap
= op
.v
&& find_top(f
.t
[pile
])>10;
778 /* normal overlap: */
779 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
780 /* extreme overlap on closed cards: */
781 || (extreme_overlap
&&
783 f
.t
[pile
][row
[pile
]] < 0 &&
784 f
.t
[pile
][row
[pile
]+1] <0)
785 /* extreme overlap on sequences: */
786 || (extreme_overlap
&&
787 !TOP_HI(active
) && /*always show top selected card*/
788 line
[pile
] >= 1 && row
[pile
] > 0 &&
789 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
790 is_consecutive (f
.t
[pile
], row
[pile
]) &&
791 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
792 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
799 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
801 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
803 line_had_card
|= !!card
;
804 did_placeholders
|= row
[pile
] > 0;
807 } while (line_had_card
|| !did_placeholders
);
810 void visbell (void) {
811 printf ("\033[?5h"); fflush (stdout
);
813 printf ("\033[?5l"); fflush (stdout
);
815 void win_anim(void) {
816 printf ("\033[?25l"); /* hide cursor */
818 /* set cursor to random location */
819 int row
= 1+random()%(24-op
.s
->width
);
820 int col
= 1+random()%(80-op
.s
->height
);
822 /* draw random card */
823 int face
= 1 + random() % 52;
824 for (int l
= 0; l
< op
.s
->height
; l
++) {
825 printf ("\033[%d;%dH", row
+l
, col
);
826 printf ("%s", op
.s
->card
[face
][l
]);
830 /* exit on keypress */
831 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
832 if (poll (&p
, 1, 80)) goto fin
;
835 printf ("\033[?25h"); /* show cursor */
841 void undo_push (int _f
, int t
, int n
, int o
) {
842 struct undo
* new = malloc(sizeof(struct undo
));
852 void undo_pop (struct undo
* u
) {
853 if (u
== &undo_sentinel
) return;
856 if (u
->f
== FOUNDATION
) {
857 /* foundation -> tableu */
858 int top_f
= find_top(f
.f
[u
->n
]);
859 int top_t
= find_top(f
.t
[u
->t
]);
860 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
861 f
.t
[u
->t
][top_t
] = NO_CARD
;
862 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
863 /* waste -> foundation */
864 /* split u->n into wst and fnd: */
865 int wst
= u
->n
& 0xffff;
866 int fnd
= u
->n
>> 16;
867 /* move stock cards one position up to make room: */
868 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
869 /* move one card from foundation to waste: */
870 int top
= find_top(f
.f
[fnd
]);
871 f
.s
[wst
] = f
.f
[fnd
][top
];
872 f
.f
[fnd
][top
] = NO_CARD
;
875 } else if (u
->f
== WASTE
) {
876 /* waste -> tableu */
877 /* move stock cards one position up to make room: */
878 for (int i
= f
.z
; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
879 /* move one card from tableu to waste: */
880 int top
= find_top(f
.t
[u
->t
]);
881 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
882 f
.t
[u
->t
][top
] = NO_CARD
;
885 } else if (u
->t
== FOUNDATION
) {
886 /* tableu -> foundation */
887 int top_f
= find_top(f
.t
[u
->f
]);
888 int top_t
= find_top(f
.f
[u
->n
]);
889 /* close topcard if previous action caused turn_over(): */
890 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
891 /* move one card from foundation to tableu: */
892 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
893 f
.f
[u
->n
][top_t
] = NO_CARD
;
895 /* tableu -> tableu */
896 int top_f
= find_top(f
.t
[u
->f
]);
897 int top_t
= find_top(f
.t
[u
->t
]);
898 /* close topcard if previous action caused turn_over(): */
899 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
900 /* move n cards from tableu[f] to tableu[t]: */
901 for (int i
= 0; i
< u
->n
; i
++) {
902 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
903 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
908 /* stock -> tableu */
909 /*remove a card from each pile and put it back onto the stock:*/
910 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
911 int top
= find_top(f
.t
[pile
]);
912 f
.s
[f
.z
++] = f
.t
[pile
][top
];
913 f
.t
[pile
][top
] = NO_CARD
;
915 } else if (u
->t
== FOUNDATION
) {
916 /* tableu -> foundation */
917 int top
= find_top(f
.t
[u
->f
]);
918 /* close topcard if previous action caused turn_over(): */
919 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
920 /* append cards from foundation to tableu */
921 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
922 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
923 f
.f
[u
->n
][i
] = NO_CARD
;
925 f
.w
--; /* decrement complete-foundation-counter */
928 /* tableu -> tableu */
929 int top_f
= find_top(f
.t
[u
->f
]);
930 int top_t
= find_top(f
.t
[u
->t
]);
931 /* close topcard if previous action caused turn_over(): */
932 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
933 /* move n cards from tableu[f] to tableu[t]: */
934 for (int i
= 0; i
< u
->n
; i
++) {
935 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
936 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
945 void free_undo (struct undo
* u
) {
946 while (u
&& u
!= &undo_sentinel
) {
954 // initialization stuff {{{
955 void screen_setup (int enable
) {
958 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
959 printf ("\033[H\033[J"); /* reset cursor, clear screen */
960 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
962 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
963 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
968 void raw_mode(int enable
) {
969 static struct termios saved_term_mode
;
970 struct termios raw_term_mode
;
973 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
974 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
975 raw_term_mode
= saved_term_mode
;
976 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
977 raw_term_mode
.c_cc
[VMIN
] = 1 ;
978 raw_term_mode
.c_cc
[VTIME
] = 0;
979 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
981 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
985 void signal_handler (int signum
) {
990 print_table(NO_HI
, NO_HI
);
996 void signal_setup(void) {
997 struct sigaction saction
;
999 saction
.sa_handler
= signal_handler
;
1000 sigemptyset(&saction
.sa_mask
);
1001 saction
.sa_flags
= 0;
1002 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1006 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1013 //vim: foldmethod=marker