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