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
) /*i.e. failed foundation move*/
130 ret
= t2c(from
, STOCK
, 0);
133 if (ret
== ERR
&& is_tableu(from
) && is_tableu(to
))
134 /* try again with from/to swapped: */
135 ret
= action
[to
][from
](to
,from
,opt
);
138 case ERR
: visbell(); break;
139 case WON
: return GAME_WON
;
145 case ERR
: visbell(); break;
146 case WON
: return GAME_WON
;
149 case CMD_HINT
: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
150 case CMD_UNDO
: undo_pop(f
.u
); break;
151 case CMD_INVAL
: visbell(); break;
152 case CMD_NEW
: return GAME_NEW
;
153 case CMD_AGAIN
: goto restart
;
154 case CMD_QUIT
: return GAME_QUIT
;
156 printf (KEYHELP
"\nPress any key to continue.");
169 // card games helper functions {{{
170 #define get_suit(card) \
171 ((card-1) % NUM_SUITS)
172 #define get_rank(card) \
173 ((card-1) / NUM_SUITS)
174 #define get_color(card) \
175 ((get_suit(card) ^ get_suit(card)>>1) & 1)
177 int find_top(card_t
* pile
) {
179 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
182 int first_movable(card_t
* pile
) {
183 /* NOTE: in FREECELL this does not take max_move into account! */
185 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
188 int turn_over(card_t
* pile
) {
189 int top
= find_top(pile
);
195 int check_won(void) {
196 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
197 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
201 int rank_next (card_t a
, card_t b
) {
202 return get_rank(a
) == get_rank(b
)-1;
204 int is_consecutive (card_t
* pile
, int pos
) {
205 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
206 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
208 #if defined KLONDIKE || defined FREECELL
209 /* ranks consecutive? */
210 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
211 /* color opposite? */
212 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
214 /* ranks consecutive? */
215 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
217 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
223 int is_movable(card_t
* pile
, int n
) {
225 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
226 #elif defined SPIDER || defined FREECELL
227 int top
= find_top(pile
);
228 for (int i
= top
; i
>= 0; i
--) {
229 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
230 if (!is_consecutive(pile
, i
)) return 0;
231 if (i
== n
) return 1; /* card reached, must be movable */
238 // takeable actions {{{
240 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
241 card_t card
= f
.s
[f
.w
];
242 /* move stack one over, so there are no gaps in it: */
243 for (int i
= f
.w
; i
< f
.z
-1; i
++)
246 f
.w
--; /* make previous card visible again */
249 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
250 (void) to
; (void) opt
; /* don't need */
251 int top_from
= find_top(f
.t
[from
]);
252 to
= get_suit(f
.t
[from
][top_from
]);
253 int top_to
= find_top(f
.f
[to
]);
254 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
255 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
256 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
257 f
.t
[from
][top_from
] = NO_CARD
;
258 undo_push(from
, FOUNDATION
, to
,
259 turn_over(f
.t
[from
]));
260 if (check_won()) return WON
;
264 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
265 (void) from
; (void) to
; (void) opt
; /* don't need */
266 if (f
.w
< 0) return ERR
;
267 to
= get_suit(f
.s
[f
.w
]);
268 int top_to
= find_top(f
.f
[to
]);
269 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
270 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
271 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
272 f
.f
[to
][top_to
+1] = stack_take();
273 if (check_won()) return WON
;
278 int s2w(int from
, int to
, int opt
) { /* stock to waste */
279 (void) from
; (void) to
; (void) opt
; /* don't need */
280 if (f
.z
== 0) return ERR
;
282 if (f
.w
== f
.z
) f
.w
= -1;
285 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
286 (void) from
; (void) to
; (void) opt
; /* don't need */
287 if (f
.z
== 0) return ERR
;
289 if (f
.w
< -1) f
.w
= f
.z
-1;
292 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
293 (void) from
; /* don't need */
294 int top_to
= find_top(f
.t
[to
]);
296 int top_from
= find_top(f
.f
[from
]);
298 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
299 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
300 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
301 f
.f
[from
][top_from
] = NO_CARD
;
302 undo_push(FOUNDATION
, to
, from
, 0);
306 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
307 (void) from
; (void) opt
; /* don't need */
308 if (f
.w
< 0) return ERR
;
309 int top_to
= find_top(f
.t
[to
]);
310 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
311 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
312 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
313 undo_push(WASTE
, to
, f
.w
, 0);
314 f
.t
[to
][top_to
+1] = stack_take();
318 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
319 (void) opt
; /* don't need */
320 int top_to
= find_top(f
.t
[to
]);
321 int top_from
= find_top(f
.t
[from
]);
322 int count
= 0; //NOTE: could probably be factored out
323 for (int i
= top_from
; i
>=0; i
--) {
324 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
325 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
326 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
327 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
328 /* move cards [i..top_from] to their destination */
329 for (;i
<= top_from
; i
++) {
331 f
.t
[to
][top_to
] = f
.t
[from
][i
];
332 f
.t
[from
][i
] = NO_CARD
;
335 undo_push(from
, to
, count
,
336 turn_over(f
.t
[from
]));
340 return ERR
; /* no such move possible */
343 int remove_if_complete (int pileno
) { //cleanup!
344 card_t
* pile
= f
.t
[pileno
];
345 /* test if K...A complete; move to foundation if so */
346 int top_from
= find_top(pile
);
347 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
348 for (int i
= top_from
; i
>=0; i
--) {
349 if (!is_consecutive (pile
, i
)) return 0;
350 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
351 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
352 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
353 f
.f
[f
.w
][j
] = pile
[i
];
356 undo_push(pileno
, FOUNDATION
, f
.w
,
365 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
366 int top_from
= find_top(f
.t
[from
]);
367 int top_to
= find_top(f
.t
[to
]);
368 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
369 int count
= 0; //NOTE: could probably be factored out
371 for (int i
= top_from
; i
>= 0; i
--) {
372 if (!is_consecutive(f
.t
[from
], i
)) break;
374 /* is consecutive OR to empty pile and rank ok? */
375 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
376 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
377 for (;i
<= top_from
; i
++) {
379 f
.t
[to
][top_to
] = f
.t
[from
][i
];
380 f
.t
[from
][i
] = NO_CARD
;
383 undo_push(from
, to
, count
,
384 turn_over(f
.t
[from
]));
385 remove_if_complete(to
);
386 if (check_won()) return WON
;
391 return ERR
; /* no such move possible */
393 int s2t(int from
, int to
, int opt
) {
394 (void) from
; (void) to
; (void) opt
; /* don't need */
395 if (f
.z
<= 0) return ERR
; /* stack out of cards */
396 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
397 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
398 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
399 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
400 remove_if_complete(pile
);
401 if (check_won()) return WON
;
403 undo_push(STOCK
, TABLEU
, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
406 int t2f(int from
, int to
, int opt
) {
407 (void) to
; (void) opt
; /* don't need */
408 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
409 return remove_if_complete(from
)?OK
:ERR
;
411 #elif defined FREECELL
412 int max_move(int from
, int to
) {
413 /* returns the maximum number of cards that can be moved */
414 /* see also: https://boardgames.stackexchange.com/a/45157/26498 */
415 int free_tabs
= 0, free_cells
= 0;
416 for (int i
= 0; i
< NUM_PILES
; i
++) free_tabs
+= f
.t
[i
][0] == NO_CARD
;
417 for (int i
= 0; i
< NUM_CELLS
; i
++) free_cells
+= f
.s
[i
] == NO_CARD
;
419 /* don't count the tableau we are moving to: */
420 if (to
>= 0 && f
.t
[to
][0] == NO_CARD
) free_tabs
--;
422 /* theoretic maximum is limited by the number of cards on the pile */
423 int max_theory
= (1<<free_tabs
) * (free_cells
+ 1);
424 int max_effective
= 1 + find_top(f
.t
[from
]) - first_movable(f
.t
[from
]);
425 return max_effective
< max_theory
? max_effective
: max_theory
;
427 //TODO FREECELL: auto move to tableu after each move (not all cards possible, only when it is the smallest rank still on the board)
428 int t2t(int from
, int to
, int opt
) {
429 int top_to
= find_top(f
.t
[to
]);
430 int top_from
= find_top(f
.t
[from
]);
431 int count
= 0; //NOTE: could probably be factored out
432 int cards
= max_move(from
, to
);
433 if (top_to
< 0) { /* moving to empty pile? */
435 return ERR
; /* cannot execute move */
436 cards
= opt
; /* user wants to move n cards*/
439 for (int i
= top_from
; i
>=0; i
--) {
440 if (cards
-->0/*enough space and not more attempted than wanted*/
441 && ((top_to
>= 0 /* if destn. not empty: check rank/color */
442 && ((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
443 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))))
444 || (top_to
< 0 && !cards
))) {/*if dest empty and right # cards*/
445 /* move cards [i..top_from] to their destination */
446 for (;i
<= top_from
; i
++) {
448 f
.t
[to
][top_to
] = f
.t
[from
][i
];
449 f
.t
[from
][i
] = NO_CARD
;
452 undo_push(from
, to
, count
, 0);
456 return ERR
; /* no such move possible */
458 int t2f(int from
, int to
, int opt
) { /* 1:1 copy from KLONDIKE */
459 (void) to
; (void) opt
; /* don't need */
460 int top_from
= find_top(f
.t
[from
]);
461 to
= get_suit(f
.t
[from
][top_from
]);
462 int top_to
= find_top(f
.f
[to
]);
463 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
464 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
465 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
466 f
.t
[from
][top_from
] = NO_CARD
;
467 undo_push(from
, FOUNDATION
, to
, 0);
468 if (check_won()) return WON
;
472 int f2t(int from
, int to
, int opt
) {
473 (void) from
; /* don't need */
474 int top_to
= find_top(f
.t
[to
]);
476 int top_from
= find_top(f
.f
[from
]);
478 if (top_to
< 0 /* empty tableu? */
479 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
480 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
])))) {
481 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
482 f
.f
[from
][top_from
] = NO_CARD
;
483 undo_push(FOUNDATION
, to
, from
, 0);
487 int t2c(int from
, int to
, int opt
) {
488 (void) to
; (void) opt
; /* don't need */
489 /* is a cell free? */
490 if (f
.w
== (1<<NUM_CELLS
)-1)
492 for (to
= 0; to
< NUM_CELLS
; to
++)
493 if (!(f
.w
>>to
&1)) break;
495 int top_from
= find_top(f
.t
[from
]);
496 f
.s
[to
] = f
.t
[from
][top_from
];
497 f
.t
[from
][top_from
] = NO_CARD
;
498 f
.w
|= 1<<to
; /* mark cell as occupied */
499 undo_push(from
, STOCK
, to
, 0);
503 int c2t(int from
, int to
, int opt
) {
504 (void) from
; /* don't need */
505 int top_to
= find_top(f
.t
[to
]);
508 if (top_to
< 0 /* empty tableu? */
509 ||((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[from
]))
510 && (rank_next(f
.s
[from
], f
.t
[to
][top_to
])))) {
511 f
.t
[to
][top_to
+1] = f
.s
[from
];
513 f
.w
&= ~(1<<from
); /* mark cell as free */
514 undo_push(STOCK
, to
, from
, 0);
519 int c2f(int from
, int to
, int opt
) {
520 (void) from
; (void) to
; /* don't need */
522 to
= get_suit(f
.s
[from
]);
523 int top_to
= find_top(f
.f
[to
]);
524 if ((top_to
< 0 && get_rank(f
.s
[from
]) == RANK_A
)
525 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.s
[from
]))) {
526 f
.f
[to
][top_to
+1] = f
.s
[from
];
528 f
.w
&= ~(1<<from
); /* mark cell as free */
529 undo_push(STOCK
, FOUNDATION
, from
| to
<<16, 0);
530 if (check_won()) return WON
;
534 int f2c(int from
, int to
, int opt
) {
535 (void) from
; (void) to
; /* don't need */
536 /* is a cell free? */
537 if (f
.w
== (1<<NUM_CELLS
)-1)
539 for (to
= 0; to
< NUM_CELLS
; to
++)
540 if (!(f
.w
>>to
&1)) break;
543 int top_from
= find_top(f
.f
[from
]);
544 f
.s
[to
] = f
.f
[from
][top_from
];
545 f
.f
[from
][top_from
] = NO_CARD
;
546 f
.w
|= 1<<to
; /* mark cell as occupied */
547 undo_push(FOUNDATION
, STOCK
, from
| to
<<16, 0);
553 //TODO: generalize prediction engine for CMD_HINT
555 #define would_complete(pile) 0
557 #define would_complete(pile) \
558 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
559 && get_rank(f.t[to][bottom_to]) == RANK_K)
560 #elif defined FREECELL
561 #define would_complete(pile) 0
563 #define would_turn(pile) \
564 (f.t[pile][r[pile].pos-1] < 0)
565 #define would_empty(pile) \
569 int top_to
= find_top(f
.t
[to
]);
571 int bottom_to
= first_movable(f
.t
[to
]);
575 if (to
== WASTE
|| to
== STOCK
) return ERR
; /*why would you do that!?*/
577 if (to
== FOUNDATION
) {
579 for (int i
= 0; i
<= TAB_MAX
; i
++)
580 switch ((i
?t2f
:w2f
)(i
-1, FOUNDATION
, 0)) {
581 case WON
: return WON
;
582 case OK
: status
= OK
;
588 if (top_to
< 0) { /* move a king to empty pile: */
589 for (int i
= 0; i
< TAB_MAX
; i
++) {
590 if (f
.t
[i
][0] < 0) /* i.e. would turn? */
591 if (t2t(i
, to
, 0) == OK
) return OK
;
593 return w2t(WASTE
, to
, 0);
598 int ok
:1; /* card to move in pile? */
599 int above
; /* number of movable cards above */
600 int below
; /* number of cards below ours */
601 int pos
; /* where the card to move is in the pile */
602 int top
; /* find_top() */
603 } r
[NUM_PILES
] = {{0}};
604 int complete
= 0;/* SPIDER: true if any pile would complete a stack */
605 int turn
= 0; /* SPIDER: true if any pile would turn_over */
606 int empty
= 0; /* true if any pile would become empty */
608 /* 1. rate each pile: */
611 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
612 if (pile
== to
) continue;
613 int top
= find_top(f
.t
[pile
]);
614 int bottom
= first_movable(f
.t
[pile
]);
615 r
[pile
].pos
= bottom
; /* need for would_empty */
617 if (top
< 0) continue; /* no cards to move */
618 if (would_empty(pile
)) continue; /* doesn't help */
621 r
[pile
].above
= 0; /* always take as many as possible */
622 r
[pile
].below
= top
- bottom
;
624 complete
|= would_complete(pile
); /* never happens */
625 turn
|= would_turn(pile
);
626 empty
|= would_empty(pile
);
630 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
631 r
[pile
].top
= r
[pile
].pos
= find_top(f
.t
[pile
]);
632 /* backtrack until we find a compatible-to-'to'-pile card: */
633 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
634 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
635 - get_rank(f
.t
[to
][top_to
]);
636 if (rankdiff
>= 0) break; /* past our card */
637 if (rankdiff
== -1 /* rank matches */
639 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
640 != get_color(f
.t
[to
][top_to
])
642 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
643 == get_suit(f
.t
[to
][top_to
])
647 complete
|= would_complete(pile
);
648 turn
|= would_turn(pile
);
649 empty
|= would_empty(pile
);
650 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
651 if (is_movable(f
.t
[pile
], i
-1))
661 /* 2. find optimal pile: (optimized for spider) */
662 //todo: in spider, prefer longest piles if above==0 (faster completions)
664 for (int pile
= 0, above
= 99, below
= 99; pile
< NUM_PILES
; pile
++) {
665 if (!r
[pile
].ok
) continue;
666 /* don't bother if another pile would be better: prefer ... */
667 /* ... to complete a stack: */
668 if (!would_complete(pile
) && complete
) continue;
669 /* ... emptying piles: */
670 if (!would_empty(pile
) && empty
&& !complete
) continue;
671 /* ... to turn_over: */
672 if (!would_turn(pile
) && turn
&& !complete
&& !empty
) continue;
673 /* ... not to rip apart too many cards: */
674 if (r
[pile
].above
> above
) continue;
675 /* if tied, prefer ... */
676 if (r
[pile
].above
== above
677 /* ... larger pile if destination is empty */
678 && (top_to
< 0? r
[pile
].below
< below
679 /* ... shorter pile otherwise */
680 : r
[pile
].below
> below
))
684 above
= r
[pile
].above
;
685 below
= r
[pile
].below
;
688 /* 3. move cards over and return: */
690 /* prefer waste if it wouldn't turn_over: */
691 /* NOTE: does not attempt to take from froundation */
692 if (!turn
&& w2t(WASTE
, to
, 0) == OK
) //TODO: gives higher priority to waste than to empty!
694 if (from
< 0) /* nothing found */
696 return t2t(from
, to
, 0);
698 if (from
< 0) /* nothing found */
700 int bottom
= first_movable(f
.t
[from
]);
701 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
702 #elif defined FREECELL
704 return ERR
; //TODO FREECELL: implement join
709 #undef would_complete
710 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
713 // keyboard input handling {{{
714 // cursor functions{{{
716 void cursor_left (struct cursor
* cursor
) {
718 if (is_tableu(cursor
->pile
)) {
719 if (cursor
->pile
> 0) cursor
->pile
--;
721 } else { /* stock/waste/foundation*/
722 switch (cursor
->pile
) {
723 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
725 if (cursor
->opt
<= 0)
726 cursor
->pile
= WASTE
;
732 void cursor_down (struct cursor
* cursor
) {
734 if (!is_tableu(cursor
->pile
)) {
735 switch (cursor
->pile
) {
736 case STOCK
: cursor
->pile
= TAB_1
; break;
737 case WASTE
: cursor
->pile
= TAB_2
; break;
739 cursor
->pile
= TAB_4
+ cursor
->opt
;
744 void cursor_up (struct cursor
* cursor
) {
746 if (is_tableu(cursor
->pile
)) {
747 switch (cursor
->pile
) { //ugly :|
748 case TAB_1
: cursor
->pile
= STOCK
; break;
749 case TAB_2
: cursor
->pile
= WASTE
; break;
750 case TAB_3
: cursor
->pile
= WASTE
; break;
751 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
752 cursor
->opt
=cursor
->pile
-TAB_4
;
753 cursor
->pile
= FOUNDATION
;
758 void cursor_right (struct cursor
* cursor
) {
760 if (is_tableu(cursor
->pile
)) {
761 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
764 switch (cursor
->pile
) {
765 case STOCK
: cursor
->pile
= WASTE
; break;
766 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
768 if (cursor
->opt
< NUM_SUITS
-1)
774 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
775 void cursor_left (struct cursor
* cursor
) {
777 if (cursor
->pile
> 0) cursor
->pile
--;
780 void cursor_down (struct cursor
* cursor
) {
782 int first
= first_movable(f
.t
[cursor
->pile
]);
783 int top
= find_top(f
.t
[cursor
->pile
]);
784 if (first
+ cursor
->opt
< top
)
787 void cursor_up (struct cursor
* cursor
) {
789 if (cursor
->opt
> 0) cursor
->opt
--;
791 void cursor_right (struct cursor
* cursor
) {
793 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
796 #elif defined FREECELL
797 void cursor_left (struct cursor
* cursor
) {
799 if (is_tableu(cursor
->pile
)) {
800 if (cursor
->pile
> 0) cursor
->pile
--;
802 } else { /* cells/foundation*/
803 switch (cursor
->pile
) {
809 if (cursor
->opt
<= 0) {
810 cursor
->pile
= STOCK
;
818 void cursor_down (struct cursor
* cursor
) {
820 if (is_tableu(cursor
->pile
)) {
821 if (cursor
->opt
< max_move(cursor
->pile
, -1)-1)
824 cursor
->pile
= cursor
->opt
+NUM_CELLS
*(cursor
->pile
==FOUNDATION
);
828 void cursor_up (struct cursor
* cursor
) {
830 if (is_tableu(cursor
->pile
)) {
831 if (cursor
->opt
> 0) {
834 switch (cursor
->pile
) {
835 case TAB_1
: case TAB_2
: case TAB_3
: case TAB_4
:
836 cursor
->opt
= cursor
->pile
; /*assumes TAB_1==0*/
837 cursor
->pile
= STOCK
;
839 case TAB_5
: case TAB_6
: case TAB_7
: case TAB_8
:
840 cursor
->opt
= cursor
->pile
- NUM_CELLS
;
841 cursor
->pile
= FOUNDATION
;
846 void cursor_right (struct cursor
* cursor
) {
848 if (is_tableu(cursor
->pile
)) {
849 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
852 switch (cursor
->pile
) {
854 if (cursor
->opt
< NUM_SUITS
-1) {
857 cursor
->pile
= FOUNDATION
;
861 if (cursor
->opt
< NUM_SUITS
-1)
867 void cursor_to (struct cursor
* cursor
, int pile
) {
872 int set_mouse(int pile
, int* main
, int* opt
) {
873 //TODO: this should set cursor.opt, so card selector choice dialog does not trigger!
875 if (pile
< 0) return 1;
878 if (pile
>= FOUNDATION
)//TODO: check upper bound!
880 *opt
= pile
- FOUNDATION
;
883 #elif defined FREECELL
884 if (pile
> TAB_MAX
) {
885 *main
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
886 *opt
= (pile
-STOCK
) % 4;
892 int get_cmd (int* from
, int* to
, int* opt
) {
894 unsigned char mouse
[6] = {0}; /* must clear [3]! */
895 struct cursor inactive
= {-1,-1};
896 static struct cursor active
= {0,0};
897 if (is_tableu(active
.pile
))
901 from_l
: print_table(&active
, &inactive
);
905 /* direct addressing: */
906 case '1': *from
= TAB_1
; break;
907 case '2': *from
= TAB_2
; break;
908 case '3': *from
= TAB_3
; break;
909 case '4': *from
= TAB_4
; break;
910 case '5': *from
= TAB_5
; break;
911 case '6': *from
= TAB_6
; break;
912 case '7': *from
= TAB_7
; break;
914 case '8': *from
= TAB_8
; break;
915 case '9': *from
= TAB_9
; break;
916 case '0': *from
= TAB_10
;break;
917 #elif defined FREECELL
918 case '8': *from
= TAB_8
; break;
919 case '9': *from
= STOCK
; break;
920 case '0': *from
= FOUNDATION
; break;
921 #elif defined KLONDIKE
922 case '9': *from
= WASTE
; break;
923 case '0': *from
= FOUNDATION
; break;
924 case '8': /* fallthrough */
927 case '\n': *from
= STOCK
; break;
929 /* cursor keys addressing: */
931 case 'h': cursor_left (&active
); goto from_l
;
933 case 'j': cursor_down (&active
); goto from_l
;
935 case 'k': cursor_up (&active
); goto from_l
;
937 case 'l': cursor_right(&active
); goto from_l
;
939 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
941 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
943 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
944 case ' ': /* continue with second cursor */
947 *opt
= active
.opt
; /* when FOUNDATION */
951 /* mouse addressing: */
952 case MOUSE_MIDDLE
: return CMD_NONE
;
954 if (set_mouse(term2pile(mouse
), to
, opt
))
958 if (set_mouse(term2pile(mouse
), from
, opt
))
960 if (!is_tableu(*from
))
961 inactive
.opt
= *opt
; /* prevents card selector dialog */
966 fprintf (stderr
, ":");
967 raw_mode(0); /* turn on echo */
968 fgets (buf
, 256, stdin
);
971 case 'q': return CMD_QUIT
;
972 case 'n': return CMD_NEW
;
973 case 'r': return CMD_AGAIN
;
974 case 'h': return CMD_HELP
;
975 default: return CMD_INVAL
;
981 if (*to
== FOUNDATION
) return CMD_JOIN
;
983 if (*to
> TAB_MAX
) return CMD_INVAL
;
985 case 'K': /* fallthrough */
986 case '?': return CMD_HINT
;
987 case 'u': return CMD_UNDO
;
988 case 002: return CMD_NONE
; /* sent by SIGWINCH */
989 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
990 default: return CMD_INVAL
;
992 inactive
.pile
= *from
; /* for direct addressing highlighting */
993 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
996 if (*from
== STOCK
) {
1003 to_l
: print_table(&active
, &inactive
);
1008 case 'h': cursor_left (&active
); goto to_l
;
1010 case 'j': cursor_down (&active
); goto to_l
;
1012 case 'k': cursor_up (&active
); goto to_l
;
1014 case 'l': cursor_right(&active
); goto to_l
;
1016 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
1018 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
1020 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
1021 case 'J': /* fallthrough; just join selected pile */
1024 break; /* continues with the foundation/empty tableu check */
1026 case MOUSE_RIGHT
: return CMD_NONE
;
1028 if (set_mouse(term2pile(mouse
), to
, opt
))
1031 //TODO: set opt if to field is empty; suppress "up do" dialog from below
1032 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
1033 int top = find_top(f.t[*from]);
1034 if (top < 0) return CMD_INVAL;
1035 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
1036 *opt = get_rank(f.t[*from][top]);
1043 case 'K': /* fallthrough */
1044 case '?': return CMD_HINT
;
1045 case 'u': return CMD_NONE
; /* cancel selection */
1046 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
1048 if (t
< '0' || t
> '9') return CMD_INVAL
;
1052 #elif defined SPIDER
1054 #elif defined FREECELL
1065 if (*from
== FOUNDATION
) {
1066 if (inactive
.opt
>= 0) {
1067 *opt
= inactive
.opt
;
1070 int top
= find_top(f
.t
[*to
]);
1071 if (top
< 0) return CMD_INVAL
;
1072 int color
= get_color(f
.t
[*to
][top
]);
1073 int choice_1
= 1-color
; /* selects piles of */
1074 int choice_2
= 2+color
; /* the opposite color */
1075 int top_c1
= find_top(f
.f
[choice_1
]);
1076 int top_c2
= find_top(f
.f
[choice_2
]);
1078 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
1079 && top_c1
>= 0 ) << 0
1080 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
1081 && top_c2
>= 0 ) << 1) {
1082 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
1083 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
1084 case (1<<1 | 1<<0): /* both, ask user which to pick from */
1085 printf ("take from (1-4): "); fflush (stdout
);
1086 *opt
= getch(NULL
) - '1';
1087 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1089 default: return CMD_INVAL
; /* none matched */
1091 /* `opt` is the foundation index (0..3) */
1093 #elif defined SPIDER
1094 /* moving to empty tableu? */
1095 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1096 int bottom
= first_movable(f
.t
[*from
]);
1097 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
1098 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
1101 int top
= find_top(f
.t
[*from
]);
1102 if (top
< 0) return CMD_INVAL
;
1103 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
1104 *opt
= get_rank(f
.t
[*from
][top
]);
1105 } else { /* only ask the user if it's unclear: */
1106 printf ("\rup to ([a23456789xjqk] or space/return): ");
1109 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
1110 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
1111 case 'a': case 'A': *opt
= RANK_A
; break;
1112 case '0': /* fallthrough */
1113 case 'x': case 'X': *opt
= RANK_X
; break;
1114 case 'j': case 'J': *opt
= RANK_J
; break;
1115 case 'q': case 'Q': *opt
= RANK_Q
; break;
1116 case 'k': case 'K': *opt
= RANK_K
; break;
1117 default: *opt
-= '1';
1119 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
1121 /* `opt` is the rank of the highest card to move */
1123 #elif defined FREECELL
1124 //TODO FREECELL: card selector choice dialog
1126 /* if it was selected with a cursor, it's obvious: */
1127 if (inactive
.opt
>= 0) {
1128 if (is_tableu(*from
)) {
1129 /* NOTE: max_move same as in cursor_down() */
1130 *opt
= max_move(*from
, -1) - inactive
.opt
;
1132 *opt
= inactive
.opt
;
1134 /* moving from tableu to empty tableu? */
1135 } else if (is_tableu(*from
) && is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
1136 // how many cards? (NOTE: spider asks "up to rank?"; do this then convert to number of cards?
1137 printf ("take how many (1-9): "); fflush (stdout
);
1138 *opt
= getch(NULL
) - '0';
1139 if (*opt
< 1 || *opt
> 9) return CMD_INVAL
;
1140 /* moving between stock/foundation? */
1141 } else if (*from
== FOUNDATION
&& *to
== STOCK
) {
1142 //can take from all non-empty foundations
1143 printf ("take from (1-4): "); fflush (stdout
);
1144 *opt
= getch(NULL
) - '1';
1145 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1146 } else if (*from
== STOCK
&& *to
== FOUNDATION
) {
1147 //check all non-empty cells
1148 printf ("take from (1-4): "); fflush (stdout
);
1149 *opt
= getch(NULL
) - '1';
1150 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1151 } else if (*from
== FOUNDATION
|| *from
== STOCK
) { /* -> tableu */
1152 //foundation: 2 choices
1154 printf ("take from (1-4): "); fflush (stdout
);
1155 *opt
= getch(NULL
) - '1';
1156 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
1162 int getctrlseq(unsigned char* buf
) {
1170 int offset
= 0x20; /* never sends control chars as data */
1171 while ((c
= getchar()) != EOF
) {
1175 case '\033': state
=ESC_SENT
; break;
1181 case '[': state
=CSI_SENT
; break;
1182 default: return KEY_INVAL
;
1187 case 'A': return KEY_UP
;
1188 case 'B': return KEY_DOWN
;
1189 case 'C': return KEY_RIGHT
;
1190 case 'D': return KEY_LEFT
;
1191 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
1192 case 'H': return KEY_HOME
;
1193 case 'F': return KEY_END
;
1194 case '2': getchar(); return KEY_INS
;
1195 case '5': getchar(); return KEY_PGUP
;
1196 case '6': getchar(); return KEY_PGDN
;
1197 case 'M': state
=MOUSE_EVENT
; break;
1198 default: return KEY_INVAL
;
1202 if (buf
== NULL
) return KEY_INVAL
;
1203 buf
[0] = c
- offset
;
1204 buf
[1] = getchar() - offset
;
1205 buf
[2] = getchar() - offset
;
1213 int term2pile(unsigned char *mouse
) {
1214 int line
= (mouse
[2]-1);
1215 int column
= (mouse
[1]-1) / op
.s
->width
;
1217 if (line
< op
.s
->height
) { /* first line */
1220 case 0: return STOCK
;
1221 case 1: return WASTE
;
1222 case 2: return -1; /* spacer */
1223 case 3: return FOUNDATION
+0;
1224 case 4: return FOUNDATION
+1;
1225 case 5: return FOUNDATION
+2;
1226 case 6: return FOUNDATION
+3;
1228 #elif defined SPIDER
1229 if (column
< 3) return STOCK
;
1231 #elif defined FREECELL
1232 if (column
< NUM_SUITS
+ NUM_CELLS
) return STOCK
+column
;
1235 } else if (line
> op
.s
->height
) { /* tableu */
1236 if (column
<= TAB_MAX
) return column
;
1240 int wait_mouse_up(unsigned char* mouse
) {
1241 //TODO: mouse drag: start gets inactive, hovering gets active cursors
1242 struct cursor cur
= {-1,-1};
1244 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
1246 /* display a cursor while mouse button is pushed: */
1247 int pile
= term2pile(mouse
);
1250 if (pile
>= FOUNDATION
) {
1251 cur
.pile
= FOUNDATION
;
1252 cur
.opt
= pile
-FOUNDATION
;
1254 #elif defined FREECELL
1255 if (pile
> TAB_MAX
) {
1256 cur
.pile
= pile
-STOCK
< NUM_CELLS
? STOCK
: FOUNDATION
;
1257 cur
.opt
= (pile
-STOCK
) % 4;
1260 /* need to temporarily show the cursor, then revert to last state: */
1261 int old_show_cursor_hi
= op
.h
; //TODO: ARGH! that's awful!
1263 print_table(&cur
, NO_HI
); //TODO: should not overwrite inactive cursor!
1264 op
.h
= old_show_cursor_hi
;
1267 if (getctrlseq (mouse
+3) == MOUSE_ANY
) {
1268 /* ignore mouse wheel events: */
1269 if (mouse
[3] & 0x40) continue;
1271 else if((mouse
[3]&3) == 3) level
--; /* release event */
1272 else level
++; /* another button pressed */
1276 int success
= mouse
[1] == mouse
[4] && mouse
[2] == mouse
[5];
1283 int getch(unsigned char* buf
) {
1284 /* returns a character, EOF, or constant for an escape/control sequence - NOT
1285 compatible with the ncurses implementation of same name */
1287 if (buf
&& buf
[3]) {
1288 /* mouse was dragged; return 'ungetted' previous destination */
1289 action
= MOUSE_DRAG
;
1290 /* keep original [0], as [3] only contains release event */
1295 action
= getctrlseq(buf
);
1300 if (buf
[0] > 3) break; /* ignore wheel events */
1305 case 0: return MOUSE_LEFT
;
1306 case 1: return MOUSE_MIDDLE
;
1307 case 2: return MOUSE_RIGHT
;
1315 // shuffling and dealing {{{
1316 void deal(long seed
) {
1317 f
= (const struct playfield
){0}; /* clear playfield */
1318 card_t deck
[DECK_SIZE
*NUM_DECKS
];
1319 int avail
= DECK_SIZE
*NUM_DECKS
;
1320 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
1322 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
1323 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
1324 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
1325 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
1329 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
1330 int j
= rand() % (i
+1);
1331 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
1335 for (int i
= 0; i
< NUM_PILES
; i
++) {
1338 int count
= i
; /* pile n has n closed cards, then 1 open */
1339 #elif defined SPIDER
1341 int count
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1342 #elif defined FREECELL
1344 int count
= i
<4?6:5;/*like spider, but cards are dealt face-up*/
1346 /* "SIGN": face down cards are negated */
1347 for (int j
= 0; j
< count
; j
++) f
.t
[i
][j
] = SIGN deck
[--avail
];
1348 f
.t
[i
][count
] = deck
[--avail
]; /* the face-up card */
1351 /* rest of the cards to the stock: */
1352 /* NOTE: assert(avail==50) for spider, assert(avail==0) for freecell */
1353 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
1355 f
.w
= -1; /* @start: nothing on waste */
1356 #elif defined SPIDER
1357 f
.w
= 0; /* number of used foundations */
1358 #elif defined FREECELL
1359 f
.w
= 0; /* bitmask of used free cells */
1362 f
.u
= &undo_sentinel
;
1366 // screen drawing routines {{{
1367 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
1368 if (!op
.h
) invert
= 0; /* don't show invert if we used the mouse last */
1369 if (bold
&& op
.s
== &unicode_large_color
){ //awful hack for bold + faint
1370 int offset
= str
[3]==017?16:str
[4]==017?17:0;
1371 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1372 "\033[1m", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1373 offset
, str
, bold
?"\033[1m":"", str
+offset
,
1374 grey_bg
?"\033[49m":"", invert
?"\033[27m":"","\033[22m");
1377 printf ("%s%s%s%s%s%s%s",
1378 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
1380 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
1382 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
1383 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1385 /* print stock, waste and foundation: */
1386 for (int line
= 0; line
< op
.s
->height
; line
++) {
1388 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
1389 (f
.w
< f
.z
-1)?op
.s
->facedown
1390 :op
.s
->placeholder
)[line
]);
1392 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
1393 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1394 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
1395 :op
.s
->placeholder
)[line
]);
1396 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
1398 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1399 int card
= find_top(f
.f
[pile
]);
1400 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1401 inactive
->pile
==FOUNDATION
&& (
1402 /* cursor addr. || direct addr. */
1403 inactive
->opt
==pile
|| inactive
->opt
< 0
1405 (card
< 0)?op
.s
->foundation
[line
]
1406 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1411 #elif defined SPIDER
1412 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
1413 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
1414 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
1415 int spacer_to
= NUM_PILES
*op
.s
->width
-
1416 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
1417 for (int line
= 0; line
< op
.s
->height
; line
++) {
1418 /* available stock: */
1419 for (int i
= f
.z
/10; i
; i
--) {
1420 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
1421 else printf ("%s", op
.s
->halfstack
[line
]);
1424 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
1425 /* foundation (overlapping): */
1426 for (int i
= NUM_DECKS
*NUM_SUITS
-1, half
= 0; i
>= 0; i
--) {
1427 int overlap
= half
? op
.s
->halfcard
[line
]: 0;
1428 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
1429 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
),
1435 #elif defined FREECELL
1436 /* print open cells, foundation: */
1437 for (int line
= 0; line
< op
.s
->height
; line
++) {
1438 for (int pile
= 0; pile
< NUM_CELLS
; pile
++)
1439 print_hi (active
->pile
==STOCK
&& active
->opt
==pile
,
1440 inactive
->pile
==STOCK
&& (
1441 /* cursor addr. || direct addr. */
1442 inactive
->opt
==pile
|| inactive
->opt
< 0
1444 ((f
.s
[pile
])?op
.s
->card
[f
.s
[pile
]]
1445 :op
.s
->placeholder
)[line
]);
1446 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
1447 int card
= find_top(f
.f
[pile
]);
1448 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
1449 inactive
->pile
==FOUNDATION
&& (
1450 /* cursor addr. || direct addr. */
1451 inactive
->opt
==pile
|| inactive
->opt
< 0
1453 (card
< 0)?op
.s
->foundation
[line
]
1454 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
1461 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1462 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1463 #elif defined SPIDER || defined FREECELL
1464 int offset
[NUM_PILES
]={0}; /* first card to highlight */
1466 int bottom
[NUM_PILES
]; /* first movable card */
1467 for (int i
=0; i
<NUM_PILES
; i
++)
1468 bottom
[i
] = find_top(f
.t
[i
]) - max_move(i
,-1);
1470 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1471 && offset[pile] >= cursor->opt)
1472 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1473 && offset[pile] == cursor->opt)
1475 /* print tableu piles: */
1476 int row
[NUM_PILES
] = {0};
1477 int line
[NUM_PILES
]= {0};
1478 int label
[NUM_PILES
]={0};
1480 int did_placeholders
= 0;
1483 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
1484 card_t card
= f
.t
[pile
][row
[pile
]];
1485 card_t next
= f
.t
[pile
][row
[pile
]+1];
1486 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
1488 if(row
[pile
] <= bottom
[pile
]) movable
= 0;
1490 int empty
= !card
&& row
[pile
] == 0;
1492 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
1493 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
1494 :(card
<0)?op
.s
->facedown
1498 int extreme_overlap
= ( 3 /* spacer, labels, status */
1499 + 2 * op
.s
->height
/* stock, top tableu card */
1500 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
1501 /* normal overlap: */
1502 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
1503 /* extreme overlap on closed cards: */
1504 || (extreme_overlap
&&
1506 f
.t
[pile
][row
[pile
]] < 0 &&
1507 f
.t
[pile
][row
[pile
]+1] <0)
1508 /* extreme overlap on sequences: */
1509 || (extreme_overlap
&&
1510 !TOP_HI(active
) && /*always show top selected card*/
1511 line
[pile
] >= 1 && row
[pile
] > 0 &&
1512 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
1513 is_consecutive (f
.t
[pile
], row
[pile
]) &&
1514 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
1515 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
1519 #if defined SPIDER || defined FREECELL
1520 if (movable
) offset
[pile
]++;
1523 /* tableu labels: */
1524 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
1526 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
1528 line_had_card
|= !!card
;
1529 did_placeholders
|= row
[pile
] > 0;
1532 } while (line_had_card
|| !did_placeholders
);
1535 void visbell (void) {
1537 printf ("\033[?5h"); fflush (stdout
);
1539 printf ("\033[?5l"); fflush (stdout
);
1541 void win_anim(void) {
1542 printf ("\033[?25l"); /* hide cursor */
1544 /* set cursor to random location */
1545 int row
= 1+rand()%(1+op
.w
[0]-op
.s
->height
);
1546 int col
= 1+rand()%(1+op
.w
[1]-op
.s
->width
);
1548 /* draw random card */
1549 int face
= 1 + rand() % 52;
1550 for (int l
= 0; l
< op
.s
->height
; l
++) {
1551 printf ("\033[%d;%dH", row
+l
, col
);
1552 printf ("%s", op
.s
->card
[face
][l
]);
1556 /* exit on keypress */
1557 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
1558 if (poll (&p
, 1, 80)) goto fin
;
1561 printf ("\033[?25h"); /* show cursor */
1567 void undo_push (int _f
, int t
, int n
, int o
) {
1568 struct undo
* new = malloc(sizeof(struct undo
));
1578 void undo_pop (struct undo
* u
) {
1579 if (u
== &undo_sentinel
) return;
1582 if (u
->f
== FOUNDATION
) {
1583 /* foundation -> tableu */
1584 int top_f
= find_top(f
.f
[u
->n
]);
1585 int top_t
= find_top(f
.t
[u
->t
]);
1586 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1587 f
.t
[u
->t
][top_t
] = NO_CARD
;
1588 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
1589 /* waste -> foundation */
1590 /* split u->n into wst and fnd: */
1591 int wst
= u
->n
& 0xffff;
1592 int fnd
= u
->n
>> 16;
1593 /* move stock cards one position up to make room: */
1594 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
1595 /* move one card from foundation to waste: */
1596 int top
= find_top(f
.f
[fnd
]);
1597 f
.s
[wst
] = f
.f
[fnd
][top
];
1598 f
.f
[fnd
][top
] = NO_CARD
;
1601 } else if (u
->f
== WASTE
) {
1602 /* waste -> tableu */
1603 /* move stock cards one position up to make room: */
1604 for (int i
= f
.z
-1; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
1605 /* move one card from tableu to waste: */
1606 int top
= find_top(f
.t
[u
->t
]);
1607 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
1608 f
.t
[u
->t
][top
] = NO_CARD
;
1611 } else if (u
->t
== FOUNDATION
) {
1612 /* tableu -> foundation */
1613 int top_f
= find_top(f
.t
[u
->f
]);
1614 int top_t
= find_top(f
.f
[u
->n
]);
1615 /* close topcard if previous action caused turn_over(): */
1616 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1617 /* move one card from foundation to tableu: */
1618 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1619 f
.f
[u
->n
][top_t
] = NO_CARD
;
1621 /* tableu -> tableu */
1622 int top_f
= find_top(f
.t
[u
->f
]);
1623 int top_t
= find_top(f
.t
[u
->t
]);
1624 /* close topcard if previous action caused turn_over(): */
1625 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1626 /* move n cards from tableu[f] to tableu[t]: */
1627 for (int i
= 0; i
< u
->n
; i
++) {
1628 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1629 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1632 #elif defined SPIDER
1633 if (u
->f
== STOCK
) {
1634 /* stock -> tableu */
1635 /*remove a card from each pile and put it back onto the stock:*/
1636 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
1637 int top
= find_top(f
.t
[pile
]);
1638 f
.s
[f
.z
++] = f
.t
[pile
][top
];
1639 f
.t
[pile
][top
] = NO_CARD
;
1641 } else if (u
->t
== FOUNDATION
) {
1642 /* tableu -> foundation */
1643 int top
= find_top(f
.t
[u
->f
]);
1644 /* close topcard if previous action caused turn_over(): */
1645 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1646 /* append cards from foundation to tableu */
1647 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1648 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1649 f
.f
[u
->n
][i
] = NO_CARD
;
1651 f
.w
--; /* decrement complete-foundation-counter */
1654 /* tableu -> tableu */
1655 int top_f
= find_top(f
.t
[u
->f
]);
1656 int top_t
= find_top(f
.t
[u
->t
]);
1657 /* close topcard if previous action caused turn_over(): */
1658 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1659 /* move n cards from tableu[f] to tableu[t]: */
1660 for (int i
= 0; i
< u
->n
; i
++) {
1661 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1662 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1665 #elif defined FREECELL
1666 /*NOTE: if from and to are both stock/foundation, opt = from | to<<16 */
1667 if (u
->f
== STOCK
&& u
->t
== FOUNDATION
) {
1668 /* free cells -> foundation */
1669 /* split u->n into cll and fnd: */
1670 int cll
= u
->n
& 0xffff;
1671 int fnd
= u
->n
>> 16;
1672 /* move one card from foundation to free cell: */
1673 int top
= find_top(f
.f
[fnd
]);
1674 f
.s
[cll
] = f
.f
[fnd
][top
];
1675 f
.f
[fnd
][top
] = NO_CARD
;
1676 f
.w
|= 1<<cll
; /* mark cell as occupied */
1677 } else if (u
->f
== STOCK
) {
1678 /* free cells -> cascade */
1679 int top_t
= find_top(f
.t
[u
->t
]);
1680 f
.s
[u
->n
] = f
.t
[u
->t
][top_t
];
1681 f
.t
[u
->t
][top_t
] = NO_CARD
;
1682 f
.w
|= 1<<u
->n
; /* mark cell as occupied */
1683 } else if (u
->f
== FOUNDATION
&& u
->t
== STOCK
) {
1684 /* foundation -> free cells */
1685 /* split u->n into cll and fnd: */
1686 int cll
= u
->n
>> 16;
1687 int fnd
= u
->n
& 0xffff;
1688 /* move 1 card from free cell to foundation: */
1689 int top_f
= find_top(f
.f
[fnd
]);
1690 f
.f
[fnd
][top_f
+1] = f
.s
[cll
];
1692 f
.w
&= ~(1<<cll
); /* mark cell as free */
1693 } else if (u
->f
== FOUNDATION
) {
1694 /* foundation -> cascade */
1695 int top_f
= find_top(f
.f
[u
->n
]);
1696 int top_t
= find_top(f
.t
[u
->t
]);
1697 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
1698 f
.t
[u
->t
][top_t
] = NO_CARD
;
1699 } else if (u
->t
== STOCK
) {
1700 /* cascade -> free cells */
1701 int top_f
= find_top(f
.t
[u
->f
]);
1702 f
.t
[u
->f
][top_f
+1] = f
.s
[u
->n
];
1703 f
.s
[u
->n
] = NO_CARD
;
1704 f
.w
&= ~(1<<u
->n
); /* mark cell as free */
1705 } else if (u
->t
== FOUNDATION
) {
1706 /* cascade -> foundation */
1707 int top_f
= find_top(f
.t
[u
->f
]);
1708 int top_t
= find_top(f
.f
[u
->n
]);
1709 /* move one card from foundation to cascade: */
1710 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
1711 f
.f
[u
->n
][top_t
] = NO_CARD
;
1713 /* cascade -> cascade */
1714 int top_f
= find_top(f
.t
[u
->f
]);
1715 int top_t
= find_top(f
.t
[u
->t
]);
1716 /* move n cards from tableu[f] to tableu[t]: */
1717 for (int i
= 0; i
< u
->n
; i
++) {
1718 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1719 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1728 void free_undo (struct undo
* u
) {
1729 while (u
&& u
!= &undo_sentinel
) {
1737 // initialization stuff {{{
1738 void screen_setup (int enable
) {
1741 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1742 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1743 printf ("\033[?1000h"); /* enable mouse */
1745 printf ("\033[?1000l"); /* disable mouse */
1746 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1751 void raw_mode(int enable
) {
1752 static struct termios saved_term_mode
;
1753 struct termios raw_term_mode
;
1756 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1757 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1758 raw_term_mode
= saved_term_mode
;
1759 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1760 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1761 raw_term_mode
.c_cc
[VTIME
] = 0;
1762 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1764 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1768 void signal_handler (int signum
) {
1773 signal(SIGTSTP
, SIG_DFL
); /* NOTE: assumes SysV semantics! */
1778 print_table(NO_HI
, NO_HI
);
1780 case SIGINT
: //TODO: don't exit; just warn like vim does
1783 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1789 void signal_setup(void) {
1790 struct sigaction saction
;
1792 saction
.sa_handler
= signal_handler
;
1793 sigemptyset(&saction
.sa_mask
);
1794 saction
.sa_flags
= 0;
1795 if (sigaction(SIGTSTP
, &saction
, NULL
) < 0) {
1799 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1803 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1807 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1808 perror ("SIGWINCH");
1814 //vim: foldmethod=marker