1 #define _DEFAULT_SOURCE
15 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
16 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
18 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
22 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
24 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
27 #define get_suit(card) \
28 ((card-1) % NUM_SUITS)
29 #define get_rank(card) \
30 ((card-1) / NUM_SUITS)
31 #define get_color(card) \
32 ((get_suit(card) ^ get_suit(card)>>1) & 1)
34 #define is_tableu(where) (where <= TAB_MAX)
37 card_t s
[MAX_STOCK
]; /* stock */
38 int z
; /* stock size */
39 int w
; /* waste; index into stock (const -1 in spider) */
40 card_t f
[NUM_DECKS
*NUM_SUITS
][PILE_SIZE
]; /* foundation */
41 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
43 int f
; /* pile cards were taken from */
44 int t
; /* pile cards were moved to */
45 int n
; /* if tableu: number of cards moved */
46 /* else: index into stock/foundation */
53 int m
; /* difficulty mode */
55 const struct scheme
* s
;
59 /* stores a function pointer for every takeable action; called by game loop */
60 int (*action
[NUM_PLACES
][10])(int,int,int) = {
62 /* 1 2 3 4 5 6 7 stk wst fnd*/
63 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
64 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
65 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
66 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
67 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
68 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
69 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
70 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
71 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
72 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
74 /* 1 2 3 4 5 6 7 8 9 10*/
75 /* 1 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
76 /* 2 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
77 /* 3 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
78 /* 4 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
79 /* 5 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
80 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
81 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
82 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
83 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
84 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
85 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
90 int main(int argc
, char** argv
) {
91 /* opinionated defaults: */
92 op
.s
= &unicode_large_color
;
98 opterr
= 0; /* don't print message on unrecognized option */
99 while ((optget
= getopt (argc
, argv
, "+:hd:s:")) != -1) {
102 case 'd': /* difficulty */
103 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
104 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
105 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
108 case 's': /* scheme */
109 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
110 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
111 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
114 case ':': //missing optarg
116 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
117 return optget
!= 'h';
121 //TODO: signal setup, atexit()
126 case GAME_NEW
: goto newgame
;
128 print_table(NO_HI
, NO_HI
);
130 if (getchar()=='q') goto quit
;
132 case GAME_QUIT
: goto quit
;
136 screen_setup(0); //TODO: handled by atexit() in the future
144 print_table(NO_HI
, NO_HI
);
146 switch (get_cmd(&from
, &to
, &opt
)) {
148 switch (action
[from
][to
](from
,to
,opt
)) {
150 case ERR
: visbell(); break;
151 case WON
: return GAME_WON
;
154 case CMD_INVAL
: visbell(); break;
155 case CMD_NEW
: return GAME_NEW
;
156 case CMD_QUIT
: return GAME_QUIT
;
158 print_table(NO_HI
, NO_HI
);
162 int find_top(card_t
* pile
) {
164 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
167 int first_movable(card_t
* pile
) {
169 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
172 void turn_over(card_t
* pile
) {
173 int top
= find_top(pile
);
174 if (pile
[top
] < 0) pile
[top
] *= -1;
176 int check_won(void) {
177 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
178 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
182 int is_consecutive (card_t
* pile
, int pos
) {
183 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
184 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
187 /* ranks consecutive? */
188 if (get_rank(pile
[pos
+1]) != get_rank(pile
[pos
])-1) return 0;
189 /* color opposite? */
190 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
192 /* ranks consecutive? */
193 if (get_rank(pile
[pos
+1]) != get_rank(pile
[pos
])-1) return 0;
195 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
200 void win_anim(void) {
201 printf ("\033[?25l"); /* hide cursor */
203 /* set cursor to random location */
204 int row
= 1+random()%(24-op
.s
->width
);
205 int col
= 1+random()%(80-op
.s
->height
);
207 /* draw random card */
208 int face
= 1 + random() % 52;
209 for (int l
= 0; l
< op
.s
->height
; l
++) {
210 printf ("\033[%d;%dH", row
+l
, col
);
211 printf ("%s", op
.s
->card
[face
][l
]);
215 /* exit on keypress */
216 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
217 if (poll (&p
, 1, 80)) goto fin
;
220 printf ("\033[?25h"); /* show cursor */
223 // takeable actions {{{
224 //TODO XXX: deduplicate code (e.g. rank_consecutive() macro)
226 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
227 card_t card
= f
.s
[f
.w
];
228 /* move stack one over, so there are no gaps in it: */
229 for (int i
= f
.w
; i
< f
.z
-1; i
++)
232 f
.w
--; /* make previous card visible again */
235 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
236 (void) to
; (void) opt
; /* don't need */
237 int top_from
= find_top(f
.t
[from
]);
238 to
= get_suit(f
.t
[from
][top_from
]);
239 int top_to
= find_top(f
.f
[to
]);
240 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
241 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
242 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
243 f
.t
[from
][top_from
] = NO_CARD
;
244 turn_over(f
.t
[from
]);
245 if (check_won()) return WON
;
249 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
250 (void) from
; (void) to
; (void) opt
; /* don't need */
251 if (f
.w
< 0) return ERR
;
252 to
= get_suit(f
.s
[f
.w
]);
253 int top_to
= find_top(f
.f
[to
]);
254 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
255 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
256 f
.f
[to
][top_to
+1] = stack_take();
257 if (check_won()) return WON
;
262 int s2w(int from
, int to
, int opt
) { /* stock to waste */
263 (void) from
; (void) to
; (void) opt
; /* don't need */
264 if (f
.z
== 0) return ERR
;
266 if (f
.w
== f
.z
) f
.w
= -1;
269 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
270 (void) from
; (void) to
; (void) opt
; /* don't need */
271 if (f
.z
== 0) return ERR
;
273 if (f
.w
< -1) f
.w
= f
.z
-1;
276 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
277 (void) from
; /* don't need */
278 int top_to
= find_top(f
.t
[to
]);
280 int top_from
= find_top(f
.f
[from
]);
282 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
283 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
284 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
285 f
.f
[from
][top_from
] = NO_CARD
;
289 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
290 (void) from
; (void) opt
; /* don't need */
291 int top_to
= find_top(f
.t
[to
]);
292 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
293 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
294 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
295 f
.t
[to
][top_to
+1] = stack_take();
299 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
300 (void) opt
; /* don't need */
301 int top_to
= find_top(f
.t
[to
]);
302 int top_from
= find_top(f
.t
[from
]);
303 for (int i
= top_from
; i
>=0; i
--) {
304 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
305 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
306 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
307 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
308 /* move cards [i..top_from] to their destination */
309 for (;i
<= top_from
; i
++) {
311 f
.t
[to
][top_to
] = f
.t
[from
][i
];
312 f
.t
[from
][i
] = NO_CARD
;
314 turn_over(f
.t
[from
]);
318 return ERR
; /* no such move possible */
321 void remove_if_complete (card_t
* pile
) { //TODO: cleanup
322 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
323 /* test if K...A complete; move to foundation if so */
324 int top_from
= find_top(pile
);
325 if (get_rank(pile
[top_from
]) != RANK_A
) return;
326 for (int i
= top_from
; i
>=0; i
--) {
327 if (!is_consecutive (pile
, i
)) return;
328 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
329 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
330 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
331 f
.f
[foundation
][j
] = pile
[i
];
340 int t2t(int from
, int to
, int opt
) { //TODO: in dire need of cleanup
341 //TODO: segfaulted once on large column
342 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
344 int top_from
= find_top(f
.t
[from
]);
345 int top_to
= find_top(f
.t
[to
]);
346 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
348 for (int i
= top_from
; i
>= 0; i
--) {
349 if (!is_consecutive(f
.t
[from
], i
)) break;
351 /* is consecutive OR to empty pile and rank ok? */
352 if ((get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1)
353 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
354 for (;i
<= top_from
; i
++) {
356 f
.t
[to
][top_to
] = f
.t
[from
][i
];
357 f
.t
[from
][i
] = NO_CARD
;
359 turn_over(f
.t
[from
]);
360 remove_if_complete (f
.t
[to
]);
361 if (check_won()) return WON
;
366 return ERR
; /* no such move possible */
368 int s2t(int from
, int to
, int opt
) {
369 (void) from
; (void) to
; (void) opt
; /* don't need */
370 if (f
.z
<= 0) return ERR
; /* stack out of cards */
371 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
372 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
373 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
374 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
375 remove_if_complete (f
.t
[pile
]);
376 if (check_won()) return WON
;
381 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
384 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
385 #ifdef KLONDIKE // cursor functions{{{
386 void cursor_left (struct cursor
* cursor
) {
387 if (is_tableu(cursor
->pile
)) {
388 if (cursor
->pile
> 0) cursor
->pile
--;
390 } else { /* stock/waste/foundation*/
391 switch (cursor
->pile
) {
392 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
394 if (cursor
->opt
<= 0)
395 cursor
->pile
= WASTE
;
401 void cursor_down (struct cursor
* cursor
) {
402 if (!is_tableu(cursor
->pile
)) {
403 switch (cursor
->pile
) {
404 case STOCK
: cursor
->pile
= TAB_1
; break;
405 case WASTE
: cursor
->pile
= TAB_2
; break;
407 cursor
->pile
= TAB_4
+ cursor
->opt
;
412 void cursor_up (struct cursor
* cursor
) {
413 if (is_tableu(cursor
->pile
)) {
414 switch (cursor
->pile
) { //ugly :|
415 case TAB_1
: cursor
->pile
= STOCK
; break;
416 case TAB_2
: cursor
->pile
= WASTE
; break;
417 case TAB_3
: cursor
->pile
= WASTE
; break;
418 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
419 cursor
->opt
=cursor
->pile
-TAB_4
;
420 cursor
->pile
= FOUNDATION
;
425 void cursor_right (struct cursor
* cursor
) {
426 if (is_tableu(cursor
->pile
)) {
427 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
429 switch (cursor
->pile
) {
430 case STOCK
: cursor
->pile
= WASTE
; break;
431 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
433 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
439 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
440 void cursor_left (struct cursor
* cursor
) {
441 if (cursor
->pile
> 0) cursor
->pile
--;
444 void cursor_down (struct cursor
* cursor
) {
445 int first
= first_movable(f
.t
[cursor
->pile
]);
446 int top
= find_top(f
.t
[cursor
->pile
]);
447 if (first
+ cursor
->opt
< top
)
450 void cursor_up (struct cursor
* cursor
) {
451 if (cursor
->opt
> 0) cursor
->opt
--;
453 void cursor_right (struct cursor
* cursor
) {
454 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
458 #pragma GCC diagnostic pop
459 int get_cmd (int* from
, int* to
, int* opt
) {
460 //TODO: escape sequences (mouse, cursor keys)
462 struct cursor inactive
= {-1,-1};
463 static struct cursor active
= {0,0};
464 active
.opt
= 0; /* always reset offset, but keep pile */
467 from_l
: print_table(&active
, &inactive
);
471 /* direct addressing: */
472 case '1': *from
= TAB_1
; break;
473 case '2': *from
= TAB_2
; break;
474 case '3': *from
= TAB_3
; break;
475 case '4': *from
= TAB_4
; break;
476 case '5': *from
= TAB_5
; break;
477 case '6': *from
= TAB_6
; break;
478 case '7': *from
= TAB_7
; break;
480 case '8': *from
= TAB_8
; break;
481 case '9': *from
= TAB_9
; break;
482 case '0': *from
= TAB_10
;break;
483 #elif defined KLONDIKE
484 case '9': *from
= WASTE
; break;
485 case '0': *from
= FOUNDATION
; break;
486 case '8': /* fallthrough */
488 case '\n': /* shortcut for dealing from stock */
492 /* cursor keys addressing: */
493 case 'h': cursor_left (&active
); goto from_l
;
494 case 'j': cursor_down (&active
); goto from_l
;
495 case 'k': cursor_up (&active
); goto from_l
;
496 case 'l': cursor_right(&active
); goto from_l
;
497 //TODO: first/last tableu (H/L? 0/^/$?)
498 //TODO: real cursor keys
499 case ' ': /* continue with second cursor */
501 if (*from
== STOCK
) {
506 *opt
= active
.opt
; /* when FOUNDATION */
511 case 'q': return CMD_QUIT
;
512 case 'r': return CMD_NEW
; //TODO
513 case 'H': return CMD_HINT
; //TODO
514 case '?': return CMD_HELP
; //TODO
515 case '/': return CMD_FIND
; //TODO: highlight card of given rank (even non-movable)
516 case '\033': return CMD_INVAL
; //TODO: cntlseq
517 default: return CMD_INVAL
;
519 inactive
.pile
= *from
; /* for direct addressing highlighting */
520 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
523 to_l
: print_table(&active
, &inactive
);
527 case 'h': cursor_left (&active
); goto to_l
;
528 case 'j': cursor_down (&active
); goto to_l
;
529 case 'k': cursor_up (&active
); goto to_l
;
530 case 'l': cursor_right(&active
); goto to_l
;
533 break; /* continues with the foundation/empty tableu check */
535 if (t
< '0' || t
> '9') return CMD_INVAL
;
548 if (*from
== FOUNDATION
) {
549 int top
= find_top(f
.t
[*to
]);
550 if (top
< 0) return CMD_INVAL
;
551 int color
= get_color(f
.t
[*to
][top
]);
552 int choice_1
= 1-color
; /* selects piles of */
553 int choice_2
= 2+color
; /* the opposite color */
554 int top_c1
= find_top(f
.f
[choice_1
]);
555 int top_c2
= find_top(f
.f
[choice_2
]);
557 switch ((top_c1
>= 0 && get_rank(f
.t
[*to
][top
])-1
558 == get_rank(f
.f
[choice_1
][top_c1
])) << 0 |
559 (top_c2
>= 0 && get_rank(f
.t
[*to
][top
])-1
560 == get_rank(f
.f
[choice_2
][top_c2
])) << 1) {
561 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
562 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
563 case (1<<1 | 1<<0): /* both, ask user which to pick from */
564 printf ("take from (1-4): "); fflush (stdout
);
565 *opt
= getchar() - '1';
566 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
568 default: return CMD_INVAL
; /* none matched */
570 /* `opt` is the foundation index (0..3) */
573 /* moving to empty tableu? */
574 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
575 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
576 *opt
= get_rank(f
.t
[*from
][first_movable(f
.t
[*from
])+inactive
.opt
]);
579 int top
= find_top(f
.t
[*from
]);
580 if (top
< 0) return CMD_INVAL
;
581 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
582 *opt
= get_rank(f
.t
[*from
][top
]);
583 } else { /* only ask the user if it's unclear: */
584 printf ("\rup to (a23456789xjqk): ");
587 case 'a': case 'A': *opt
= RANK_A
; break;
588 case '0': /* fallthrough */
589 case 'x': case 'X': *opt
= RANK_X
; break;
590 case 'j': case 'J': *opt
= RANK_J
; break;
591 case 'q': case 'Q': *opt
= RANK_Q
; break;
592 case 'k': case 'K': *opt
= RANK_K
; break;
593 default: *opt
-= '1';
595 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
597 /* `opt` is the rank of the highest card to move */
604 f
= (const struct playfield
){0}; /* clear playfield */
605 card_t deck
[DECK_SIZE
*NUM_DECKS
];
606 int avail
= DECK_SIZE
*NUM_DECKS
;
607 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
609 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
610 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
611 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
612 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
615 srandom (time(NULL
));
616 long seed
= time(NULL
);
618 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
619 int j
= random() % (i
+1);
620 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
624 for (int i
= 0; i
< NUM_PILES
; i
++) {
626 int closed
= i
; /* pile n has n closed cards, then 1 open */
628 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
630 /* face down cards are negated: */
631 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
632 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
634 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
635 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
636 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
639 int is_movable(card_t
* pile
, int n
) {
641 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
643 int top
= find_top(pile
);
644 for (int i
= top
; i
>= 0; i
--) {
645 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
646 if (!is_consecutive(pile
, i
)) return 0;
647 if (i
== n
) return 1; /* card reached, must be movable */
652 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
653 printf ("%s%s%s%s%s%s%s",
654 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
656 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
658 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) { //{{{
659 printf("\033[2J\033[H"); /* clear screen, reset cursor */
661 /* print stock, waste and foundation: */
662 for (int line
= 0; line
< op
.s
->height
; line
++) {
664 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
665 (f
.w
< f
.z
-1)?op
.s
->facedown
666 :op
.s
->placeholder
)[line
]);
668 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
669 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
670 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
671 :op
.s
->placeholder
)[line
]);
672 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
674 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
675 int card
= find_top(f
.f
[pile
]);
676 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
677 inactive
->pile
==FOUNDATION
&& (
678 /* cursor addr. || direct addr. */
679 inactive
->opt
==pile
|| inactive
->opt
< 0
681 (card
< 0)?op
.s
->placeholder
[line
]
682 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
688 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
689 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
690 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
691 int spacer_to
= NUM_PILES
*op
.s
->width
-
692 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
693 for (int line
= 0; line
< op
.s
->height
; line
++) {
694 /* available stock: */
695 for (int i
= f
.z
/10; i
; i
--) {
696 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
697 else printf ("%s", op
.s
->halfstack
[line
]);
700 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
701 /* foundation (overlapping): */
702 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
703 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
704 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
705 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
712 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
715 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
716 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
717 && offset[pile] > cursor->opt
718 #define INC_OFFSET if (movable) offset[pile]++
720 /* print tableu piles: */
721 int row
[NUM_PILES
] = {0};
722 int line
[NUM_PILES
]= {0};
723 int label
[NUM_PILES
]={0};
727 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
728 card_t card
= f
.t
[pile
][row
[pile
]];
729 card_t next
= f
.t
[pile
][row
[pile
]+1];
730 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
731 int empty
= !card
&& row
[pile
] == 0;
733 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
734 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
735 :(card
<0)?op
.s
->facedown
739 int extreme_overlap
= 0; //TODO: activate iff space constrained (per pile)
740 /* normal overlap: */
741 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
742 /* extreme overlap on closed cards: */
743 || (extreme_overlap
&&
745 f
.t
[pile
][row
[pile
]] < 0 &&
746 f
.t
[pile
][row
[pile
]+1] <0)
747 /* extreme overlap on sequences: */
748 || (extreme_overlap
&&
749 line
[pile
] >= 1 && row
[pile
] > 0 &&
750 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
751 is_consecutive (f
.t
[pile
], row
[pile
]) &&
752 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
753 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
760 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
762 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
764 line_had_card
|= !!card
;
767 } while (line_had_card
);
770 void visbell (void) {
771 printf ("\033[?5h"); fflush (stdout
);
773 printf ("\033[?5l"); fflush (stdout
);
776 void append_undo (int n
, int f
, int t
) {
777 (void)n
;(void)f
;(void)t
;
778 //check if we have to free redo buffer (.next)
781 //TODO: undo; needs operations to be written by x2y()
784 void screen_setup (int enable
) {
787 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
788 printf ("\033[H\033[J"); /* reset cursor, clear screen */
789 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
791 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
792 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
797 void raw_mode(int enable
) { //{{{
798 static struct termios saved_term_mode
;
799 struct termios raw_term_mode
;
802 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
803 raw_term_mode
= saved_term_mode
;
804 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
805 raw_term_mode
.c_cc
[VMIN
] = 1 ;
806 raw_term_mode
.c_cc
[VTIME
] = 0;
807 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
809 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
813 //vim: foldmethod=marker