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