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