]> git.gir.st - solVItaire.git/blob - sol.c
fix turn over bug
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
3 #include <poll.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <time.h>
9 #include <termios.h>
10 #include <unistd.h>
11
12 #include "sol.h"
13 #include "schemes.h"
14
15 struct playfield f;
16 struct opts op;
17
18 // action table {{{
19 /* stores a function pointer for every takeable action; called by game loop */
20 int (*action[NUM_PLACES][10])(int,int,int) = {
21 #ifdef KLONDIKE
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 },
33 #elif defined SPIDER
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 },
46 #endif
47 };
48 // }}}
49
50 // argv parsing, game loops, cleanup {{{
51 int main(int argc, char** argv) {
52 /* opinionated defaults: */
53 op.s = &unicode_large_color;
54 #ifdef SPIDER
55 op.m = MEDIUM;
56 #endif
57 op.v = 0;
58
59 int optget;
60 opterr = 0; /* don't print message on unrecognized option */
61 while ((optget = getopt (argc, argv, "+:hd:o:s:")) != -1) {
62 switch (optget) {
63 #ifdef SPIDER
64 case 'd': /* difficulty */
65 if(!strcmp(optarg, "easy")) op.m = EASY;
66 if(!strcmp(optarg, "medium")) op.m = MEDIUM;
67 if(!strcmp(optarg, "hard")) op.m = NORMAL;
68 break;
69 #endif
70 case 'o': /* misc. options */
71 if(!strcmp(optarg, "consv")) op.v = 1;
72 break;
73 case 's': /* scheme */
74 if(!strcmp(optarg,"color")) op.s = &unicode_large_color;
75 if(!strcmp(optarg, "mono")) op.s = &unicode_large_mono;
76 if(!strcmp(optarg,"small")) op.s = &unicode_small_mono;
77 break;
78 case 'h':
79 case ':': //missing optarg
80 default:
81 fprintf (stderr, SHORTHELP LONGHELP KEYHELP, argv[0]);
82 return optget != 'h';
83 }
84 }
85
86 signal_setup();
87 atexit (*quit);
88
89 newgame:
90 screen_setup(1);
91
92 switch(sol()) {
93 case GAME_NEW: goto newgame;
94 case GAME_WON:
95 print_table(NO_HI, NO_HI);
96 win_anim();
97 if (getchar()=='q') return 0;
98 goto newgame;
99 case GAME_QUIT: return 0;
100 }
101 }
102
103 int sol(void) {
104 /* clean undo (from previous game): */
105 free_undo(f.u);
106
107 deal();
108
109 int from, to, opt;
110 for(;;) {
111 switch (get_cmd(&from, &to, &opt)) {
112 case CMD_MOVE:
113 switch (action[from][to](from,to,opt)) {
114 case OK: break;
115 case ERR: visbell(); break;
116 case WON: return GAME_WON;
117 }
118 break;
119 case CMD_HINT: //TODO: show a possible (and sensible) move
120 case CMD_JOIN: //TODO: join any pile to here (longest if possible)
121 case CMD_UNDO: undo_pop(f.u); break;
122 case CMD_INVAL: visbell(); break;
123 case CMD_NEW: return GAME_NEW;
124 case CMD_AGAIN: //TODO: restart with same seed
125 case CMD_QUIT: return GAME_QUIT;
126 }
127 }
128 }
129
130 void quit(void) {
131 screen_setup(0);
132 /* free undo data structures: */
133 free_undo(f.u);
134 }
135 //}}}
136
137 // card games helper functions {{{
138 #define get_suit(card) \
139 ((card-1) % NUM_SUITS)
140 #define get_rank(card) \
141 ((card-1) / NUM_SUITS)
142 #define get_color(card) \
143 ((get_suit(card) ^ get_suit(card)>>1) & 1)
144
145 #define is_tableu(where) (where <= TAB_MAX)
146
147 int find_top(card_t* pile) {
148 int i;
149 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
150 return i;
151 }
152 int first_movable(card_t* pile) {
153 int i = 0;
154 for (;pile[i] && !is_movable(pile, i); i++);
155 return i;
156 }
157 int turn_over(card_t* pile) {
158 int top = find_top(pile);
159 if (pile[top] < 0) {
160 pile[top] *= -1;
161 return 1;
162 } else return 0;
163 }
164 int check_won(void) {
165 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
166 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
167
168 return 1;
169 }
170 int rank_next (card_t a, card_t b) {
171 return get_rank(a) == get_rank(b)-1;
172 }
173 int is_consecutive (card_t* pile, int pos) {
174 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
175 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
176
177 #ifdef KLONDIKE
178 /* ranks consecutive? */
179 if (!rank_next(pile[pos+1], pile[pos])) return 0;
180 /* color opposite? */
181 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
182 #elif defined SPIDER
183 /* ranks consecutive? */
184 if (!rank_next(pile[pos+1], pile[pos])) return 0;
185 /* same suit? */
186 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
187 #endif
188
189 return 1;
190 }
191
192 int is_movable(card_t* pile, int n) {
193 #ifdef KLONDIKE
194 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
195 #elif defined SPIDER
196 int top = find_top(pile);
197 for (int i = top; i >= 0; i--) {
198 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
199 if (!is_consecutive(pile, i)) return 0;
200 if (i == n) return 1; /* card reached, must be movable */
201 }
202 return 0;
203 #endif
204 }
205 //}}}
206
207 // takeable actions {{{
208 #ifdef KLONDIKE
209 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
210 card_t card = f.s[f.w];
211 /* move stack one over, so there are no gaps in it: */
212 for (int i = f.w; i < f.z-1; i++)
213 f.s[i] = f.s[i+1];
214 f.z--;
215 f.w--; /* make previous card visible again */
216 return card;
217 }
218 int t2f(int from, int to, int opt) { /* tableu to foundation */
219 (void) to; (void) opt; /* don't need */
220 int top_from = find_top(f.t[from]);
221 to = get_suit(f.t[from][top_from]);
222 int top_to = find_top(f.f[to]);
223 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
224 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
225 f.f[to][top_to+1] = f.t[from][top_from];
226 f.t[from][top_from] = NO_CARD;
227 undo_push(from, FOUNDATION, to,
228 turn_over(f.t[from]));
229 if (check_won()) return WON;
230 return OK;
231 } else return ERR;
232 }
233 int w2f(int from, int to, int opt) { /* waste to foundation */
234 (void) from; (void) to; (void) opt; /* don't need */
235 if (f.w < 0) return ERR;
236 to = get_suit(f.s[f.w]);
237 int top_to = find_top(f.f[to]);
238 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
239 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
240 undo_push(WASTE, FOUNDATION, f.w | to<<16, 0);//ugly encoding :|
241 f.f[to][top_to+1] = stack_take();
242 if (check_won()) return WON;
243 return OK;
244 } else return ERR;
245
246 }
247 int s2w(int from, int to, int opt) { /* stock to waste */
248 (void) from; (void) to; (void) opt; /* don't need */
249 if (f.z == 0) return ERR;
250 f.w++;
251 if (f.w == f.z) f.w = -1;
252 return OK;
253 }
254 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
255 (void) from; (void) to; (void) opt; /* don't need */
256 if (f.z == 0) return ERR;
257 f.w--;
258 if (f.w < -1) f.w = f.z-1;
259 return OK;
260 }
261 int f2t(int from, int to, int opt) { /* foundation to tableu */
262 (void) from; /* don't need */
263 int top_to = find_top(f.t[to]);
264 from = opt;
265 int top_from = find_top(f.f[from]);
266
267 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
268 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
269 f.t[to][top_to+1] = f.f[from][top_from];
270 f.f[from][top_from] = NO_CARD;
271 undo_push(FOUNDATION, to, from, 0);
272 return OK;
273 } else return ERR;
274 }
275 int w2t(int from, int to, int opt) { /* waste to tableu */
276 (void) from; (void) opt; /* don't need */
277 int top_to = find_top(f.t[to]);
278 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
279 && (rank_next(f.s[f.w], f.t[to][top_to])))
280 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
281 undo_push(WASTE, to, f.w, 0);
282 f.t[to][top_to+1] = stack_take();
283 return OK;
284 } else return ERR;
285 }
286 int t2t(int from, int to, int opt) { /* tableu to tableu */
287 (void) opt; /* don't need */
288 int top_to = find_top(f.t[to]);
289 int top_from = find_top(f.t[from]);
290 int count = 0; //NOTE: could probably be factored out
291 for (int i = top_from; i >=0; i--) {
292 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
293 && (rank_next(f.t[from][i], f.t[to][top_to]))
294 && f.t[from][i] > NO_CARD) /* card face up? */
295 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
296 /* move cards [i..top_from] to their destination */
297 for (;i <= top_from; i++) {
298 top_to++;
299 f.t[to][top_to] = f.t[from][i];
300 f.t[from][i] = NO_CARD;
301 count++;
302 }
303 undo_push(from, to, count,
304 turn_over(f.t[from]));
305 return OK;
306 }
307 }
308 return ERR; /* no such move possible */
309 }
310 #elif defined SPIDER
311 void remove_if_complete (int pileno) { //cleanup!
312 static int foundation = 0;
313 if (pileno == -1) { foundation--; return; } /* for undo_pop() */
314
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;
319 for (int i = top_from; i>=0; i--) {
320 if (!is_consecutive (pile, i)) return;
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[foundation][j] = pile[i];
325 pile[i] = NO_CARD;
326 }
327 undo_push(pileno, FOUNDATION, foundation,
328 turn_over(pile));
329 foundation++;
330 return;
331 }
332 }
333 }
334 int t2t(int from, int to, int opt) { //in dire need of cleanup
335 int top_from = find_top(f.t[from]);
336 int top_to = find_top(f.t[to]);
337 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
338 int count = 0; //NOTE: could probably be factored out
339
340 for (int i = top_from; i >= 0; i--) {
341 if (!is_consecutive(f.t[from], i)) break;
342
343 /* is consecutive OR to empty pile and rank ok? */
344 if (rank_next(f.t[from][i], f.t[to][top_to])
345 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
346 for (;i <= top_from; i++) {
347 top_to++;
348 f.t[to][top_to] = f.t[from][i];
349 f.t[from][i] = NO_CARD;
350 count++;
351 }
352 undo_push(from, to, count,
353 turn_over(f.t[from]));
354 remove_if_complete(to);
355 if (check_won()) return WON;
356 return OK;
357 }
358 }
359
360 return ERR; /* no such move possible */
361 }
362 int s2t(int from, int to, int opt) {
363 (void) from; (void) to; (void) opt; /* don't need */
364 if (f.z <= 0) return ERR; /* stack out of cards */
365 for (int pile = 0; pile < NUM_PILES; pile++)
366 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
367 for (int pile = 0; pile < NUM_PILES; pile++) {
368 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
369 remove_if_complete(pile);
370 if (check_won()) return WON;
371 }
372 undo_push(STOCK, TABLEU, 1, 0); /*NOTE: puts 1 card on each tableu pile*/
373 return OK;
374 }
375 int t2f(int from, int to, int opt) {
376 (void) to; (void) opt; /* don't need */
377 /* manually retrigger remove_if_complete() (e.g. after undo_pop) */
378 remove_if_complete(from);
379 return OK;
380 }
381 #endif
382 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
383 // }}}
384
385 // keyboard input handling {{{
386 // cursor functions{{{
387 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
388 #ifdef KLONDIKE
389 void cursor_left (struct cursor* cursor) {
390 if (is_tableu(cursor->pile)) {
391 if (cursor->pile > 0) cursor->pile--;
392 cursor->opt = 0;
393 } else { /* stock/waste/foundation*/
394 switch (cursor->pile) {
395 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
396 case FOUNDATION:
397 if (cursor->opt <= 0)
398 cursor->pile = WASTE;
399 else
400 cursor->opt--;
401 }
402 }
403 }
404 void cursor_down (struct cursor* cursor) {
405 if (!is_tableu(cursor->pile)) {
406 switch (cursor->pile) {
407 case STOCK: cursor->pile = TAB_1; break;
408 case WASTE: cursor->pile = TAB_2; break;
409 case FOUNDATION:
410 cursor->pile = TAB_4 + cursor->opt;
411 }
412 cursor->opt = 0;
413 }
414 }
415 void cursor_up (struct cursor* cursor) {
416 if (is_tableu(cursor->pile)) {
417 switch (cursor->pile) { //ugly :|
418 case TAB_1: cursor->pile = STOCK; break;
419 case TAB_2: cursor->pile = WASTE; break;
420 case TAB_3: cursor->pile = WASTE; break;
421 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
422 cursor->opt=cursor->pile-TAB_4;
423 cursor->pile = FOUNDATION;
424 break;
425 }
426 }
427 }
428 void cursor_right (struct cursor* cursor) {
429 if (is_tableu(cursor->pile)) {
430 if (cursor->pile < TAB_MAX) cursor->pile++;
431 } else {
432 switch (cursor->pile) {
433 case STOCK: cursor->pile = WASTE; break;
434 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
435 case FOUNDATION:
436 if (cursor->opt < NUM_DECKS*NUM_SUITS)
437 cursor->opt++;
438 }
439 }
440 }
441 #elif defined SPIDER
442 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
443 void cursor_left (struct cursor* cursor) {
444 if (cursor->pile > 0) cursor->pile--;
445 cursor->opt = 0;
446 }
447 void cursor_down (struct cursor* cursor) {
448 int first = first_movable(f.t[cursor->pile]);
449 int top = find_top(f.t[cursor->pile]);
450 if (first + cursor->opt < top)
451 cursor->opt++;
452 }
453 void cursor_up (struct cursor* cursor) {
454 if (cursor->opt > 0) cursor->opt--;
455 }
456 void cursor_right (struct cursor* cursor) {
457 if (cursor->pile < TAB_MAX) cursor->pile++;
458 cursor->opt = 0;
459 }
460 #endif
461 void cursor_to (struct cursor* cursor, int pile) {
462 cursor->pile = pile;
463 cursor->opt = 0;
464 }
465 #pragma GCC diagnostic pop
466 //}}}
467 int get_cmd (int* from, int* to, int* opt) {
468 //TODO: escape sequences (mouse, cursor keys)
469 int _f, t;
470 struct cursor inactive = {-1,-1};
471 static struct cursor active = {0,0};
472 active.opt = 0; /* always reset offset, but keep pile */
473
474 /***/
475 from_l: print_table(&active, &inactive);
476 _f = getchar();
477
478 switch (_f) {
479 /* direct addressing: */
480 case '1': *from = TAB_1; break;
481 case '2': *from = TAB_2; break;
482 case '3': *from = TAB_3; break;
483 case '4': *from = TAB_4; break;
484 case '5': *from = TAB_5; break;
485 case '6': *from = TAB_6; break;
486 case '7': *from = TAB_7; break;
487 #ifdef SPIDER
488 case '8': *from = TAB_8; break;
489 case '9': *from = TAB_9; break;
490 case '0': *from = TAB_10;break;
491 #elif defined KLONDIKE
492 case '9': *from = WASTE; break;
493 case '0': *from = FOUNDATION; break;
494 case '8': /* fallthrough */
495 #endif
496 case '\n': /* shortcut for dealing from stock */
497 *from = STOCK;
498 *to = WASTE;
499 return CMD_MOVE;
500 /* cursor keys addressing: */
501 case 'h': cursor_left (&active); goto from_l;
502 case 'j': cursor_down (&active); goto from_l;
503 case 'k': cursor_up (&active); goto from_l;
504 case 'l': cursor_right(&active); goto from_l;
505 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
506 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
507 //TODO: real cursor keys, home/end
508 case ' ': /* continue with second cursor */
509 *from = active.pile;
510 if (*from == STOCK) {
511 *to = WASTE;
512 return CMD_MOVE;
513 }
514 #ifdef KLONDIKE
515 *opt = active.opt; /* when FOUNDATION */
516 #endif
517 inactive = active;
518 break;
519 /* misc keys: */
520 case ':':
521 {char buf[256];
522 fprintf (stderr, ":");
523 raw_mode(0); /* turn on echo */
524 fgets (buf, 256, stdin);
525 raw_mode(1);
526 switch(buf[0]) {
527 case 'q': return CMD_QUIT;
528 case 'n': return CMD_NEW;
529 case 'r': return CMD_AGAIN;
530 default: return CMD_INVAL;
531 }}
532 case 'J': return CMD_JOIN;
533 case 'K': /* fallthrough */
534 case '?': return CMD_HINT;
535 case 'u': return CMD_UNDO;
536 case EOF: return CMD_NONE; /* sent by SIGCONT */
537 default: return CMD_INVAL;
538 }
539 inactive.pile = *from; /* for direct addressing highlighting */
540 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
541
542 /***/
543 to_l: print_table(&active, &inactive);
544 t = getchar();
545
546 switch (t) {
547 case 'h': cursor_left (&active); goto to_l;
548 case 'j': cursor_down (&active); goto to_l;
549 case 'k': cursor_up (&active); goto to_l;
550 case 'l': cursor_right(&active); goto to_l;
551 case 'H': cursor_to(&active,TAB_1); goto to_l;
552 case 'L': cursor_to(&active,TAB_MAX);goto to_l;
553 case 'J': /* fallthrough; key makes no sense on destination */
554 case ' ':
555 *to = active.pile;
556 break; /* continues with the foundation/empty tableu check */
557 case 'K': /* fallthrough */
558 case '?': return CMD_HINT;
559 case 'u': return CMD_NONE; /* cancel selection */
560 case EOF: return CMD_NONE; /* sent by SIGCONT */
561 default:
562 if (t < '0' || t > '9') return CMD_INVAL;
563 if (t == '0')
564 #ifdef KLONDIKE
565 *to = FOUNDATION;
566 #elif defined SPIDER
567 *to = TAB_10;
568 #endif
569 else
570 *to = t-'1';
571 }
572
573 /***/
574 #ifdef KLONDIKE
575 if (*from == FOUNDATION) {
576 int top = find_top(f.t[*to]);
577 if (top < 0) return CMD_INVAL;
578 int color = get_color(f.t[*to][top]);
579 int choice_1 = 1-color; /* selects piles of */
580 int choice_2 = 2+color; /* the opposite color */
581 int top_c1 = find_top(f.f[choice_1]);
582 int top_c2 = find_top(f.f[choice_2]);
583
584 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
585 && top_c1 >= 0 ) << 0
586 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
587 && top_c2 >= 0 ) << 1) {
588 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
589 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
590 case (1<<1 | 1<<0): /* both, ask user which to pick from */
591 printf ("take from (1-4): "); fflush (stdout);
592 *opt = getchar() - '1';
593 if (*opt < 0 || *opt > 3) return CMD_INVAL;
594 break;
595 default: return CMD_INVAL; /* none matched */
596 }
597 /* `opt` is the foundation index (0..3) */
598 }
599 #elif defined SPIDER
600 /* moving to empty tableu? */
601 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
602 int bottom = first_movable(f.t[*from]);
603 if (inactive.opt >= 0) { /*if from was cursor addressed: */
604 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
605 return CMD_MOVE;
606 }
607 int top = find_top(f.t[*from]);
608 if (top < 0) return CMD_INVAL;
609 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
610 *opt = get_rank(f.t[*from][top]);
611 } else { /* only ask the user if it's unclear: */
612 printf ("\rup to ([a23456789xjqk] or space/return): ");
613 *opt = getchar();
614 switch (*opt) {
615 case ' ': *opt = get_rank(f.t[*from][top]); break;
616 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
617 case 'a': case 'A': *opt = RANK_A; break;
618 case '0': /* fallthrough */
619 case 'x': case 'X': *opt = RANK_X; break;
620 case 'j': case 'J': *opt = RANK_J; break;
621 case 'q': case 'Q': *opt = RANK_Q; break;
622 case 'k': case 'K': *opt = RANK_K; break;
623 default: *opt -= '1';
624 }
625 if (*opt < RANK_A || *opt > RANK_K) return ERR;
626 }
627 /* `opt` is the rank of the highest card to move */
628 }
629 #endif
630 return CMD_MOVE;
631 }
632 // }}}
633
634 // shuffling and dealing {{{
635 void deal(void) {
636 f = (const struct playfield){0}; /* clear playfield */
637 card_t deck[DECK_SIZE*NUM_DECKS];
638 int avail = DECK_SIZE*NUM_DECKS;
639 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
640 #ifdef SPIDER
641 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
642 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
643 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
644 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
645 }
646 #endif
647 srandom (time(NULL));
648 long seed = time(NULL);
649 srandom (seed);
650 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { /* fisher-yates */
651 int j = random() % (i+1);
652 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
653 }
654
655 /* deal cards: */
656 for (int i = 0; i < NUM_PILES; i++) {
657 #ifdef KLONDIKE
658 int closed = i; /* pile n has n closed cards, then 1 open */
659 #elif defined SPIDER
660 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
661 #endif
662 /* face down cards are negated: */
663 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
664 f.t[i][closed] = deck[--avail]; /* the face-up card */
665 }
666 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
667 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
668 f.w = -1; /* @start: nothing on waste (no waste in spider -> const) */
669
670 f.u = &undo_sentinel;
671 }
672 //}}}
673
674 // screen drawing routines {{{
675 void print_hi(int invert, int grey_bg, int bold, char* str) {
676 printf ("%s%s%s%s%s%s%s",
677 bold?"\033[1m":"", invert?"\033[7m":"", grey_bg?"\033[100m":"",
678 str,
679 grey_bg?"\033[49m":"", invert?"\033[27m":"",bold?"\033[22m":"");
680 }
681 void print_table(const struct cursor* active, const struct cursor* inactive) {
682 printf("\033[2J\033[H"); /* clear screen, reset cursor */
683 #ifdef KLONDIKE
684 /* print stock, waste and foundation: */
685 for (int line = 0; line < op.s->height; line++) {
686 /* stock: */
687 print_hi (active->pile == STOCK, inactive->pile == STOCK, 1, (
688 (f.w < f.z-1)?op.s->facedown
689 :op.s->placeholder)[line]);
690 /* waste: */
691 print_hi (active->pile == WASTE, inactive->pile == WASTE, 1, (
692 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
693 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
694 :op.s->placeholder)[line]);
695 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
696 /* foundation: */
697 for (int pile = 0; pile < NUM_SUITS; pile++) {
698 int card = find_top(f.f[pile]);
699 print_hi (active->pile==FOUNDATION && active->opt==pile,
700 inactive->pile==FOUNDATION && (
701 /* cursor addr. || direct addr. */
702 inactive->opt==pile || inactive->opt < 0
703 ), 1,
704 (card < 0)?op.s->placeholder[line]
705 :op.s->card[f.f[pile][card]][line]);
706 }
707 printf("\n");
708 }
709 printf("\n");
710 #elif defined SPIDER
711 int fdone; for (fdone = NUM_DECKS*NUM_SUITS; fdone; fdone--)
712 if (f.f[fdone-1][RANK_K]) break; /*number of completed stacks*/
713 int spacer_from = f.z?(f.z/10-1) * op.s->halfwidth[0] + op.s->width:0;
714 int spacer_to = NUM_PILES*op.s->width -
715 ((fdone?(fdone-1) * op.s->halfwidth[1]:0)+op.s->width);
716 for (int line = 0; line < op.s->height; line++) {
717 /* available stock: */
718 for (int i = f.z/10; i; i--) {
719 if (i==1) printf ("%s", op.s->facedown[line]);
720 else printf ("%s", op.s->halfstack[line]);
721 }
722 /* spacer: */
723 for (int i = spacer_from; i < spacer_to; i++) printf (" ");
724 /* foundation (overlapping): */
725 for (int i = 0; i < NUM_DECKS*NUM_SUITS; i++) {
726 int overlap = i? op.s->halfcard[line]: 0;
727 if (f.f[i][RANK_K]) printf ("%.*s", op.s->halfwidth[2],
728 op.s->card[f.f[i][RANK_K]][line]+overlap);
729 }
730 printf("\n");
731 }
732 printf("\n");
733 #endif
734 #ifdef KLONDIKE
735 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
736 #define INC_OFFSET
737 #elif defined SPIDER
738 int offset[NUM_PILES]={1,1,1,1,1,1,1,1,1,1}; // :|
739 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
740 && offset[pile] > cursor->opt
741 #define INC_OFFSET if (movable) offset[pile]++
742 #endif
743 /* print tableu piles: */
744 int row[NUM_PILES] = {0};
745 int line[NUM_PILES]= {0};
746 int label[NUM_PILES]={0};
747 int line_had_card;
748 int did_placeholders = 0;
749 do {
750 line_had_card = 0;
751 for (int pile = 0; pile < NUM_PILES; pile++) {
752 card_t card = f.t[pile][row[pile]];
753 card_t next = f.t[pile][row[pile]+1];
754 int movable = is_movable(f.t[pile], row[pile]);
755 int empty = !card && row[pile] == 0;
756
757 print_hi (DO_HI(active), DO_HI(inactive), movable, (
758 (!card && row[pile] == 0)?op.s->placeholder
759 :(card<0)?op.s->facedown
760 :op.s->card[card]
761 )[line[pile]]);
762
763 int extreme_overlap = op.v && find_top(f.t[pile])>10;
764 /* normal overlap: */
765 if (++line[pile] >= (next?op.s->overlap:op.s->height)
766 /* extreme overlap on closed cards: */
767 || (extreme_overlap &&
768 line[pile] >= 1 &&
769 f.t[pile][row[pile]] < 0 &&
770 f.t[pile][row[pile]+1] <0)
771 /* extreme overlap on sequences: */
772 || (extreme_overlap &&
773 line[pile] >= 1 && row[pile] > 0 &&
774 f.t[pile][row[pile]-1] > NO_CARD &&
775 is_consecutive (f.t[pile], row[pile]) &&
776 is_consecutive (f.t[pile], row[pile]-1) &&
777 f.t[pile][row[pile]+1] != NO_CARD)
778 ) {
779 line[pile]=0;
780 row[pile]++;
781 INC_OFFSET;
782 }
783 /* tableu labels: */
784 if(!card && !label[pile] && row[pile]>0&&line[pile]>0) {
785 label[pile] = 1;
786 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
787 }
788 line_had_card |= !!card;
789 did_placeholders |= row[pile] > 0;
790 }
791 printf ("\n");
792 } while (line_had_card || !did_placeholders);
793 }
794
795 void visbell (void) {
796 printf ("\033[?5h"); fflush (stdout);
797 usleep (100000);
798 printf ("\033[?5l"); fflush (stdout);
799 }
800 void win_anim(void) {
801 printf ("\033[?25l"); /* hide cursor */
802 for (;;) {
803 /* set cursor to random location */
804 int row = 1+random()%(24-op.s->width);
805 int col = 1+random()%(80-op.s->height);
806
807 /* draw random card */
808 int face = 1 + random() % 52;
809 for (int l = 0; l < op.s->height; l++) {
810 printf ("\033[%d;%dH", row+l, col);
811 printf ("%s", op.s->card[face][l]);
812 }
813 fflush (stdout);
814
815 /* exit on keypress */
816 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
817 if (poll (&p, 1, 80)) goto fin;
818 }
819 fin:
820 printf ("\033[?25h"); /* show cursor */
821 return;
822 }
823 //}}}
824
825 // undo logic {{{
826 void undo_push (int _f, int t, int n, int o) {
827 struct undo* new = malloc(sizeof(struct undo));
828 new->f = _f;
829 new->t = t;
830 new->n = n;
831 new->o = o;
832 new->prev = f.u;
833 new->next = NULL;
834 f.u->next = new;
835 f.u = f.u->next;
836 }
837 void undo_pop (struct undo* u) {
838 if (u == &undo_sentinel) return;
839
840 #ifdef KLONDIKE
841 if (u->f == FOUNDATION) {
842 /* foundation -> tableu */
843 int top_f = find_top(f.f[u->n]);
844 int top_t = find_top(f.t[u->t]);
845 f.f[u->n][top_f+1] = f.t[u->t][top_t];
846 f.t[u->t][top_t] = NO_CARD;
847 } else if (u->f == WASTE && u->t == FOUNDATION) {
848 /* waste -> foundation */
849 /* split u->n into wst and fnd: */
850 int wst = u->n & 0xffff;
851 int fnd = u->n >> 16;
852 /* move stock cards one position up to make room: */
853 for (int i = f.z; i >= wst; i--) f.s[i+1] = f.s[i];
854 /* move one card from foundation to waste: */
855 int top = find_top(f.f[fnd]);
856 f.s[wst] = f.f[fnd][top];
857 f.f[fnd][top] = NO_CARD;
858 f.z++;
859 f.w++;
860 } else if (u->f == WASTE) {
861 /* waste -> tableu */
862 /* move stock cards one position up to make room: */
863 for (int i = f.z; i >= u->n; i--) f.s[i+1] = f.s[i];
864 /* move one card from tableu to waste: */
865 int top = find_top(f.t[u->t]);
866 f.s[u->n] = f.t[u->t][top];
867 f.t[u->t][top] = NO_CARD;
868 f.z++;
869 f.w++;
870 } else if (u->t == FOUNDATION) {
871 /* tableu -> foundation */
872 int top_f = find_top(f.t[u->f]);
873 int top_t = find_top(f.f[u->n]);
874 /* close topcard if previous action caused turn_over(): */
875 if (u->o) f.t[u->f][top_f] *= -1;
876 /* move one card from foundation to tableu: */
877 f.t[u->f][top_f+1] = f.f[u->n][top_t];
878 f.f[u->n][top_t] = NO_CARD;
879 } else {
880 /* tableu -> tableu */
881 int top_f = find_top(f.t[u->f]);
882 int top_t = find_top(f.t[u->t]);
883 /* close topcard if previous action caused turn_over(): */
884 if (u->o) f.t[u->f][top_f] *= -1;
885 /* move n cards from tableu[f] to tableu[t]: */
886 for (int i = 0; i < u->n; i++) {
887 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
888 f.t[u->t][top_t-i] = NO_CARD;
889 }
890 }
891 #elif defined SPIDER
892 if (u->f == STOCK) {
893 /* stock -> tableu */
894 /*remove a card from each pile and put it back onto the stock:*/
895 for (int pile = NUM_PILES-1; pile >= 0; pile--) {
896 int top = find_top(f.t[pile]);
897 f.s[f.z++] = f.t[pile][top];
898 f.t[pile][top] = NO_CARD;
899 }
900 } else if (u->t == FOUNDATION) {
901 /* tableu -> foundation */
902 int top = find_top(f.t[u->f]);
903 /* close topcard if previous action caused turn_over(): */
904 if (u->o) f.t[u->f][top] *= -1;
905 /* append cards from foundation to tableu */
906 for (int i = RANK_K; i >= RANK_A; i--) {
907 f.t[u->f][++top] = f.f[u->n][i];
908 f.f[u->n][i] = NO_CARD;
909 }
910 /* decrement complete-foundation-counter: */
911 remove_if_complete(-1);
912 } else {
913 /* tableu -> tableu */
914 int top_f = find_top(f.t[u->f]);
915 int top_t = find_top(f.t[u->t]);
916 /* close topcard if previous action caused turn_over(): */
917 if (u->o) f.t[u->f][top_f] *= -1;
918 /* move n cards from tableu[f] to tableu[t]: */
919 for (int i = 0; i < u->n; i++) {
920 f.t[u->f][top_f+u->n-i] = f.t[u->t][top_t-i];
921 f.t[u->t][top_t-i] = NO_CARD;
922 }
923 }
924 #endif
925
926 void* old = f.u;
927 f.u = f.u->prev;
928 free(old);
929 }
930 void free_undo (struct undo* u) {
931 while (u && u != &undo_sentinel) {
932 void* old = u;
933 u = u->prev;
934 free (old);
935 }
936 }
937 //}}}
938
939 // initialization stuff {{{
940 void screen_setup (int enable) {
941 if (enable) {
942 raw_mode(1);
943 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
944 printf ("\033[H\033[J"); /* reset cursor, clear screen */
945 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
946 } else {
947 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
948 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
949 raw_mode(0);
950 }
951 }
952
953 void raw_mode(int enable) {
954 static struct termios saved_term_mode;
955 struct termios raw_term_mode;
956
957 if (enable) {
958 if (saved_term_mode.c_lflag == 0)/*don't overwrite stored mode*/
959 tcgetattr(STDIN_FILENO, &saved_term_mode);
960 raw_term_mode = saved_term_mode;
961 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
962 raw_term_mode.c_cc[VMIN] = 1 ;
963 raw_term_mode.c_cc[VTIME] = 0;
964 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
965 } else {
966 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
967 }
968 }
969
970 void signal_handler (int signum) {
971 switch (signum) {
972 case SIGCONT:
973 screen_setup(0);
974 screen_setup(1);
975 print_table(NO_HI, NO_HI);
976 break;
977 case SIGINT:
978 exit(128+SIGINT);
979 }
980 }
981 void signal_setup(void) {
982 struct sigaction saction;
983
984 saction.sa_handler = signal_handler;
985 sigemptyset(&saction.sa_mask);
986 saction.sa_flags = 0;
987 if (sigaction(SIGCONT, &saction, NULL) < 0) {
988 perror ("SIGCONT");
989 exit (1);
990 }
991 if (sigaction(SIGINT, &saction, NULL) < 0) {
992 perror ("SIGINT");
993 exit (1);
994 }
995 }
996 //}}}
997
998 //vim: foldmethod=marker
Imprint / Impressum