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