]> git.gir.st - solVItaire.git/blob - sol.c
implement 'up to' space/return
[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 for(;;) {
108 switch (get_cmd(&from, &to, &opt)) {
109 case CMD_MOVE:
110 switch (action[from][to](from,to,opt)) {
111 case OK: break;
112 case ERR: visbell(); break;
113 case WON: return GAME_WON;
114 }
115 break;
116 case CMD_HINT: //TODO: show a possible (and sensible) move
117 case CMD_JOIN: //TODO: join any pile to here (longest if possible)
118 case CMD_INVAL: visbell(); break;
119 case CMD_NEW: return GAME_NEW;
120 case CMD_AGAIN: //TODO: restart with same seed
121 case CMD_QUIT: return GAME_QUIT;
122 }
123 }
124 }
125
126 void quit(void) {
127 screen_setup(0);
128 //TODO: free undo data structures
129 }
130 //}}}
131
132 // card games helper functions {{{
133 #define get_suit(card) \
134 ((card-1) % NUM_SUITS)
135 #define get_rank(card) \
136 ((card-1) / NUM_SUITS)
137 #define get_color(card) \
138 ((get_suit(card) ^ get_suit(card)>>1) & 1)
139
140 #define is_tableu(where) (where <= TAB_MAX)
141
142 int find_top(card_t* pile) {
143 int i;
144 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
145 return i;
146 }
147 int first_movable(card_t* pile) {
148 int i = 0;
149 for (;pile[i] && !is_movable(pile, i); i++);
150 return i;
151 }
152 int turn_over(card_t* pile) {
153 int top = find_top(pile);
154 if (pile[top] < 0) {
155 pile[top] *= -1;
156 return 1;
157 } else return 0;
158 }
159 int check_won(void) {
160 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
161 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
162
163 return 1;
164 }
165 int rank_next (card_t a, card_t b) {
166 return get_rank(a) == get_rank(b)-1;
167 }
168 int is_consecutive (card_t* pile, int pos) {
169 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
170 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
171
172 #ifdef KLONDIKE
173 /* ranks consecutive? */
174 if (!rank_next(pile[pos+1], pile[pos])) return 0;
175 /* color opposite? */
176 if (get_color(pile[pos+1]) == get_color(pile[pos])) return 0;
177 #elif defined SPIDER
178 /* ranks consecutive? */
179 if (!rank_next(pile[pos+1], pile[pos])) return 0;
180 /* same suit? */
181 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0;
182 #endif
183
184 return 1;
185 }
186
187 int is_movable(card_t* pile, int n) {
188 #ifdef KLONDIKE
189 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
190 #elif defined SPIDER
191 int top = find_top(pile);
192 for (int i = top; i >= 0; i--) {
193 if (pile[i] <= NO_CARD) return 0; /*no card or card face down?*/
194 if (!is_consecutive(pile, i)) return 0;
195 if (i == n) return 1; /* card reached, must be movable */
196 }
197 return 0;
198 #endif
199 }
200 //}}}
201
202 // takeable actions {{{
203 #ifdef KLONDIKE
204 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
205 card_t card = f.s[f.w];
206 /* move stack one over, so there are no gaps in it: */
207 for (int i = f.w; i < f.z-1; i++)
208 f.s[i] = f.s[i+1];
209 f.z--;
210 f.w--; /* make previous card visible again */
211 return card;
212 }
213 int t2f(int from, int to, int opt) { /* tableu to foundation */
214 (void) to; (void) opt; /* don't need */
215 int top_from = find_top(f.t[from]);
216 to = get_suit(f.t[from][top_from]);
217 int top_to = find_top(f.f[to]);
218 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
219 || (top_to >= 0 && rank_next(f.f[to][top_to],f.t[from][top_from]))) {
220 f.f[to][top_to+1] = f.t[from][top_from];
221 f.t[from][top_from] = NO_CARD;
222 turn_over(f.t[from])
223 ?undo_push(from, FOUNDATION, -to)
224 :undo_push(from, FOUNDATION, to);
225 if (check_won()) return WON;
226 return OK;
227 } else return ERR;
228 }
229 int w2f(int from, int to, int opt) { /* waste to foundation */
230 (void) from; (void) to; (void) opt; /* don't need */
231 if (f.w < 0) return ERR;
232 to = get_suit(f.s[f.w]);
233 int top_to = find_top(f.f[to]);
234 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
235 || (top_to >= 0 && rank_next(f.f[to][top_to], f.s[f.w]))) {
236 undo_push(WASTE, FOUNDATION, f.w | to<<16); //ugly encoding :|
237 f.f[to][top_to+1] = stack_take();
238 if (check_won()) return WON;
239 return OK;
240 } else return ERR;
241
242 }
243 int s2w(int from, int to, int opt) { /* stock to waste */
244 (void) from; (void) to; (void) opt; /* don't need */
245 if (f.z == 0) return ERR;
246 f.w++;
247 if (f.w == f.z) f.w = -1;
248 return OK;
249 }
250 int w2s(int from, int to, int opt) { /* waste to stock (undo stock to waste) */
251 (void) from; (void) to; (void) opt; /* don't need */
252 if (f.z == 0) return ERR;
253 f.w--;
254 if (f.w < -1) f.w = f.z-1;
255 return OK;
256 }
257 int f2t(int from, int to, int opt) { /* foundation to tableu */
258 (void) from; /* don't need */
259 int top_to = find_top(f.t[to]);
260 from = opt;
261 int top_from = find_top(f.f[from]);
262
263 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
264 && (rank_next(f.f[from][top_from], f.t[to][top_to]))) {
265 f.t[to][top_to+1] = f.f[from][top_from];
266 f.f[from][top_from] = NO_CARD;
267 undo_push(FOUNDATION, to, from);
268 return OK;
269 } else return ERR;
270 }
271 int w2t(int from, int to, int opt) { /* waste to tableu */
272 (void) from; (void) opt; /* don't need */
273 int top_to = find_top(f.t[to]);
274 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
275 && (rank_next(f.s[f.w], f.t[to][top_to])))
276 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
277 undo_push(WASTE, to, f.w);
278 f.t[to][top_to+1] = stack_take();
279 return OK;
280 } else return ERR;
281 }
282 int t2t(int from, int to, int opt) { /* tableu to tableu */
283 (void) opt; /* don't need */
284 int top_to = find_top(f.t[to]);
285 int top_from = find_top(f.t[from]);
286 int count = 0; //NOTE: could probably be factored out
287 for (int i = top_from; i >=0; i--) {
288 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
289 && (rank_next(f.t[from][i], f.t[to][top_to]))
290 && f.t[from][i] > NO_CARD) /* card face up? */
291 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
292 /* move cards [i..top_from] to their destination */
293 for (;i <= top_from; i++) {
294 top_to++;
295 f.t[to][top_to] = f.t[from][i];
296 f.t[from][i] = NO_CARD;
297 count++;
298 }
299 turn_over(f.t[from])
300 ?undo_push(from, to, -count)
301 :undo_push(from, to, count);
302 return OK;
303 }
304 }
305 return ERR; /* no such move possible */
306 }
307 #elif defined SPIDER
308 void remove_if_complete (int pileno) { //cleanup!
309 static int foundation = 0; /* where to put pile onto (1 set per stack)*/
310 card_t* pile = f.t[pileno];
311 /* test if K...A complete; move to foundation if so */
312 int top_from = find_top(pile);
313 if (get_rank(pile[top_from]) != RANK_A) return;
314 for (int i = top_from; i>=0; i--) {
315 if (!is_consecutive (pile, i)) return;
316 if (i+RANK_K == top_from /* if ace to king: remove it */
317 && get_rank(pile[top_from-RANK_K]) == RANK_K) {
318 for(int i=top_from, j=0; i>top_from-NUM_RANKS; i--,j++){
319 f.f[foundation][j] = pile[i];
320 pile[i] = NO_CARD;
321 }
322 turn_over(pile)
323 ?undo_push(pileno, FOUNDATION, -foundation)
324 :undo_push(pileno, FOUNDATION, foundation);
325 foundation++;
326 return;
327 }
328 }
329 }
330 int t2t(int from, int to, int opt) { //in dire need of cleanup
331 int top_from = find_top(f.t[from]);
332 int top_to = find_top(f.t[to]);
333 int empty_to = (top_to < 0)? opt: -1; /* empty pile? */
334 int count = 0; //NOTE: could probably be factored out
335
336 for (int i = top_from; i >= 0; i--) {
337 if (!is_consecutive(f.t[from], i)) break;
338
339 /* is consecutive OR to empty pile and rank ok? */
340 if (rank_next(f.t[from][i], f.t[to][top_to])
341 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) {
342 for (;i <= top_from; i++) {
343 top_to++;
344 f.t[to][top_to] = f.t[from][i];
345 f.t[from][i] = NO_CARD;
346 count++;
347 }
348 turn_over(f.t[from])
349 ?undo_push(from, to, -count)
350 :undo_push(from, to, count);
351 remove_if_complete(to);
352 if (check_won()) return WON;
353 return OK;
354 }
355 }
356
357 return ERR; /* no such move possible */
358 }
359 int s2t(int from, int to, int opt) {
360 (void) from; (void) to; (void) opt; /* don't need */
361 if (f.z <= 0) return ERR; /* stack out of cards */
362 for (int pile = 0; pile < NUM_PILES; pile++)
363 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
364 for (int pile = 0; pile < NUM_PILES; pile++) {
365 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
366 remove_if_complete(pile);
367 if (check_won()) return WON;
368 }
369 undo_push(STOCK, TABLEU, 1); /*NOTE: puts 1 card on each tableu pile*/
370 return OK;
371 }
372 #endif
373 int nop(int from, int to, int opt) { (void)from;(void)to;(void)opt;return ERR; }
374 // }}}
375
376 // keyboard input handling {{{
377 // cursor functions{{{
378 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
379 #ifdef KLONDIKE
380 void cursor_left (struct cursor* cursor) {
381 if (is_tableu(cursor->pile)) {
382 if (cursor->pile > 0) cursor->pile--;
383 cursor->opt = 0;
384 } else { /* stock/waste/foundation*/
385 switch (cursor->pile) {
386 case WASTE: cursor->pile = STOCK; cursor->opt = 0; break;
387 case FOUNDATION:
388 if (cursor->opt <= 0)
389 cursor->pile = WASTE;
390 else
391 cursor->opt--;
392 }
393 }
394 }
395 void cursor_down (struct cursor* cursor) {
396 if (!is_tableu(cursor->pile)) {
397 switch (cursor->pile) {
398 case STOCK: cursor->pile = TAB_1; break;
399 case WASTE: cursor->pile = TAB_2; break;
400 case FOUNDATION:
401 cursor->pile = TAB_4 + cursor->opt;
402 }
403 cursor->opt = 0;
404 }
405 }
406 void cursor_up (struct cursor* cursor) {
407 if (is_tableu(cursor->pile)) {
408 switch (cursor->pile) { //ugly :|
409 case TAB_1: cursor->pile = STOCK; break;
410 case TAB_2: cursor->pile = WASTE; break;
411 case TAB_3: cursor->pile = WASTE; break;
412 case TAB_4: case TAB_5: case TAB_6: case TAB_7:
413 cursor->opt=cursor->pile-TAB_4;
414 cursor->pile = FOUNDATION;
415 break;
416 }
417 }
418 }
419 void cursor_right (struct cursor* cursor) {
420 if (is_tableu(cursor->pile)) {
421 if (cursor->pile < TAB_MAX) cursor->pile++;
422 } else {
423 switch (cursor->pile) {
424 case STOCK: cursor->pile = WASTE; break;
425 case WASTE: cursor->pile = FOUNDATION;cursor->opt = 0; break;
426 case FOUNDATION:
427 if (cursor->opt < NUM_DECKS*NUM_SUITS)
428 cursor->opt++;
429 }
430 }
431 }
432 #elif defined SPIDER
433 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
434 void cursor_left (struct cursor* cursor) {
435 if (cursor->pile > 0) cursor->pile--;
436 cursor->opt = 0;
437 }
438 void cursor_down (struct cursor* cursor) {
439 int first = first_movable(f.t[cursor->pile]);
440 int top = find_top(f.t[cursor->pile]);
441 if (first + cursor->opt < top)
442 cursor->opt++;
443 }
444 void cursor_up (struct cursor* cursor) {
445 if (cursor->opt > 0) cursor->opt--;
446 }
447 void cursor_right (struct cursor* cursor) {
448 if (cursor->pile < TAB_MAX) cursor->pile++;
449 cursor->opt = 0;
450 }
451 #endif
452 void cursor_to (struct cursor* cursor, int pile) {
453 cursor->pile = pile;
454 cursor->opt = 0;
455 }
456 #pragma GCC diagnostic pop
457 //}}}
458 int get_cmd (int* from, int* to, int* opt) {
459 //TODO: escape sequences (mouse, cursor keys)
460 int _f, t;
461 struct cursor inactive = {-1,-1};
462 static struct cursor active = {0,0};
463 active.opt = 0; /* always reset offset, but keep pile */
464
465 /***/
466 from_l: print_table(&active, &inactive);
467 _f = getchar();
468
469 switch (_f) {
470 /* direct addressing: */
471 case '1': *from = TAB_1; break;
472 case '2': *from = TAB_2; break;
473 case '3': *from = TAB_3; break;
474 case '4': *from = TAB_4; break;
475 case '5': *from = TAB_5; break;
476 case '6': *from = TAB_6; break;
477 case '7': *from = TAB_7; break;
478 #ifdef SPIDER
479 case '8': *from = TAB_8; break;
480 case '9': *from = TAB_9; break;
481 case '0': *from = TAB_10;break;
482 #elif defined KLONDIKE
483 case '9': *from = WASTE; break;
484 case '0': *from = FOUNDATION; break;
485 case '8': /* fallthrough */
486 #endif
487 case '\n': /* shortcut for dealing from stock */
488 *from = STOCK;
489 *to = WASTE;
490 return CMD_MOVE;
491 /* cursor keys addressing: */
492 case 'h': cursor_left (&active); goto from_l;
493 case 'j': cursor_down (&active); goto from_l;
494 case 'k': cursor_up (&active); goto from_l;
495 case 'l': cursor_right(&active); goto from_l;
496 case 'H': cursor_to(&active,TAB_1); goto from_l; /* leftmost tableu */
497 case 'L': cursor_to(&active,TAB_MAX);goto from_l; /* rigthmost tableu */
498 //TODO: real cursor keys, home/end
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 ':':
512 {char buf[256];
513 fprintf (stderr, ":");
514 raw_mode(0); /* turn on echo */
515 fgets (buf, 256, stdin);
516 raw_mode(1);
517 switch(buf[0]) {
518 case 'q': return CMD_QUIT;
519 case 'n': return CMD_NEW;
520 case 'r': return CMD_AGAIN;
521 default: return CMD_INVAL;
522 }}
523 case 'J': return CMD_JOIN;
524 case 'K': /* fallthrough */
525 case '?': return CMD_HINT;
526 case EOF: return CMD_NONE; /* sent by SIGCONT */
527 default: return CMD_INVAL;
528 }
529 inactive.pile = *from; /* for direct addressing highlighting */
530 if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL;
531
532 /***/
533 to_l: print_table(&active, &inactive);
534 t = getchar();
535
536 switch (t) {
537 case 'h': cursor_left (&active); goto to_l;
538 case 'j': cursor_down (&active); goto to_l;
539 case 'k': cursor_up (&active); goto to_l;
540 case 'l': cursor_right(&active); goto to_l;
541 case 'H': cursor_to(&active,TAB_1); goto to_l;
542 case 'L': cursor_to(&active,TAB_MAX);goto to_l;
543 case 'J': /* fallthrough; key makes no sense on destination */
544 case ' ':
545 *to = active.pile;
546 break; /* continues with the foundation/empty tableu check */
547 case 'K': /* fallthrough */
548 case '?': return CMD_HINT;
549 case 'G'&0x1f: return CMD_NONE; /* cancel move with ^G */
550 case EOF: return CMD_NONE; /* sent by SIGCONT */
551 default:
552 if (t < '0' || t > '9') return CMD_INVAL;
553 if (t == '0')
554 #ifdef KLONDIKE
555 *to = FOUNDATION;
556 #elif defined SPIDER
557 *to = TAB_10;
558 #endif
559 else
560 *to = t-'1';
561 }
562
563 /***/
564 #ifdef KLONDIKE
565 if (*from == FOUNDATION) {
566 int top = find_top(f.t[*to]);
567 if (top < 0) return CMD_INVAL;
568 int color = get_color(f.t[*to][top]);
569 int choice_1 = 1-color; /* selects piles of */
570 int choice_2 = 2+color; /* the opposite color */
571 int top_c1 = find_top(f.f[choice_1]);
572 int top_c2 = find_top(f.f[choice_2]);
573
574 switch ((rank_next(f.f[choice_1][top_c1], f.t[*to][top])
575 && top_c1 >= 0 ) << 0
576 |(rank_next(f.f[choice_2][top_c2], f.t[*to][top])
577 && top_c2 >= 0 ) << 1) {
578 case ( 1<<0): *opt = choice_1; break; /* choice_1 only */
579 case (1<<1 ): *opt = choice_2; break; /* choice_2 only */
580 case (1<<1 | 1<<0): /* both, ask user which to pick from */
581 printf ("take from (1-4): "); fflush (stdout);
582 *opt = getchar() - '1';
583 if (*opt < 0 || *opt > 3) return CMD_INVAL;
584 break;
585 default: return CMD_INVAL; /* none matched */
586 }
587 /* `opt` is the foundation index (0..3) */
588 }
589 #elif defined SPIDER
590 /* moving to empty tableu? */
591 if (is_tableu(*to) && f.t[*to][0] == NO_CARD) {
592 int bottom = first_movable(f.t[*from]);
593 if (inactive.opt >= 0) { /*if from was cursor addressed: */
594 *opt = get_rank(f.t[*from][bottom + inactive.opt]);
595 return CMD_MOVE;
596 }
597 int top = find_top(f.t[*from]);
598 if (top < 0) return CMD_INVAL;
599 if (top >= 0 && !is_movable(f.t[*from], top-1)) {
600 *opt = get_rank(f.t[*from][top]);
601 } else { /* only ask the user if it's unclear: */
602 printf ("\rup to ([a23456789xjqk] or space/return): ");
603 *opt = getchar();
604 switch (*opt) {
605 case ' ': *opt = get_rank(f.t[*from][top]); break;
606 case'\n': *opt = get_rank(f.t[*from][bottom]); break;
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