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