1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
17 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
18 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
20 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
24 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
26 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
29 #define get_suit(card) \
30 ((card-1) % NUM_SUITS)
31 #define get_rank(card) \
32 ((card-1) / NUM_SUITS)
33 #define get_color(card) \
34 ((get_suit(card) ^ get_suit(card)>>1) & 1)
36 #define is_tableu(where) (where <= TAB_MAX)
39 card_t s
[MAX_STOCK
]; /* stock */
40 int z
; /* stock size */
41 int w
; /* waste; index into stock (const -1 in spider) */
42 card_t f
[NUM_DECKS
*NUM_SUITS
][PILE_SIZE
]; /* foundation */
43 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
45 int f
; /* pile cards were taken from */
46 int t
; /* pile cards were moved to */
47 int n
; /* if tableu: number of cards moved */
48 /* else: index into stock/foundation */
55 int m
; /* difficulty mode */
57 const struct scheme
* s
;
61 /* stores a function pointer for every takeable action; called by game loop */
62 int (*action
[NUM_PLACES
][10])(int,int,int) = {
64 /* 1 2 3 4 5 6 7 stk wst fnd*/
65 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
66 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
67 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
68 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
69 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
70 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
71 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
72 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
73 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
74 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
76 /* 1 2 3 4 5 6 7 8 9 10*/
77 /* 1 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
78 /* 2 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
79 /* 3 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
80 /* 4 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
81 /* 5 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
82 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
83 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
84 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
85 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
86 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
87 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
92 int main(int argc
, char** argv
) {
93 /* opinionated defaults: */
94 op
.s
= &unicode_large_color
;
100 opterr
= 0; /* don't print message on unrecognized option */
101 while ((optget
= getopt (argc
, argv
, "+:hd:s:")) != -1) {
104 case 'd': /* difficulty */
105 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
106 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
107 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
110 case 's': /* scheme */
111 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
112 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
113 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
116 case ':': //missing optarg
118 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
119 return optget
!= 'h';
130 case GAME_NEW
: goto newgame
;
132 print_table(NO_HI
, NO_HI
);
134 if (getchar()=='q') return 0;
136 case GAME_QUIT
: return 0;
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_HINT
: //TODO: show a possible (and sensible) move
155 case CMD_HELP
: //TODO: display keyhelp
156 case CMD_JOIN
: //TODO: join any pile to here
157 case CMD_INVAL
: visbell(); break;
158 case CMD_NEW
: return GAME_NEW
;
159 case CMD_QUIT
: return GAME_QUIT
;
161 print_table(NO_HI
, NO_HI
);
167 //TODO: free undo data structures
170 int find_top(card_t
* pile
) {
172 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
175 int first_movable(card_t
* pile
) {
177 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
180 void turn_over(card_t
* pile
) {
181 int top
= find_top(pile
);
182 if (pile
[top
] < 0) pile
[top
] *= -1;
184 int check_won(void) {
185 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
186 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
190 int is_consecutive (card_t
* pile
, int pos
) {
191 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
192 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
195 /* ranks consecutive? */
196 if (get_rank(pile
[pos
+1]) != get_rank(pile
[pos
])-1) return 0;
197 /* color opposite? */
198 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
200 /* ranks consecutive? */
201 if (get_rank(pile
[pos
+1]) != get_rank(pile
[pos
])-1) return 0;
203 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
208 void win_anim(void) {
209 printf ("\033[?25l"); /* hide cursor */
211 /* set cursor to random location */
212 int row
= 1+random()%(24-op
.s
->width
);
213 int col
= 1+random()%(80-op
.s
->height
);
215 /* draw random card */
216 int face
= 1 + random() % 52;
217 for (int l
= 0; l
< op
.s
->height
; l
++) {
218 printf ("\033[%d;%dH", row
+l
, col
);
219 printf ("%s", op
.s
->card
[face
][l
]);
223 /* exit on keypress */
224 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
225 if (poll (&p
, 1, 80)) goto fin
;
228 printf ("\033[?25h"); /* show cursor */
231 // takeable actions {{{
232 //TODO XXX: deduplicate code (e.g. rank_consecutive() macro)
234 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
235 card_t card
= f
.s
[f
.w
];
236 /* move stack one over, so there are no gaps in it: */
237 for (int i
= f
.w
; i
< f
.z
-1; i
++)
240 f
.w
--; /* make previous card visible again */
243 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
244 (void) to
; (void) opt
; /* don't need */
245 int top_from
= find_top(f
.t
[from
]);
246 to
= get_suit(f
.t
[from
][top_from
]);
247 int top_to
= find_top(f
.f
[to
]);
248 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
249 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
250 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
251 f
.t
[from
][top_from
] = NO_CARD
;
252 turn_over(f
.t
[from
]);
253 if (check_won()) return WON
;
257 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
258 (void) from
; (void) to
; (void) opt
; /* don't need */
259 if (f
.w
< 0) return ERR
;
260 to
= get_suit(f
.s
[f
.w
]);
261 int top_to
= find_top(f
.f
[to
]);
262 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
263 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
264 f
.f
[to
][top_to
+1] = stack_take();
265 if (check_won()) return WON
;
270 int s2w(int from
, int to
, int opt
) { /* stock to waste */
271 (void) from
; (void) to
; (void) opt
; /* don't need */
272 if (f
.z
== 0) return ERR
;
274 if (f
.w
== f
.z
) f
.w
= -1;
277 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
278 (void) from
; (void) to
; (void) opt
; /* don't need */
279 if (f
.z
== 0) return ERR
;
281 if (f
.w
< -1) f
.w
= f
.z
-1;
284 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
285 (void) from
; /* don't need */
286 int top_to
= find_top(f
.t
[to
]);
288 int top_from
= find_top(f
.f
[from
]);
290 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
291 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
292 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
293 f
.f
[from
][top_from
] = NO_CARD
;
297 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
298 (void) from
; (void) opt
; /* don't need */
299 int top_to
= find_top(f
.t
[to
]);
300 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
301 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
302 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
303 f
.t
[to
][top_to
+1] = stack_take();
307 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
308 (void) opt
; /* don't need */
309 int top_to
= find_top(f
.t
[to
]);
310 int top_from
= find_top(f
.t
[from
]);
311 for (int i
= top_from
; i
>=0; i
--) {
312 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
313 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
314 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
315 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
316 /* move cards [i..top_from] to their destination */
317 for (;i
<= top_from
; i
++) {
319 f
.t
[to
][top_to
] = f
.t
[from
][i
];
320 f
.t
[from
][i
] = NO_CARD
;
322 turn_over(f
.t
[from
]);
326 return ERR
; /* no such move possible */
329 void remove_if_complete (card_t
* pile
) { //TODO: cleanup
330 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
331 /* test if K...A complete; move to foundation if so */
332 int top_from
= find_top(pile
);
333 if (get_rank(pile
[top_from
]) != RANK_A
) return;
334 for (int i
= top_from
; i
>=0; i
--) {
335 if (!is_consecutive (pile
, i
)) return;
336 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
337 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
338 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
339 f
.f
[foundation
][j
] = pile
[i
];
348 int t2t(int from
, int to
, int opt
) { //TODO: in dire need of cleanup
349 int top_from
= find_top(f
.t
[from
]);
350 int top_to
= find_top(f
.t
[to
]);
351 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
353 for (int i
= top_from
; i
>= 0; i
--) {
354 if (!is_consecutive(f
.t
[from
], i
)) break;
356 /* is consecutive OR to empty pile and rank ok? */
357 if ((get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1)
358 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
359 for (;i
<= top_from
; i
++) {
361 f
.t
[to
][top_to
] = f
.t
[from
][i
];
362 f
.t
[from
][i
] = NO_CARD
;
364 turn_over(f
.t
[from
]);
365 remove_if_complete (f
.t
[to
]);
366 if (check_won()) return WON
;
371 return ERR
; /* no such move possible */
373 int s2t(int from
, int to
, int opt
) {
374 (void) from
; (void) to
; (void) opt
; /* don't need */
375 if (f
.z
<= 0) return ERR
; /* stack out of cards */
376 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
377 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
378 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
379 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
380 remove_if_complete (f
.t
[pile
]);
381 if (check_won()) return WON
;
386 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
389 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
390 #ifdef KLONDIKE // cursor functions{{{
391 void cursor_left (struct cursor
* cursor
) {
392 if (is_tableu(cursor
->pile
)) {
393 if (cursor
->pile
> 0) cursor
->pile
--;
395 } else { /* stock/waste/foundation*/
396 switch (cursor
->pile
) {
397 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
399 if (cursor
->opt
<= 0)
400 cursor
->pile
= WASTE
;
406 void cursor_down (struct cursor
* cursor
) {
407 if (!is_tableu(cursor
->pile
)) {
408 switch (cursor
->pile
) {
409 case STOCK
: cursor
->pile
= TAB_1
; break;
410 case WASTE
: cursor
->pile
= TAB_2
; break;
412 cursor
->pile
= TAB_4
+ cursor
->opt
;
417 void cursor_up (struct cursor
* cursor
) {
418 if (is_tableu(cursor
->pile
)) {
419 switch (cursor
->pile
) { //ugly :|
420 case TAB_1
: cursor
->pile
= STOCK
; break;
421 case TAB_2
: cursor
->pile
= WASTE
; break;
422 case TAB_3
: cursor
->pile
= WASTE
; break;
423 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
424 cursor
->opt
=cursor
->pile
-TAB_4
;
425 cursor
->pile
= FOUNDATION
;
430 void cursor_right (struct cursor
* cursor
) {
431 if (is_tableu(cursor
->pile
)) {
432 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
434 switch (cursor
->pile
) {
435 case STOCK
: cursor
->pile
= WASTE
; break;
436 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
438 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
444 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
445 void cursor_left (struct cursor
* cursor
) {
446 if (cursor
->pile
> 0) cursor
->pile
--;
449 void cursor_down (struct cursor
* cursor
) {
450 int first
= first_movable(f
.t
[cursor
->pile
]);
451 int top
= find_top(f
.t
[cursor
->pile
]);
452 if (first
+ cursor
->opt
< top
)
455 void cursor_up (struct cursor
* cursor
) {
456 if (cursor
->opt
> 0) cursor
->opt
--;
458 void cursor_right (struct cursor
* cursor
) {
459 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
463 void cursor_to (struct cursor
* cursor
, int pile
) {
468 #pragma GCC diagnostic pop
469 int get_cmd (int* from
, int* to
, int* opt
) {
470 //TODO: escape sequences (mouse, cursor keys)
472 struct cursor inactive
= {-1,-1};
473 static struct cursor active
= {0,0};
474 active
.opt
= 0; /* always reset offset, but keep pile */
477 from_l
: print_table(&active
, &inactive
);
481 /* direct addressing: */
482 case '1': *from
= TAB_1
; break;
483 case '2': *from
= TAB_2
; break;
484 case '3': *from
= TAB_3
; break;
485 case '4': *from
= TAB_4
; break;
486 case '5': *from
= TAB_5
; break;
487 case '6': *from
= TAB_6
; break;
488 case '7': *from
= TAB_7
; break;
490 case '8': *from
= TAB_8
; break;
491 case '9': *from
= TAB_9
; break;
492 case '0': *from
= TAB_10
;break;
493 #elif defined KLONDIKE
494 case '9': *from
= WASTE
; break;
495 case '0': *from
= FOUNDATION
; break;
496 case '8': /* fallthrough */
498 case '\n': /* shortcut for dealing from stock */
502 /* cursor keys addressing: */
503 case 'h': cursor_left (&active
); goto from_l
;
504 case 'j': cursor_down (&active
); goto from_l
;
505 case 'k': cursor_up (&active
); goto from_l
;
506 case 'l': cursor_right(&active
); goto from_l
;
507 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
508 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
509 //TODO: real cursor keys, home/end
510 case ' ': /* continue with second cursor */
512 if (*from
== STOCK
) {
517 *opt
= active
.opt
; /* when FOUNDATION */
522 case 'q': return CMD_QUIT
;
523 case 'r': return CMD_NEW
;
524 case 'J': return CMD_JOIN
;
525 case 'K': return CMD_HINT
;
526 case '?': return CMD_HELP
;
527 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
528 default: return CMD_INVAL
;
530 inactive
.pile
= *from
; /* for direct addressing highlighting */
531 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
534 to_l
: print_table(&active
, &inactive
);
538 case 'h': cursor_left (&active
); goto to_l
;
539 case 'j': cursor_down (&active
); goto to_l
;
540 case 'k': cursor_up (&active
); goto to_l
;
541 case 'l': cursor_right(&active
); goto to_l
;
542 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
543 case 'L': cursor_to(&active
,TAB_MAX
);goto to_l
;
544 case 'J': /* fallthrough; key makes no sense on destination */
547 break; /* continues with the foundation/empty tableu check */
548 case 'K': return CMD_HELP
;
549 case 'G'&0x1f: return CMD_NONE
; /* cancel move with ^G */
550 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
552 if (t
< '0' || t
> '9') return CMD_INVAL
;
565 if (*from
== FOUNDATION
) {
566 int top
= find_top(f
.t
[*to
]);
567 if (top
< 0) return CMD_INVAL
;
568 int color
= get_color(f
.t
[*to
][top
]);
569 int choice_1
= 1-color
; /* selects piles of */
570 int choice_2
= 2+color
; /* the opposite color */
571 int top_c1
= find_top(f
.f
[choice_1
]);
572 int top_c2
= find_top(f
.f
[choice_2
]);
574 switch ((top_c1
>= 0 && get_rank(f
.t
[*to
][top
])-1
575 == get_rank(f
.f
[choice_1
][top_c1
])) << 0 |
576 (top_c2
>= 0 && get_rank(f
.t
[*to
][top
])-1
577 == get_rank(f
.f
[choice_2
][top_c2
])) << 1) {
578 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
579 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
580 case (1<<1 | 1<<0): /* both, ask user which to pick from */
581 printf ("take from (1-4): "); fflush (stdout
);
582 *opt
= getchar() - '1';
583 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
585 default: return CMD_INVAL
; /* none matched */
587 /* `opt` is the foundation index (0..3) */
590 /* moving to empty tableu? */
591 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
592 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
593 *opt
= get_rank(f
.t
[*from
][first_movable(f
.t
[*from
])+inactive
.opt
]);
596 int top
= find_top(f
.t
[*from
]);
597 if (top
< 0) return CMD_INVAL
;
598 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
599 *opt
= get_rank(f
.t
[*from
][top
]);
600 } else { /* only ask the user if it's unclear: */
601 printf ("\rup to (a23456789xjqk): ");
604 case 'a': case 'A': *opt
= RANK_A
; break;
605 case '0': /* fallthrough */
606 case 'x': case 'X': *opt
= RANK_X
; break;
607 case 'j': case 'J': *opt
= RANK_J
; break;
608 case 'q': case 'Q': *opt
= RANK_Q
; break;
609 case 'k': case 'K': *opt
= RANK_K
; break;
610 default: *opt
-= '1';
612 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
614 /* `opt` is the rank of the highest card to move */
621 f
= (const struct playfield
){0}; /* clear playfield */
622 card_t deck
[DECK_SIZE
*NUM_DECKS
];
623 int avail
= DECK_SIZE
*NUM_DECKS
;
624 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
626 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
627 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
628 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
629 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
632 srandom (time(NULL
));
633 long seed
= time(NULL
);
635 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
636 int j
= random() % (i
+1);
637 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
641 for (int i
= 0; i
< NUM_PILES
; i
++) {
643 int closed
= i
; /* pile n has n closed cards, then 1 open */
645 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
647 /* face down cards are negated: */
648 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
649 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
651 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
652 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
653 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
656 int is_movable(card_t
* pile
, int n
) {
658 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
660 int top
= find_top(pile
);
661 for (int i
= top
; i
>= 0; i
--) {
662 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
663 if (!is_consecutive(pile
, i
)) return 0;
664 if (i
== n
) return 1; /* card reached, must be movable */
669 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
670 printf ("%s%s%s%s%s%s%s",
671 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
673 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
675 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) { //{{{
676 printf("\033[2J\033[H"); /* clear screen, reset cursor */
678 /* print stock, waste and foundation: */
679 for (int line
= 0; line
< op
.s
->height
; line
++) {
681 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
682 (f
.w
< f
.z
-1)?op
.s
->facedown
683 :op
.s
->placeholder
)[line
]);
685 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
686 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
687 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
688 :op
.s
->placeholder
)[line
]);
689 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
691 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
692 int card
= find_top(f
.f
[pile
]);
693 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
694 inactive
->pile
==FOUNDATION
&& (
695 /* cursor addr. || direct addr. */
696 inactive
->opt
==pile
|| inactive
->opt
< 0
698 (card
< 0)?op
.s
->placeholder
[line
]
699 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
705 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
706 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
707 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
708 int spacer_to
= NUM_PILES
*op
.s
->width
-
709 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
710 for (int line
= 0; line
< op
.s
->height
; line
++) {
711 /* available stock: */
712 for (int i
= f
.z
/10; i
; i
--) {
713 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
714 else printf ("%s", op
.s
->halfstack
[line
]);
717 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
718 /* foundation (overlapping): */
719 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
720 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
721 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
722 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
729 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
732 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
733 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
734 && offset[pile] > cursor->opt
735 #define INC_OFFSET if (movable) offset[pile]++
737 /* print tableu piles: */
738 int row
[NUM_PILES
] = {0};
739 int line
[NUM_PILES
]= {0};
740 int label
[NUM_PILES
]={0};
744 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
745 card_t card
= f
.t
[pile
][row
[pile
]];
746 card_t next
= f
.t
[pile
][row
[pile
]+1];
747 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
748 int empty
= !card
&& row
[pile
] == 0;
750 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
751 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
752 :(card
<0)?op
.s
->facedown
756 int extreme_overlap
= 0; //TODO: activate iff space constrained (per pile)
757 /* normal overlap: */
758 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
759 /* extreme overlap on closed cards: */
760 || (extreme_overlap
&&
762 f
.t
[pile
][row
[pile
]] < 0 &&
763 f
.t
[pile
][row
[pile
]+1] <0)
764 /* extreme overlap on sequences: */
765 || (extreme_overlap
&&
766 line
[pile
] >= 1 && row
[pile
] > 0 &&
767 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
768 is_consecutive (f
.t
[pile
], row
[pile
]) &&
769 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
770 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
777 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
779 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
781 line_had_card
|= !!card
;
784 } while (line_had_card
);
787 void visbell (void) {
788 printf ("\033[?5h"); fflush (stdout
);
790 printf ("\033[?5l"); fflush (stdout
);
793 void append_undo (int n
, int f
, int t
) {
794 (void)n
;(void)f
;(void)t
;
795 //check if we have to free redo buffer (.next)
798 //TODO: undo; needs operations to be written by x2y()
801 void screen_setup (int enable
) {
804 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
805 printf ("\033[H\033[J"); /* reset cursor, clear screen */
806 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
808 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
809 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
814 void raw_mode(int enable
) {
815 static struct termios saved_term_mode
;
816 struct termios raw_term_mode
;
819 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
820 raw_term_mode
= saved_term_mode
;
821 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
822 raw_term_mode
.c_cc
[VMIN
] = 1 ;
823 raw_term_mode
.c_cc
[VTIME
] = 0;
824 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
826 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
830 void signal_handler (int signum
) {
835 print_table(NO_HI
, NO_HI
);
841 void signal_setup(void) {
842 struct sigaction saction
;
844 saction
.sa_handler
= signal_handler
;
845 sigemptyset(&saction
.sa_mask
);
846 saction
.sa_flags
= 0;
847 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
851 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
857 //vim: foldmethod=marker