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
);
129 if (ret
== ERR
&& is_tableu(from
) && to
== from
)
130 /* t2f failed? try t2c! */
131 ret
= t2c(from
, STOCK
, 0);
134 if (ret
== ERR
&& is_tableu(from
) && is_tableu(to
))
135 /* try again with from/to swapped: */
136 ret
= action
[to
][from
](to
,from
,opt
);
139 case ERR
: visbell(); break;
140 case WON
: return GAME_WON
;
146 case ERR
: visbell(); break;
147 case WON
: return GAME_WON
;
150 case CMD_HINT
: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
151 case CMD_UNDO
: undo_pop(f
.u
); break;
152 case CMD_INVAL
: visbell(); break;
153 case CMD_NEW
: return GAME_NEW
;
154 case CMD_AGAIN
: goto restart
;
155 case CMD_QUIT
: return GAME_QUIT
;
157 printf (KEYHELP
"\nPress any key to continue.");
170 // card games helper functions {{{
171 #define get_suit(card) \
172 ((card-1) % NUM_SUITS)
173 #define get_rank(card) \
174 ((card-1) / NUM_SUITS)
175 #define get_color(card) \
176 ((get_suit(card) ^ get_suit(card)>>1) & 1)
178 int find_top(card_t
* pile
) {
180 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
183 int first_movable(card_t
* pile
) {
184 /* NOTE: in FREECELL this does not take max_move into account! */
186 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
189 int turn_over(card_t
* pile
) {
190 int top
= find_top(pile
);
196 int check_won(void) {
197 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
198 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
202 int rank_next (card_t a
, card_t b
) {
203 return get_rank(a
) == get_rank(b
)-1;
205 int is_consecutive (card_t
* pile
, int pos
) {
206 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
207 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
209 #if defined KLONDIKE || defined FREECELL
210 /* ranks consecutive? */
211 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
212 /* color opposite? */
213 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
215 /* ranks consecutive? */
216 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
218 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
224 int is_movable(card_t
* pile
, int n
) {
226 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
227 #elif defined SPIDER || defined FREECELL
228 int top
= find_top(pile
);
229 for (int i
= top
; i
>= 0; i
--) {
230 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
231 if (!is_consecutive(pile
, i
)) return 0;
232 if (i
== n
) return 1; /* card reached, must be movable */
239 // takeable actions {{{
241 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
242 card_t card
= f
.s
[f
.w
];
243 /* move stack one over, so there are no gaps in it: */
244 for (int i
= f
.w
; i
< f
.z
-1; i
++)
247 f
.w
--; /* make previous card visible again */
250 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
251 (void) to
; (void) opt
; /* don't need */
252 int top_from
= find_top(f
.t
[from
]);
253 to
= get_suit(f
.t
[from
][top_from
]);
254 int top_to
= find_top(f
.f
[to
]);
255 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
256 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
257 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
258 f
.t
[from
][top_from
] = NO_CARD
;
259 undo_push(from
, FOUNDATION
, to
,
260 turn_over(f
.t
[from
]));
261 if (check_won()) return WON
;
265 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
266 (void) from
; (void) to
; (void) opt
; /* don't need */
267 if (f
.w
< 0) return ERR
;
268 to
= get_suit(f
.s
[f
.w
]);
269 int top_to
= find_top(f
.f
[to
]);
270 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
271 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
272 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
273 f
.f
[to
][top_to
+1] = stack_take();
274 if (check_won()) return WON
;
279 int s2w(int from
, int to
, int opt
) { /* stock to waste */
280 (void) from
; (void) to
; (void) opt
; /* don't need */
281 if (f
.z
== 0) return ERR
;
283 if (f
.w
== f
.z
) f
.w
= -1;
286 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
287 (void) from
; (void) to
; (void) opt
; /* don't need */
288 if (f
.z
== 0) return ERR
;
290 if (f
.w
< -1) f
.w
= f
.z
-1;
293 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
294 (void) from
; /* don't need */
295 int top_to
= find_top(f
.t
[to
]);
297 int top_from
= find_top(f
.f
[from
]);
299 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
300 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
301 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
302 f
.f
[from
][top_from
] = NO_CARD
;
303 undo_push(FOUNDATION
, to
, from
, 0);
307 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
308 (void) from
; (void) opt
; /* don't need */
309 if (f
.w
< 0) return ERR
;
310 int top_to
= find_top(f
.t
[to
]);
311 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
312 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
313 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
314 undo_push(WASTE
, to
, f
.w
, 0);
315 f
.t
[to
][top_to
+1] = stack_take();
319 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
320 (void) opt
; /* don't need */
321 int top_to
= find_top(f
.t
[to
]);
322 int top_from
= find_top(f
.t
[from
]);
323 int count
= 0; //NOTE: could probably be factored out
324 for (int i
= top_from
; i
>=0; i
--) {
325 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
326 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
327 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
328 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
329 /* move cards [i..top_from] to their destination */
330 for (;i
<= top_from
; i
++) {
332 f
.t
[to
][top_to
] = f
.t
[from
][i
];
333 f
.t
[from
][i
] = NO_CARD
;
336 undo_push(from
, to
, count
,
337 turn_over(f
.t
[from
]));
341 return ERR
; /* no such move possible */
344 int remove_if_complete (int pileno
) { //cleanup!
345 card_t
* pile
= f
.t
[pileno
];
346 /* test if K...A complete; move to foundation if so */
347 int top_from
= find_top(pile
);
348 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
349 for (int i
= top_from
; i
>=0; i
--) {
350 if (!is_consecutive (pile
, i
)) return 0;
351 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
352 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
353 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
354 f
.f
[f
.w
][j
] = pile
[i
];
357 undo_push(pileno
, FOUNDATION
, f
.w
,
366 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
367 int top_from
= find_top(f
.t
[from
]);
368 int top_to
= find_top(f
.t
[to
]);
369 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
370 int count
= 0; //NOTE: could probably be factored out
372 for (int i
= top_from
; i
>= 0; i
--) {
373 if (!is_consecutive(f
.t
[from
], i
)) break;
375 /* is consecutive OR to empty pile and rank ok? */
376 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
377 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
378 for (;i
<= top_from
; i
++) {
380 f
.t
[to
][top_to
] = f
.t
[from
][i
];
381 f
.t
[from
][i
] = NO_CARD
;
384 undo_push(from
, to
, count
,
385 turn_over(f
.t
[from
]));
386 remove_if_complete(to
);
387 if (check_won()) return WON
;
392 return ERR
; /* no such move possible */
394 int s2t(int from
, int to
, int opt
) {
395 (void) from
; (void) to
; (void) opt
; /* don't need */
396 if (f
.z
<= 0) return ERR
; /* stack out of cards */
397 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
398 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
399 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
400 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
401 remove_if_complete(pile
);
402 if (check_won()) return WON
;
404 undo_push(STOCK
, TABLEU
, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
407 int t2f(int from
, int to
, int opt
) {
408 (void) to
; (void) opt
; /* don't need */
409 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
410 return remove_if_complete(from
)?OK
:ERR
;
412 #elif defined FREECELL
413 int max_move(int from
, int to
) {
414 /* returns the maximum number of cards that can be moved */
415 /* see also: https://boardgames.stackexchange.com/a/45157/26498 */
416 int free_tabs
= 0, free_cells
= 0;
417 for (int i
= 0; i
< NUM_PILES
; i
++) free_tabs
+= f
.t
[i
][0] == NO_CARD
;
418 for (int i
= 0; i
< NUM_CELLS
; i
++) free_cells
+= f
.s
[i
] == NO_CARD
;
420 /* don't count the tableau we are moving to: */
421 if (to
>= 0 && f
.t
[to
][0] == NO_CARD
) free_tabs
--;
423 /* theoretic maximum is limited by the number of cards on the pile */
424 int max_theory
= (1<<free_tabs
) * (free_cells
+ 1);
425 int max_effective
= 1 + find_top(f
.t
[from
]) - first_movable(f
.t
[from
]);
426 return max_effective
< max_theory
? max_effective
: max_theory
;
428 //TODO FREECELL: auto move to tableu after each move (not all cards possible, only when it is the smallest rank still on the board)
429 int t2t(int from
, int to
, int opt
) {
430 int top_to
= find_top(f
.t
[to
]);
431 int top_from
= find_top(f
.t
[from
]);
432 int count
= 0; //NOTE: could probably be factored out
433 int cards
= max_move(from
, to
);
434 if (top_to
< 0) { /* moving to empty pile? */
436 return ERR
; /* cannot execute move */
437 cards
= opt
; /* user wants to move n cards*/
440 for (int i
= top_from
; i
>=0; i
--) {
441 if (cards
-->0/*enough space and not more attempted than wanted*/
442 && ((top_to
>= 0 /* if destn. not empty: check rank/color */
443 && ((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
444 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))))
445 || (top_to
< 0 && !cards
))) {/*if dest empty and right # cards*/
446 /* move cards [i..top_from] to their destination */
447 for (;i
<= top_from
; i
++) {
449 f
.t
[to
][top_to
] = f
.t
[from
][i
];
450 f
.t
[from
][i
] = NO_CARD
;
453 undo_push(from
, to
, count
, 0);
457 return ERR
; /* no such move possible */
459 int t2f(int from
, int to
, int opt
) { /* 1:1 copy from KLONDIKE */
460 (void) to
; (void) opt
; /* don't need */
461 int top_from
= find_top(f
.t
[from
]);
462 to
= get_suit(f
.t
[from
][top_from
]);
463 int top_to
= find_top(f
.f
[to
]);
464 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
465 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
466 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
467 f
.t
[from
][top_from
] = NO_CARD
;
468 undo_push(from
, FOUNDATION
, to
, 0);
469 if (check_won()) return WON
;
473 int f2t(int from
, int to
, int opt
) {
474 (void) from
; /* don't need */
475 int top_to
= find_top(f
.t
[to
]);
477 int top_from
= find_top(f
.f
[from
]);
479 if (top_to
< 0 /* empty tableu? */
480 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
481 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
])))) {
482 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
483 f
.f
[from
][top_from
] = NO_CARD
;
484 undo_push(FOUNDATION
, to
, from
, 0);
488 int t2c(int from
, int to
, int opt
) {
489 (void) to
; (void) opt
; /* don't need */
490 /* is a cell free? */
491 if (f
.w
== (1<<NUM_CELLS
)-1)
493 for (to
= 0; to
< NUM_CELLS
; to
++)
494 if (!(f
.w
>>to
&1)) break;
496 int top_from
= find_top(f
.t
[from
]);
497 f
.s
[to
] = f
.t
[from
][top_from
];
498 f
.t
[from
][top_from
] = NO_CARD
;
499 f
.w
|= 1<<to
; /* mark cell as occupied */
500 undo_push(from
, STOCK
, to
, 0);
504 int c2t(int from
, int to
, int opt
) {
505 (void) from
; /* don't need */
506 int top_to
= find_top(f
.t
[to
]);
509 if (top_to
< 0 /* empty tableu? */
510 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[from
]))
511 && (rank_next(f
.s
[from
], f
.t
[to
][top_to
])))) {
512 f
.t
[to
][top_to
+1] = f
.s
[from
];
514 f
.w
&= ~(1<<from
); /* mark cell as free */
515 undo_push(STOCK
, to
, from
, 0);
520 int c2f(int from
, int to
, int opt
) {
521 (void) from
; (void) to
; /* don't need */
523 to
= get_suit(f
.s
[from
]);
524 int top_to
= find_top(f
.f
[to
]);
525 if ((top_to
< 0 && get_rank(f
.s
[from
]) == RANK_A
)
526 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.s
[from
]))) {
527 f
.f
[to
][top_to
+1] = f
.s
[from
];
529 f
.w
&= ~(1<<from
); /* mark cell as free */
530 undo_push(STOCK
, FOUNDATION
, from
| to
<<16, 0);
531 if (check_won()) return WON
;
535 int f2c(int from
, int to
, int opt
) {
536 (void) from
; (void) to
; /* don't need */
537 /* is a cell free? */
538 if (f
.w
== (1<<NUM_CELLS
)-1)
540 for (to
= 0; to
< NUM_CELLS
; to
++)
541 if (!(f
.w
>>to
&1)) break;
544 int top_from
= find_top(f
.f
[from
]);
545 f
.s
[to
] = f
.f
[from
][top_from
];
546 f
.f
[from
][top_from
] = NO_CARD
;
547 f
.w
|= 1<<to
; /* mark cell as occupied */
548 undo_push(FOUNDATION
, STOCK
, from
| to
<<16, 0);
554 //TODO: generalize prediction engine for CMD_HINT
556 #define would_complete(pile) 0
558 #define would_complete(pile) \
559 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
560 && get_rank(f.t[to][bottom_to]) == RANK_K)
561 #elif defined FREECELL
562 #define would_complete(pile) 0
564 #define would_turn(pile) \
565 (f.t[pile][r[pile].pos-1] < 0)
566 #define would_empty(pile) \
570 //TODO FREECELL: join to empty tableu (longest cascade?)
571 int top_to
= find_top(f
.t
[to
]);
573 int bottom_to
= first_movable(f
.t
[to
]);
577 if (to
== WASTE
|| to
== STOCK
) return ERR
; /*why would you do that!?*/
579 if (to
== FOUNDATION
) {
581 for (int i
= 0; i
<= TAB_MAX
; i
++)
582 switch ((i
?t2f
:w2f
)(i
-1, FOUNDATION
, 0)) {
583 case WON
: return WON
;
584 case OK
: status
= OK
;
590 if (top_to
< 0) { /* move a king to empty pile: */
591 for (int i
= 0; i
< TAB_MAX
; i
++) {
592 if (f
.t
[i
][0] < 0) /* i.e. would turn? */
593 if (t2t(i
, to
, 0) == OK
) return OK
;
595 return w2t(WASTE
, to
, 0);
600 int ok
:1; /* card to move in pile? */
601 int above
; /* number of movable cards above */
602 int below
; /* number of cards below ours */
603 int pos
; /* where the card to move is in the pile */
604 int top
; /* find_top() */
605 } r
[NUM_PILES
] = {{0}};
606 int complete
= 0;/* SPIDER: true if any pile would complete a stack */
607 int turn
= 0; /* SPIDER: true if any pile would turn_over */
608 int empty
= 0; /* true if any pile would become empty */
610 /* 1. rate each pile: */
613 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
614 if (pile
== to
) continue;
615 int top
= find_top(f
.t
[pile
]);
616 int bottom
= first_movable(f
.t
[pile
]);
617 r
[pile
].pos
= bottom
; /* need for would_empty */
619 if (top
< 0) continue; /* no cards to move */
620 if (would_empty(pile
)) continue; /* doesn't help */
623 r
[pile
].above
= 0; /* always take as many as possible */
624 r
[pile
].below
= top
- bottom
;
626 complete
|= would_complete(pile
); /* never happens */
627 turn
|= would_turn(pile
);
628 empty
|= would_empty(pile
);
632 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
633 r
[pile
].top
= r
[pile
].pos
= find_top(f
.t
[pile
]);
634 /* backtrack until we find a compatible-to-'to'-pile card: */
636 int maxmove
= max_move(pile
, -1);
638 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
639 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
640 - get_rank(f
.t
[to
][top_to
]);
641 if (rankdiff
>= 0) break; /* past our card */
643 if (!maxmove
--) break; /* can't move this many cards */
645 if (rankdiff
== -1 /* rank matches */
646 #if defined KLONDIKE || defined FREECELL
647 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
648 != get_color(f
.t
[to
][top_to
])
650 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
651 == get_suit(f
.t
[to
][top_to
])
655 complete
|= would_complete(pile
);
656 turn
|= would_turn(pile
);
657 empty
|= would_empty(pile
);
658 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
659 if (is_movable(f
.t
[pile
], i
-1))
669 /* 2. find optimal pile: (optimized for spider) */
670 //todo: in spider, prefer longest piles if above==0 (faster completions)
672 for (int pile
= 0, above
= 99, below
= 99; pile
< NUM_PILES
; pile
++) {
673 if (!r
[pile
].ok
) continue;
674 /* don't bother if another pile would be better: prefer ... */
675 /* ... to complete a stack: */
676 if (!would_complete(pile
) && complete
) continue;
677 /* ... emptying piles: */
678 if (!would_empty(pile
) && empty
&& !complete
) continue;
679 /* ... to turn_over: */
680 if (!would_turn(pile
) && turn
&& !complete
&& !empty
) continue;
681 /* ... not to rip apart too many cards: */
682 if (r
[pile
].above
> above
) continue;
683 /* if tied, prefer ... */
684 if (r
[pile
].above
== above
685 /* ... larger pile if destination is empty */
686 && (top_to
< 0? r
[pile
].below
< below
687 /* ... shorter pile otherwise */
688 : r
[pile
].below
> below
))
692 above
= r
[pile
].above
;
693 below
= r
[pile
].below
;
696 /* 3. move cards over and return: */
698 /* prefer waste if it wouldn't turn_over: */
699 /* NOTE: does not attempt to take from froundation */
700 if (!empty
&& !turn
&& w2t(WASTE
, to
, 0) == OK
)
702 if (from
< 0) /* nothing found */
704 return t2t(from
, to
, 0);
706 if (from
< 0) /* nothing found */
708 int bottom
= first_movable(f
.t
[from
]);
709 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
710 #elif defined FREECELL
711 if (from
< 0) /* no tableu move found */ {
712 /* try all free cells before giving up: */
713 for (int i
= 0; i
< NUM_CELLS
; i
++)
714 if (c2t(STOCK
, to
, i
) == OK
) return OK
;
717 return t2t(from
, to
, 0);
722 #undef would_complete
723 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
726 // keyboard input handling {{{
727 // cursor functions{{{
729 void cursor_left (struct cursor
* cursor
) {
731 if (is_tableu(cursor
->pile
)) {
732 if (cursor
->pile
> 0) cursor
->pile
--;
734 } else { /* stock/waste/foundation*/
735 switch (cursor
->pile
) {
736 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
738 if (cursor
->opt
<= 0)
739 cursor
->pile
= WASTE
;
745 void cursor_down (struct cursor
* cursor
) {
747 if (!is_tableu(cursor
->pile
)) {
748 switch (cursor
->pile
) {
749 case STOCK
: cursor
->pile
= TAB_1
; break;
750 case WASTE
: cursor
->pile
= TAB_2
; break;
752 cursor
->pile
= TAB_4
+ cursor
->opt
;
757 void cursor_up (struct cursor
* cursor
) {
759 if (is_tableu(cursor
->pile
)) {
760 switch (cursor
->pile
) { //ugly :|
761 case TAB_1
: cursor
->pile
= STOCK
; break;
762 case TAB_2
: cursor
->pile
= WASTE
; break;
763 case TAB_3
: cursor
->pile
= WASTE
; break;
764 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
765 cursor
->opt
=cursor
->pile
-TAB_4
;
766 cursor
->pile
= FOUNDATION
;
771 void cursor_right (struct cursor
* cursor
) {
773 if (is_tableu(cursor
->pile
)) {
774 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
777 switch (cursor
->pile
) {
778 case STOCK
: cursor
->pile
= WASTE
; break;
779 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
781 if (cursor
->opt
< NUM_SUITS
-1)
787 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
788 void cursor_left (struct cursor
* cursor
) {
790 if (cursor
->pile
> 0) cursor
->pile
--;
793 void cursor_down (struct cursor
* cursor
) {
795 int first
= first_movable(f
.t
[cursor
->pile
]);
796 int top
= find_top(f
.t
[cursor
->pile
]);
797 if (first
+ cursor
->opt
< top
)
800 void cursor_up (struct cursor
* cursor
) {
802 if (cursor
->opt
> 0) cursor
->opt
--;
804 void cursor_right (struct cursor
* cursor
) {
806 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
809 #elif defined FREECELL
810 void cursor_left (struct cursor
* cursor
) {
812 if (is_tableu(cursor
->pile
)) {
813 if (cursor
->pile
> 0) cursor
->pile
--;
815 } else { /* cells/foundation*/
816 switch (cursor
->pile
) {
822 if (cursor
->opt
<= 0) {
823 cursor
->pile
= STOCK
;
831 void cursor_down (struct cursor
* cursor
) {
833 if (is_tableu(cursor
->pile
)) {
834 if (cursor
->opt
< max_move(cursor
->pile
, -1)-1)
837 cursor
->pile
= cursor
->opt
+NUM_CELLS
*(cursor
->pile
==FOUNDATION
);
841 void cursor_up (struct cursor
* cursor
) {
843 if (is_tableu(cursor
->pile
)) {
844 if (cursor
->opt
> 0) {
847 switch (cursor
->pile
) {
848 case TAB_1
: case TAB_2
: case TAB_3
: case TAB_4
:
849 cursor
->opt
= cursor
->pile
; /*assumes TAB_1==0*/
850 cursor
->pile
= STOCK
;
852 case TAB_5
: case TAB_6
: case TAB_7
: case TAB_8
:
853 cursor
->opt
= cursor
->pile
- NUM_CELLS
;
854 cursor
->pile
= FOUNDATION
;
859 void cursor_right (struct cursor
* cursor
) {
861 if (is_tableu(cursor
->pile
)) {
862 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
865 switch (cursor
->pile
) {
867 if (cursor
->opt
< NUM_SUITS
-1) {
870 cursor
->pile
= FOUNDATION
;
874 if (cursor
->opt
< NUM_SUITS
-1)
880 void cursor_to (struct cursor
* cursor
, int pile
) {
885 int set_mouse(int pile
, int* main
, int* opt
) {
886 //TODO: this should set cursor.opt, so card selector choice dialog does not trigger!
888 if (pile
< 0) return 1;
891 if (pile
>= FOUNDATION
)//TODO: check upper bound!
893 *opt
= pile
- FOUNDATION
;
896 #elif defined FREECELL
897 if (pile
> TAB_MAX
) {
898 *main
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
899 *opt
= (pile
-STOCK
) % 4;
905 int get_cmd (int* from
, int* to
, int* opt
) {
907 unsigned char mouse
[6] = {0}; /* must clear [3]! */
908 struct cursor inactive
= {-1,-1};
909 static struct cursor active
= {0,0};
910 if (is_tableu(active
.pile
))
914 from_l
: print_table(&active
, &inactive
);
918 /* direct addressing: */
919 case '1': *from
= TAB_1
; break;
920 case '2': *from
= TAB_2
; break;
921 case '3': *from
= TAB_3
; break;
922 case '4': *from
= TAB_4
; break;
923 case '5': *from
= TAB_5
; break;
924 case '6': *from
= TAB_6
; break;
925 case '7': *from
= TAB_7
; break;
927 case '8': *from
= TAB_8
; break;
928 case '9': *from
= TAB_9
; break;
929 case '0': *from
= TAB_10
;break;
930 #elif defined FREECELL
931 case '8': *from
= TAB_8
; break;
932 case '9': *from
= STOCK
; break;
933 case '0': *from
= FOUNDATION
; break;
934 #elif defined KLONDIKE
935 case '9': *from
= WASTE
; break;
936 case '0': *from
= FOUNDATION
; break;
937 case '8': /* fallthrough */
940 case '\n': *from
= STOCK
; break;
942 /* cursor keys addressing: */
944 case 'h': cursor_left (&active
); goto from_l
;
946 case 'j': cursor_down (&active
); goto from_l
;
948 case 'k': cursor_up (&active
); goto from_l
;
950 case 'l': cursor_right(&active
); goto from_l
;
952 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
954 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
956 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
957 case ' ': /* continue with second cursor */
960 *opt
= active
.opt
; /* when FOUNDATION */
964 /* mouse addressing: */
965 case MOUSE_MIDDLE
: return CMD_NONE
;
967 if (set_mouse(term2pile(mouse
), to
, opt
))
971 if (set_mouse(term2pile(mouse
), from
, opt
))
973 if (!is_tableu(*from
))
974 inactive
.opt
= *opt
; /* prevents card selector dialog */
979 fprintf (stderr
, ":");
980 raw_mode(0); /* turn on echo */
981 fgets (buf
, 256, stdin
);
984 case 'q': return CMD_QUIT
;
985 case 'n': return CMD_NEW
;
986 case 'r': return CMD_AGAIN
;
987 case 'h': return CMD_HELP
;
988 default: return CMD_INVAL
;
994 if (*to
== FOUNDATION
) return CMD_JOIN
;
996 if (*to
> TAB_MAX
) return CMD_INVAL
;
998 case 'K': /* fallthrough */
999 case '?': return CMD_HINT
;
1000 case 'u': return CMD_UNDO
;
1001 case 002: return CMD_NONE
; /* sent by SIGWINCH */
1002 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
1003 default: return CMD_INVAL
;
1005 inactive
.pile
= *from
; /* for direct addressing highlighting */
1006 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
1009 if (*from
== STOCK
) {
1016 to_l
: print_table(&active
, &inactive
);
1021 case 'h': cursor_left (&active
); goto to_l
;
1023 case 'j': cursor_down (&active
); goto to_l
;
1025 case 'k': cursor_up (&active
); goto to_l
;
1027 case 'l': cursor_right(&active
); goto to_l
;
1029 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
1031 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
1033 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
1034 case 'J': /* fallthrough; just join selected pile */
1037 break; /* continues with the foundation/empty tableu check */
1039 case MOUSE_RIGHT
: return CMD_NONE
;
1041 if (set_mouse(term2pile(mouse
), to
, opt
))
1044 //TODO: set opt if to field is empty; suppress "up do" dialog from below
1045 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1046 int top = find_top(f.t[*from]);
1047 if (top < 0) return CMD_INVAL;
1048 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
1049 *opt = get_rank(f.t[*from][top]);
1056 case 'K': /* fallthrough */
1057 case '?': return CMD_HINT
;
1058 case 'u': return CMD_NONE
; /* cancel selection */
1059 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
1061 if (t
< '0' || t
> '9') return CMD_INVAL
;
1065 #elif defined SPIDER
1067 #elif defined FREECELL
1078 if (*from
== FOUNDATION
) {
1079 if (inactive
.opt
>= 0) {
1080 *opt
= inactive
.opt
;
1083 int top
= find_top(f
.t
[*to
]);
1084 if (top
< 0) return CMD_INVAL
;
1085 int color
= get_color(f
.t
[*to
][top
]);
1086 int choice_1
= 1-color
; /* selects piles of */
1087 int choice_2
= 2+color
; /* the opposite color */
1088 int top_c1
= find_top(f
.f
[choice_1
]);
1089 int top_c2
= find_top(f
.f
[choice_2
]);
1091 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
1092 && top_c1
>= 0 ) << 0
1093 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
1094 && top_c2
>= 0 ) << 1) {
1095 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
1096 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
1097 case (1<<1 | 1<<0): /* both, ask user which to pick from */
1098 printf ("take from (1-4): "); fflush (stdout
);
1099 *opt
= getch(NULL
) - '1';
1100 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1102 default: return CMD_INVAL
; /* none matched */
1104 /* `opt` is the foundation index (0..3) */
1106 #elif defined SPIDER
1107 /* moving to empty tableu? */
1108 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1109 int bottom
= first_movable(f
.t
[*from
]);
1110 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
1111 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
1114 int top
= find_top(f
.t
[*from
]);
1115 if (top
< 0) return CMD_INVAL
;
1116 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
1117 *opt
= get_rank(f
.t
[*from
][top
]);
1118 } else { /* only ask the user if it's unclear: */
1119 printf ("\rup to ([a23456789xjqk] or space/return): ");
1122 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
1123 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
1124 case 'a': case 'A': *opt
= RANK_A
; break;
1125 case '0': /* fallthrough */
1126 case 'x': case 'X': *opt
= RANK_X
; break;
1127 case 'j': case 'J': *opt
= RANK_J
; break;
1128 case 'q': case 'Q': *opt
= RANK_Q
; break;
1129 case 'k': case 'K': *opt
= RANK_K
; break;
1130 default: *opt
-= '1';
1132 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
1134 /* `opt` is the rank of the highest card to move */
1136 #elif defined FREECELL
1137 //TODO FREECELL: card selector choice dialog
1139 /* if it was selected with a cursor, it's obvious: */
1140 if (inactive
.opt
>= 0) {
1141 if (is_tableu(*from
)) {
1142 /* NOTE: max_move same as in cursor_down() */
1143 *opt
= max_move(*from
, -1) - inactive
.opt
;
1145 *opt
= inactive
.opt
;
1147 /* moving from tableu to empty tableu? */
1148 } else if(is_tableu(*from
) && is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
){
1149 // how many cards? (NOTE: spider asks "up to rank?"; do this then convert to number of cards?
1150 printf ("take how many (1-9): "); fflush (stdout
);
1151 *opt
= getch(NULL
) - '0';
1152 if (*opt
< 1 || *opt
> 9) return CMD_INVAL
;
1153 /* moving between stock/foundation? */
1154 } else if (*from
== FOUNDATION
&& *to
== STOCK
) {
1155 //can take from all non-empty foundations
1156 if (f
.w
== (1<<NUM_CELLS
)-1) return CMD_INVAL
; /*no free cells*/
1157 int ok_foundation
; /* find compatible foundations: */
1158 int used_fs
=0; for (int i
= 0; i
< NUM_SUITS
; i
++)
1159 if (!!f
.f
[i
][0]) ok_foundation
= i
, used_fs
++;
1161 if (used_fs
== 0) return CMD_INVAL
; /* nowhere to take from */
1162 if (used_fs
== 1) { /* take from the only one */
1163 return *opt
= ok_foundation
, CMD_MOVE
;
1164 } else { /* ask user */
1165 printf ("take from (1-4): "); fflush (stdout
);
1166 *opt
= getch(NULL
) - '1';
1167 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1169 /* `opt` is the foundation index (0..3) */
1170 } else if (*from
== STOCK
&& *to
== FOUNDATION
) {
1171 //check all non-empty cells
1172 if (!f
.w
) return CMD_INVAL
; /* no cell to take from */
1173 int ok_cell
; /* find compatible cells: */
1174 int used_cs
=0; for (int i
= 0; i
< NUM_CELLS
; i
++) {
1175 int to
= get_suit(f
.s
[i
]);
1176 int top_to
= find_top(f
.f
[to
]);
1177 if ((top_to
<0 && get_rank(f
.s
[i
]) == RANK_A
)
1178 || (rank_next(f
.f
[to
][top_to
], f
.s
[i
])))
1179 ok_cell
= i
, used_cs
++;
1182 if (used_cs
== 0) return CMD_INVAL
; /* nowhere to take from */
1183 if (used_cs
== 1) { /* take from the only one */
1184 return *opt
= ok_cell
, CMD_MOVE
;
1185 } else { /* ask user */
1186 printf ("take from (1-4): "); fflush (stdout
);
1187 *opt
= getch(NULL
) - '1';
1188 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1190 /* `opt` is the cell index (0..3) */
1191 } else if (*from
== FOUNDATION
|| *from
== STOCK
) { /* -> tableu */
1192 //foundation: 2 choices
1194 printf ("take from (1-4): "); fflush (stdout
);
1195 *opt
= getch(NULL
) - '1';
1196 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1202 int getctrlseq(unsigned char* buf
) {
1210 int offset
= 0x20; /* never sends control chars as data */
1211 while ((c
= getchar()) != EOF
) {
1215 case '\033': state
=ESC_SENT
; break;
1221 case '[': state
=CSI_SENT
; break;
1222 default: return KEY_INVAL
;
1227 case 'A': return KEY_UP
;
1228 case 'B': return KEY_DOWN
;
1229 case 'C': return KEY_RIGHT
;
1230 case 'D': return KEY_LEFT
;
1231 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
1232 case 'H': return KEY_HOME
;
1233 case 'F': return KEY_END
;
1234 case '2': getchar(); return KEY_INS
;
1235 case '5': getchar(); return KEY_PGUP
;
1236 case '6': getchar(); return KEY_PGDN
;
1237 case 'M': state
=MOUSE_EVENT
; break;
1238 default: return KEY_INVAL
;
1242 if (buf
== NULL
) return KEY_INVAL
;
1243 buf
[0] = c
- offset
;
1244 buf
[1] = getchar() - offset
;
1245 buf
[2] = getchar() - offset
;
1253 int term2pile(unsigned char *mouse
) {
1254 int line
= (mouse
[2]-1);
1255 int column
= (mouse
[1]-1) / op
.s
->width
;
1257 if (line
< op
.s
->height
) { /* first line */
1260 case 0: return STOCK
;
1261 case 1: return WASTE
;
1262 case 2: return -1; /* spacer */
1263 case 3: return FOUNDATION
+0;
1264 case 4: return FOUNDATION
+1;
1265 case 5: return FOUNDATION
+2;
1266 case 6: return FOUNDATION
+3;
1268 #elif defined SPIDER
1269 if (column
< 3) return STOCK
;
1271 #elif defined FREECELL
1272 if (column
< NUM_SUITS
+ NUM_CELLS
) return STOCK
+column
;
1275 } else if (line
> op
.s
->height
) { /* tableu */
1276 if (column
<= TAB_MAX
) return column
;
1280 int wait_mouse_up(unsigned char* mouse
) {
1281 //TODO: mouse drag: start gets inactive, hovering gets active cursors
1282 struct cursor cur
= {-1,-1};
1284 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
1286 /* display a cursor while mouse button is pushed: */
1287 int pile
= term2pile(mouse
);
1290 if (pile
>= FOUNDATION
) {
1291 cur
.pile
= FOUNDATION
;
1292 cur
.opt
= pile
-FOUNDATION
;
1294 #elif defined FREECELL
1295 if (pile
> TAB_MAX
) {
1296 cur
.pile
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
1297 cur
.opt
= (pile
-STOCK
) % 4;
1300 /* need to temporarily show the cursor, then revert to last state: */
1301 int old_show_cursor_hi
= op
.h
; //TODO: ARGH! that's awful!
1303 print_table(&cur
, NO_HI
); //TODO: should not overwrite inactive cursor!
1304 op
.h
= old_show_cursor_hi
;
1307 if (getctrlseq (mouse
+3) == MOUSE_ANY
) {
1308 /* ignore mouse wheel events: */
1309 if (mouse
[3] & 0x40) continue;
1311 else if((mouse
[3]&3) == 3) level
--; /* release event */
1312 else level
++; /* another button pressed */
1316 int success
= mouse
[1] == mouse
[4] && mouse
[2] == mouse
[5];
1323 int getch(unsigned char* buf
) {
1324 //TODO: if buf==NULL disable mouse input
1325 /* returns a character, EOF, or constant for an escape/control sequence - NOT
1326 compatible with the ncurses implementation of same name */
1328 if (buf
&& buf
[3]) {
1329 /* mouse was dragged; return 'ungetted' previous destination */
1330 action
= MOUSE_DRAG
;
1331 /* keep original [0], as [3] only contains release event */
1336 action
= getctrlseq(buf
);
1341 if (buf
[0] > 3) break; /* ignore wheel events */
1346 case 0: return MOUSE_LEFT
;
1347 case 1: return MOUSE_MIDDLE
;
1348 case 2: return MOUSE_RIGHT
;
1356 // shuffling and dealing {{{
1357 void deal(long seed
) {
1358 f
= (const struct playfield
){0}; /* clear playfield */
1359 card_t deck
[DECK_SIZE
*NUM_DECKS
];
1360 int avail
= DECK_SIZE
*NUM_DECKS
;
1361 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
1363 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
1364 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
1365 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
1366 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
1370 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
1371 int j
= rand() % (i
+1);
1372 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
1376 for (int i
= 0; i
< NUM_PILES
; i
++) {
1379 int count
= i
; /* pile n has n closed cards, then 1 open */
1380 #elif defined SPIDER
1382 int count
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1383 #elif defined FREECELL
1385 int count
= i
<4?6:5;/*like spider, but cards are dealt face-up*/
1387 /* "SIGN": face down cards are negated */
1388 for (int j
= 0; j
< count
; j
++) f
.t
[i
][j
] = SIGN deck
[--avail
];
1389 f
.t
[i
][count
] = deck
[--avail
]; /* the face-up card */
1392 /* rest of the cards to the stock: */
1393 /* NOTE: assert(avail==50) for spider, assert(avail==0) for freecell */
1394 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
1396 f
.w
= -1; /* @start: nothing on waste */
1397 #elif defined SPIDER
1398 f
.w
= 0; /* number of used foundations */
1399 #elif defined FREECELL
1400 f
.w
= 0; /* bitmask of used free cells */
1403 f
.u
= &undo_sentinel
;
1407 // screen drawing routines {{{
1408 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
1409 if (!op
.h
) invert
= 0; /* don't show invert if we used the mouse last */
1410 if (bold
&& op
.s
== &unicode_large_color
){ //awful hack for bold + faint
1411 int offset
= str
[3]==017?16:str
[4]==017?17:0;
1412 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1413 "\033[1m", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1414 offset
, str
, bold
?"\033[1m":"", str
+offset
,
1415 grey_bg
?"\033[49m":"", invert
?"\033[27m":"","\033[22m");
1418 printf ("%s%s%s%s%s%s%s",
1419 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1421 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
1423 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
1424 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1426 /* print stock, waste and foundation: */
1427 for (int line
= 0; line
< op
.s
->height
; line
++) {
1429 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
1430 (f
.w
< f
.z
-1)?op
.s
->facedown
1431 :op
.s
->placeholder
)[line
]);
1433 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
1434 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1435 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
1436 :op
.s
->placeholder
)[line
]);
1437 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
1439 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1440 int card
= find_top(f
.f
[pile
]);
1441 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1442 inactive
->pile
==FOUNDATION
&& (
1443 /* cursor addr. || direct addr. */
1444 inactive
->opt
==pile
|| inactive
->opt
< 0
1446 (card
< 0)?op
.s
->foundation
[line
]
1447 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1452 #elif defined SPIDER
1453 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
1454 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
1455 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
1456 int spacer_to
= NUM_PILES
*op
.s
->width
-
1457 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
1458 for (int line
= 0; line
< op
.s
->height
; line
++) {
1459 /* available stock: */
1460 for (int i
= f
.z
/10; i
; i
--) {
1461 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
1462 else printf ("%s", op
.s
->halfstack
[line
]);
1465 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
1466 /* foundation (overlapping): */
1467 for (int i
= NUM_DECKS
*NUM_SUITS
-1, half
= 0; i
>= 0; i
--) {
1468 int overlap
= half
? op
.s
->halfcard
[line
]: 0;
1469 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
1470 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
),
1476 #elif defined FREECELL
1477 /* print open cells, foundation: */
1478 for (int line
= 0; line
< op
.s
->height
; line
++) {
1479 for (int pile
= 0; pile
< NUM_CELLS
; pile
++)
1480 print_hi (active
->pile
==STOCK
&& active
->opt
==pile
,
1481 inactive
->pile
==STOCK
&& (
1482 /* cursor addr. || direct addr. */
1483 inactive
->opt
==pile
|| inactive
->opt
< 0
1485 ((f
.s
[pile
])?op
.s
->card
[f
.s
[pile
]]
1486 :op
.s
->placeholder
)[line
]);
1487 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1488 int card
= find_top(f
.f
[pile
]);
1489 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1490 inactive
->pile
==FOUNDATION
&& (
1491 /* cursor addr. || direct addr. */
1492 inactive
->opt
==pile
|| inactive
->opt
< 0
1494 (card
< 0)?op
.s
->foundation
[line
]
1495 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1502 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1503 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1504 #elif defined SPIDER || defined FREECELL
1505 int offset
[NUM_PILES
]={0}; /* first card to highlight */
1507 int bottom
[NUM_PILES
]; /* first movable card */
1508 for (int i
=0; i
<NUM_PILES
; i
++)
1509 bottom
[i
] = find_top(f
.t
[i
]) - max_move(i
,-1);
1511 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1512 && offset[pile] >= cursor->opt)
1513 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1514 && offset[pile] == cursor->opt)
1516 /* print tableu piles: */
1517 int row
[NUM_PILES
] = {0};
1518 int line
[NUM_PILES
]= {0};
1519 int label
[NUM_PILES
]={0};
1521 int did_placeholders
= 0;
1524 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
1525 card_t card
= f
.t
[pile
][row
[pile
]];
1526 card_t next
= f
.t
[pile
][row
[pile
]+1];
1527 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
1529 if(row
[pile
] <= bottom
[pile
]) movable
= 0;
1531 int empty
= !card
&& row
[pile
] == 0;
1533 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
1534 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
1535 :(card
<0)?op
.s
->facedown
1539 int extreme_overlap
= ( 3 /* spacer, labels, status */
1540 + 2 * op
.s
->height
/* stock, top tableu card */
1541 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
1542 /* normal overlap: */
1543 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
1544 /* extreme overlap on closed cards: */
1545 || (extreme_overlap
&&
1547 f
.t
[pile
][row
[pile
]] < 0 &&
1548 f
.t
[pile
][row
[pile
]+1] <0)
1549 /* extreme overlap on sequences: */
1550 || (extreme_overlap
&&
1551 !TOP_HI(active
) && /*always show top selected card*/
1552 line
[pile
] >= 1 && row
[pile
] > 0 &&
1553 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
1554 is_consecutive (f
.t
[pile
], row
[pile
]) &&
1555 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
1556 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
1560 #if defined SPIDER || defined FREECELL
1561 if (movable
) offset
[pile
]++;
1564 /* tableu labels: */
1565 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
1567 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
1569 line_had_card
|= !!card
;
1570 did_placeholders
|= row
[pile
] > 0;
1573 } while (line_had_card
|| !did_placeholders
);
1576 void visbell (void) {
1578 printf ("\033[?5h"); fflush (stdout
);
1580 printf ("\033[?5l"); fflush (stdout
);
1582 void win_anim(void) {
1583 printf ("\033[?25l"); /* hide cursor */
1585 /* set cursor to random location */
1586 int row
= 1+rand()%(1+op
.w
[0]-op
.s
->height
);
1587 int col
= 1+rand()%(1+op
.w
[1]-op
.s
->width
);
1589 /* draw random card */
1590 int face
= 1 + rand() % 52;
1591 for (int l
= 0; l
< op
.s
->height
; l
++) {
1592 printf ("\033[%d;%dH", row
+l
, col
);
1593 printf ("%s", op
.s
->card
[face
][l
]);
1597 /* exit on keypress */
1598 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
1599 if (poll (&p
, 1, 80)) goto fin
;
1602 printf ("\033[?25h"); /* show cursor */
1608 void undo_push (int _f
, int t
, int n
, int o
) {
1609 struct undo
* new = malloc(sizeof(struct undo
));
1619 void undo_pop (struct undo
* u
) {
1620 if (u
== &undo_sentinel
) return;
1623 if (u
->f
== FOUNDATION
) {
1624 /* foundation -> tableu */
1625 int top_f
= find_top(f
.f
[u
->n
]);
1626 int top_t
= find_top(f
.t
[u
->t
]);
1627 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1628 f
.t
[u
->t
][top_t
] = NO_CARD
;
1629 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
1630 /* waste -> foundation */
1631 /* split u->n into wst and fnd: */
1632 int wst
= u
->n
& 0xffff;
1633 int fnd
= u
->n
>> 16;
1634 /* move stock cards one position up to make room: */
1635 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
1636 /* move one card from foundation to waste: */
1637 int top
= find_top(f
.f
[fnd
]);
1638 f
.s
[wst
] = f
.f
[fnd
][top
];
1639 f
.f
[fnd
][top
] = NO_CARD
;
1642 } else if (u
->f
== WASTE
) {
1643 /* waste -> tableu */
1644 /* move stock cards one position up to make room: */
1645 for (int i
= f
.z
-1; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
1646 /* move one card from tableu to waste: */
1647 int top
= find_top(f
.t
[u
->t
]);
1648 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
1649 f
.t
[u
->t
][top
] = NO_CARD
;
1652 } else if (u
->t
== FOUNDATION
) {
1653 /* tableu -> foundation */
1654 int top_f
= find_top(f
.t
[u
->f
]);
1655 int top_t
= find_top(f
.f
[u
->n
]);
1656 /* close topcard if previous action caused turn_over(): */
1657 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1658 /* move one card from foundation to tableu: */
1659 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1660 f
.f
[u
->n
][top_t
] = NO_CARD
;
1662 /* tableu -> tableu */
1663 int top_f
= find_top(f
.t
[u
->f
]);
1664 int top_t
= find_top(f
.t
[u
->t
]);
1665 /* close topcard if previous action caused turn_over(): */
1666 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1667 /* move n cards from tableu[f] to tableu[t]: */
1668 for (int i
= 0; i
< u
->n
; i
++) {
1669 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1670 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1673 #elif defined SPIDER
1674 if (u
->f
== STOCK
) {
1675 /* stock -> tableu */
1676 /*remove a card from each pile and put it back onto the stock:*/
1677 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
1678 int top
= find_top(f
.t
[pile
]);
1679 f
.s
[f
.z
++] = f
.t
[pile
][top
];
1680 f
.t
[pile
][top
] = NO_CARD
;
1682 } else if (u
->t
== FOUNDATION
) {
1683 /* tableu -> foundation */
1684 int top
= find_top(f
.t
[u
->f
]);
1685 /* close topcard if previous action caused turn_over(): */
1686 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1687 /* append cards from foundation to tableu */
1688 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1689 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1690 f
.f
[u
->n
][i
] = NO_CARD
;
1692 f
.w
--; /* decrement complete-foundation-counter */
1695 /* tableu -> tableu */
1696 int top_f
= find_top(f
.t
[u
->f
]);
1697 int top_t
= find_top(f
.t
[u
->t
]);
1698 /* close topcard if previous action caused turn_over(): */
1699 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1700 /* move n cards from tableu[f] to tableu[t]: */
1701 for (int i
= 0; i
< u
->n
; i
++) {
1702 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1703 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1706 #elif defined FREECELL
1707 /*NOTE: if from and to are both stock/foundation, opt = from | to<<16 */
1708 if (u
->f
== STOCK
&& u
->t
== FOUNDATION
) {
1709 /* free cells -> foundation */
1710 /* split u->n into cll and fnd: */
1711 int cll
= u
->n
& 0xffff;
1712 int fnd
= u
->n
>> 16;
1713 /* move one card from foundation to free cell: */
1714 int top
= find_top(f
.f
[fnd
]);
1715 f
.s
[cll
] = f
.f
[fnd
][top
];
1716 f
.f
[fnd
][top
] = NO_CARD
;
1717 f
.w
|= 1<<cll
; /* mark cell as occupied */
1718 } else if (u
->f
== STOCK
) {
1719 /* free cells -> cascade */
1720 int top_t
= find_top(f
.t
[u
->t
]);
1721 f
.s
[u
->n
] = f
.t
[u
->t
][top_t
];
1722 f
.t
[u
->t
][top_t
] = NO_CARD
;
1723 f
.w
|= 1<<u
->n
; /* mark cell as occupied */
1724 } else if (u
->f
== FOUNDATION
&& u
->t
== STOCK
) {
1725 /* foundation -> free cells */
1726 /* split u->n into cll and fnd: */
1727 int cll
= u
->n
>> 16;
1728 int fnd
= u
->n
& 0xffff;
1729 /* move 1 card from free cell to foundation: */
1730 int top_f
= find_top(f
.f
[fnd
]);
1731 f
.f
[fnd
][top_f
+1] = f
.s
[cll
];
1733 f
.w
&= ~(1<<cll
); /* mark cell as free */
1734 } else if (u
->f
== FOUNDATION
) {
1735 /* foundation -> cascade */
1736 int top_f
= find_top(f
.f
[u
->n
]);
1737 int top_t
= find_top(f
.t
[u
->t
]);
1738 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1739 f
.t
[u
->t
][top_t
] = NO_CARD
;
1740 } else if (u
->t
== STOCK
) {
1741 /* cascade -> free cells */
1742 int top_f
= find_top(f
.t
[u
->f
]);
1743 f
.t
[u
->f
][top_f
+1] = f
.s
[u
->n
];
1744 f
.s
[u
->n
] = NO_CARD
;
1745 f
.w
&= ~(1<<u
->n
); /* mark cell as free */
1746 } else if (u
->t
== FOUNDATION
) {
1747 /* cascade -> foundation */
1748 int top_f
= find_top(f
.t
[u
->f
]);
1749 int top_t
= find_top(f
.f
[u
->n
]);
1750 /* move one card from foundation to cascade: */
1751 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1752 f
.f
[u
->n
][top_t
] = NO_CARD
;
1754 /* cascade -> cascade */
1755 int top_f
= find_top(f
.t
[u
->f
]);
1756 int top_t
= find_top(f
.t
[u
->t
]);
1757 /* move n cards from tableu[f] to tableu[t]: */
1758 for (int i
= 0; i
< u
->n
; i
++) {
1759 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1760 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1769 void free_undo (struct undo
* u
) {
1770 while (u
&& u
!= &undo_sentinel
) {
1778 // initialization stuff {{{
1779 void screen_setup (int enable
) {
1782 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1783 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1784 printf ("\033[?1000h"); /* enable mouse */
1786 printf ("\033[?1000l"); /* disable mouse */
1787 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1792 void raw_mode(int enable
) {
1793 static struct termios saved_term_mode
;
1794 struct termios raw_term_mode
;
1797 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1798 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1799 raw_term_mode
= saved_term_mode
;
1800 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1801 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1802 raw_term_mode
.c_cc
[VTIME
] = 0;
1803 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1805 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1809 void signal_handler (int signum
) {
1814 signal(SIGTSTP
, SIG_DFL
); /* NOTE: assumes SysV semantics! */
1819 print_table(NO_HI
, NO_HI
);
1821 case SIGINT
: //TODO: don't exit; just warn like vim does
1824 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1830 void signal_setup(void) {
1831 struct sigaction saction
;
1833 saction
.sa_handler
= signal_handler
;
1834 sigemptyset(&saction
.sa_mask
);
1835 saction
.sa_flags
= 0;
1836 if (sigaction(SIGTSTP
, &saction
, NULL
) < 0) {
1840 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1844 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1848 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1849 perror ("SIGWINCH");
1855 //vim: foldmethod=marker