]> git.gir.st - solVItaire.git/blob - sol.c
'join' with right mouse, update keybindings help
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE /* for getopt, sigaction, usleep */
2 #include <poll.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/ioctl.h>
7 #include <time.h>
8 #include <termios.h>
9 #include <unistd.h>
10
11 #include "sol.h"
12 #include "schemes.h"
13
14 struct playfield f;
15 struct opts op;
16
17 // action table {{{
18 /* stores a function pointer for every takeable action; called by game loop */
19 int (*action[NUM_PLACES][10])(int,int,int) = {
20 #ifdef KLONDIKE
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 },
32 #elif defined SPIDER
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 #endif
46 };
47 // }}}
48
49 // argv parsing, game loops, cleanup {{{
50 int main(int argc, char** argv) {
51 /* opinionated defaults: */
52 op.s = &unicode_large_color;
53 #ifdef SPIDER
54 op.m = MEDIUM;
55 #endif
56
57 int optget;
58 opterr = 0; /* don't print message on unrecognized option */
59 while ((optget = getopt (argc, argv, "+:hs:vbcm")) != -1) {
60 switch (optget) {
61 #ifdef SPIDER
62 case 's': /* number of suits */
63 switch (optarg[0]) {
64 case '1': op.m = EASY; break;
65 case '2': op.m = MEDIUM; break;
66 case '4': op.m = NORMAL; break;
67 default: goto error;
68 } break;
69 #endif
70 case 'b': op.s = &unicode_large_mono; break;
71 case 'c': op.s = &unicode_large_color; break;
72 case 'm': op.s = &unicode_small_mono; break; /* "mini" */
73 case 'h': default: goto error;
74 error:
75 fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
76 return optget != 'h';
77 }
78 }
79
80 signal_setup();
81 atexit (*quit);
82
83 signal_handler(SIGWINCH); /* initialize window size */
84
85 newgame:
86 screen_setup(1);
87
88 switch(sol()) {
89 case GAME_NEW: goto newgame;
90 case GAME_WON:
91 print_table(NO_HI, NO_HI);
92 win_anim();
93 if (getch(NULL)=='q') return 0;
94 goto newgame;
95 case GAME_QUIT: return 0;
96 }
97 }
98
99 int sol(void) {
100 int ret;
101 long seed = time(NULL);
102 restart:
103 free_undo(f.u);
104 deal(seed);
105
106 int from, to, opt;
107 for(;;) {
108 switch (get_cmd(&from, &to, &opt)) {
109 case CMD_MOVE:
110 ret = action[from][to](from,to,opt);
111 if (ret == ERR) /* try again with from/to swapped: */
112 ret = action[to][from](to,from,opt);
113 switch (ret) {
114 case OK: break;
115 case ERR: visbell(); break;
116 case WON: return GAME_WON;
117 }
118 break;
119 case CMD_JOIN:
120 switch (join(to)) {
121 case OK: break;
122 case ERR: visbell(); break;
123 case WON: return GAME_WON;
124 }
125 break;
126 case CMD_HINT: break;//TODO: show a possible (and sensible) move. if possible, involve active cursor
127 case CMD_UNDO: undo_pop(f.u); break;
128 case CMD_INVAL: visbell(); break;
129 case CMD_NEW: return GAME_NEW;
130 case CMD_AGAIN: goto restart;
131 case CMD_QUIT: return GAME_QUIT;
132 }
133 }
134 }
135
136 void quit(void) {
137 screen_setup(0);
138 free_undo(f.u);
139 }
140 //}}}
141
142 // card games helper functions {{{
143 #define get_suit(card) \
144 ((card-1) % NUM_SUITS)
145 #define get_rank(card) \
146 ((card-1) / NUM_SUITS)
147 #define get_color(card) \
148 ((get_suit(card) ^ get_suit(card)>>1) & 1)
149
150 #define is_tableu(where) (where <= TAB_MAX)
151
152 int find_top(card_t* pile) {
153 int i;
154 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
155 return i;
156 }
157 int first_movable(card_t* pile) {
158 int i = 0;
159 for (;pile[i] && !is_movable(pile, i); i++);
160 return i;
161 }
162 int turn_over(card_t* pile) {
163 int top = find_top(pile);
164 if (pile[top] < 0) {
165 pile[top] *= -1;
166 return 1;
167 } else return 0;
168 }
169 int check_won(void) {
170 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
171 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
172
173 return 1;
174 }
175 int rank_next (card_t a, card_t b) {
176 return get_rank(a) == get_rank(b)-1;
177 }
178 int is_consecutive (card_t* pile, int pos) {
179 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
180 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
181
182 #ifdef KLONDIKE
183 /* ranks consecutive? */
184 if (!rank_next(pile[pos+1], pile[pos])) return 0;
185 /* color opposite? */
186 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
187 #elif defined SPIDER
188 /* ranks consecutive? */
189 if (!rank_next(pile[pos+1], pile[pos])) return 0;
190 /* same suit? */
191 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
192 #endif
193
194 return 1;
195 }
196
197 int is_movable(card_t* pile, int n) {
198 #ifdef KLONDIKE
199 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
200 #elif defined SPIDER
201 int top = find_top(pile);
202 for (int i = top; i >= 0; i--) {
203 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
204 if (!is_consecutive(pile, i)) return 0;
205 if (i == n) return 1; /* card reached, must be movable */
206 }
207 return 0;
208 #endif
209 }
210 //}}}
211
212 // takeable actions {{{
213 #ifdef KLONDIKE
214 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
215 card_t card = f.s[f.w];
216 /* move stack one over, so there are no gaps in it: */
217 for (int i = f.w; i < f.z-1; i++)
218 f.s[i] = f.s[i+1];
219 f.z--;
220 f.w--; /* make previous card visible again */
221 return card;
222 }
223 int t2f(int from, int to, int opt) { /* tableu to foundation */
224 (void) to; (void) opt; /* don't need */
225 int top_from = find_top(f.t[from]);
226 to = get_suit(f.t[from][top_from]);
227 int top_to = find_top(f.f[to]);
228 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
229 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
230 f.f[to][top_to+1] = f.t[from][top_from];
231 f.t[from][top_from] = NO_CARD;
232 undo_push(from, FOUNDATION, to,
233 turn_over(f.t[from]));
234 if (check_won()) return WON;
235 return OK;
236 } else return ERR;
237 }
238 int w2f(int from, int to, int opt) { /* waste to foundation */
239 (void) from; (void) to; (void) opt; /* don't need */
240 if (f.w < 0) return ERR;
241 to = get_suit(f.s[f.w]);
242 int top_to = find_top(f.f[to]);
243 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
244 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
245 undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
246 f.f[to][top_to+1] = stack_take();
247 if (check_won()) return WON;
248 return OK;
249 } else return ERR;
250
251 }
252 int s2w(int from, int to, int opt) { /* stock to waste */
253 (void) from; (void) to; (void) opt; /* don't need */
254 if (f.z == 0) return ERR;
255 f.w++;
256 if (f.w == f.z) f.w = -1;
257 return OK;
258 }
259 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
260 (void) from; (void) to; (void) opt; /* don't need */
261 if (f.z == 0) return ERR;
262 f.w--;
263 if (f.w < -1) f.w = f.z-1;
264 return OK;
265 }
266 int f2t(int from, int to, int opt) { /* foundation to tableu */
267 (void) from; /* don't need */
268 int top_to = find_top(f.t[to]);
269 from = opt;
270 int top_from = find_top(f.f[from]);
271
272 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
273 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
274 f.t[to][top_to+1] = f.f[from][top_from];
275 f.f[from][top_from] = NO_CARD;
276 undo_push(FOUNDATION, to, from, 0);
277 return OK;
278 } else return ERR;
279 }
280 int w2t(int from, int to, int opt) { /* waste to tableu */
281 (void) from; (void) opt; /* don't need */
282 int top_to = find_top(f.t[to]);
283 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
284 && (rank_next(f.s[f.w], f.t[to][top_to])))
285 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
286 undo_push(WASTE, to, f.w, 0);
287 f.t[to][top_to+1] = stack_take();
288 return OK;
289 } else return ERR;
290 }
291 int t2t(int from, int to, int opt) { /* tableu to tableu */
292 (void) opt; /* don't need */
293 int top_to = find_top(f.t[to]);
294 int top_from = find_top(f.t[from]);
295 int count = 0; //NOTE: could probably be factored out
296 for (int i = top_from; i >=0; i--) {
297 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
298 && (rank_next(f.t[from][i], f.t[to][top_to]))
299 && f.t[from][i] > NO_CARD) /* card face up? */
300 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
301 /* move cards [i..top_from] to their destination */
302 for (;i <= top_from; i++) {
303 top_to++;
304 f.t[to][top_to] = f.t[from][i];
305 f.t[from][i] = NO_CARD;
306 count++;
307 }
308 undo_push(from, to, count,
309 turn_over(f.t[from]));
310 return OK;
311 }
312 }
313 return ERR; /* no such move possible */
314 }
315 #elif defined SPIDER
316 int remove_if_complete (int pileno) { //cleanup!
317 card_t* pile = f.t[pileno];
318 /* test if K...A complete; move to foundation if so */
319 int top_from = find_top(pile);
320 if (get_rank(pile[top_from]) != RANK_A) return 0;
321 for (int i = top_from; i>=0; i--) {
322 if (!is_consecutive (pile, i)) return 0;
323 if (i+RANK_K == top_from /* if ace to king: remove it */
324 && get_rank(pile[top_from-RANK_K]) == RANK_K) {
325 for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
326 f.f[f.w][j] = pile[i];
327 pile[i] = NO_CARD;
328 }
329 undo_push(pileno, FOUNDATION, f.w,
330 turn_over(pile));
331 f.w++;
332 return 1;
333 }
334 }
335
336 return 0;
337 }
338 int t2t(int from, int to, int opt) { //in dire need of cleanup
339 int top_from = find_top(f.t[from]);
340 int top_to = find_top(f.t[to]);
341 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
342 int count = 0; //NOTE: could probably be factored out
343
344 for (int i = top_from; i >= 0; i--) {
345 if (!is_consecutive(f.t[from], i)) break;
346
347 /* is consecutive OR to empty pile and rank ok? */
348 if (rank_next(f.t[from][i], f.t[to][top_to])
349 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
350 for (;i <= top_from; i++) {
351 top_to++;
352 f.t[to][top_to] = f.t[from][i];
353 f.t[from][i] = NO_CARD;
354 count++;
355 }
356 undo_push(from, to, count,
357 turn_over(f.t[from]));
358 remove_if_complete(to);
359 if (check_won()) return WON;
360 return OK;
361 }
362 }
363
364 return ERR; /* no such move possible */
365 }
366 int s2t(int from, int to, int opt) {
367 (void) from; (void) to; (void) opt; /* don't need */
368 if (f.z <= 0) return ERR; /* stack out of cards */
369 for (int pile = 0; pile < NUM_PILES; pile++)
370 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
371 for (int pile = 0; pile < NUM_PILES; pile++) {
372 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
373 remove_if_complete(pile);
374 if (check_won()) return WON;
375 }
376 undo_push(STOCK, TABLEU, 1, 0);/*NOTE: puts 1 card on each tableu pile*/
377 return OK;
378 }
379 int t2f(int from, int to, int opt) {
380 (void) to; (void) opt; /* don't need */
381 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
382 return remove_if_complete(from)?OK:ERR;
383 }
384 #endif
385 //TODO: generalize prediction engine for CMD_HINT
386 #ifdef KLONDIKE
387 #define would_complete(pile) 0
388 #elif defined SPIDER
389 #define would_complete(pile) \
390 (get_rank(f.t[pile][r[pile].top]) == RANK_A \
391 && get_rank(f.t[to][bottom_to]) == RANK_K)
392 #endif
393 #define would_turn(pile) \
394 (f.t[pile][r[pile].pos-1] < 0)
395 #define would_empty(pile) \
396 (r[pile].pos == 0)
397
398 int join(int to) {
399 int top_to = find_top(f.t[to]);
400 #ifdef SPIDER
401 int bottom_to = first_movable(f.t[to]);
402 #endif
403
404 #ifdef KLONDIKE
405 if (to == FOUNDATION) {
406 int status = ERR;
407 for (int i = 0; i <= TAB_MAX; i++)
408 switch ((i?t2f:w2f)(i-1, FOUNDATION, 0)) {
409 case WON: return WON;
410 case OK: status = OK;
411 case ERR: /* nop */;
412 }
413 return status;
414 }
415
416 if (top_to < 0) { /* move a king to empty pile: */
417 for (int i = 0; i < TAB_MAX; i++) {
418 if (f.t[i][0] < 0) /* i.e. would turn? */ //TODO: this calculation is wrong!
419 //DOES NOT FIRE when king is in the middle
420 if (t2t(i, to, 0) == OK) return OK;
421 }
422 return w2t(WASTE, to, 0);
423 }
424 #endif
425
426 struct rating {
427 int ok:1; /* card to move in pile? */
428 int above; /* number of movable cards above */
429 int below; /* number of cards below ours */
430 int pos; /* where the card to move is in the pile */
431 int top; /* find_top() */
432 } r[NUM_PILES] = {{0}};
433 int complete = 0;/* SPIDER: true if any pile would complete a stack */
434 int turn = 0; /* SPIDER: true if any pile would turn_over */
435 int empty = 0; /* true if any pile would become empty */
436
437 /* 1. rate each pile: */
438 #ifdef SPIDER
439 if (top_to < 0) {
440 for (int pile = 0; pile < NUM_PILES; pile++) {
441 if (pile == to) continue;
442 int top = find_top(f.t[pile]);
443 int bottom = first_movable(f.t[pile]);
444 r[pile].pos = bottom; /* need for would_empty */
445
446 if (top < 0) continue; /* no cards to move */
447 if (would_empty(pile)) continue; /* doesn't help */
448
449 r[pile].ok++;
450 r[pile].above = 0; /* always take as many as possible */
451 r[pile].below = top - bottom;
452 r[pile].top = top;
453 complete |= would_complete(pile); /* never happens */
454 turn |= would_turn(pile);
455 empty |= would_empty(pile);
456 }
457 } else
458 #endif
459 for (int pile = 0; pile < NUM_PILES; pile++) {
460 r[pile].top = r[pile].pos = find_top(f.t[pile]);
461 /* backtrack until we find a compatible-to-'to'-pile card: */
462 while (r[pile].pos >= 0 && is_movable(f.t[pile], r[pile].pos)) {
463 int rankdiff = get_rank(f.t[pile][r[pile].pos])
464 - get_rank(f.t[to][top_to]);
465 if (rankdiff >= 0) break; /* past our card */
466 if (rankdiff == -1 /* rank matches */
467 #ifdef KLONDIKE
468 && get_color(f.t[pile][r[pile].pos]) /* color OK */
469 != get_color(f.t[to][top_to])
470 #elif defined SPIDER
471 && get_suit(f.t[pile][r[pile].pos]) /* color OK */
472 == get_suit(f.t[to][top_to])
473 #endif
474 ) {
475 r[pile].ok++;
476 complete |= would_complete(pile);
477 turn |= would_turn(pile);
478 empty |= would_empty(pile);
479 for (int i = r[pile].pos; i >= 0; i--)
480 if (is_movable(f.t[pile], i-1))
481 r[pile].above++;
482 else break;
483 break;
484 }
485 r[pile].pos--;
486 r[pile].below++;
487 }
488 }
489
490 /* 2. find optimal pile: (optimized for spider) */
491 //todo: in spider, prefer longest piles if above==0 (faster completions)
492 int from = -1;
493 for (int pile = 0, above = 99, below = 99; pile < NUM_PILES; pile++) {
494 if (!r[pile].ok) continue;
495 /* don't bother if another pile would be better: prefer ... */
496 /* ... to complete a stack: */
497 if (!would_complete(pile) && complete) continue;
498 /* ... emptying piles: */
499 if (!would_empty(pile) && empty && !complete) continue;
500 /* ... to turn_over: */
501 if (!would_turn(pile) && turn && !complete && !empty) continue;
502 /* ... not to rip apart too many cards: */
503 if (r[pile].above > above) continue;
504 /* if tied, prefer ... */
505 if (r[pile].above == above
506 /* ... larger pile if destination is empty */
507 && (top_to < 0? r[pile].below < below
508 /* ... shorter pile otherwise */
509 : r[pile].below > below))
510 continue;
511
512 from = pile;
513 above = r[pile].above;
514 below = r[pile].below;
515 }
516
517 /* 3. move cards over and return: */
518 #ifdef KLONDIKE
519 /* prefer waste if it wouldn't turn_over: */
520 if (!turn && w2t(WASTE, to, 0) == OK)
521 return OK;
522 if (from < 0) /* nothing found */
523 return ERR;
524 return t2t(from, to, 0);
525 #elif defined SPIDER
526 if (from < 0) /* nothing found */
527 return ERR;
528 int bottom = first_movable(f.t[from]);
529 return t2t(from, to, get_rank(f.t[from][bottom]));
530 #endif
531 }
532 #undef would_empty
533 #undef would_turn
534 #undef would_complete
535 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
536 // }}}
537
538 // keyboard input handling {{{
539 // cursor functions{{{
540 #ifdef KLONDIKE
541 void cursor_left (struct cursor* cursor) {
542 if (is_tableu(cursor->pile)) {
543 if (cursor->pile > 0) cursor->pile--;
544 cursor->opt = 0;
545 } else { /* stock/waste/foundation*/
546 switch (cursor->pile) {
547 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
548 case FOUNDATION:
549 if (cursor->opt <= 0)
550 cursor->pile = WASTE;
551 else
552 cursor->opt--;
553 }
554 }
555 }
556 void cursor_down (struct cursor* cursor) {
557 if (!is_tableu(cursor->pile)) {
558 switch (cursor->pile) {
559 case STOCK: cursor->pile = TAB_1; break;
560 case WASTE: cursor->pile = TAB_2; break;
561 case FOUNDATION:
562 cursor->pile = TAB_4 + cursor->opt;
563 }
564 cursor->opt = 0;
565 }
566 }
567 void cursor_up (struct cursor* cursor) {
568 if (is_tableu(cursor->pile)) {
569 switch (cursor->pile) { //ugly :|
570 case TAB_1: cursor->pile = STOCK; break;
571 case TAB_2: cursor->pile = WASTE; break;
572 case TAB_3: cursor->pile = WASTE; break;
573 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
574 cursor->opt=cursor->pile-TAB_4;
575 cursor->pile = FOUNDATION;
576 break;
577 }
578 }
579 }
580 void cursor_right (struct cursor* cursor) {
581 if (is_tableu(cursor->pile)) {
582 if (cursor->pile < TAB_MAX) cursor->pile++;
583 } else {
584 switch (cursor->pile) {
585 case STOCK: cursor->pile = WASTE; break;
586 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
587 case FOUNDATION:
588 if (cursor->opt < NUM_SUITS-1)
589 cursor->opt++;
590 }
591 }
592 }
593 #elif defined SPIDER
594 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
595 void cursor_left (struct cursor* cursor) {
596 if (cursor->pile > 0) cursor->pile--;
597 cursor->opt = 0;
598 }
599 void cursor_down (struct cursor* cursor) {
600 int first = first_movable(f.t[cursor->pile]);
601 int top = find_top(f.t[cursor->pile]);
602 if (first + cursor->opt < top)
603 cursor->opt++;
604 }
605 void cursor_up (struct cursor* cursor) {
606 if (cursor->opt > 0) cursor->opt--;
607 }
608 void cursor_right (struct cursor* cursor) {
609 if (cursor->pile < TAB_MAX) cursor->pile++;
610 cursor->opt = 0;
611 }
612 #endif
613 void cursor_to (struct cursor* cursor, int pile) {
614 cursor->pile = pile;
615 cursor->opt = 0;
616 }
617 int set_mouse(int pile, int* main, int* opt) {
618 if (pile < 0) return 1;
619 *main = pile;
620 #ifdef KLONDIKE
621 if (pile >= FOUNDATION)
622 *main = FOUNDATION,
623 *opt = pile - FOUNDATION;
624 #elif defined SPIDER
625 (void)opt;
626 #endif
627 return 0;
628 }
629 //}}}
630 int get_cmd (int* from, int* to, int* opt) {
631 int _f, t;
632 unsigned char mouse[6] = {0}; /* must clear [3]! */
633 struct cursor inactive = {-1,-1};
634 static struct cursor active = {0,0};
635 active.opt = 0; /* always reset offset, but keep pile */
636
637 /***/
638 from_l: print_table(&active, &inactive);
639 _f = getch(mouse);
640
641 switch (_f) {
642 /* direct addressing: */
643 case '1': *from = TAB_1; break;
644 case '2': *from = TAB_2; break;
645 case '3': *from = TAB_3; break;
646 case '4': *from = TAB_4; break;
647 case '5': *from = TAB_5; break;
648 case '6': *from = TAB_6; break;
649 case '7': *from = TAB_7; break;
650 #ifdef SPIDER
651 case '8': *from = TAB_8; break;
652 case '9': *from = TAB_9; break;
653 case '0': *from = TAB_10;break;
654 #elif defined KLONDIKE
655 case '9': *from = WASTE; break;
656 case '0': *from = FOUNDATION; break;
657 case '8': /* fallthrough */
658 #endif
659 case '\n': *from = STOCK; break;
660 /* cursor keys addressing: */
661 case KEY_LEFT:
662 case 'h': cursor_left (&active); goto from_l;
663 case KEY_DOWN:
664 case 'j': cursor_down (&active); goto from_l;
665 case KEY_UP:
666 case 'k': cursor_up (&active); goto from_l;
667 case KEY_RIGHT:
668 case 'l': cursor_right(&active); goto from_l;
669 case KEY_HOME:
670 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
671 case KEY_END:
672 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
673 case KEY_INS:
674 case 'M': cursor_to(&active,TAB_MAX/2); goto from_l; /* center tableu */
675 case ' ': /* continue with second cursor */
676 *from = active.pile;
677 #ifdef KLONDIKE
678 *opt = active.opt; /* when FOUNDATION */
679 #endif
680 inactive = active;
681 break;
682 /* mouse addressing: */
683 case MOUSE_MIDDLE: return CMD_NONE;
684 case MOUSE_RIGHT:
685 if (set_mouse(term2pile(mouse), to, opt))
686 return CMD_INVAL;
687 return CMD_JOIN;
688 case MOUSE_LEFT:
689 if (set_mouse(term2pile(mouse), from, opt))
690 return CMD_INVAL;
691 break;
692 /* misc keys: */
693 case ':':
694 {char buf[256];
695 fprintf (stderr, ":");
696 raw_mode(0); /* turn on echo */
697 fgets (buf, 256, stdin);
698 raw_mode(1);
699 switch(buf[0]) {
700 case 'q': return CMD_QUIT;
701 case 'n': return CMD_NEW;
702 case 'r': return CMD_AGAIN;
703 default: return CMD_INVAL;
704 }}
705 case 'J':
706 *to = active.pile;
707 #ifdef KLONDIKE
708 if (*to == FOUNDATION) return CMD_JOIN;
709 #endif
710 if (*to > TAB_MAX) return CMD_INVAL;
711 return CMD_JOIN;
712 case 'K': /* fallthrough */
713 case '?': return CMD_HINT;
714 case 'u': return CMD_UNDO;
715 case EOF: return CMD_NONE; /* sent by SIGCONT */
716 default: return CMD_INVAL;
717 }
718 inactive.pile = *from; /* for direct addressing highlighting */
719 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
720
721 if (*from == STOCK) {
722 *to = WASTE;
723 return CMD_MOVE;
724 }
725
726 /***/
727 to_l: print_table(&active, &inactive);
728 t = getch(mouse);
729
730 switch (t) {
731 case KEY_LEFT:
732 case 'h': cursor_left (&active); goto to_l;
733 case KEY_DOWN:
734 case 'j': cursor_down (&active); goto to_l;
735 case KEY_UP:
736 case 'k': cursor_up (&active); goto to_l;
737 case KEY_RIGHT:
738 case 'l': cursor_right(&active); goto to_l;
739 case KEY_HOME:
740 case 'H': cursor_to(&active,TAB_1); goto to_l;
741 case KEY_END:
742 case 'L': cursor_to(&active,TAB_MAX); goto to_l;
743 case KEY_INS:
744 case 'M': cursor_to(&active,TAB_MAX/2); goto to_l;
745 case 'J': /* fallthrough; just join selected pile */
746 case ' ':
747 *to = active.pile;
748 break; /* continues with the foundation/empty tableu check */
749 case MOUSE_MIDDLE:
750 case MOUSE_RIGHT: return CMD_NONE;
751 case MOUSE_LEFT:
752 if (set_mouse(term2pile(mouse), to, opt))
753 return CMD_INVAL;
754 /*#ifdef SPIDER
755 //TODO: set opt if to field is empty; suppress "up do" dialog from below
756 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
757 int top = find_top(f.t[*from]);
758 if (top < 0) return CMD_INVAL;
759 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
760 *opt = get_rank(f.t[*from][top]);
761 } else {
762 // ask user
763 }
764 }
765 #endif*/
766 break;
767 case 'K': /* fallthrough */
768 case '?': return CMD_HINT;
769 case 'u': return CMD_NONE; /* cancel selection */
770 case EOF: return CMD_NONE; /* sent by SIGCONT */
771 default:
772 if (t < '0' || t > '9') return CMD_INVAL;
773 if (t == '0')
774 #ifdef KLONDIKE
775 *to = FOUNDATION;
776 #elif defined SPIDER
777 *to = TAB_10;
778 #endif
779 else
780 *to = t-'1';
781 }
782
783 /***/
784 #ifdef KLONDIKE
785 if (*from == FOUNDATION) {
786 int top = find_top(f.t[*to]);
787 if (top < 0) return CMD_INVAL;
788 int color = get_color(f.t[*to][top]);
789 int choice_1 = 1-color; /* selects piles of */
790 int choice_2 = 2+color; /* the opposite color */
791 int top_c1 = find_top(f.f[choice_1]);
792 int top_c2 = find_top(f.f[choice_2]);
793
794 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
795 && top_c1 >= 0 ) << 0
796 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
797 && top_c2 >= 0 ) << 1) {
798 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
799 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
800 case (1<<1 | 1<<0): /* both, ask user which to pick from */
801 printf ("take from (1-4): "); fflush (stdout);
802 *opt = getch(NULL) - '1';
803 if (*opt < 0 || *opt > 3) return CMD_INVAL;
804 break;
805 default: return CMD_INVAL; /* none matched */
806 }
807 /* `opt` is the foundation index (0..3) */
808 }
809 #elif defined SPIDER
810 /* moving to empty tableu? */
811 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
812 int bottom = first_movable(f.t[*from]);
813 if (inactive.opt >= 0) { /*if from was cursor addressed: */
814 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
815 return CMD_MOVE;
816 }
817 int top = find_top(f.t[*from]);
818 if (top < 0) return CMD_INVAL;
819 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
820 *opt = get_rank(f.t[*from][top]);
821 } else { /* only ask the user if it's unclear: */
822 printf ("\rup to ([a23456789xjqk] or space/return): ");
823 *opt = getch(NULL);
824 switch (*opt) {
825 case ' ': *opt = get_rank(f.t[*from][top]); break;
826 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
827 case 'a': case 'A': *opt = RANK_A; break;
828 case '0': /* fallthrough */
829 case 'x': case 'X': *opt = RANK_X; break;
830 case 'j': case 'J': *opt = RANK_J; break;
831 case 'q': case 'Q': *opt = RANK_Q; break;
832 case 'k': case 'K': *opt = RANK_K; break;
833 default: *opt -= '1';
834 }
835 if (*opt < RANK_A || *opt > RANK_K) return ERR;
836 }
837 /* `opt` is the rank of the highest card to move */
838 }
839 #endif
840 return CMD_MOVE;
841 }
842
843 int getctrlseq(unsigned char* buf) {
844 int c;
845 enum esc_states {
846 START,
847 ESC_SENT,
848 CSI_SENT,
849 MOUSE_EVENT,
850 } state = START;
851 int offset = 0x20; /* never sends control chars as data */
852 while ((c = getchar()) != EOF) {
853 switch (state) {
854 case START:
855 switch (c) {
856 case '\033': state=ESC_SENT; break;
857 default: return c;
858 }
859 break;
860 case ESC_SENT:
861 switch (c) {
862 case '[': state=CSI_SENT; break;
863 default: return KEY_INVAL;
864 }
865 break;
866 case CSI_SENT:
867 switch (c) {
868 case 'A': return KEY_UP;
869 case 'B': return KEY_DOWN;
870 case 'C': return KEY_RIGHT;
871 case 'D': return KEY_LEFT;
872 /*NOTE: home/end send ^[[x~ . no support for modifiers*/
873 case 'H': return KEY_HOME;
874 case 'F': return KEY_END;
875 case '2': getchar(); return KEY_INS;
876 case '5': getchar(); return KEY_PGUP;
877 case '6': getchar(); return KEY_PGDN;
878 case 'M': state=MOUSE_EVENT; break;
879 default: return KEY_INVAL;
880 }
881 break;
882 case MOUSE_EVENT:
883 if (buf == NULL) return KEY_INVAL;
884 buf[0] = c - offset;
885 buf[1] = getchar() - offset;
886 buf[2] = getchar() - offset;
887 return MOUSE_ANY;
888 default:
889 return KEY_INVAL;
890 }
891 }
892 return 2;
893 }
894 int term2pile(unsigned char *mouse) {
895 int line = (mouse[2]-1);
896 int column = (mouse[1]-1) / op.s->width;
897
898 if (line < op.s->height) { /* first line */
899 #ifdef KLONDIKE
900 switch (column) {
901 case 0: return STOCK;
902 case 1: return WASTE;
903 case 2: return -1; /* spacer */
904 case 3: return FOUNDATION+0;
905 case 4: return FOUNDATION+1;
906 case 5: return FOUNDATION+2;
907 case 6: return FOUNDATION+3;
908 }
909 #elif defined SPIDER
910 if (column < 3) return STOCK;
911 return -1;
912 #endif
913 } else if (line > op.s->height) { /* tableu */
914 if (column <= TAB_MAX) return column;
915 }
916 return -1;
917 }
918 int wait_mouse_up(unsigned char* mouse) {
919 struct cursor cur = {-1,-1};
920 int level = 1;
921 /* note: if dragged [3]==1 and second position is in mouse[0,4,5] */
922
923 /* display a cursor while mouse button is pushed: */
924 int pile = term2pile(mouse);
925 cur.pile = pile;
926 #ifdef KLONDIKE
927 if (pile >= FOUNDATION) {
928 cur.pile = FOUNDATION;
929 cur.opt = pile-FOUNDATION;
930 }
931 #endif
932 print_table(&cur, NO_HI); //TODO: should not overwrite inactive cursor!
933
934 while (level > 0) {
935 if (getctrlseq (mouse+3) == MOUSE_ANY) {
936 /* ignore mouse wheel events: */
937 if (mouse[3] & 0x40) continue;
938
939 else if((mouse[3]&3) == 3) level--; /* release event */
940 else level++; /* another button pressed */
941 }
942 }
943
944 int success = mouse[1] == mouse[4] && mouse[2] == mouse[5];
945 if (success) {
946 mouse[3] = 0;
947 }
948 return success;
949 }
950
951 int getch(unsigned char* buf) {
952 /* returns a character, EOF, or constant for an escape/control sequence - NOT
953 compatible with the ncurses implementation of same name */
954 int action;
955 if (buf && buf[3]) {
956 /* mouse was dragged; return 'ungetted' previous destination */
957 action = MOUSE_DRAG;
958 /* keep original [0], as [3] only contains release event */
959 buf[1] = buf[4];
960 buf[2] = buf[5];
961 buf[3] = 0;
962 } else {
963 action = getctrlseq(buf);
964 }
965
966 switch (action) {
967 case MOUSE_ANY:
968 if (buf[0] > 3) break; /* ignore wheel events */
969 wait_mouse_up(buf);
970 /* fallthrough */
971 case MOUSE_DRAG:
972 switch (buf[0]) {
973 case 0: return MOUSE_LEFT;
974 case 1: return MOUSE_MIDDLE;
975 case 2: return MOUSE_RIGHT;
976 }
977 }
978
979 return action;
980 }
981 // }}}
982
983 // shuffling and dealing {{{
984 void deal(long seed) {
985 f = (const struct playfield){0}; /* clear playfield */
986 card_t deck[DECK_SIZE*NUM_DECKS];
987 int avail = DECK_SIZE*NUM_DECKS;
988 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
989 #ifdef SPIDER
990 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
991 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
992 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
993 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
994 }
995 #endif
996 srand (seed);
997 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
998 int j = rand() % (i+1);
999 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
1000 }
1001
1002 /* deal cards: */
1003 for (int i = 0; i < NUM_PILES; i++) {
1004 #ifdef KLONDIKE
1005 int closed = i; /* pile n has n closed cards, then 1 open */
1006 #elif defined SPIDER
1007 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
1008 #endif
1009 /* face down cards are negated: */
1010 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
1011 f.t[i][closed] = deck[--avail]; /* the face-up card */
1012 }
1013 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
1014 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
1015 #ifdef KLONDIKE
1016 f.w = -1; /* @start: nothing on waste */
1017 #elif defined SPIDER
1018 f.w = 0; /* number of used foundations */
1019 #endif
1020
1021 f.u = &undo_sentinel;
1022 }
1023 //}}}
1024
1025 // screen drawing routines {{{
1026 void print_hi(int invert, int grey_bg, int bold, char* str) {
1027 if (bold && op.s == &unicode_large_color){ //awful hack for bold + faint
1028 int offset = str[3]==017?16:str[4]==017?17:0;
1029 printf ("%s%s%s""%.*s%s%s""%s%s%s",
1030 "\033[1m", invert?"\033[7m":"", grey_bg?"\033[100m":"",
1031 offset, str, bold?"\033[1m":"", str+offset,
1032 grey_bg?"\033[49m":"", invert?"\033[27m":"","\033[22m");
1033 return;
1034 }
1035 printf ("%s%s%s%s%s%s%s",
1036 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
1037 str,
1038 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
1039 }
1040 void print_table(const struct cursor* active, const struct cursor* inactive) {
1041 printf("\033[2J\033[H"); /* clear screen, reset cursor */
1042 #ifdef KLONDIKE
1043 /* print stock, waste and foundation: */
1044 for (int line = 0; line < op.s->height; line++) {
1045 /* stock: */
1046 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
1047 (f.w < f.z-1)?op.s->facedown
1048 :op.s->placeholder)[line]);
1049 /* waste: */
1050 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
1051 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
1052 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
1053 :op.s->placeholder)[line]);
1054 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
1055 /* foundation: */
1056 for (int pile = 0; pile < NUM_SUITS; pile++) {
1057 int card = find_top(f.f[pile]);
1058 print_hi (active->pile==FOUNDATION && active->opt==pile,
1059 inactive->pile==FOUNDATION && (
1060 /* cursor addr. || direct addr. */
1061 inactive->opt==pile || inactive->opt < 0
1062 ), 1,
1063 (card < 0)?op.s->placeholder[line]
1064 :op.s->card[f.f[pile][card]][line]);
1065 }
1066 printf("\n");
1067 }
1068 printf("\n");
1069 #elif defined SPIDER
1070 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
1071 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
1072 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
1073 int spacer_to = NUM_PILES*op.s->width -
1074 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
1075 for (int line = 0; line < op.s->height; line++) {
1076 /* available stock: */
1077 for (int i = f.z/10; i; i--) {
1078 if (i==1) printf ("%s", op.s->facedown[line]);
1079 else printf ("%s", op.s->halfstack[line]);
1080 }
1081 /* spacer: */
1082 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
1083 /* foundation (overlapping): */
1084 for (int i = NUM_DECKS*NUM_SUITS-1, half = 0; i >= 0; i--) {
1085 int overlap = half? op.s->halfcard[line]: 0;
1086 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
1087 op.s->card[f.f[i][RANK_K]][line]+overlap),
1088 half++;
1089 }
1090 printf("\n");
1091 }
1092 printf("\n");
1093 #endif
1094 #ifdef KLONDIKE
1095 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty))
1096 #define TOP_HI(c) 1 /* can't select partial stacks in KLONDIKE */
1097 #define INC_OFFSET
1098 #elif defined SPIDER
1099 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
1100 #define DO_HI(cursor) (cursor->pile == pile && (movable || empty) \
1101 && offset[pile] > cursor->opt)
1102 #define TOP_HI(cursor) (cursor->pile == pile && movable \
1103 && offset[pile]-1 == cursor->opt)
1104 #define INC_OFFSET if (movable) offset[pile]++
1105 #endif
1106 /* print tableu piles: */
1107 int row[NUM_PILES] = {0};
1108 int line[NUM_PILES]= {0};
1109 int label[NUM_PILES]={0};
1110 int line_had_card;
1111 int did_placeholders = 0;
1112 do {
1113 line_had_card = 0;
1114 for (int pile = 0; pile < NUM_PILES; pile++) {
1115 card_t card = f.t[pile][row[pile]];
1116 card_t next = f.t[pile][row[pile]+1];
1117 int movable = is_movable(f.t[pile], row[pile]);
1118 int empty = !card && row[pile] == 0;
1119
1120 print_hi (DO_HI(active), DO_HI(inactive), movable, (
1121 (!card && row[pile] == 0)?op.s->placeholder
1122 :(card<0)?op.s->facedown
1123 :op.s->card[card]
1124 )[line[pile]]);
1125
1126 int extreme_overlap = ( 3 /* spacer, labels, status */
1127 + 2 * op.s->height /* stock, top tableu card */
1128 + find_top(f.t[pile]) * op.s->overlap) >op.w[0];
1129 /* normal overlap: */
1130 if (++line[pile] >= (next?op.s->overlap:op.s->height)
1131 /* extreme overlap on closed cards: */
1132 || (extreme_overlap &&
1133 line[pile] >= 1 &&
1134 f.t[pile][row[pile]] < 0 &&
1135 f.t[pile][row[pile]+1] <0)
1136 /* extreme overlap on sequences: */
1137 || (extreme_overlap &&
1138 !TOP_HI(active) && /*always show top selected card*/
1139 line[pile] >= 1 && row[pile] > 0 &&
1140 f.t[pile][row[pile]-1] > NO_CARD &&
1141 is_consecutive (f.t[pile], row[pile]) &&
1142 is_consecutive (f.t[pile], row[pile]-1) &&
1143 f.t[pile][row[pile]+1] != NO_CARD)
1144 ) {
1145 line[pile]=0;
1146 row[pile]++;
1147 INC_OFFSET;
1148 }
1149 /* tableu labels: */
1150 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
1151 label[pile] = 1;
1152 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
1153 }
1154 line_had_card |= !!card;
1155 did_placeholders |= row[pile] > 0;
1156 }
1157 printf ("\n");
1158 } while (line_had_card || !did_placeholders);
1159 }
1160
1161 void visbell (void) {
1162 printf ("\033[?5h"); fflush (stdout);
1163 usleep (100000);
1164 printf ("\033[?5l"); fflush (stdout);
1165 }
1166 void win_anim(void) {
1167 printf ("\033[?25l"); /* hide cursor */
1168 for (;;) {
1169 /* set cursor to random location */
1170 int row = 1+rand()%(24-op.s->width);
1171 int col = 1+rand()%(80-op.s->height);
1172
1173 /* draw random card */
1174 int face = 1 + rand() % 52;
1175 for (int l = 0; l < op.s->height; l++) {
1176 printf ("\033[%d;%dH", row+l, col);
1177 printf ("%s", op.s->card[face][l]);
1178 }
1179 fflush (stdout);
1180
1181 /* exit on keypress */
1182 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
1183 if (poll (&p, 1, 80)) goto fin;
1184 }
1185 fin:
1186 printf ("\033[?25h"); /* show cursor */
1187 return;
1188 }
1189 //}}}
1190
1191 // undo logic {{{
1192 void undo_push (int _f, int t, int n, int o) {
1193 struct undo* new = malloc(sizeof(struct undo));
1194 new->f = _f;
1195 new->t = t;
1196 new->n = n;
1197 new->o = o;
1198 new->prev = f.u;
1199 new->next = NULL;
1200 f.u->next = new;
1201 f.u = f.u->next;
1202 }
1203 void undo_pop (struct undo* u) {
1204 if (u == &undo_sentinel) return;
1205
1206 #ifdef KLONDIKE
1207 if (u->f == FOUNDATION) {
1208 /* foundation -> tableu */
1209 int top_f = find_top(f.f[u->n]);
1210 int top_t = find_top(f.t[u->t]);
1211 f.f[u->n][top_f+1] = f.t[u->t][top_t];
1212 f.t[u->t][top_t] = NO_CARD;
1213 } else if (u->f == WASTE && u->t == FOUNDATION) {
1214 /* waste -> foundation */
1215 /* split u->n into wst and fnd: */
1216 int wst = u->n & 0xffff;
1217 int fnd = u->n >> 16;
1218 /* move stock cards one position up to make room: */
1219 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
1220 /* move one card from foundation to waste: */
1221 int top = find_top(f.f[fnd]);
1222 f.s[wst] = f.f[fnd][top];
1223 f.f[fnd][top] = NO_CARD;
1224 f.z++;
1225 f.w++;
1226 } else if (u->f == WASTE) {
1227 /* waste -> tableu */
1228 /* move stock cards one position up to make room: */
1229 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
1230 /* move one card from tableu to waste: */
1231 int top = find_top(f.t[u->t]);
1232 f.s[u->n] = f.t[u->t][top];
1233 f.t[u->t][top] = NO_CARD;
1234 f.z++;
1235 f.w++;
1236 } else if (u->t == FOUNDATION) {
1237 /* tableu -> foundation */
1238 int top_f = find_top(f.t[u->f]);
1239 int top_t = find_top(f.f[u->n]);
1240 /* close topcard if previous action caused turn_over(): */
1241 if (u->o) f.t[u->f][top_f] *= -1;
1242 /* move one card from foundation to tableu: */
1243 f.t[u->f][top_f+1] = f.f[u->n][top_t];
1244 f.f[u->n][top_t] = NO_CARD;
1245 } else {
1246 /* tableu -> tableu */
1247 int top_f = find_top(f.t[u->f]);
1248 int top_t = find_top(f.t[u->t]);
1249 /* close topcard if previous action caused turn_over(): */
1250 if (u->o) f.t[u->f][top_f] *= -1;
1251 /* move n cards from tableu[f] to tableu[t]: */
1252 for (int i = 0; i < u->n; i++) {
1253 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1254 f.t[u->t][top_t-i] = NO_CARD;
1255 }
1256 }
1257 #elif defined SPIDER
1258 if (u->f == STOCK) {
1259 /* stock -> tableu */
1260 /*remove a card from each pile and put it back onto the stock:*/
1261 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
1262 int top = find_top(f.t[pile]);
1263 f.s[f.z++] = f.t[pile][top];
1264 f.t[pile][top] = NO_CARD;
1265 }
1266 } else if (u->t == FOUNDATION) {
1267 /* tableu -> foundation */
1268 int top = find_top(f.t[u->f]);
1269 /* close topcard if previous action caused turn_over(): */
1270 if (u->o) f.t[u->f][top] *= -1;
1271 /* append cards from foundation to tableu */
1272 for (int i = RANK_K; i >= RANK_A; i--) {
1273 f.t[u->f][++top] = f.f[u->n][i];
1274 f.f[u->n][i] = NO_CARD;
1275 }
1276 f.w--; /* decrement complete-foundation-counter */
1277
1278 } else {
1279 /* tableu -> tableu */
1280 int top_f = find_top(f.t[u->f]);
1281 int top_t = find_top(f.t[u->t]);
1282 /* close topcard if previous action caused turn_over(): */
1283 if (u->o) f.t[u->f][top_f] *= -1;
1284 /* move n cards from tableu[f] to tableu[t]: */
1285 for (int i = 0; i < u->n; i++) {
1286 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
1287 f.t[u->t][top_t-i] = NO_CARD;
1288 }
1289 }
1290 #endif
1291
1292 void* old = f.u;
1293 f.u = f.u->prev;
1294 free(old);
1295 }
1296 void free_undo (struct undo* u) {
1297 while (u && u != &undo_sentinel) {
1298 void* old = u;
1299 u = u->prev;
1300 free (old);
1301 }
1302 }
1303 //}}}
1304
1305 // initialization stuff {{{
1306 void screen_setup (int enable) {
1307 if (enable) {
1308 raw_mode(1);
1309 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
1310 printf ("\033[H\033[J"); /* reset cursor, clear screen */
1311 printf ("\033[?1000h"); /* enable mouse */
1312 } else {
1313 printf ("\033[?1000l"); /* disable mouse */
1314 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
1315 raw_mode(0);
1316 }
1317 }
1318
1319 void raw_mode(int enable) {
1320 static struct termios saved_term_mode;
1321 struct termios raw_term_mode;
1322
1323 if (enable) {
1324 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
1325 tcgetattr(STDIN_FILENO, &saved_term_mode);
1326 raw_term_mode = saved_term_mode;
1327 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
1328 raw_term_mode.c_cc[VMIN] = 1 ;
1329 raw_term_mode.c_cc[VTIME] = 0;
1330 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
1331 } else {
1332 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
1333 }
1334 }
1335
1336 void signal_handler (int signum) {
1337 struct winsize w;
1338 switch (signum) {
1339 case SIGTSTP:
1340 screen_setup(0);
1341 signal(SIGTSTP, SIG_DFL); /* NOTE: assumes SysV semantics! */
1342 raise(SIGTSTP);
1343 break;
1344 case SIGCONT:
1345 screen_setup(1);
1346 print_table(NO_HI, NO_HI);
1347 break;
1348 case SIGINT: //TODO: don't exit; just warn like vim does
1349 exit(128+SIGINT);
1350 case SIGWINCH:
1351 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
1352 op.w[0] = w.ws_row;
1353 op.w[1] = w.ws_col;
1354 break;
1355 }
1356 }
1357 void signal_setup(void) {
1358 struct sigaction saction;
1359
1360 saction.sa_handler = signal_handler;
1361 sigemptyset(&saction.sa_mask);
1362 saction.sa_flags = 0;
1363 if (sigaction(SIGTSTP, &saction, NULL) < 0) {
1364 perror ("SIGTSTP");
1365 exit (1);
1366 }
1367 if (sigaction(SIGCONT, &saction, NULL) < 0) {
1368 perror ("SIGCONT");
1369 exit (1);
1370 }
1371 if (sigaction(SIGINT, &saction, NULL) < 0) {
1372 perror ("SIGINT");
1373 exit (1);
1374 }
1375 if (sigaction(SIGWINCH, &saction, NULL) < 0) {
1376 perror ("SIGWINCH");
1377 exit (1);
1378 }
1379 }
1380 //}}}
1381
1382 //vim: foldmethod=marker
Imprint / Impressum