1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
19 /* stores a function pointer for every takeable action; called by game loop */
20 int (*action
[NUM_PLACES
][10])(int,int,int) = {
22 /* 1 2 3 4 5 6 7 stk wst fnd*/
23 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
24 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
25 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
26 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
27 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
28 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
29 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
30 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
31 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
32 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
34 /* 1 2 3 4 5 6 7 8 9 10*/
35 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
36 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
37 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
38 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
39 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
},
40 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
},
41 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
},
42 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
},
43 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
},
44 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
},
45 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
50 // argv parsing, game loops, cleanup {{{
51 int main(int argc
, char** argv
) {
52 /* opinionated defaults: */
53 op
.s
= &unicode_large_color
;
59 opterr
= 0; /* don't print message on unrecognized option */
60 while ((optget
= getopt (argc
, argv
, "+:hs:vbcm")) != -1) {
63 case 's': /* number of suits */
65 case '1': op
.m
= EASY
; break;
66 case '2': op
.m
= MEDIUM
; break;
67 case '4': op
.m
= NORMAL
; break;
71 case 'b': op
.s
= &unicode_large_mono
; break;
72 case 'c': op
.s
= &unicode_large_color
; break;
73 case 'm': op
.s
= &unicode_small_mono
; break; /* "mini" */
74 case 'h': default: goto error
;
76 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
84 signal_handler(SIGWINCH
); /* initialize window size */
90 case GAME_NEW
: goto newgame
;
92 print_table(NO_HI
, NO_HI
);
94 if (getchar()=='q') return 0;
96 case GAME_QUIT
: return 0;
101 /* clean undo (from previous game): */
108 switch (get_cmd(&from
, &to
, &opt
)) {
110 switch (action
[from
][to
](from
,to
,opt
)) {
112 case ERR
: visbell(); break;
113 case WON
: return GAME_WON
;
116 case CMD_HINT
: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
120 case ERR
: visbell(); break;
121 case WON
: return GAME_WON
;
124 case CMD_UNDO
: undo_pop(f
.u
); break;
125 case CMD_INVAL
: visbell(); break;
126 case CMD_NEW
: return GAME_NEW
;
127 case CMD_AGAIN
: //TODO: restart with same seed
128 case CMD_QUIT
: return GAME_QUIT
;
135 /* free undo data structures: */
140 // card games helper functions {{{
141 #define get_suit(card) \
142 ((card-1) % NUM_SUITS)
143 #define get_rank(card) \
144 ((card-1) / NUM_SUITS)
145 #define get_color(card) \
146 ((get_suit(card) ^ get_suit(card)>>1) & 1)
148 #define is_tableu(where) (where <= TAB_MAX)
150 int find_top(card_t
* pile
) {
152 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
155 int first_movable(card_t
* pile
) {
157 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
160 int turn_over(card_t
* pile
) {
161 int top
= find_top(pile
);
167 int check_won(void) {
168 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
169 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
173 int rank_next (card_t a
, card_t b
) {
174 return get_rank(a
) == get_rank(b
)-1;
176 int is_consecutive (card_t
* pile
, int pos
) {
177 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
178 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
181 /* ranks consecutive? */
182 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
183 /* color opposite? */
184 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
186 /* ranks consecutive? */
187 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
189 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
195 int is_movable(card_t
* pile
, int n
) {
197 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
199 int top
= find_top(pile
);
200 for (int i
= top
; i
>= 0; i
--) {
201 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
202 if (!is_consecutive(pile
, i
)) return 0;
203 if (i
== n
) return 1; /* card reached, must be movable */
210 // takeable actions {{{
212 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
213 card_t card
= f
.s
[f
.w
];
214 /* move stack one over, so there are no gaps in it: */
215 for (int i
= f
.w
; i
< f
.z
-1; i
++)
218 f
.w
--; /* make previous card visible again */
221 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
222 (void) to
; (void) opt
; /* don't need */
223 int top_from
= find_top(f
.t
[from
]);
224 to
= get_suit(f
.t
[from
][top_from
]);
225 int top_to
= find_top(f
.f
[to
]);
226 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
227 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
228 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
229 f
.t
[from
][top_from
] = NO_CARD
;
230 undo_push(from
, FOUNDATION
, to
,
231 turn_over(f
.t
[from
]));
232 if (check_won()) return WON
;
236 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
237 (void) from
; (void) to
; (void) opt
; /* don't need */
238 if (f
.w
< 0) return ERR
;
239 to
= get_suit(f
.s
[f
.w
]);
240 int top_to
= find_top(f
.f
[to
]);
241 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
242 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
243 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16, 0);//ugly encoding :|
244 f
.f
[to
][top_to
+1] = stack_take();
245 if (check_won()) return WON
;
250 int s2w(int from
, int to
, int opt
) { /* stock to waste */
251 (void) from
; (void) to
; (void) opt
; /* don't need */
252 if (f
.z
== 0) return ERR
;
254 if (f
.w
== f
.z
) f
.w
= -1;
257 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
258 (void) from
; (void) to
; (void) opt
; /* don't need */
259 if (f
.z
== 0) return ERR
;
261 if (f
.w
< -1) f
.w
= f
.z
-1;
264 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
265 (void) from
; /* don't need */
266 int top_to
= find_top(f
.t
[to
]);
268 int top_from
= find_top(f
.f
[from
]);
270 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
271 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
272 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
273 f
.f
[from
][top_from
] = NO_CARD
;
274 undo_push(FOUNDATION
, to
, from
, 0);
278 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
279 (void) from
; (void) opt
; /* don't need */
280 int top_to
= find_top(f
.t
[to
]);
281 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
282 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
283 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
284 undo_push(WASTE
, to
, f
.w
, 0);
285 f
.t
[to
][top_to
+1] = stack_take();
289 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
290 (void) opt
; /* don't need */
291 int top_to
= find_top(f
.t
[to
]);
292 int top_from
= find_top(f
.t
[from
]);
293 int count
= 0; //NOTE: could probably be factored out
294 for (int i
= top_from
; i
>=0; i
--) {
295 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
296 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
297 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
298 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
299 /* move cards [i..top_from] to their destination */
300 for (;i
<= top_from
; i
++) {
302 f
.t
[to
][top_to
] = f
.t
[from
][i
];
303 f
.t
[from
][i
] = NO_CARD
;
306 undo_push(from
, to
, count
,
307 turn_over(f
.t
[from
]));
311 return ERR
; /* no such move possible */
314 int remove_if_complete (int pileno
) { //cleanup!
315 card_t
* pile
= f
.t
[pileno
];
316 /* test if K...A complete; move to foundation if so */
317 int top_from
= find_top(pile
);
318 if (get_rank(pile
[top_from
]) != RANK_A
) return 0;
319 for (int i
= top_from
; i
>=0; i
--) {
320 if (!is_consecutive (pile
, i
)) return 0;
321 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
322 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
323 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
324 f
.f
[f
.w
][j
] = pile
[i
];
327 undo_push(pileno
, FOUNDATION
, f
.w
,
336 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
337 int top_from
= find_top(f
.t
[from
]);
338 int top_to
= find_top(f
.t
[to
]);
339 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
340 int count
= 0; //NOTE: could probably be factored out
342 for (int i
= top_from
; i
>= 0; i
--) {
343 if (!is_consecutive(f
.t
[from
], i
)) break;
345 /* is consecutive OR to empty pile and rank ok? */
346 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
347 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
348 for (;i
<= top_from
; i
++) {
350 f
.t
[to
][top_to
] = f
.t
[from
][i
];
351 f
.t
[from
][i
] = NO_CARD
;
354 undo_push(from
, to
, count
,
355 turn_over(f
.t
[from
]));
356 remove_if_complete(to
);
357 if (check_won()) return WON
;
362 return ERR
; /* no such move possible */
364 int s2t(int from
, int to
, int opt
) {
365 (void) from
; (void) to
; (void) opt
; /* don't need */
366 if (f
.z
<= 0) return ERR
; /* stack out of cards */
367 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
368 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
369 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
370 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
371 remove_if_complete(pile
);
372 if (check_won()) return WON
;
374 undo_push(STOCK
, TABLEU
, 1, 0); /*NOTE: puts 1 card on each tableu pile*/
377 int t2f(int from
, int to
, int opt
) {
378 (void) to
; (void) opt
; /* don't need */
379 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
380 return remove_if_complete(from
)?OK
:ERR
;
384 //TODO: allow joining to foundation in klondike
385 //TODO: which pile to take from should form the basis of CMD_HINT
387 int top_to
= find_top(f
.t
[to
]); //TODO: handle empty
391 int ok
:1; /* card to move in pile? */
392 int above
; /* number of movable cards above */
393 int below
; /* number of cards below ours */
394 int pos
; /* where the card to move is in the pile */
395 } r
[NUM_PILES
] = {{0}};
397 /* 1. rate each pile: */
398 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
399 r
[pile
].pos
= find_top(f
.t
[pile
]);
400 /* backtrack until we find a compatible-to-'to'-pile card: */
401 while (r
[pile
].pos
>= 0 && is_movable(f
.t
[pile
], r
[pile
].pos
)) {
402 int rankdiff
= get_rank(f
.t
[pile
][r
[pile
].pos
])
403 - get_rank(f
.t
[to
][top_to
]);
404 if (rankdiff
>= 0) break; /* past our card */
405 if (rankdiff
== -1 /* rank matches */
407 && get_color(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
408 != get_color(f
.t
[to
][top_to
])
410 && get_suit(f
.t
[pile
][r
[pile
].pos
]) /* color OK */
411 == get_suit(f
.t
[to
][top_to
])
415 for (int i
= r
[pile
].pos
; i
>= 0; i
--)
416 if (is_movable(f
.t
[pile
], i
-1))
426 /* 2. find optimal pile: (optimized for spider) */
427 for (int pile
= 0, above
= 99, turn
= 0, empty
= 0, below
= 99, e
=0,t
=0;
428 pile
< NUM_PILES
; pile
++) {
429 if (!r
[pile
].ok
) continue;
431 if ((e
=(r
[pile
].pos
== 0)) /* will become empty */
432 || ((t
=(f
.t
[pile
][r
[pile
].pos
-1] < 0)) && !empty
) /* will turn_over */
433 || (r
[pile
].above
< above
&& !empty
) /* less cards above */
434 || (r
[pile
].above
==above
&& r
[pile
].below
< below
&& !empty
&& !turn
)) { /* if tied, use shorter pile */
436 above
= r
[pile
].above
;
437 below
= r
[pile
].below
;
443 /* 3a. give up if nothing found: */
445 #ifdef KLONDIKE /* check if we can take from waste before giving up */
446 return w2t(WASTE
, to
, 0);
452 /* 3b. move cards over and return: */
454 return t2t(from
, to
, 0);
456 int bottom
= first_movable(f
.t
[from
]);
457 return t2t(from
, to
, get_rank(f
.t
[from
][bottom
]));
460 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
463 // keyboard input handling {{{
464 // cursor functions{{{
465 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
467 void cursor_left (struct cursor
* cursor
) {
468 if (is_tableu(cursor
->pile
)) {
469 if (cursor
->pile
> 0) cursor
->pile
--;
471 } else { /* stock/waste/foundation*/
472 switch (cursor
->pile
) {
473 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
475 if (cursor
->opt
<= 0)
476 cursor
->pile
= WASTE
;
482 void cursor_down (struct cursor
* cursor
) {
483 if (!is_tableu(cursor
->pile
)) {
484 switch (cursor
->pile
) {
485 case STOCK
: cursor
->pile
= TAB_1
; break;
486 case WASTE
: cursor
->pile
= TAB_2
; break;
488 cursor
->pile
= TAB_4
+ cursor
->opt
;
493 void cursor_up (struct cursor
* cursor
) {
494 if (is_tableu(cursor
->pile
)) {
495 switch (cursor
->pile
) { //ugly :|
496 case TAB_1
: cursor
->pile
= STOCK
; break;
497 case TAB_2
: cursor
->pile
= WASTE
; break;
498 case TAB_3
: cursor
->pile
= WASTE
; break;
499 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
500 cursor
->opt
=cursor
->pile
-TAB_4
;
501 cursor
->pile
= FOUNDATION
;
506 void cursor_right (struct cursor
* cursor
) {
507 if (is_tableu(cursor
->pile
)) {
508 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
510 switch (cursor
->pile
) {
511 case STOCK
: cursor
->pile
= WASTE
; break;
512 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
514 if (cursor
->opt
< NUM_SUITS
-1)
520 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
521 void cursor_left (struct cursor
* cursor
) {
522 if (cursor
->pile
> 0) cursor
->pile
--;
525 void cursor_down (struct cursor
* cursor
) {
526 int first
= first_movable(f
.t
[cursor
->pile
]);
527 int top
= find_top(f
.t
[cursor
->pile
]);
528 if (first
+ cursor
->opt
< top
)
531 void cursor_up (struct cursor
* cursor
) {
532 if (cursor
->opt
> 0) cursor
->opt
--;
534 void cursor_right (struct cursor
* cursor
) {
535 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
539 void cursor_to (struct cursor
* cursor
, int pile
) {
543 #pragma GCC diagnostic pop
545 int get_cmd (int* from
, int* to
, int* opt
) {
546 //TODO: escape sequences (mouse, cursor keys)
548 struct cursor inactive
= {-1,-1};
549 static struct cursor active
= {0,0};
550 active
.opt
= 0; /* always reset offset, but keep pile */
553 from_l
: print_table(&active
, &inactive
);
557 /* direct addressing: */
558 case '1': *from
= TAB_1
; break;
559 case '2': *from
= TAB_2
; break;
560 case '3': *from
= TAB_3
; break;
561 case '4': *from
= TAB_4
; break;
562 case '5': *from
= TAB_5
; break;
563 case '6': *from
= TAB_6
; break;
564 case '7': *from
= TAB_7
; break;
566 case '8': *from
= TAB_8
; break;
567 case '9': *from
= TAB_9
; break;
568 case '0': *from
= TAB_10
;break;
569 #elif defined KLONDIKE
570 case '9': *from
= WASTE
; break;
571 case '0': *from
= FOUNDATION
; break;
572 case '8': /* fallthrough */
574 case '\n': /* shortcut for dealing from stock */
578 /* cursor keys addressing: */
579 case 'h': cursor_left (&active
); goto from_l
;
580 case 'j': cursor_down (&active
); goto from_l
;
581 case 'k': cursor_up (&active
); goto from_l
;
582 case 'l': cursor_right(&active
); goto from_l
;
583 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
584 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
585 case 'M': cursor_to(&active
,TAB_MAX
/2); goto from_l
; /* center tableu */
586 //TODO: real cursor keys, home/end
587 case ' ': /* continue with second cursor */
589 if (*from
== STOCK
) {
594 *opt
= active
.opt
; /* when FOUNDATION */
601 fprintf (stderr
, ":");
602 raw_mode(0); /* turn on echo */
603 fgets (buf
, 256, stdin
);
606 case 'q': return CMD_QUIT
;
607 case 'n': return CMD_NEW
;
608 case 'r': return CMD_AGAIN
;
609 default: return CMD_INVAL
;
613 if (*to
> TAB_MAX
) return CMD_INVAL
;
615 case 'K': /* fallthrough */
616 case '?': return CMD_HINT
;
617 case 'u': return CMD_UNDO
;
618 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
619 default: return CMD_INVAL
;
621 inactive
.pile
= *from
; /* for direct addressing highlighting */
622 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
625 to_l
: print_table(&active
, &inactive
);
629 case 'h': cursor_left (&active
); goto to_l
;
630 case 'j': cursor_down (&active
); goto to_l
;
631 case 'k': cursor_up (&active
); goto to_l
;
632 case 'l': cursor_right(&active
); goto to_l
;
633 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
634 case 'L': cursor_to(&active
,TAB_MAX
); goto to_l
;
635 case 'M': cursor_to(&active
,TAB_MAX
/2); goto to_l
;
636 case 'J': /* fallthrough; just join selected pile */
639 break; /* continues with the foundation/empty tableu check */
640 case 'K': /* fallthrough */
641 case '?': return CMD_HINT
;
642 case 'u': return CMD_NONE
; /* cancel selection */
643 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
645 if (t
< '0' || t
> '9') return CMD_INVAL
;
658 if (*from
== FOUNDATION
) {
659 int top
= find_top(f
.t
[*to
]);
660 if (top
< 0) return CMD_INVAL
;
661 int color
= get_color(f
.t
[*to
][top
]);
662 int choice_1
= 1-color
; /* selects piles of */
663 int choice_2
= 2+color
; /* the opposite color */
664 int top_c1
= find_top(f
.f
[choice_1
]);
665 int top_c2
= find_top(f
.f
[choice_2
]);
667 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
668 && top_c1
>= 0 ) << 0
669 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
670 && top_c2
>= 0 ) << 1) {
671 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
672 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
673 case (1<<1 | 1<<0): /* both, ask user which to pick from */
674 printf ("take from (1-4): "); fflush (stdout
);
675 *opt
= getchar() - '1';
676 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
678 default: return CMD_INVAL
; /* none matched */
680 /* `opt` is the foundation index (0..3) */
683 /* moving to empty tableu? */
684 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
685 int bottom
= first_movable(f
.t
[*from
]);
686 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
687 *opt
= get_rank(f
.t
[*from
][bottom
+ inactive
.opt
]);
690 int top
= find_top(f
.t
[*from
]);
691 if (top
< 0) return CMD_INVAL
;
692 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
693 *opt
= get_rank(f
.t
[*from
][top
]);
694 } else { /* only ask the user if it's unclear: */
695 printf ("\rup to ([a23456789xjqk] or space/return): ");
698 case ' ': *opt
= get_rank(f
.t
[*from
][top
]); break;
699 case'\n': *opt
= get_rank(f
.t
[*from
][bottom
]); break;
700 case 'a': case 'A': *opt
= RANK_A
; break;
701 case '0': /* fallthrough */
702 case 'x': case 'X': *opt
= RANK_X
; break;
703 case 'j': case 'J': *opt
= RANK_J
; break;
704 case 'q': case 'Q': *opt
= RANK_Q
; break;
705 case 'k': case 'K': *opt
= RANK_K
; break;
706 default: *opt
-= '1';
708 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
710 /* `opt` is the rank of the highest card to move */
717 // shuffling and dealing {{{
719 f
= (const struct playfield
){0}; /* clear playfield */
720 card_t deck
[DECK_SIZE
*NUM_DECKS
];
721 int avail
= DECK_SIZE
*NUM_DECKS
;
722 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
724 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
725 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
726 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
727 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
730 srandom (time(NULL
));
731 long seed
= time(NULL
);
733 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
734 int j
= random() % (i
+1);
735 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
739 for (int i
= 0; i
< NUM_PILES
; i
++) {
741 int closed
= i
; /* pile n has n closed cards, then 1 open */
743 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
745 /* face down cards are negated: */
746 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
747 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
749 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
750 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
752 f
.w
= -1; /* @start: nothing on waste */
754 f
.w
= 0; /* number of used foundations */
757 f
.u
= &undo_sentinel
;
761 // screen drawing routines {{{
762 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
763 if (bold
&& op
.s
== &unicode_large_color
){//ARGH! awful hack for bold with faint
764 int offset
= str
[3]==017?16:str
[4]==017?17:0;
765 printf ("%s%s%s""%.*s%s%s""%s%s%s",
766 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
767 offset
, str
, bold
?"\033[1m":"", str
+offset
,
768 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
771 printf ("%s%s%s%s%s%s%s",
772 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
774 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
776 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
777 printf("\033[2J\033[H"); /* clear screen, reset cursor */
779 /* print stock, waste and foundation: */
780 for (int line
= 0; line
< op
.s
->height
; line
++) {
782 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
783 (f
.w
< f
.z
-1)?op
.s
->facedown
784 :op
.s
->placeholder
)[line
]);
786 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
787 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
788 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
789 :op
.s
->placeholder
)[line
]);
790 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
792 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
793 int card
= find_top(f
.f
[pile
]);
794 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
795 inactive
->pile
==FOUNDATION
&& (
796 /* cursor addr. || direct addr. */
797 inactive
->opt
==pile
|| inactive
->opt
< 0
799 (card
< 0)?op
.s
->placeholder
[line
]
800 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
806 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
807 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
808 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
809 int spacer_to
= NUM_PILES
*op
.s
->width
-
810 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
811 for (int line
= 0; line
< op
.s
->height
; line
++) {
812 /* available stock: */
813 for (int i
= f
.z
/10; i
; i
--) {
814 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
815 else printf ("%s", op
.s
->halfstack
[line
]);
818 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
819 /* foundation (overlapping): */
820 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) { //TODO: print in revrse order (otherwise new piles get put 'below' older ones)
821 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
822 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
823 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
830 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
831 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
834 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
835 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
836 && offset[pile] > cursor->opt)
837 #define TOP_HI(cursor) (cursor->pile == pile && movable \
838 && offset[pile]-1 == cursor->opt)
839 #define INC_OFFSET if (movable) offset[pile]++
841 /* print tableu piles: */
842 int row
[NUM_PILES
] = {0};
843 int line
[NUM_PILES
]= {0};
844 int label
[NUM_PILES
]={0};
846 int did_placeholders
= 0;
849 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
850 card_t card
= f
.t
[pile
][row
[pile
]];
851 card_t next
= f
.t
[pile
][row
[pile
]+1];
852 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
853 int empty
= !card
&& row
[pile
] == 0;
855 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
856 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
857 :(card
<0)?op
.s
->facedown
861 int extreme_overlap
= ( 3 /* spacer, labels, status */
862 + 2 * op
.s
->height
/* stock, top tableu card */
863 + find_top(f
.t
[pile
]) * op
.s
->overlap
) >op
.w
[0];
864 /* normal overlap: */
865 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
866 /* extreme overlap on closed cards: */
867 || (extreme_overlap
&&
869 f
.t
[pile
][row
[pile
]] < 0 &&
870 f
.t
[pile
][row
[pile
]+1] <0)
871 /* extreme overlap on sequences: */
872 || (extreme_overlap
&&
873 !TOP_HI(active
) && /*always show top selected card*/
874 line
[pile
] >= 1 && row
[pile
] > 0 &&
875 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
876 is_consecutive (f
.t
[pile
], row
[pile
]) &&
877 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
878 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
885 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
887 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
889 line_had_card
|= !!card
;
890 did_placeholders
|= row
[pile
] > 0;
893 } while (line_had_card
|| !did_placeholders
);
896 void visbell (void) {
897 printf ("\033[?5h"); fflush (stdout
);
899 printf ("\033[?5l"); fflush (stdout
);
901 void win_anim(void) {
902 printf ("\033[?25l"); /* hide cursor */
904 /* set cursor to random location */
905 int row
= 1+random()%(24-op
.s
->width
);
906 int col
= 1+random()%(80-op
.s
->height
);
908 /* draw random card */
909 int face
= 1 + random() % 52;
910 for (int l
= 0; l
< op
.s
->height
; l
++) {
911 printf ("\033[%d;%dH", row
+l
, col
);
912 printf ("%s", op
.s
->card
[face
][l
]);
916 /* exit on keypress */
917 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
918 if (poll (&p
, 1, 80)) goto fin
;
921 printf ("\033[?25h"); /* show cursor */
927 void undo_push (int _f
, int t
, int n
, int o
) {
928 struct undo
* new = malloc(sizeof(struct undo
));
938 void undo_pop (struct undo
* u
) {
939 if (u
== &undo_sentinel
) return;
942 if (u
->f
== FOUNDATION
) {
943 /* foundation -> tableu */
944 int top_f
= find_top(f
.f
[u
->n
]);
945 int top_t
= find_top(f
.t
[u
->t
]);
946 f
.f
[u
->n
][top_f
+1] = f
.t
[u
->t
][top_t
];
947 f
.t
[u
->t
][top_t
] = NO_CARD
;
948 } else if (u
->f
== WASTE
&& u
->t
== FOUNDATION
) {
949 /* waste -> foundation */
950 /* split u->n into wst and fnd: */
951 int wst
= u
->n
& 0xffff;
952 int fnd
= u
->n
>> 16;
953 /* move stock cards one position up to make room: */
954 for (int i
= f
.z
; i
>= wst
; i
--) f
.s
[i
+1] = f
.s
[i
];
955 /* move one card from foundation to waste: */
956 int top
= find_top(f
.f
[fnd
]);
957 f
.s
[wst
] = f
.f
[fnd
][top
];
958 f
.f
[fnd
][top
] = NO_CARD
;
961 } else if (u
->f
== WASTE
) {
962 /* waste -> tableu */
963 /* move stock cards one position up to make room: */
964 for (int i
= f
.z
; i
>= u
->n
; i
--) f
.s
[i
+1] = f
.s
[i
];
965 /* move one card from tableu to waste: */
966 int top
= find_top(f
.t
[u
->t
]);
967 f
.s
[u
->n
] = f
.t
[u
->t
][top
];
968 f
.t
[u
->t
][top
] = NO_CARD
;
971 } else if (u
->t
== FOUNDATION
) {
972 /* tableu -> foundation */
973 int top_f
= find_top(f
.t
[u
->f
]);
974 int top_t
= find_top(f
.f
[u
->n
]);
975 /* close topcard if previous action caused turn_over(): */
976 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
977 /* move one card from foundation to tableu: */
978 f
.t
[u
->f
][top_f
+1] = f
.f
[u
->n
][top_t
];
979 f
.f
[u
->n
][top_t
] = NO_CARD
;
981 /* tableu -> tableu */
982 int top_f
= find_top(f
.t
[u
->f
]);
983 int top_t
= find_top(f
.t
[u
->t
]);
984 /* close topcard if previous action caused turn_over(): */
985 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
986 /* move n cards from tableu[f] to tableu[t]: */
987 for (int i
= 0; i
< u
->n
; i
++) {
988 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
989 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
994 /* stock -> tableu */
995 /*remove a card from each pile and put it back onto the stock:*/
996 for (int pile
= NUM_PILES
-1; pile
>= 0; pile
--) {
997 int top
= find_top(f
.t
[pile
]);
998 f
.s
[f
.z
++] = f
.t
[pile
][top
];
999 f
.t
[pile
][top
] = NO_CARD
;
1001 } else if (u
->t
== FOUNDATION
) {
1002 /* tableu -> foundation */
1003 int top
= find_top(f
.t
[u
->f
]);
1004 /* close topcard if previous action caused turn_over(): */
1005 if (u
->o
) f
.t
[u
->f
][top
] *= -1;
1006 /* append cards from foundation to tableu */
1007 for (int i
= RANK_K
; i
>= RANK_A
; i
--) {
1008 f
.t
[u
->f
][++top
] = f
.f
[u
->n
][i
];
1009 f
.f
[u
->n
][i
] = NO_CARD
;
1011 f
.w
--; /* decrement complete-foundation-counter */
1014 /* tableu -> tableu */
1015 int top_f
= find_top(f
.t
[u
->f
]);
1016 int top_t
= find_top(f
.t
[u
->t
]);
1017 /* close topcard if previous action caused turn_over(): */
1018 if (u
->o
) f
.t
[u
->f
][top_f
] *= -1;
1019 /* move n cards from tableu[f] to tableu[t]: */
1020 for (int i
= 0; i
< u
->n
; i
++) {
1021 f
.t
[u
->f
][top_f
+u
->n
-i
] = f
.t
[u
->t
][top_t
-i
];
1022 f
.t
[u
->t
][top_t
-i
] = NO_CARD
;
1031 void free_undo (struct undo
* u
) {
1032 while (u
&& u
!= &undo_sentinel
) {
1040 // initialization stuff {{{
1041 void screen_setup (int enable
) {
1044 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1045 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1046 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
1048 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
1049 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1054 void raw_mode(int enable
) {
1055 static struct termios saved_term_mode
;
1056 struct termios raw_term_mode
;
1059 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
1060 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
1061 raw_term_mode
= saved_term_mode
;
1062 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
1063 raw_term_mode
.c_cc
[VMIN
] = 1 ;
1064 raw_term_mode
.c_cc
[VTIME
] = 0;
1065 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
1067 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
1071 void signal_handler (int signum
) {
1077 print_table(NO_HI
, NO_HI
);
1079 case SIGINT
: //TODO: don't exit; just warn like vim does
1082 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
1088 void signal_setup(void) {
1089 struct sigaction saction
;
1091 saction
.sa_handler
= signal_handler
;
1092 sigemptyset(&saction
.sa_mask
);
1093 saction
.sa_flags
= 0;
1094 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
1098 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
1102 if (sigaction(SIGWINCH
, &saction
, NULL
) < 0) {
1103 perror ("SIGWINCH");
1109 //vim: foldmethod=marker