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