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