1 #define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
18 /* stores a function pointer for every takeable action; called by game loop */
19 int (*action
[NUM_PLACES
][10])(int,int,int) = {
21 /* 1 2 3 4 5 6 7 stk wst fnd*/
22 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
23 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
24 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
25 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
26 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
27 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
28 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
29 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
30 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
31 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
33 /* 1 2 3 4 5 6 7 8 9 10*/
34 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
35 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
36 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
37 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
38 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
},
39 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
},
40 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
},
41 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
},
42 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
},
43 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
},
44 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
45 #elif defined FREECELL
46 /* 1 2 3 4 5 6 7 8 cll fnd*/
47 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2c
, t2f
},
48 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2c
, t2f
},
49 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2c
, t2f
},
50 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2c
, t2f
},
51 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2c
, t2f
},
52 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2c
, t2f
},
53 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2c
, t2f
},
54 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2c
, t2f
},
55 /*cll*/ { c2t
, c2t
, c2t
, c2t
, c2t
, c2t
, c2t
, c2t
, c2f
, c2f
},
56 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2c
, nop
},
61 // argv parsing, game loops, cleanup {{{
62 int main(int argc
, char** argv
) {
63 /* opinionated defaults: */
64 op
.s
= &unicode_large_color
;
65 op
.v
= 1; /* enable fake visbell by default */
71 opterr
= 0; /* don't print message on unrecognized option */
72 while ((optget
= getopt (argc
, argv
, "+:hs:vbcmMV")) != -1) {
75 case 's': /* number of suits */
77 case '1': op
.m
= EASY
; break;
78 case '2': op
.m
= MEDIUM
; break;
79 case '4': op
.m
= NORMAL
; break;
83 case 'b': op
.s
= &unicode_large_mono
; break;
84 case 'c': op
.s
= &unicode_large_color
; break;
85 case 'm': op
.s
= &unicode_small_mono
; break; /* "mini, monochrome" */
86 case 'M': op
.s
= &unicode_small_color
; break; /* "mini, colorful" */
87 case 'V': op
.v
= 0; /* WARN: experimental; might change */
88 case 'h': default: goto error
;
90 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
98 signal_handler(SIGWINCH
); /* initialize window size */
104 case GAME_NEW
: goto newgame
;
106 print_table(NO_HI
, NO_HI
);
108 if (getch(NULL
)=='q') return 0;
110 case GAME_QUIT
: return 0;
114 #define is_tableu(where) (where <= TAB_MAX) // "card games helper functions"
118 long seed
= time(NULL
);
125 switch (get_cmd(&from
, &to
, &opt
)) {
127 ret
= action
[from
][to
](from
,to
,opt
);
128 if (ret
== ERR
&& is_tableu(from
) && is_tableu(to
))
129 /* try again with from/to swapped: */
130 ret
= action
[to
][from
](to
,from
,opt
);
133 case ERR
: visbell(); break;
134 case WON
: return GAME_WON
;
140 case ERR
: visbell(); break;
141 case WON
: return GAME_WON
;
144 case CMD_HINT
: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
145 case CMD_UNDO
: undo_pop(f
.u
); break;
146 case CMD_INVAL
: visbell(); break;
147 case CMD_NEW
: return GAME_NEW
;
148 case CMD_AGAIN
: goto restart
;
149 case CMD_QUIT
: return GAME_QUIT
;
151 printf (KEYHELP
"\nPress any key to continue.");
164 // card games helper functions {{{
165 #define get_suit(card) \
166 ((card-1) % NUM_SUITS)
167 #define get_rank(card) \
168 ((card-1) / NUM_SUITS)
169 #define get_color(card) \
170 ((get_suit(card) ^ get_suit(card)>>1) & 1)
172 int find_top(card_t
* pile
) {
174 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
177 int first_movable(card_t
* pile
) {
179 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
182 int turn_over(card_t
* pile
) {
183 int top
= find_top(pile
);
189 int check_won(void) {
190 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
191 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
195 int rank_next (card_t a
, card_t b
) {
196 return get_rank(a
) == get_rank(b
)-1;
198 int is_consecutive (card_t
* pile
, int pos
) {
199 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
200 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
202 #if defined KLONDIKE || defined FREECELL
203 /* ranks consecutive? */
204 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
205 /* color opposite? */
206 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
208 /* ranks consecutive? */
209 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
211 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
217 int is_movable(card_t
* pile
, int n
) {
219 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
220 #elif defined SPIDER || defined FREECELL
221 int top
= find_top(pile
);
222 for (int i
= top
; i
>= 0; i
--) {
223 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
224 if (!is_consecutive(pile
, i
)) return 0;
225 if (i
== n
) return 1; /* card reached, must be movable */
232 // takeable actions {{{
234 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
235 card_t card
= f
.s
[f
.w
];
236 /* move stack one over, so there are no gaps in it: */
237 for (int i
= f
.w
; i
< f
.z
-1; i
++)
240 f
.w
--; /* make previous card visible again */
243 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
244 (void) to
; (void) opt
; /* don't need */
245 int top_from
= find_top(f
.t
[from
]);
246 to
= get_suit(f
.t
[from
][top_from
]);
247 int top_to
= find_top(f
.f
[to
]);
248 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
249 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
250 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
251 f
.t
[from
][top_from
] = NO_CARD
;
252 undo_push(from
, FOUNDATION
, to
,
253 turn_over(f
.t
[from
]));
254 if (check_won()) return WON
;
258 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
259 (void) from
; (void) to
; (void) opt
; /* don't need */
260 if (f
.w
< 0) return ERR
;
261 to
= get_suit(f
.s
[f
.w
]);
262 int top_to
= find_top(f
.f
[to
]);
263 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
264 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
265 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
266 f
.f
[to
][top_to
+1] = stack_take();
267 if (check_won()) return WON
;
272 int s2w(int from
, int to
, int opt
) { /* stock to waste */
273 (void) from
; (void) to
; (void) opt
; /* don't need */
274 if (f
.z
== 0) return ERR
;
276 if (f
.w
== f
.z
) f
.w
= -1;
279 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
280 (void) from
; (void) to
; (void) opt
; /* don't need */
281 if (f
.z
== 0) return ERR
;
283 if (f
.w
< -1) f
.w
= f
.z
-1;
286 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
287 (void) from
; /* don't need */
288 int top_to
= find_top(f
.t
[to
]);
290 int top_from
= find_top(f
.f
[from
]);
292 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
293 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
294 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
295 f
.f
[from
][top_from
] = NO_CARD
;
296 undo_push(FOUNDATION
, to
, from
, 0);
300 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
301 (void) from
; (void) opt
; /* don't need */
302 if (f
.w
< 0) return ERR
;
303 int top_to
= find_top(f
.t
[to
]);
304 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
305 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
306 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
307 undo_push(WASTE
, to
, f
.w
, 0);
308 f
.t
[to
][top_to
+1] = stack_take();
312 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
313 (void) opt
; /* don't need */
314 int top_to
= find_top(f
.t
[to
]);
315 int top_from
= find_top(f
.t
[from
]);
316 int count
= 0; //NOTE: could probably be factored out
317 for (int i
= top_from
; i
>=0; i
--) {
318 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
319 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
320 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
321 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
322 /* move cards [i..top_from] to their destination */
323 for (;i
<= top_from
; i
++) {
325 f
.t
[to
][top_to
] = f
.t
[from
][i
];
326 f
.t
[from
][i
] = NO_CARD
;
329 undo_push(from
, to
, count
,
330 turn_over(f
.t
[from
]));
334 return ERR
; /* no such move possible */
337 int remove_if_complete (int pileno
) { //cleanup!
338 card_t
* pile
= f
.t
[pileno
];
339 /* test if K...A complete; move to foundation if so */
340 int top_from
= find_top(pile
);
341 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
342 for (int i
= top_from
; i
>=0; i
--) {
343 if (!is_consecutive (pile
, i
)) return 0;
344 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
345 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
346 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
347 f
.f
[f
.w
][j
] = pile
[i
];
350 undo_push(pileno
, FOUNDATION
, f
.w
,
359 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
360 int top_from
= find_top(f
.t
[from
]);
361 int top_to
= find_top(f
.t
[to
]);
362 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
363 int count
= 0; //NOTE: could probably be factored out
365 for (int i
= top_from
; i
>= 0; i
--) {
366 if (!is_consecutive(f
.t
[from
], i
)) break;
368 /* is consecutive OR to empty pile and rank ok? */
369 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
370 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
371 for (;i
<= top_from
; i
++) {
373 f
.t
[to
][top_to
] = f
.t
[from
][i
];
374 f
.t
[from
][i
] = NO_CARD
;
377 undo_push(from
, to
, count
,
378 turn_over(f
.t
[from
]));
379 remove_if_complete(to
);
380 if (check_won()) return WON
;
385 return ERR
; /* no such move possible */
387 int s2t(int from
, int to
, int opt
) {
388 (void) from
; (void) to
; (void) opt
; /* don't need */
389 if (f
.z
<= 0) return ERR
; /* stack out of cards */
390 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
391 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
392 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
393 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
394 remove_if_complete(pile
);
395 if (check_won()) return WON
;
397 undo_push(STOCK
, TABLEU
, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
400 int t2f(int from
, int to
, int opt
) {
401 (void) to
; (void) opt
; /* don't need */
402 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
403 return remove_if_complete(from
)?OK
:ERR
;
405 #elif defined FREECELL
406 int t2t(int from
, int to
, int opt
) {
407 (void) from
; (void) to
; (void) opt
;
408 //XXX FREECELL TODO: tableu to tableu
409 //allow moving as many cards as free spaces on the field?
410 //use cascades to increase number of cards moved at once?
413 int t2f(int from
, int to
, int opt
) { /* 1:1 copy from KLONDIKE */
414 (void) to
; (void) opt
; /* don't need */
415 int top_from
= find_top(f
.t
[from
]);
416 to
= get_suit(f
.t
[from
][top_from
]);
417 int top_to
= find_top(f
.f
[to
]);
418 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
419 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
420 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
421 f
.t
[from
][top_from
] = NO_CARD
;
422 undo_push(from
, FOUNDATION
, to
, 0);
423 if (check_won()) return WON
;
427 int f2t(int from
, int to
, int opt
) {
428 (void) from
; /* don't need */
429 int top_to
= find_top(f
.t
[to
]);
431 int top_from
= find_top(f
.f
[from
]);
433 if (top_to
< 0 /* empty tableu? */
434 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
435 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
])))) {
436 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
437 f
.f
[from
][top_from
] = NO_CARD
;
438 undo_push(FOUNDATION
, to
, from
, 0);
442 int t2c(int from
, int to
, int opt
) {
443 (void) to
; (void) opt
; /* don't need */
444 /* is a cell free? */
445 if (f
.w
== (1<<NUM_CELLS
)-1)
447 for (to
= 0; to
< NUM_CELLS
; to
++)
448 if (!(f
.w
>>to
&1)) break;
450 int top_from
= find_top(f
.t
[from
]);
451 f
.s
[to
] = f
.t
[from
][top_from
];
452 f
.t
[from
][top_from
] = NO_CARD
;
453 f
.w
|= 1<<to
; /* mark cell as occupied */
454 undo_push(from
, STOCK
, to
, 0);
458 int c2t(int from
, int to
, int opt
) {
459 (void) from
; /* don't need */
460 int top_to
= find_top(f
.t
[to
]);
463 if (top_to
< 0 /* empty tableu? */
464 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[from
]))
465 && (rank_next(f
.s
[from
], f
.t
[to
][top_to
])))) {
466 f
.t
[to
][top_to
+1] = f
.s
[from
];
468 f
.w
&= ~(1<<from
); /* mark cell as free */
469 undo_push(STOCK
, to
, from
, 0);
474 int c2f(int from
, int to
, int opt
) {
475 (void) from
; (void) to
; /* don't need */
477 to
= get_suit(f
.s
[from
]);
478 int top_to
= find_top(f
.f
[to
]);
479 if ((top_to
< 0 && get_rank(f
.s
[from
]) == RANK_A
)
480 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.s
[from
]))) {
481 f
.f
[to
][top_to
+1] = f
.s
[from
];
483 f
.w
&= ~(1<<from
); /* mark cell as free */
484 undo_push(STOCK
, FOUNDATION
, from
| to
<<16, 0);
485 if (check_won()) return WON
;
489 int f2c(int from
, int to
, int opt
) {
490 (void) from
; (void) to
; /* don't need */
491 /* is a cell free? */
492 if (f
.w
== (1<<NUM_CELLS
)-1)
494 for (to
= 0; to
< NUM_CELLS
; to
++)
495 if (!(f
.w
>>to
&1)) break;
498 int top_from
= find_top(f
.f
[from
]);
499 f
.s
[to
] = f
.f
[from
][top_from
];
500 f
.f
[from
][top_from
] = NO_CARD
;
501 f
.w
|= 1<<to
; /* mark cell as occupied */
502 undo_push(FOUNDATION
, STOCK
, from
| to
<<16, 0);
508 //TODO: generalize prediction engine for CMD_HINT
510 #define would_complete(pile) 0
512 #define would_complete(pile) \
513 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
514 && get_rank(f.t[to][bottom_to]) == RANK_K)
515 #elif defined FREECELL
516 #define would_complete(pile) 0
518 #define would_turn(pile) \
519 (f.t[pile][r[pile].pos-1] < 0)
520 #define would_empty(pile) \
524 int top_to
= find_top(f
.t
[to
]);
526 int bottom_to
= first_movable(f
.t
[to
]);
530 if (to
== WASTE
|| to
== STOCK
) return ERR
; /*why would you do that!?*/
532 if (to
== FOUNDATION
) {
534 for (int i
= 0; i
<= TAB_MAX
; i
++)
535 switch ((i
?t2f
:w2f
)(i
-1, FOUNDATION
, 0)) {
536 case WON
: return WON
;
537 case OK
: status
= OK
;
543 if (top_to
< 0) { /* move a king to empty pile: */
544 for (int i
= 0; i
< TAB_MAX
; i
++) {
545 if (f
.t
[i
][0] < 0) /* i.e. would turn? */
546 if (t2t(i
, to
, 0) == OK
) return OK
;
548 return w2t(WASTE
, to
, 0);
553 int ok
:1; /* card to move in pile? */
554 int above
; /* number of movable cards above */
555 int below
; /* number of cards below ours */
556 int pos
; /* where the card to move is in the pile */
557 int top
; /* find_top() */
558 } r
[NUM_PILES
] = {{0}};
559 int complete
= 0;/* SPIDER: true if any pile would complete a stack */
560 int turn
= 0; /* SPIDER: true if any pile would turn_over */
561 int empty
= 0; /* true if any pile would become empty */
563 /* 1. rate each pile: */
566 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
567 if (pile
== to
) continue;
568 int top
= find_top(f
.t
[pile
]);
569 int bottom
= first_movable(f
.t
[pile
]);
570 r
[pile
].pos
= bottom
; /* need for would_empty */
572 if (top
< 0) continue; /* no cards to move */
573 if (would_empty(pile
)) continue; /* doesn't help */
576 r
[pile
].above
= 0; /* always take as many as possible */
577 r
[pile
].below
= top
- bottom
;
579 complete
|= would_complete(pile
); /* never happens */
580 turn
|= would_turn(pile
);
581 empty
|= would_empty(pile
);
585 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
586 r
[pile
].top
= r
[pile
].pos
= find_top(f
.t
[pile
]);
587 /* backtrack until we find a compatible-to-'to'-pile card: */
588 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
589 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
590 - get_rank(f
.t
[to
][top_to
]);
591 if (rankdiff
>= 0) break; /* past our card */
592 if (rankdiff
== -1 /* rank matches */
594 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
595 != get_color(f
.t
[to
][top_to
])
597 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
598 == get_suit(f
.t
[to
][top_to
])
602 complete
|= would_complete(pile
);
603 turn
|= would_turn(pile
);
604 empty
|= would_empty(pile
);
605 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
606 if (is_movable(f
.t
[pile
], i
-1))
616 /* 2. find optimal pile: (optimized for spider) */
617 //todo: in spider, prefer longest piles if above==0 (faster completions)
619 for (int pile
= 0, above
= 99, below
= 99; pile
< NUM_PILES
; pile
++) {
620 if (!r
[pile
].ok
) continue;
621 /* don't bother if another pile would be better: prefer ... */
622 /* ... to complete a stack: */
623 if (!would_complete(pile
) && complete
) continue;
624 /* ... emptying piles: */
625 if (!would_empty(pile
) && empty
&& !complete
) continue;
626 /* ... to turn_over: */
627 if (!would_turn(pile
) && turn
&& !complete
&& !empty
) continue;
628 /* ... not to rip apart too many cards: */
629 if (r
[pile
].above
> above
) continue;
630 /* if tied, prefer ... */
631 if (r
[pile
].above
== above
632 /* ... larger pile if destination is empty */
633 && (top_to
< 0? r
[pile
].below
< below
634 /* ... shorter pile otherwise */
635 : r
[pile
].below
> below
))
639 above
= r
[pile
].above
;
640 below
= r
[pile
].below
;
643 /* 3. move cards over and return: */
645 /* prefer waste if it wouldn't turn_over: */
646 /* NOTE: does not attempt to take from froundation */
647 if (!turn
&& w2t(WASTE
, to
, 0) == OK
) //TODO: gives higher priority to waste than to empty!
649 if (from
< 0) /* nothing found */
651 return t2t(from
, to
, 0);
653 if (from
< 0) /* nothing found */
655 int bottom
= first_movable(f
.t
[from
]);
656 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
657 #elif defined FREECELL
659 return ERR
; //TODO FREECELL: implement join
664 #undef would_complete
665 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
668 // keyboard input handling {{{
669 // cursor functions{{{
671 void cursor_left (struct cursor
* cursor
) {
673 if (is_tableu(cursor
->pile
)) {
674 if (cursor
->pile
> 0) cursor
->pile
--;
676 } else { /* stock/waste/foundation*/
677 switch (cursor
->pile
) {
678 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
680 if (cursor
->opt
<= 0)
681 cursor
->pile
= WASTE
;
687 void cursor_down (struct cursor
* cursor
) {
689 if (!is_tableu(cursor
->pile
)) {
690 switch (cursor
->pile
) {
691 case STOCK
: cursor
->pile
= TAB_1
; break;
692 case WASTE
: cursor
->pile
= TAB_2
; break;
694 cursor
->pile
= TAB_4
+ cursor
->opt
;
699 void cursor_up (struct cursor
* cursor
) {
701 if (is_tableu(cursor
->pile
)) {
702 switch (cursor
->pile
) { //ugly :|
703 case TAB_1
: cursor
->pile
= STOCK
; break;
704 case TAB_2
: cursor
->pile
= WASTE
; break;
705 case TAB_3
: cursor
->pile
= WASTE
; break;
706 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
707 cursor
->opt
=cursor
->pile
-TAB_4
;
708 cursor
->pile
= FOUNDATION
;
713 void cursor_right (struct cursor
* cursor
) {
715 if (is_tableu(cursor
->pile
)) {
716 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
719 switch (cursor
->pile
) {
720 case STOCK
: cursor
->pile
= WASTE
; break;
721 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
723 if (cursor
->opt
< NUM_SUITS
-1)
729 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
730 void cursor_left (struct cursor
* cursor
) {
732 if (cursor
->pile
> 0) cursor
->pile
--;
735 void cursor_down (struct cursor
* cursor
) {
737 int first
= first_movable(f
.t
[cursor
->pile
]);
738 int top
= find_top(f
.t
[cursor
->pile
]);
739 if (first
+ cursor
->opt
< top
)
742 void cursor_up (struct cursor
* cursor
) {
744 if (cursor
->opt
> 0) cursor
->opt
--;
746 void cursor_right (struct cursor
* cursor
) {
748 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
751 #elif defined FREECELL
752 void cursor_left (struct cursor
* cursor
) {
754 if (is_tableu(cursor
->pile
)) {
755 if (cursor
->pile
> 0) cursor
->pile
--;
757 } else { /* cells/foundation*/
758 switch (cursor
->pile
) {
764 if (cursor
->opt
<= 0) {
765 cursor
->pile
= STOCK
;
773 void cursor_down (struct cursor
* cursor
) {
775 if (is_tableu(cursor
->pile
)) {
776 int first
= first_movable(f
.t
[cursor
->pile
]);
777 int top
= find_top(f
.t
[cursor
->pile
]);
778 if (first
+ cursor
->opt
< top
)
781 cursor
->pile
= cursor
->opt
+NUM_CELLS
*(cursor
->pile
==FOUNDATION
);
785 void cursor_up (struct cursor
* cursor
) {
787 if (is_tableu(cursor
->pile
)) {
788 if (cursor
->opt
> 0) {
791 switch (cursor
->pile
) {
792 case TAB_1
: case TAB_2
: case TAB_3
: case TAB_4
:
793 cursor
->opt
= cursor
->pile
; /*assumes TAB_1==0*/
794 cursor
->pile
= STOCK
;
796 case TAB_5
: case TAB_6
: case TAB_7
: case TAB_8
:
797 cursor
->opt
= cursor
->pile
- NUM_CELLS
;
798 cursor
->pile
= FOUNDATION
;
803 void cursor_right (struct cursor
* cursor
) {
805 if (is_tableu(cursor
->pile
)) {
806 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
809 switch (cursor
->pile
) {
811 if (cursor
->opt
< NUM_SUITS
-1) {
814 cursor
->pile
= FOUNDATION
;
818 if (cursor
->opt
< NUM_SUITS
-1)
824 void cursor_to (struct cursor
* cursor
, int pile
) {
829 int set_mouse(int pile
, int* main
, int* opt
) {
831 if (pile
< 0) return 1;
834 if (pile
>= FOUNDATION
)//TODO: check upper bound!
836 *opt
= pile
- FOUNDATION
;
839 #elif defined FREECELL
840 (void)opt
; //TODO FREECELL: needs to decode not-yet-decided term2pile STOCK/FOUNDATION encodings
845 int get_cmd (int* from
, int* to
, int* opt
) {
847 unsigned char mouse
[6] = {0}; /* must clear [3]! */
848 struct cursor inactive
= {-1,-1};
849 static struct cursor active
= {0,0};
850 if (is_tableu(active
.pile
))
854 from_l
: print_table(&active
, &inactive
);
858 /* direct addressing: */
859 case '1': *from
= TAB_1
; break;
860 case '2': *from
= TAB_2
; break;
861 case '3': *from
= TAB_3
; break;
862 case '4': *from
= TAB_4
; break;
863 case '5': *from
= TAB_5
; break;
864 case '6': *from
= TAB_6
; break;
865 case '7': *from
= TAB_7
; break;
867 case '8': *from
= TAB_8
; break;
868 case '9': *from
= TAB_9
; break;
869 case '0': *from
= TAB_10
;break;
870 #elif defined FREECELL
871 case '8': *from
= TAB_8
; break;
872 case '9': *from
= STOCK
; break;
873 case '0': *from
= FOUNDATION
; break;
874 #elif defined KLONDIKE
875 case '9': *from
= WASTE
; break;
876 case '0': *from
= FOUNDATION
; break;
877 case '8': /* fallthrough */
880 case '\n': *from
= STOCK
; break;
882 /* cursor keys addressing: */
884 case 'h': cursor_left (&active
); goto from_l
;
886 case 'j': cursor_down (&active
); goto from_l
;
888 case 'k': cursor_up (&active
); goto from_l
;
890 case 'l': cursor_right(&active
); goto from_l
;
892 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
894 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
896 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
897 case ' ': /* continue with second cursor */
900 *opt
= active
.opt
; /* when FOUNDATION */
905 case 0x7f: case '\b': /* backspace key sends DEL on most terminals */
906 if (active
.pile
== STOCK
) return CMD_INVAL
;
908 *opt
= active
.opt
; /* when FOUNDATION */
911 case '\n': return CMD_INVAL
;//TODO: move card to foundation?
913 /* mouse addressing: */
914 case MOUSE_MIDDLE
: return CMD_NONE
;
916 if (set_mouse(term2pile(mouse
), to
, opt
))
920 if (set_mouse(term2pile(mouse
), from
, opt
))
926 fprintf (stderr
, ":");
927 raw_mode(0); /* turn on echo */
928 fgets (buf
, 256, stdin
);
931 case 'q': return CMD_QUIT
;
932 case 'n': return CMD_NEW
;
933 case 'r': return CMD_AGAIN
;
934 case 'h': return CMD_HELP
;
935 default: return CMD_INVAL
;
941 if (*to
== FOUNDATION
) return CMD_JOIN
;
943 if (*to
> TAB_MAX
) return CMD_INVAL
;
945 case 'K': /* fallthrough */
946 case '?': return CMD_HINT
;
947 case 'u': return CMD_UNDO
;
948 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
949 default: return CMD_INVAL
;
951 inactive
.pile
= *from
; /* for direct addressing highlighting */
952 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
955 if (*from
== STOCK
) {
962 to_l
: print_table(&active
, &inactive
);
967 case 'h': cursor_left (&active
); goto to_l
;
969 case 'j': cursor_down (&active
); goto to_l
;
971 case 'k': cursor_up (&active
); goto to_l
;
973 case 'l': cursor_right(&active
); goto to_l
;
975 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
977 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
979 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
980 case 'J': /* fallthrough; just join selected pile */
983 break; /* continues with the foundation/empty tableu check */
985 case MOUSE_RIGHT
: return CMD_NONE
;
987 if (set_mouse(term2pile(mouse
), to
, opt
))
990 //TODO: set opt if to field is empty; suppress "up do" dialog from below
991 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
992 int top = find_top(f.t[*from]);
993 if (top < 0) return CMD_INVAL;
994 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
995 *opt = get_rank(f.t[*from][top]);
1002 case 'K': /* fallthrough */
1003 case '?': return CMD_HINT
;
1004 case 'u': return CMD_NONE
; /* cancel selection */
1005 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
1007 if (t
< '0' || t
> '9') return CMD_INVAL
;
1011 #elif defined SPIDER
1013 #elif defined FREECELL
1024 if (*from
== FOUNDATION
) {
1025 if (inactive
.opt
>= 0) {
1026 *opt
= inactive
.opt
;
1029 int top
= find_top(f
.t
[*to
]);
1030 if (top
< 0) return CMD_INVAL
;
1031 int color
= get_color(f
.t
[*to
][top
]);
1032 int choice_1
= 1-color
; /* selects piles of */
1033 int choice_2
= 2+color
; /* the opposite color */
1034 int top_c1
= find_top(f
.f
[choice_1
]);
1035 int top_c2
= find_top(f
.f
[choice_2
]);
1037 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
1038 && top_c1
>= 0 ) << 0
1039 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
1040 && top_c2
>= 0 ) << 1) {
1041 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
1042 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
1043 case (1<<1 | 1<<0): /* both, ask user which to pick from */
1044 printf ("take from (1-4): "); fflush (stdout
);
1045 *opt
= getch(NULL
) - '1';
1046 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1048 default: return CMD_INVAL
; /* none matched */
1050 /* `opt` is the foundation index (0..3) */
1052 #elif defined SPIDER
1053 /* moving to empty tableu? */
1054 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1055 int bottom
= first_movable(f
.t
[*from
]);
1056 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
1057 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
1060 int top
= find_top(f
.t
[*from
]);
1061 if (top
< 0) return CMD_INVAL
;
1062 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
1063 *opt
= get_rank(f
.t
[*from
][top
]);
1064 } else { /* only ask the user if it's unclear: */
1065 printf ("\rup to ([a23456789xjqk] or space/return): ");
1068 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
1069 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
1070 case 'a': case 'A': *opt
= RANK_A
; break;
1071 case '0': /* fallthrough */
1072 case 'x': case 'X': *opt
= RANK_X
; break;
1073 case 'j': case 'J': *opt
= RANK_J
; break;
1074 case 'q': case 'Q': *opt
= RANK_Q
; break;
1075 case 'k': case 'K': *opt
= RANK_K
; break;
1076 default: *opt
-= '1';
1078 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
1080 /* `opt` is the rank of the highest card to move */
1082 #elif defined FREECELL
1083 //TODO: if from is foundation or from is free cell, which one (if two possibilities), like klondike
1084 //TODO FREECELL: if (*from==STOCK || *from==FOUNDATION) "select correct one"
1085 //NOTE: this is more complicated, because *to can also point to stock or foundation in addition to tableu.
1087 /* if it was selected with a cursor, it's obvious: */
1088 if (inactive
.opt
>= 0) {
1089 *opt
= inactive
.opt
;
1092 if (*from
== FOUNDATION
&& *to
== STOCK
) {
1093 //can take from all non-empty foundations
1094 } else if (*from
== STOCK
&& *to
== FOUNDATION
) {
1095 //check which combinations are possible, 4*4 theoretically
1096 } else if (*from
== FOUNDATION
|| *from
== STOCK
) { // -> tableu
1097 //foundation: 2 choices
1107 int getctrlseq(unsigned char* buf
) {
1115 int offset
= 0x20; /* never sends control chars as data */
1116 while ((c
= getchar()) != EOF
) {
1120 case '\033': state
=ESC_SENT
; break;
1126 case '[': state
=CSI_SENT
; break;
1127 default: return KEY_INVAL
;
1132 case 'A': return KEY_UP
;
1133 case 'B': return KEY_DOWN
;
1134 case 'C': return KEY_RIGHT
;
1135 case 'D': return KEY_LEFT
;
1136 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
1137 case 'H': return KEY_HOME
;
1138 case 'F': return KEY_END
;
1139 case '2': getchar(); return KEY_INS
;
1140 case '5': getchar(); return KEY_PGUP
;
1141 case '6': getchar(); return KEY_PGDN
;
1142 case 'M': state
=MOUSE_EVENT
; break;
1143 default: return KEY_INVAL
;
1147 if (buf
== NULL
) return KEY_INVAL
;
1148 buf
[0] = c
- offset
;
1149 buf
[1] = getchar() - offset
;
1150 buf
[2] = getchar() - offset
;
1158 int term2pile(unsigned char *mouse
) {
1159 int line
= (mouse
[2]-1);
1160 int column
= (mouse
[1]-1) / op
.s
->width
;
1162 if (line
< op
.s
->height
) { /* first line */
1165 case 0: return STOCK
;
1166 case 1: return WASTE
;
1167 case 2: return -1; /* spacer */
1168 case 3: return FOUNDATION
+0;
1169 case 4: return FOUNDATION
+1;
1170 case 5: return FOUNDATION
+2;
1171 case 6: return FOUNDATION
+3;
1173 #elif defined SPIDER
1174 if (column
< 3) return STOCK
;
1176 #elif defined FREECELL
1177 //TODO FREECELL: term2pile cells/foundation (how to encode open cells?)
1179 } else if (line
> op
.s
->height
) { /* tableu */
1180 if (column
<= TAB_MAX
) return column
;
1184 int wait_mouse_up(unsigned char* mouse
) {
1185 //TODO: mouse drag: start gets inactive, hovering gets active cursors
1186 struct cursor cur
= {-1,-1};
1188 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
1190 /* display a cursor while mouse button is pushed: */
1191 int pile
= term2pile(mouse
);
1194 //TODO: need something similar from FREECELL
1195 if (pile
>= FOUNDATION
) {
1196 cur
.pile
= FOUNDATION
;
1197 cur
.opt
= pile
-FOUNDATION
;
1200 /* need to temporarily show the cursor, then revert to last state: */
1201 int old_show_cursor_hi
= op
.h
; //TODO: ARGH! that's awful!
1203 print_table(&cur
, NO_HI
); //TODO: should not overwrite inactive cursor!
1204 op
.h
= old_show_cursor_hi
;
1207 if (getctrlseq (mouse
+3) == MOUSE_ANY
) {
1208 /* ignore mouse wheel events: */
1209 if (mouse
[3] & 0x40) continue;
1211 else if((mouse
[3]&3) == 3) level
--; /* release event */
1212 else level
++; /* another button pressed */
1216 int success
= mouse
[1] == mouse
[4] && mouse
[2] == mouse
[5];
1223 int getch(unsigned char* buf
) {
1224 /* returns a character, EOF, or constant for an escape/control sequence - NOT
1225 compatible with the ncurses implementation of same name */
1227 if (buf
&& buf
[3]) {
1228 /* mouse was dragged; return 'ungetted' previous destination */
1229 action
= MOUSE_DRAG
;
1230 /* keep original [0], as [3] only contains release event */
1235 action
= getctrlseq(buf
);
1240 if (buf
[0] > 3) break; /* ignore wheel events */
1245 case 0: return MOUSE_LEFT
;
1246 case 1: return MOUSE_MIDDLE
;
1247 case 2: return MOUSE_RIGHT
;
1255 // shuffling and dealing {{{
1256 void deal(long seed
) {
1257 f
= (const struct playfield
){0}; /* clear playfield */
1258 card_t deck
[DECK_SIZE
*NUM_DECKS
];
1259 int avail
= DECK_SIZE
*NUM_DECKS
;
1260 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
1262 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
1263 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
1264 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
1265 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
1269 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
1270 int j
= rand() % (i
+1);
1271 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
1275 for (int i
= 0; i
< NUM_PILES
; i
++) {
1278 int count
= i
; /* pile n has n closed cards, then 1 open */
1279 #elif defined SPIDER
1281 int count
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1282 #elif defined FREECELL
1284 int count
= i
<4?6:5;/*like spider, but cards are dealt face-up*/
1286 /* "SIGN": face down cards are negated */
1287 for (int j
= 0; j
< count
; j
++) f
.t
[i
][j
] = SIGN deck
[--avail
];
1288 f
.t
[i
][count
] = deck
[--avail
]; /* the face-up card */
1291 /* rest of the cards to the stock: */
1292 /* NOTE: assert(avail==50) for spider, assert(avail==0) for freecell */
1293 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
1295 f
.w
= -1; /* @start: nothing on waste */
1296 #elif defined SPIDER
1297 f
.w
= 0; /* number of used foundations */
1298 #elif defined FREECELL
1299 f
.w
= 0; /* bitmask of used free cells */
1302 f
.u
= &undo_sentinel
;
1306 // screen drawing routines {{{
1307 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
1308 if (!op
.h
) invert
= 0; /* don't show invert if we used the mouse last */
1309 if (bold
&& op
.s
== &unicode_large_color
){ //awful hack for bold + faint
1310 int offset
= str
[3]==017?16:str
[4]==017?17:0;
1311 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1312 "\033[1m", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1313 offset
, str
, bold
?"\033[1m":"", str
+offset
,
1314 grey_bg
?"\033[49m":"", invert
?"\033[27m":"","\033[22m");
1317 printf ("%s%s%s%s%s%s%s",
1318 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1320 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
1322 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
1323 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1325 /* print stock, waste and foundation: */
1326 for (int line
= 0; line
< op
.s
->height
; line
++) {
1328 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
1329 (f
.w
< f
.z
-1)?op
.s
->facedown
1330 :op
.s
->placeholder
)[line
]);
1332 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
1333 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1334 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
1335 :op
.s
->placeholder
)[line
]);
1336 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
1338 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1339 int card
= find_top(f
.f
[pile
]);
1340 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1341 inactive
->pile
==FOUNDATION
&& (
1342 /* cursor addr. || direct addr. */
1343 inactive
->opt
==pile
|| inactive
->opt
< 0
1345 (card
< 0)?op
.s
->placeholder
[line
]
1346 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1351 #elif defined SPIDER
1352 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
1353 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
1354 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
1355 int spacer_to
= NUM_PILES
*op
.s
->width
-
1356 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
1357 for (int line
= 0; line
< op
.s
->height
; line
++) {
1358 /* available stock: */
1359 for (int i
= f
.z
/10; i
; i
--) {
1360 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
1361 else printf ("%s", op
.s
->halfstack
[line
]);
1364 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
1365 /* foundation (overlapping): */
1366 for (int i
= NUM_DECKS
*NUM_SUITS
-1, half
= 0; i
>= 0; i
--) {
1367 int overlap
= half
? op
.s
->halfcard
[line
]: 0;
1368 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
1369 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
),
1375 #elif defined FREECELL
1376 /* print open cells, foundation: */
1377 for (int line
= 0; line
< op
.s
->height
; line
++) {
1378 //FREECELL TODO: cells and foundation look the same! (different placeholder?)
1379 for (int pile
= 0; pile
< NUM_CELLS
; pile
++)
1380 print_hi (active
->pile
==STOCK
&& active
->opt
==pile
,
1381 inactive
->pile
==STOCK
&& (
1382 /* cursor addr. || direct addr. */
1383 inactive
->opt
==pile
|| inactive
->opt
< 0
1385 ((f
.s
[pile
])?op
.s
->card
[f
.s
[pile
]]
1386 :op
.s
->placeholder
)[line
]);
1387 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1388 int card
= find_top(f
.f
[pile
]);
1389 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1390 inactive
->pile
==FOUNDATION
&& (
1391 /* cursor addr. || direct addr. */
1392 inactive
->opt
==pile
|| inactive
->opt
< 0
1394 (card
< 0)?op
.s
->placeholder
[line
]
1395 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1402 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1403 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1404 #elif defined SPIDER || defined FREECELL
1405 int offset
[NUM_PILES
]={0};
1406 //TODO FREECELL: multi-card-moving constraint! for DO_HI() and TOP_HI()
1407 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1408 && offset[pile] >= cursor->opt)
1409 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1410 && offset[pile] == cursor->opt)
1412 /* print tableu piles: */
1413 int row
[NUM_PILES
] = {0};
1414 int line
[NUM_PILES
]= {0};
1415 int label
[NUM_PILES
]={0};
1417 int did_placeholders
= 0;
1420 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
1421 card_t card
= f
.t
[pile
][row
[pile
]];
1422 card_t next
= f
.t
[pile
][row
[pile
]+1];
1423 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
1424 int empty
= !card
&& row
[pile
] == 0;
1426 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
1427 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
1428 :(card
<0)?op
.s
->facedown
1432 int extreme_overlap
= ( 3 /* spacer, labels, status */
1433 + 2 * op
.s
->height
/* stock, top tableu card */
1434 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
1435 /* normal overlap: */
1436 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
1437 /* extreme overlap on closed cards: */
1438 || (extreme_overlap
&&
1440 f
.t
[pile
][row
[pile
]] < 0 &&
1441 f
.t
[pile
][row
[pile
]+1] <0)
1442 /* extreme overlap on sequences: */
1443 || (extreme_overlap
&&
1444 !TOP_HI(active
) && /*always show top selected card*/
1445 line
[pile
] >= 1 && row
[pile
] > 0 &&
1446 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
1447 is_consecutive (f
.t
[pile
], row
[pile
]) &&
1448 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
1449 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
1453 #if defined SPIDER || defined FREECELL
1454 if (movable
) offset
[pile
]++;
1457 /* tableu labels: */
1458 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
1460 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
1462 line_had_card
|= !!card
;
1463 did_placeholders
|= row
[pile
] > 0;
1466 } while (line_had_card
|| !did_placeholders
);
1469 void visbell (void) {
1471 printf ("\033[?5h"); fflush (stdout
);
1473 printf ("\033[?5l"); fflush (stdout
);
1475 void win_anim(void) {
1476 printf ("\033[?25l"); /* hide cursor */
1478 /* set cursor to random location */
1479 //TODO: use actual terminal size instead!
1480 int row
= 1+rand()%(24-op
.s
->width
);
1481 int col
= 1+rand()%(80-op
.s
->height
);
1483 /* draw random card */
1484 int face
= 1 + rand() % 52;
1485 for (int l
= 0; l
< op
.s
->height
; l
++) {
1486 printf ("\033[%d;%dH", row
+l
, col
);
1487 printf ("%s", op
.s
->card
[face
][l
]);
1491 /* exit on keypress */
1492 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
1493 if (poll (&p
, 1, 80)) goto fin
;
1496 printf ("\033[?25h"); /* show cursor */
1502 void undo_push (int _f
, int t
, int n
, int o
) {
1503 struct undo
* new = malloc(sizeof(struct undo
));
1513 void undo_pop (struct undo
* u
) {
1514 if (u
== &undo_sentinel
) return;
1517 if (u
->f
== FOUNDATION
) {
1518 /* foundation -> tableu */
1519 int top_f
= find_top(f
.f
[u
->n
]);
1520 int top_t
= find_top(f
.t
[u
->t
]);
1521 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1522 f
.t
[u
->t
][top_t
] = NO_CARD
;
1523 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
1524 /* waste -> foundation */
1525 /* split u->n into wst and fnd: */
1526 int wst
= u
->n
& 0xffff;
1527 int fnd
= u
->n
>> 16;
1528 /* move stock cards one position up to make room: */
1529 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
1530 /* move one card from foundation to waste: */
1531 int top
= find_top(f
.f
[fnd
]);
1532 f
.s
[wst
] = f
.f
[fnd
][top
];
1533 f
.f
[fnd
][top
] = NO_CARD
;
1536 } else if (u
->f
== WASTE
) {
1537 /* waste -> tableu */
1538 /* move stock cards one position up to make room: */
1539 for (int i
= f
.z
; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
1540 /* move one card from tableu to waste: */
1541 int top
= find_top(f
.t
[u
->t
]);
1542 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
1543 f
.t
[u
->t
][top
] = NO_CARD
;
1546 } else if (u
->t
== FOUNDATION
) {
1547 /* tableu -> foundation */
1548 int top_f
= find_top(f
.t
[u
->f
]);
1549 int top_t
= find_top(f
.f
[u
->n
]);
1550 /* close topcard if previous action caused turn_over(): */
1551 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1552 /* move one card from foundation to tableu: */
1553 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1554 f
.f
[u
->n
][top_t
] = NO_CARD
;
1556 /* tableu -> tableu */
1557 int top_f
= find_top(f
.t
[u
->f
]);
1558 int top_t
= find_top(f
.t
[u
->t
]);
1559 /* close topcard if previous action caused turn_over(): */
1560 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1561 /* move n cards from tableu[f] to tableu[t]: */
1562 for (int i
= 0; i
< u
->n
; i
++) {
1563 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1564 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1567 #elif defined SPIDER
1568 if (u
->f
== STOCK
) {
1569 /* stock -> tableu */
1570 /*remove a card from each pile and put it back onto the stock:*/
1571 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
1572 int top
= find_top(f
.t
[pile
]);
1573 f
.s
[f
.z
++] = f
.t
[pile
][top
];
1574 f
.t
[pile
][top
] = NO_CARD
;
1576 } else if (u
->t
== FOUNDATION
) {
1577 /* tableu -> foundation */
1578 int top
= find_top(f
.t
[u
->f
]);
1579 /* close topcard if previous action caused turn_over(): */
1580 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1581 /* append cards from foundation to tableu */
1582 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1583 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1584 f
.f
[u
->n
][i
] = NO_CARD
;
1586 f
.w
--; /* decrement complete-foundation-counter */
1589 /* tableu -> tableu */
1590 int top_f
= find_top(f
.t
[u
->f
]);
1591 int top_t
= find_top(f
.t
[u
->t
]);
1592 /* close topcard if previous action caused turn_over(): */
1593 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1594 /* move n cards from tableu[f] to tableu[t]: */
1595 for (int i
= 0; i
< u
->n
; i
++) {
1596 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1597 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1600 #elif defined FREECELL
1601 /*NOTE: if from and to are both stock/foundation, opt = from | to<<16 */
1602 if (u
->f
== STOCK
&& u
->t
== FOUNDATION
) {
1603 /* free cells -> foundation */
1604 /* split u->n into cll and fnd: */
1605 int cll
= u
->n
& 0xffff;
1606 int fnd
= u
->n
>> 16;
1607 /* move one card from foundation to free cell: */
1608 int top
= find_top(f
.f
[fnd
]);
1609 f
.s
[cll
] = f
.f
[fnd
][top
];
1610 f
.f
[fnd
][top
] = NO_CARD
;
1611 f
.w
|= 1<<cll
; /* mark cell as occupied */
1612 } else if (u
->f
== STOCK
) {
1613 /* free cells -> cascade */
1614 int top_t
= find_top(f
.t
[u
->t
]);
1615 f
.s
[u
->n
] = f
.t
[u
->t
][top_t
];
1616 f
.t
[u
->t
][top_t
] = NO_CARD
;
1617 f
.w
|= 1<<u
->n
; /* mark cell as occupied */
1618 } else if (u
->f
== FOUNDATION
&& u
->t
== STOCK
) {
1619 /* foundation -> free cells */
1620 /* split u->n into cll and fnd: */
1621 int cll
= u
->n
>> 16;
1622 int fnd
= u
->n
& 0xffff;
1623 /* move 1 card from free cell to foundation: */
1624 int top_f
= find_top(f
.f
[fnd
]);
1625 f
.f
[fnd
][top_f
+1] = f
.s
[cll
];
1627 f
.w
&= ~(1<<cll
); /* mark cell as free */
1628 } else if (u
->f
== FOUNDATION
) {
1629 /* foundation -> cascade */
1630 int top_f
= find_top(f
.f
[u
->n
]);
1631 int top_t
= find_top(f
.t
[u
->t
]);
1632 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1633 f
.t
[u
->t
][top_t
] = NO_CARD
;
1634 } else if (u
->t
== STOCK
) {
1635 /* cascade -> free cells */
1636 int top_f
= find_top(f
.t
[u
->f
]);
1637 f
.t
[u
->f
][top_f
+1] = f
.s
[u
->n
];
1638 f
.s
[u
->n
] = NO_CARD
;
1639 f
.w
&= ~(1<<u
->n
); /* mark cell as free */
1640 } else if (u
->t
== FOUNDATION
) {
1641 /* cascade -> foundation */
1642 int top_f
= find_top(f
.t
[u
->f
]);
1643 int top_t
= find_top(f
.f
[u
->n
]);
1644 /* move one card from foundation to cascade: */
1645 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1646 f
.f
[u
->n
][top_t
] = NO_CARD
;
1647 } else { // tableu -> tableu
1648 //TODO FREECELL: undo tableu->tableu
1656 void free_undo (struct undo
* u
) {
1657 while (u
&& u
!= &undo_sentinel
) {
1665 // initialization stuff {{{
1666 void screen_setup (int enable
) {
1669 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1670 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1671 printf ("\033[?1000h"); /* enable mouse */
1673 printf ("\033[?1000l"); /* disable mouse */
1674 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1679 void raw_mode(int enable
) {
1680 static struct termios saved_term_mode
;
1681 struct termios raw_term_mode
;
1684 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1685 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1686 raw_term_mode
= saved_term_mode
;
1687 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1688 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1689 raw_term_mode
.c_cc
[VTIME
] = 0;
1690 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1692 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1696 void signal_handler (int signum
) {
1701 signal(SIGTSTP
, SIG_DFL
); /* NOTE: assumes SysV semantics! */
1706 print_table(NO_HI
, NO_HI
);
1708 case SIGINT
: //TODO: don't exit; just warn like vim does
1711 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1717 void signal_setup(void) {
1718 struct sigaction saction
;
1720 saction
.sa_handler
= signal_handler
;
1721 sigemptyset(&saction
.sa_mask
);
1722 saction
.sa_flags
= 0;
1723 if (sigaction(SIGTSTP
, &saction
, NULL
) < 0) {
1727 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1731 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1735 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1736 perror ("SIGWINCH");
1742 //vim: foldmethod=marker