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