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