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; break; /* 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 max_move(int from
, int to
) {
407 /* returns the maximum number of cards that can be moved */
408 /* see also: https://boardgames.stackexchange.com/a/45157/26498 */
409 int free_tabs
= 0, free_cells
= 0;
410 for (int i
= 0; i
< NUM_PILES
; i
++) free_tabs
+= f
.t
[i
][0] == NO_CARD
;
411 for (int i
= 0; i
< NUM_CELLS
; i
++) free_cells
+= f
.s
[i
] == NO_CARD
;
413 /* don't count the tableau we are moving to: */
414 if (f
.t
[to
][0] == NO_CARD
) free_tabs
--;
416 /* theoretic maximum is limited by the number of cards on the pile */
417 int max_theory
= (1<<free_tabs
) * (free_cells
+ 1);
418 int max_effective
= 1 + find_top(f
.t
[from
]) - first_movable(f
.t
[from
]);
419 return max_effective
< max_theory
? max_effective
: max_theory
;
421 //TODO FREECELL: auto move to tableu after each move (not all cards possible, only when it is the smallest rank still on the board)
422 int t2t(int from
, int to
, int opt
) {
423 int top_to
= find_top(f
.t
[to
]);
424 int top_from
= find_top(f
.t
[from
]);
425 int count
= 0; //NOTE: could probably be factored out
426 int cards
= max_move(from
, to
);
427 if (top_to
< 0) { /* moving to empty pile? */
429 return ERR
; /* cannot execute move */
430 cards
= opt
; /* user wants to move n cards*/
433 for (int i
= top_from
; i
>=0; i
--) {
434 if (cards
-->0/*enough space and not more attempted than wanted*/
435 && ((top_to
>= 0 /* if destn. not empty: check rank/color */
436 && ((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
437 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))))
438 || (top_to
< 0 && !cards
))) {/*if dest empty and right # cards*/
439 /* move cards [i..top_from] to their destination */
440 for (;i
<= top_from
; i
++) {
442 f
.t
[to
][top_to
] = f
.t
[from
][i
];
443 f
.t
[from
][i
] = NO_CARD
;
446 undo_push(from
, to
, count
, 0);
450 return ERR
; /* no such move possible */
452 int t2f(int from
, int to
, int opt
) { /* 1:1 copy from KLONDIKE */
453 (void) to
; (void) opt
; /* don't need */
454 int top_from
= find_top(f
.t
[from
]);
455 to
= get_suit(f
.t
[from
][top_from
]);
456 int top_to
= find_top(f
.f
[to
]);
457 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
458 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
459 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
460 f
.t
[from
][top_from
] = NO_CARD
;
461 undo_push(from
, FOUNDATION
, to
, 0);
462 if (check_won()) return WON
;
466 int f2t(int from
, int to
, int opt
) {
467 (void) from
; /* don't need */
468 int top_to
= find_top(f
.t
[to
]);
470 int top_from
= find_top(f
.f
[from
]);
472 if (top_to
< 0 /* empty tableu? */
473 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
474 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
])))) {
475 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
476 f
.f
[from
][top_from
] = NO_CARD
;
477 undo_push(FOUNDATION
, to
, from
, 0);
481 int t2c(int from
, int to
, int opt
) {
482 (void) to
; (void) opt
; /* don't need */
483 /* is a cell free? */
484 if (f
.w
== (1<<NUM_CELLS
)-1)
486 for (to
= 0; to
< NUM_CELLS
; to
++)
487 if (!(f
.w
>>to
&1)) break;
489 int top_from
= find_top(f
.t
[from
]);
490 f
.s
[to
] = f
.t
[from
][top_from
];
491 f
.t
[from
][top_from
] = NO_CARD
;
492 f
.w
|= 1<<to
; /* mark cell as occupied */
493 undo_push(from
, STOCK
, to
, 0);
497 int c2t(int from
, int to
, int opt
) {
498 (void) from
; /* don't need */
499 int top_to
= find_top(f
.t
[to
]);
502 if (top_to
< 0 /* empty tableu? */
503 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[from
]))
504 && (rank_next(f
.s
[from
], f
.t
[to
][top_to
])))) {
505 f
.t
[to
][top_to
+1] = f
.s
[from
];
507 f
.w
&= ~(1<<from
); /* mark cell as free */
508 undo_push(STOCK
, to
, from
, 0);
513 int c2f(int from
, int to
, int opt
) {
514 (void) from
; (void) to
; /* don't need */
516 to
= get_suit(f
.s
[from
]);
517 int top_to
= find_top(f
.f
[to
]);
518 if ((top_to
< 0 && get_rank(f
.s
[from
]) == RANK_A
)
519 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.s
[from
]))) {
520 f
.f
[to
][top_to
+1] = f
.s
[from
];
522 f
.w
&= ~(1<<from
); /* mark cell as free */
523 undo_push(STOCK
, FOUNDATION
, from
| to
<<16, 0);
524 if (check_won()) return WON
;
528 int f2c(int from
, int to
, int opt
) {
529 (void) from
; (void) to
; /* don't need */
530 /* is a cell free? */
531 if (f
.w
== (1<<NUM_CELLS
)-1)
533 for (to
= 0; to
< NUM_CELLS
; to
++)
534 if (!(f
.w
>>to
&1)) break;
537 int top_from
= find_top(f
.f
[from
]);
538 f
.s
[to
] = f
.f
[from
][top_from
];
539 f
.f
[from
][top_from
] = NO_CARD
;
540 f
.w
|= 1<<to
; /* mark cell as occupied */
541 undo_push(FOUNDATION
, STOCK
, from
| to
<<16, 0);
547 //TODO: generalize prediction engine for CMD_HINT
549 #define would_complete(pile) 0
551 #define would_complete(pile) \
552 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
553 && get_rank(f.t[to][bottom_to]) == RANK_K)
554 #elif defined FREECELL
555 #define would_complete(pile) 0
557 #define would_turn(pile) \
558 (f.t[pile][r[pile].pos-1] < 0)
559 #define would_empty(pile) \
563 int top_to
= find_top(f
.t
[to
]);
565 int bottom_to
= first_movable(f
.t
[to
]);
569 if (to
== WASTE
|| to
== STOCK
) return ERR
; /*why would you do that!?*/
571 if (to
== FOUNDATION
) {
573 for (int i
= 0; i
<= TAB_MAX
; i
++)
574 switch ((i
?t2f
:w2f
)(i
-1, FOUNDATION
, 0)) {
575 case WON
: return WON
;
576 case OK
: status
= OK
;
582 if (top_to
< 0) { /* move a king to empty pile: */
583 for (int i
= 0; i
< TAB_MAX
; i
++) {
584 if (f
.t
[i
][0] < 0) /* i.e. would turn? */
585 if (t2t(i
, to
, 0) == OK
) return OK
;
587 return w2t(WASTE
, to
, 0);
592 int ok
:1; /* card to move in pile? */
593 int above
; /* number of movable cards above */
594 int below
; /* number of cards below ours */
595 int pos
; /* where the card to move is in the pile */
596 int top
; /* find_top() */
597 } r
[NUM_PILES
] = {{0}};
598 int complete
= 0;/* SPIDER: true if any pile would complete a stack */
599 int turn
= 0; /* SPIDER: true if any pile would turn_over */
600 int empty
= 0; /* true if any pile would become empty */
602 /* 1. rate each pile: */
605 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
606 if (pile
== to
) continue;
607 int top
= find_top(f
.t
[pile
]);
608 int bottom
= first_movable(f
.t
[pile
]);
609 r
[pile
].pos
= bottom
; /* need for would_empty */
611 if (top
< 0) continue; /* no cards to move */
612 if (would_empty(pile
)) continue; /* doesn't help */
615 r
[pile
].above
= 0; /* always take as many as possible */
616 r
[pile
].below
= top
- bottom
;
618 complete
|= would_complete(pile
); /* never happens */
619 turn
|= would_turn(pile
);
620 empty
|= would_empty(pile
);
624 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
625 r
[pile
].top
= r
[pile
].pos
= find_top(f
.t
[pile
]);
626 /* backtrack until we find a compatible-to-'to'-pile card: */
627 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
628 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
629 - get_rank(f
.t
[to
][top_to
]);
630 if (rankdiff
>= 0) break; /* past our card */
631 if (rankdiff
== -1 /* rank matches */
633 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
634 != get_color(f
.t
[to
][top_to
])
636 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
637 == get_suit(f
.t
[to
][top_to
])
641 complete
|= would_complete(pile
);
642 turn
|= would_turn(pile
);
643 empty
|= would_empty(pile
);
644 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
645 if (is_movable(f
.t
[pile
], i
-1))
655 /* 2. find optimal pile: (optimized for spider) */
656 //todo: in spider, prefer longest piles if above==0 (faster completions)
658 for (int pile
= 0, above
= 99, below
= 99; pile
< NUM_PILES
; pile
++) {
659 if (!r
[pile
].ok
) continue;
660 /* don't bother if another pile would be better: prefer ... */
661 /* ... to complete a stack: */
662 if (!would_complete(pile
) && complete
) continue;
663 /* ... emptying piles: */
664 if (!would_empty(pile
) && empty
&& !complete
) continue;
665 /* ... to turn_over: */
666 if (!would_turn(pile
) && turn
&& !complete
&& !empty
) continue;
667 /* ... not to rip apart too many cards: */
668 if (r
[pile
].above
> above
) continue;
669 /* if tied, prefer ... */
670 if (r
[pile
].above
== above
671 /* ... larger pile if destination is empty */
672 && (top_to
< 0? r
[pile
].below
< below
673 /* ... shorter pile otherwise */
674 : r
[pile
].below
> below
))
678 above
= r
[pile
].above
;
679 below
= r
[pile
].below
;
682 /* 3. move cards over and return: */
684 /* prefer waste if it wouldn't turn_over: */
685 /* NOTE: does not attempt to take from froundation */
686 if (!turn
&& w2t(WASTE
, to
, 0) == OK
) //TODO: gives higher priority to waste than to empty!
688 if (from
< 0) /* nothing found */
690 return t2t(from
, to
, 0);
692 if (from
< 0) /* nothing found */
694 int bottom
= first_movable(f
.t
[from
]);
695 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
696 #elif defined FREECELL
698 return ERR
; //TODO FREECELL: implement join
703 #undef would_complete
704 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
707 // keyboard input handling {{{
708 // cursor functions{{{
710 void cursor_left (struct cursor
* cursor
) {
712 if (is_tableu(cursor
->pile
)) {
713 if (cursor
->pile
> 0) cursor
->pile
--;
715 } else { /* stock/waste/foundation*/
716 switch (cursor
->pile
) {
717 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
719 if (cursor
->opt
<= 0)
720 cursor
->pile
= WASTE
;
726 void cursor_down (struct cursor
* cursor
) {
728 if (!is_tableu(cursor
->pile
)) {
729 switch (cursor
->pile
) {
730 case STOCK
: cursor
->pile
= TAB_1
; break;
731 case WASTE
: cursor
->pile
= TAB_2
; break;
733 cursor
->pile
= TAB_4
+ cursor
->opt
;
738 void cursor_up (struct cursor
* cursor
) {
740 if (is_tableu(cursor
->pile
)) {
741 switch (cursor
->pile
) { //ugly :|
742 case TAB_1
: cursor
->pile
= STOCK
; break;
743 case TAB_2
: cursor
->pile
= WASTE
; break;
744 case TAB_3
: cursor
->pile
= WASTE
; break;
745 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
746 cursor
->opt
=cursor
->pile
-TAB_4
;
747 cursor
->pile
= FOUNDATION
;
752 void cursor_right (struct cursor
* cursor
) {
754 if (is_tableu(cursor
->pile
)) {
755 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
758 switch (cursor
->pile
) {
759 case STOCK
: cursor
->pile
= WASTE
; break;
760 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
762 if (cursor
->opt
< NUM_SUITS
-1)
768 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
769 void cursor_left (struct cursor
* cursor
) {
771 if (cursor
->pile
> 0) cursor
->pile
--;
774 void cursor_down (struct cursor
* cursor
) {
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 void cursor_up (struct cursor
* cursor
) {
783 if (cursor
->opt
> 0) cursor
->opt
--;
785 void cursor_right (struct cursor
* cursor
) {
787 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
790 #elif defined FREECELL
791 void cursor_left (struct cursor
* cursor
) {
793 if (is_tableu(cursor
->pile
)) {
794 if (cursor
->pile
> 0) cursor
->pile
--;
796 } else { /* cells/foundation*/
797 switch (cursor
->pile
) {
803 if (cursor
->opt
<= 0) {
804 cursor
->pile
= STOCK
;
812 void cursor_down (struct cursor
* cursor
) {
814 if (is_tableu(cursor
->pile
)) {
815 int first
= first_movable(f
.t
[cursor
->pile
]);
816 int top
= find_top(f
.t
[cursor
->pile
]);
817 if (first
+ cursor
->opt
< top
)
820 cursor
->pile
= cursor
->opt
+NUM_CELLS
*(cursor
->pile
==FOUNDATION
);
824 void cursor_up (struct cursor
* cursor
) {
826 if (is_tableu(cursor
->pile
)) {
827 if (cursor
->opt
> 0) {
830 switch (cursor
->pile
) {
831 case TAB_1
: case TAB_2
: case TAB_3
: case TAB_4
:
832 cursor
->opt
= cursor
->pile
; /*assumes TAB_1==0*/
833 cursor
->pile
= STOCK
;
835 case TAB_5
: case TAB_6
: case TAB_7
: case TAB_8
:
836 cursor
->opt
= cursor
->pile
- NUM_CELLS
;
837 cursor
->pile
= FOUNDATION
;
842 void cursor_right (struct cursor
* cursor
) {
844 if (is_tableu(cursor
->pile
)) {
845 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
848 switch (cursor
->pile
) {
850 if (cursor
->opt
< NUM_SUITS
-1) {
853 cursor
->pile
= FOUNDATION
;
857 if (cursor
->opt
< NUM_SUITS
-1)
863 void cursor_to (struct cursor
* cursor
, int pile
) {
868 int set_mouse(int pile
, int* main
, int* opt
) {
869 //TODO: this should set cursor.opt, so card selector choice dialog does not trigger!
871 if (pile
< 0) return 1;
874 if (pile
>= FOUNDATION
)//TODO: check upper bound!
876 *opt
= pile
- FOUNDATION
;
879 #elif defined FREECELL
880 if (pile
> TAB_MAX
) {
881 *main
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
882 *opt
= (pile
-STOCK
) % 4;
888 int get_cmd (int* from
, int* to
, int* opt
) {
890 unsigned char mouse
[6] = {0}; /* must clear [3]! */
891 struct cursor inactive
= {-1,-1};
892 static struct cursor active
= {0,0};
893 if (is_tableu(active
.pile
))
897 from_l
: print_table(&active
, &inactive
);
901 /* direct addressing: */
902 case '1': *from
= TAB_1
; break;
903 case '2': *from
= TAB_2
; break;
904 case '3': *from
= TAB_3
; break;
905 case '4': *from
= TAB_4
; break;
906 case '5': *from
= TAB_5
; break;
907 case '6': *from
= TAB_6
; break;
908 case '7': *from
= TAB_7
; break;
910 case '8': *from
= TAB_8
; break;
911 case '9': *from
= TAB_9
; break;
912 case '0': *from
= TAB_10
;break;
913 #elif defined FREECELL
914 case '8': *from
= TAB_8
; break;
915 case '9': *from
= STOCK
; break;
916 case '0': *from
= FOUNDATION
; break;
917 #elif defined KLONDIKE
918 case '9': *from
= WASTE
; break;
919 case '0': *from
= FOUNDATION
; break;
920 case '8': /* fallthrough */
923 case '\n': *from
= STOCK
; break;
925 /* cursor keys addressing: */
927 case 'h': cursor_left (&active
); goto from_l
;
929 case 'j': cursor_down (&active
); goto from_l
;
931 case 'k': cursor_up (&active
); goto from_l
;
933 case 'l': cursor_right(&active
); goto from_l
;
935 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
937 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
939 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
940 case ' ': /* continue with second cursor */
943 *opt
= active
.opt
; /* when FOUNDATION */
948 //TODO: instead of backspace, use doublespace (first try x2t, then x2c)
949 case 0x7f: case '\b': /* backspace key sends DEL on most terminals */
950 if (active
.pile
== STOCK
) return CMD_INVAL
;
952 *opt
= active
.opt
; /* when FOUNDATION */
955 case '\n': return CMD_INVAL
;//TODO: move card to foundation?
957 /* mouse addressing: */
958 case MOUSE_MIDDLE
: return CMD_NONE
;
960 if (set_mouse(term2pile(mouse
), to
, opt
))
964 if (set_mouse(term2pile(mouse
), from
, opt
))
966 if (!is_tableu(*from
))
967 inactive
.opt
= *opt
; /* prevents card selector dialog */
972 fprintf (stderr
, ":");
973 raw_mode(0); /* turn on echo */
974 fgets (buf
, 256, stdin
);
977 case 'q': return CMD_QUIT
;
978 case 'n': return CMD_NEW
;
979 case 'r': return CMD_AGAIN
;
980 case 'h': return CMD_HELP
;
981 default: return CMD_INVAL
;
987 if (*to
== FOUNDATION
) return CMD_JOIN
;
989 if (*to
> TAB_MAX
) return CMD_INVAL
;
991 case 'K': /* fallthrough */
992 case '?': return CMD_HINT
;
993 case 'u': return CMD_UNDO
;
994 case 002: return CMD_NONE
; /* sent by SIGWINCH */
995 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
996 default: return CMD_INVAL
;
998 inactive
.pile
= *from
; /* for direct addressing highlighting */
999 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
1002 if (*from
== STOCK
) {
1009 to_l
: print_table(&active
, &inactive
);
1014 case 'h': cursor_left (&active
); goto to_l
;
1016 case 'j': cursor_down (&active
); goto to_l
;
1018 case 'k': cursor_up (&active
); goto to_l
;
1020 case 'l': cursor_right(&active
); goto to_l
;
1022 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
1024 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
1026 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
1027 case 'J': /* fallthrough; just join selected pile */
1030 break; /* continues with the foundation/empty tableu check */
1032 case MOUSE_RIGHT
: return CMD_NONE
;
1034 if (set_mouse(term2pile(mouse
), to
, opt
))
1037 //TODO: set opt if to field is empty; suppress "up do" dialog from below
1038 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1039 int top = find_top(f.t[*from]);
1040 if (top < 0) return CMD_INVAL;
1041 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
1042 *opt = get_rank(f.t[*from][top]);
1049 case 'K': /* fallthrough */
1050 case '?': return CMD_HINT
;
1051 case 'u': return CMD_NONE
; /* cancel selection */
1052 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
1054 if (t
< '0' || t
> '9') return CMD_INVAL
;
1058 #elif defined SPIDER
1060 #elif defined FREECELL
1071 if (*from
== FOUNDATION
) {
1072 if (inactive
.opt
>= 0) {
1073 *opt
= inactive
.opt
;
1076 int top
= find_top(f
.t
[*to
]);
1077 if (top
< 0) return CMD_INVAL
;
1078 int color
= get_color(f
.t
[*to
][top
]);
1079 int choice_1
= 1-color
; /* selects piles of */
1080 int choice_2
= 2+color
; /* the opposite color */
1081 int top_c1
= find_top(f
.f
[choice_1
]);
1082 int top_c2
= find_top(f
.f
[choice_2
]);
1084 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
1085 && top_c1
>= 0 ) << 0
1086 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
1087 && top_c2
>= 0 ) << 1) {
1088 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
1089 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
1090 case (1<<1 | 1<<0): /* both, ask user which to pick from */
1091 printf ("take from (1-4): "); fflush (stdout
);
1092 *opt
= getch(NULL
) - '1';
1093 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1095 default: return CMD_INVAL
; /* none matched */
1097 /* `opt` is the foundation index (0..3) */
1099 #elif defined SPIDER
1100 /* moving to empty tableu? */
1101 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1102 int bottom
= first_movable(f
.t
[*from
]);
1103 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
1104 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
1107 int top
= find_top(f
.t
[*from
]);
1108 if (top
< 0) return CMD_INVAL
;
1109 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
1110 *opt
= get_rank(f
.t
[*from
][top
]);
1111 } else { /* only ask the user if it's unclear: */
1112 printf ("\rup to ([a23456789xjqk] or space/return): ");
1115 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
1116 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
1117 case 'a': case 'A': *opt
= RANK_A
; break;
1118 case '0': /* fallthrough */
1119 case 'x': case 'X': *opt
= RANK_X
; break;
1120 case 'j': case 'J': *opt
= RANK_J
; break;
1121 case 'q': case 'Q': *opt
= RANK_Q
; break;
1122 case 'k': case 'K': *opt
= RANK_K
; break;
1123 default: *opt
-= '1';
1125 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
1127 /* `opt` is the rank of the highest card to move */
1129 #elif defined FREECELL
1130 //TODO FREECELL: card selector choice dialog
1132 /* if it was selected with a cursor, it's obvious: */
1133 if (inactive
.opt
>= 0) {
1134 if (is_tableu(*from
)) {
1135 //WARN: inefficient!
1136 int movable
= 1 + (find_top(f
.t
[*from
]) - first_movable(f
.t
[*from
]));
1137 *opt
= movable
- inactive
.opt
;
1139 *opt
= inactive
.opt
;
1141 /* moving from tableu to empty tableu? */
1142 } else if (is_tableu(*from
) && is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1143 // how many cards? (NOTE: spider asks "up to rank?"; do this then convert to number of cards?
1144 printf ("take how many (1-9): "); fflush (stdout
);
1145 *opt
= getch(NULL
) - '0';
1146 if (*opt
< 1 || *opt
> 9) return CMD_INVAL
;
1147 /* moving between stock/foundation? */
1148 } else if (*from
== FOUNDATION
&& *to
== STOCK
) {
1149 //can take from all non-empty foundations
1150 printf ("take from (1-4): "); fflush (stdout
);
1151 *opt
= getch(NULL
) - '1';
1152 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1153 } else if (*from
== STOCK
&& *to
== FOUNDATION
) {
1154 //check all non-empty cells
1155 printf ("take from (1-4): "); fflush (stdout
);
1156 *opt
= getch(NULL
) - '1';
1157 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1158 } else if (*from
== FOUNDATION
|| *from
== STOCK
) { /* -> tableu */
1159 //foundation: 2 choices
1161 printf ("take from (1-4): "); fflush (stdout
);
1162 *opt
= getch(NULL
) - '1';
1163 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1169 int getctrlseq(unsigned char* buf
) {
1177 int offset
= 0x20; /* never sends control chars as data */
1178 while ((c
= getchar()) != EOF
) {
1182 case '\033': state
=ESC_SENT
; break;
1188 case '[': state
=CSI_SENT
; break;
1189 default: return KEY_INVAL
;
1194 case 'A': return KEY_UP
;
1195 case 'B': return KEY_DOWN
;
1196 case 'C': return KEY_RIGHT
;
1197 case 'D': return KEY_LEFT
;
1198 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
1199 case 'H': return KEY_HOME
;
1200 case 'F': return KEY_END
;
1201 case '2': getchar(); return KEY_INS
;
1202 case '5': getchar(); return KEY_PGUP
;
1203 case '6': getchar(); return KEY_PGDN
;
1204 case 'M': state
=MOUSE_EVENT
; break;
1205 default: return KEY_INVAL
;
1209 if (buf
== NULL
) return KEY_INVAL
;
1210 buf
[0] = c
- offset
;
1211 buf
[1] = getchar() - offset
;
1212 buf
[2] = getchar() - offset
;
1220 int term2pile(unsigned char *mouse
) {
1221 int line
= (mouse
[2]-1);
1222 int column
= (mouse
[1]-1) / op
.s
->width
;
1224 if (line
< op
.s
->height
) { /* first line */
1227 case 0: return STOCK
;
1228 case 1: return WASTE
;
1229 case 2: return -1; /* spacer */
1230 case 3: return FOUNDATION
+0;
1231 case 4: return FOUNDATION
+1;
1232 case 5: return FOUNDATION
+2;
1233 case 6: return FOUNDATION
+3;
1235 #elif defined SPIDER
1236 if (column
< 3) return STOCK
;
1238 #elif defined FREECELL
1239 if (column
< NUM_SUITS
+ NUM_CELLS
) return STOCK
+column
;
1242 } else if (line
> op
.s
->height
) { /* tableu */
1243 if (column
<= TAB_MAX
) return column
;
1247 int wait_mouse_up(unsigned char* mouse
) {
1248 //TODO: mouse drag: start gets inactive, hovering gets active cursors
1249 struct cursor cur
= {-1,-1};
1251 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
1253 /* display a cursor while mouse button is pushed: */
1254 int pile
= term2pile(mouse
);
1257 if (pile
>= FOUNDATION
) {
1258 cur
.pile
= FOUNDATION
;
1259 cur
.opt
= pile
-FOUNDATION
;
1261 #elif defined FREECELL
1262 if (pile
> TAB_MAX
) {
1263 cur
.pile
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
1264 cur
.opt
= (pile
-STOCK
) % 4;
1267 /* need to temporarily show the cursor, then revert to last state: */
1268 int old_show_cursor_hi
= op
.h
; //TODO: ARGH! that's awful!
1270 print_table(&cur
, NO_HI
); //TODO: should not overwrite inactive cursor!
1271 op
.h
= old_show_cursor_hi
;
1274 if (getctrlseq (mouse
+3) == MOUSE_ANY
) {
1275 /* ignore mouse wheel events: */
1276 if (mouse
[3] & 0x40) continue;
1278 else if((mouse
[3]&3) == 3) level
--; /* release event */
1279 else level
++; /* another button pressed */
1283 int success
= mouse
[1] == mouse
[4] && mouse
[2] == mouse
[5];
1290 int getch(unsigned char* buf
) {
1291 /* returns a character, EOF, or constant for an escape/control sequence - NOT
1292 compatible with the ncurses implementation of same name */
1294 if (buf
&& buf
[3]) {
1295 /* mouse was dragged; return 'ungetted' previous destination */
1296 action
= MOUSE_DRAG
;
1297 /* keep original [0], as [3] only contains release event */
1302 action
= getctrlseq(buf
);
1307 if (buf
[0] > 3) break; /* ignore wheel events */
1312 case 0: return MOUSE_LEFT
;
1313 case 1: return MOUSE_MIDDLE
;
1314 case 2: return MOUSE_RIGHT
;
1322 // shuffling and dealing {{{
1323 void deal(long seed
) {
1324 f
= (const struct playfield
){0}; /* clear playfield */
1325 card_t deck
[DECK_SIZE
*NUM_DECKS
];
1326 int avail
= DECK_SIZE
*NUM_DECKS
;
1327 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
1329 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
1330 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
1331 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
1332 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
1336 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
1337 int j
= rand() % (i
+1);
1338 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
1342 for (int i
= 0; i
< NUM_PILES
; i
++) {
1345 int count
= i
; /* pile n has n closed cards, then 1 open */
1346 #elif defined SPIDER
1348 int count
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1349 #elif defined FREECELL
1351 int count
= i
<4?6:5;/*like spider, but cards are dealt face-up*/
1353 /* "SIGN": face down cards are negated */
1354 for (int j
= 0; j
< count
; j
++) f
.t
[i
][j
] = SIGN deck
[--avail
];
1355 f
.t
[i
][count
] = deck
[--avail
]; /* the face-up card */
1358 /* rest of the cards to the stock: */
1359 /* NOTE: assert(avail==50) for spider, assert(avail==0) for freecell */
1360 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
1362 f
.w
= -1; /* @start: nothing on waste */
1363 #elif defined SPIDER
1364 f
.w
= 0; /* number of used foundations */
1365 #elif defined FREECELL
1366 f
.w
= 0; /* bitmask of used free cells */
1369 f
.u
= &undo_sentinel
;
1373 // screen drawing routines {{{
1374 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
1375 if (!op
.h
) invert
= 0; /* don't show invert if we used the mouse last */
1376 if (bold
&& op
.s
== &unicode_large_color
){ //awful hack for bold + faint
1377 int offset
= str
[3]==017?16:str
[4]==017?17:0;
1378 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1379 "\033[1m", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1380 offset
, str
, bold
?"\033[1m":"", str
+offset
,
1381 grey_bg
?"\033[49m":"", invert
?"\033[27m":"","\033[22m");
1384 printf ("%s%s%s%s%s%s%s",
1385 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1387 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
1389 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
1390 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1392 /* print stock, waste and foundation: */
1393 for (int line
= 0; line
< op
.s
->height
; line
++) {
1395 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
1396 (f
.w
< f
.z
-1)?op
.s
->facedown
1397 :op
.s
->placeholder
)[line
]);
1399 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
1400 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1401 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
1402 :op
.s
->placeholder
)[line
]);
1403 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
1405 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1406 int card
= find_top(f
.f
[pile
]);
1407 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1408 inactive
->pile
==FOUNDATION
&& (
1409 /* cursor addr. || direct addr. */
1410 inactive
->opt
==pile
|| inactive
->opt
< 0
1412 (card
< 0)?op
.s
->placeholder
[line
]
1413 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1418 #elif defined SPIDER
1419 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
1420 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
1421 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
1422 int spacer_to
= NUM_PILES
*op
.s
->width
-
1423 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
1424 for (int line
= 0; line
< op
.s
->height
; line
++) {
1425 /* available stock: */
1426 for (int i
= f
.z
/10; i
; i
--) {
1427 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
1428 else printf ("%s", op
.s
->halfstack
[line
]);
1431 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
1432 /* foundation (overlapping): */
1433 for (int i
= NUM_DECKS
*NUM_SUITS
-1, half
= 0; i
>= 0; i
--) {
1434 int overlap
= half
? op
.s
->halfcard
[line
]: 0;
1435 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
1436 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
),
1442 #elif defined FREECELL
1443 /* print open cells, foundation: */
1444 for (int line
= 0; line
< op
.s
->height
; line
++) {
1445 //FREECELL TODO: cells and foundation look the same! (different placeholder?)
1446 for (int pile
= 0; pile
< NUM_CELLS
; pile
++)
1447 print_hi (active
->pile
==STOCK
&& active
->opt
==pile
,
1448 inactive
->pile
==STOCK
&& (
1449 /* cursor addr. || direct addr. */
1450 inactive
->opt
==pile
|| inactive
->opt
< 0
1452 ((f
.s
[pile
])?op
.s
->card
[f
.s
[pile
]]
1453 :op
.s
->placeholder
)[line
]);
1454 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1455 int card
= find_top(f
.f
[pile
]);
1456 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1457 inactive
->pile
==FOUNDATION
&& (
1458 /* cursor addr. || direct addr. */
1459 inactive
->opt
==pile
|| inactive
->opt
< 0
1461 (card
< 0)?op
.s
->placeholder
[line
]
1462 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1469 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1470 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1471 #elif defined SPIDER || defined FREECELL
1472 int offset
[NUM_PILES
]={0};
1473 //TODO FREECELL: multi-card-moving constraint! for DO_HI() and TOP_HI()
1474 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1475 && offset[pile] >= cursor->opt)
1476 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1477 && offset[pile] == cursor->opt)
1479 /* print tableu piles: */
1480 int row
[NUM_PILES
] = {0};
1481 int line
[NUM_PILES
]= {0};
1482 int label
[NUM_PILES
]={0};
1484 int did_placeholders
= 0;
1487 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
1488 card_t card
= f
.t
[pile
][row
[pile
]];
1489 card_t next
= f
.t
[pile
][row
[pile
]+1];
1490 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
1491 int empty
= !card
&& row
[pile
] == 0;
1493 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
1494 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
1495 :(card
<0)?op
.s
->facedown
1499 int extreme_overlap
= ( 3 /* spacer, labels, status */
1500 + 2 * op
.s
->height
/* stock, top tableu card */
1501 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
1502 /* normal overlap: */
1503 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
1504 /* extreme overlap on closed cards: */
1505 || (extreme_overlap
&&
1507 f
.t
[pile
][row
[pile
]] < 0 &&
1508 f
.t
[pile
][row
[pile
]+1] <0)
1509 /* extreme overlap on sequences: */
1510 || (extreme_overlap
&&
1511 !TOP_HI(active
) && /*always show top selected card*/
1512 line
[pile
] >= 1 && row
[pile
] > 0 &&
1513 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
1514 is_consecutive (f
.t
[pile
], row
[pile
]) &&
1515 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
1516 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
1520 #if defined SPIDER || defined FREECELL
1521 if (movable
) offset
[pile
]++;
1524 /* tableu labels: */
1525 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
1527 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
1529 line_had_card
|= !!card
;
1530 did_placeholders
|= row
[pile
] > 0;
1533 } while (line_had_card
|| !did_placeholders
);
1536 void visbell (void) {
1538 printf ("\033[?5h"); fflush (stdout
);
1540 printf ("\033[?5l"); fflush (stdout
);
1542 void win_anim(void) {
1543 printf ("\033[?25l"); /* hide cursor */
1545 /* set cursor to random location */
1546 int row
= 1+rand()%(1+op
.w
[0]-op
.s
->height
);
1547 int col
= 1+rand()%(1+op
.w
[1]-op
.s
->width
);
1549 /* draw random card */
1550 int face
= 1 + rand() % 52;
1551 for (int l
= 0; l
< op
.s
->height
; l
++) {
1552 printf ("\033[%d;%dH", row
+l
, col
);
1553 printf ("%s", op
.s
->card
[face
][l
]);
1557 /* exit on keypress */
1558 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
1559 if (poll (&p
, 1, 80)) goto fin
;
1562 printf ("\033[?25h"); /* show cursor */
1568 void undo_push (int _f
, int t
, int n
, int o
) {
1569 struct undo
* new = malloc(sizeof(struct undo
));
1579 void undo_pop (struct undo
* u
) {
1580 if (u
== &undo_sentinel
) return;
1583 if (u
->f
== FOUNDATION
) {
1584 /* foundation -> tableu */
1585 int top_f
= find_top(f
.f
[u
->n
]);
1586 int top_t
= find_top(f
.t
[u
->t
]);
1587 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1588 f
.t
[u
->t
][top_t
] = NO_CARD
;
1589 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
1590 /* waste -> foundation */
1591 /* split u->n into wst and fnd: */
1592 int wst
= u
->n
& 0xffff;
1593 int fnd
= u
->n
>> 16;
1594 /* move stock cards one position up to make room: */
1595 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
1596 /* move one card from foundation to waste: */
1597 int top
= find_top(f
.f
[fnd
]);
1598 f
.s
[wst
] = f
.f
[fnd
][top
];
1599 f
.f
[fnd
][top
] = NO_CARD
;
1602 } else if (u
->f
== WASTE
) {
1603 /* waste -> tableu */
1604 /* move stock cards one position up to make room: */
1605 for (int i
= f
.z
-1; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
1606 /* move one card from tableu to waste: */
1607 int top
= find_top(f
.t
[u
->t
]);
1608 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
1609 f
.t
[u
->t
][top
] = NO_CARD
;
1612 } else if (u
->t
== FOUNDATION
) {
1613 /* tableu -> foundation */
1614 int top_f
= find_top(f
.t
[u
->f
]);
1615 int top_t
= find_top(f
.f
[u
->n
]);
1616 /* close topcard if previous action caused turn_over(): */
1617 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1618 /* move one card from foundation to tableu: */
1619 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1620 f
.f
[u
->n
][top_t
] = NO_CARD
;
1622 /* tableu -> tableu */
1623 int top_f
= find_top(f
.t
[u
->f
]);
1624 int top_t
= find_top(f
.t
[u
->t
]);
1625 /* close topcard if previous action caused turn_over(): */
1626 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1627 /* move n cards from tableu[f] to tableu[t]: */
1628 for (int i
= 0; i
< u
->n
; i
++) {
1629 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1630 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1633 #elif defined SPIDER
1634 if (u
->f
== STOCK
) {
1635 /* stock -> tableu */
1636 /*remove a card from each pile and put it back onto the stock:*/
1637 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
1638 int top
= find_top(f
.t
[pile
]);
1639 f
.s
[f
.z
++] = f
.t
[pile
][top
];
1640 f
.t
[pile
][top
] = NO_CARD
;
1642 } else if (u
->t
== FOUNDATION
) {
1643 /* tableu -> foundation */
1644 int top
= find_top(f
.t
[u
->f
]);
1645 /* close topcard if previous action caused turn_over(): */
1646 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1647 /* append cards from foundation to tableu */
1648 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1649 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1650 f
.f
[u
->n
][i
] = NO_CARD
;
1652 f
.w
--; /* decrement complete-foundation-counter */
1655 /* tableu -> tableu */
1656 int top_f
= find_top(f
.t
[u
->f
]);
1657 int top_t
= find_top(f
.t
[u
->t
]);
1658 /* close topcard if previous action caused turn_over(): */
1659 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1660 /* move n cards from tableu[f] to tableu[t]: */
1661 for (int i
= 0; i
< u
->n
; i
++) {
1662 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1663 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1666 #elif defined FREECELL
1667 /*NOTE: if from and to are both stock/foundation, opt = from | to<<16 */
1668 if (u
->f
== STOCK
&& u
->t
== FOUNDATION
) {
1669 /* free cells -> foundation */
1670 /* split u->n into cll and fnd: */
1671 int cll
= u
->n
& 0xffff;
1672 int fnd
= u
->n
>> 16;
1673 /* move one card from foundation to free cell: */
1674 int top
= find_top(f
.f
[fnd
]);
1675 f
.s
[cll
] = f
.f
[fnd
][top
];
1676 f
.f
[fnd
][top
] = NO_CARD
;
1677 f
.w
|= 1<<cll
; /* mark cell as occupied */
1678 } else if (u
->f
== STOCK
) {
1679 /* free cells -> cascade */
1680 int top_t
= find_top(f
.t
[u
->t
]);
1681 f
.s
[u
->n
] = f
.t
[u
->t
][top_t
];
1682 f
.t
[u
->t
][top_t
] = NO_CARD
;
1683 f
.w
|= 1<<u
->n
; /* mark cell as occupied */
1684 } else if (u
->f
== FOUNDATION
&& u
->t
== STOCK
) {
1685 /* foundation -> free cells */
1686 /* split u->n into cll and fnd: */
1687 int cll
= u
->n
>> 16;
1688 int fnd
= u
->n
& 0xffff;
1689 /* move 1 card from free cell to foundation: */
1690 int top_f
= find_top(f
.f
[fnd
]);
1691 f
.f
[fnd
][top_f
+1] = f
.s
[cll
];
1693 f
.w
&= ~(1<<cll
); /* mark cell as free */
1694 } else if (u
->f
== FOUNDATION
) {
1695 /* foundation -> cascade */
1696 int top_f
= find_top(f
.f
[u
->n
]);
1697 int top_t
= find_top(f
.t
[u
->t
]);
1698 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1699 f
.t
[u
->t
][top_t
] = NO_CARD
;
1700 } else if (u
->t
== STOCK
) {
1701 /* cascade -> free cells */
1702 int top_f
= find_top(f
.t
[u
->f
]);
1703 f
.t
[u
->f
][top_f
+1] = f
.s
[u
->n
];
1704 f
.s
[u
->n
] = NO_CARD
;
1705 f
.w
&= ~(1<<u
->n
); /* mark cell as free */
1706 } else if (u
->t
== FOUNDATION
) {
1707 /* cascade -> foundation */
1708 int top_f
= find_top(f
.t
[u
->f
]);
1709 int top_t
= find_top(f
.f
[u
->n
]);
1710 /* move one card from foundation to cascade: */
1711 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1712 f
.f
[u
->n
][top_t
] = NO_CARD
;
1714 /* cascade -> cascade */
1715 int top_f
= find_top(f
.t
[u
->f
]);
1716 int top_t
= find_top(f
.t
[u
->t
]);
1717 /* move n cards from tableu[f] to tableu[t]: */
1718 for (int i
= 0; i
< u
->n
; i
++) {
1719 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1720 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1729 void free_undo (struct undo
* u
) {
1730 while (u
&& u
!= &undo_sentinel
) {
1738 // initialization stuff {{{
1739 void screen_setup (int enable
) {
1742 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1743 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1744 printf ("\033[?1000h"); /* enable mouse */
1746 printf ("\033[?1000l"); /* disable mouse */
1747 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1752 void raw_mode(int enable
) {
1753 static struct termios saved_term_mode
;
1754 struct termios raw_term_mode
;
1757 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1758 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1759 raw_term_mode
= saved_term_mode
;
1760 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1761 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1762 raw_term_mode
.c_cc
[VTIME
] = 0;
1763 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1765 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1769 void signal_handler (int signum
) {
1774 signal(SIGTSTP
, SIG_DFL
); /* NOTE: assumes SysV semantics! */
1779 print_table(NO_HI
, NO_HI
);
1781 case SIGINT
: //TODO: don't exit; just warn like vim does
1784 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1790 void signal_setup(void) {
1791 struct sigaction saction
;
1793 saction
.sa_handler
= signal_handler
;
1794 sigemptyset(&saction
.sa_mask
);
1795 saction
.sa_flags
= 0;
1796 if (sigaction(SIGTSTP
, &saction
, NULL
) < 0) {
1800 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1804 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1808 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1809 perror ("SIGWINCH");
1815 //vim: foldmethod=marker