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