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