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 int v
; /* conserve vertical space */
58 const struct scheme
* s
;
62 /* stores a function pointer for every takeable action; called by game loop */
63 int (*action
[NUM_PLACES
][10])(int,int,int) = {
65 /* 1 2 3 4 5 6 7 stk wst fnd*/
66 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
67 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
68 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
69 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
70 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
71 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
72 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
73 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
74 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
75 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
77 /* 1 2 3 4 5 6 7 8 9 10*/
78 /* 1 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
79 /* 2 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
80 /* 3 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
81 /* 4 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
82 /* 5 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
83 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
84 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
85 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
86 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
87 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
88 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
93 int main(int argc
, char** argv
) {
94 /* opinionated defaults: */
95 op
.s
= &unicode_large_color
;
102 opterr
= 0; /* don't print message on unrecognized option */
103 while ((optget
= getopt (argc
, argv
, "+:hd:o:s:")) != -1) {
106 case 'd': /* difficulty */
107 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
108 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
109 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
112 case 'o': /* misc. options */
113 if(!strcmp(optarg
, "consv")) op
.v
= 1;
115 case 's': /* scheme */
116 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
117 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
118 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
121 case ':': //missing optarg
123 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
124 return optget
!= 'h';
135 case GAME_NEW
: goto newgame
;
137 print_table(NO_HI
, NO_HI
);
139 if (getchar()=='q') return 0;
141 case GAME_QUIT
: return 0;
149 print_table(NO_HI
, NO_HI
);
151 switch (get_cmd(&from
, &to
, &opt
)) {
153 switch (action
[from
][to
](from
,to
,opt
)) {
155 case ERR
: visbell(); break;
156 case WON
: return GAME_WON
;
159 case CMD_HINT
: //TODO: show a possible (and sensible) move
160 case CMD_JOIN
: //TODO: join any pile to here (longest if possible)
161 case CMD_INVAL
: visbell(); break;
162 case CMD_NEW
: return GAME_NEW
;
163 case CMD_AGAIN
: //TODO: restart with same seed
164 case CMD_QUIT
: return GAME_QUIT
;
166 print_table(NO_HI
, NO_HI
);
172 //TODO: free undo data structures
175 int find_top(card_t
* pile
) {
177 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
180 int first_movable(card_t
* pile
) {
182 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
185 void turn_over(card_t
* pile
) {
186 int top
= find_top(pile
);
187 if (pile
[top
] < 0) pile
[top
] *= -1;
189 int check_won(void) {
190 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
191 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
195 int rank_next (card_t a
, card_t b
) {
196 return get_rank(a
) == get_rank(b
)-1;
198 int is_consecutive (card_t
* pile
, int pos
) {
199 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
200 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
203 /* ranks consecutive? */
204 if (rank_next(pile
[pos
+1], pile
[pos
])) return 0;
205 /* color opposite? */
206 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
208 /* ranks consecutive? */
209 if (rank_next(pile
[pos
+1], pile
[pos
])) return 0;
211 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
216 void win_anim(void) {
217 printf ("\033[?25l"); /* hide cursor */
219 /* set cursor to random location */
220 int row
= 1+random()%(24-op
.s
->width
);
221 int col
= 1+random()%(80-op
.s
->height
);
223 /* draw random card */
224 int face
= 1 + random() % 52;
225 for (int l
= 0; l
< op
.s
->height
; l
++) {
226 printf ("\033[%d;%dH", row
+l
, col
);
227 printf ("%s", op
.s
->card
[face
][l
]);
231 /* exit on keypress */
232 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
233 if (poll (&p
, 1, 80)) goto fin
;
236 printf ("\033[?25h"); /* show cursor */
239 // takeable actions {{{
241 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
242 card_t card
= f
.s
[f
.w
];
243 /* move stack one over, so there are no gaps in it: */
244 for (int i
= f
.w
; i
< f
.z
-1; i
++)
247 f
.w
--; /* make previous card visible again */
250 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
251 (void) to
; (void) opt
; /* don't need */
252 int top_from
= find_top(f
.t
[from
]);
253 to
= get_suit(f
.t
[from
][top_from
]);
254 int top_to
= find_top(f
.f
[to
]);
255 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
256 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
257 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
258 f
.t
[from
][top_from
] = NO_CARD
;
259 turn_over(f
.t
[from
]);
260 if (check_won()) return WON
;
264 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
265 (void) from
; (void) to
; (void) opt
; /* don't need */
266 if (f
.w
< 0) return ERR
;
267 to
= get_suit(f
.s
[f
.w
]);
268 int top_to
= find_top(f
.f
[to
]);
269 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
270 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
271 f
.f
[to
][top_to
+1] = stack_take();
272 if (check_won()) return WON
;
277 int s2w(int from
, int to
, int opt
) { /* stock to waste */
278 (void) from
; (void) to
; (void) opt
; /* don't need */
279 if (f
.z
== 0) return ERR
;
281 if (f
.w
== f
.z
) f
.w
= -1;
284 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
285 (void) from
; (void) to
; (void) opt
; /* don't need */
286 if (f
.z
== 0) return ERR
;
288 if (f
.w
< -1) f
.w
= f
.z
-1;
291 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
292 (void) from
; /* don't need */
293 int top_to
= find_top(f
.t
[to
]);
295 int top_from
= find_top(f
.f
[from
]);
297 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
298 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
299 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
300 f
.f
[from
][top_from
] = NO_CARD
;
304 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
305 (void) from
; (void) opt
; /* don't need */
306 int top_to
= find_top(f
.t
[to
]);
307 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
308 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
309 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
310 f
.t
[to
][top_to
+1] = stack_take();
314 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
315 (void) opt
; /* don't need */
316 int top_to
= find_top(f
.t
[to
]);
317 int top_from
= find_top(f
.t
[from
]);
318 for (int i
= top_from
; i
>=0; i
--) {
319 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
320 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
321 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
322 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
323 /* move cards [i..top_from] to their destination */
324 for (;i
<= top_from
; i
++) {
326 f
.t
[to
][top_to
] = f
.t
[from
][i
];
327 f
.t
[from
][i
] = NO_CARD
;
329 turn_over(f
.t
[from
]);
333 return ERR
; /* no such move possible */
336 void remove_if_complete (card_t
* pile
) { //cleanup!
337 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
338 /* test if K...A complete; move to foundation if so */
339 int top_from
= find_top(pile
);
340 if (get_rank(pile
[top_from
]) != RANK_A
) return;
341 for (int i
= top_from
; i
>=0; i
--) {
342 if (!is_consecutive (pile
, i
)) return;
343 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
344 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
345 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
346 f
.f
[foundation
][j
] = pile
[i
];
355 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
356 int top_from
= find_top(f
.t
[from
]);
357 int top_to
= find_top(f
.t
[to
]);
358 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
360 for (int i
= top_from
; i
>= 0; i
--) {
361 if (!is_consecutive(f
.t
[from
], i
)) break;
363 /* is consecutive OR to empty pile and rank ok? */
364 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
365 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
366 for (;i
<= top_from
; i
++) {
368 f
.t
[to
][top_to
] = f
.t
[from
][i
];
369 f
.t
[from
][i
] = NO_CARD
;
371 turn_over(f
.t
[from
]);
372 remove_if_complete (f
.t
[to
]);
373 if (check_won()) return WON
;
378 return ERR
; /* no such move possible */
380 int s2t(int from
, int to
, int opt
) {
381 (void) from
; (void) to
; (void) opt
; /* don't need */
382 if (f
.z
<= 0) return ERR
; /* stack out of cards */
383 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
384 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
385 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
386 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
387 remove_if_complete (f
.t
[pile
]);
388 if (check_won()) return WON
;
393 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
396 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
397 #ifdef KLONDIKE // cursor functions{{{
398 void cursor_left (struct cursor
* cursor
) {
399 if (is_tableu(cursor
->pile
)) {
400 if (cursor
->pile
> 0) cursor
->pile
--;
402 } else { /* stock/waste/foundation*/
403 switch (cursor
->pile
) {
404 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
406 if (cursor
->opt
<= 0)
407 cursor
->pile
= WASTE
;
413 void cursor_down (struct cursor
* cursor
) {
414 if (!is_tableu(cursor
->pile
)) {
415 switch (cursor
->pile
) {
416 case STOCK
: cursor
->pile
= TAB_1
; break;
417 case WASTE
: cursor
->pile
= TAB_2
; break;
419 cursor
->pile
= TAB_4
+ cursor
->opt
;
424 void cursor_up (struct cursor
* cursor
) {
425 if (is_tableu(cursor
->pile
)) {
426 switch (cursor
->pile
) { //ugly :|
427 case TAB_1
: cursor
->pile
= STOCK
; break;
428 case TAB_2
: cursor
->pile
= WASTE
; break;
429 case TAB_3
: cursor
->pile
= WASTE
; break;
430 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
431 cursor
->opt
=cursor
->pile
-TAB_4
;
432 cursor
->pile
= FOUNDATION
;
437 void cursor_right (struct cursor
* cursor
) {
438 if (is_tableu(cursor
->pile
)) {
439 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
441 switch (cursor
->pile
) {
442 case STOCK
: cursor
->pile
= WASTE
; break;
443 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
445 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
451 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
452 void cursor_left (struct cursor
* cursor
) {
453 if (cursor
->pile
> 0) cursor
->pile
--;
456 void cursor_down (struct cursor
* cursor
) {
457 int first
= first_movable(f
.t
[cursor
->pile
]);
458 int top
= find_top(f
.t
[cursor
->pile
]);
459 if (first
+ cursor
->opt
< top
)
462 void cursor_up (struct cursor
* cursor
) {
463 if (cursor
->opt
> 0) cursor
->opt
--;
465 void cursor_right (struct cursor
* cursor
) {
466 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
470 void cursor_to (struct cursor
* cursor
, int pile
) {
475 #pragma GCC diagnostic pop
476 int get_cmd (int* from
, int* to
, int* opt
) {
477 //TODO: escape sequences (mouse, cursor keys)
479 struct cursor inactive
= {-1,-1};
480 static struct cursor active
= {0,0};
481 active
.opt
= 0; /* always reset offset, but keep pile */
484 from_l
: print_table(&active
, &inactive
);
488 /* direct addressing: */
489 case '1': *from
= TAB_1
; break;
490 case '2': *from
= TAB_2
; break;
491 case '3': *from
= TAB_3
; break;
492 case '4': *from
= TAB_4
; break;
493 case '5': *from
= TAB_5
; break;
494 case '6': *from
= TAB_6
; break;
495 case '7': *from
= TAB_7
; break;
497 case '8': *from
= TAB_8
; break;
498 case '9': *from
= TAB_9
; break;
499 case '0': *from
= TAB_10
;break;
500 #elif defined KLONDIKE
501 case '9': *from
= WASTE
; break;
502 case '0': *from
= FOUNDATION
; break;
503 case '8': /* fallthrough */
505 case '\n': /* shortcut for dealing from stock */
509 /* cursor keys addressing: */
510 case 'h': cursor_left (&active
); goto from_l
;
511 case 'j': cursor_down (&active
); goto from_l
;
512 case 'k': cursor_up (&active
); goto from_l
;
513 case 'l': cursor_right(&active
); goto from_l
;
514 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
515 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
516 //TODO: real cursor keys, home/end
517 case ' ': /* continue with second cursor */
519 if (*from
== STOCK
) {
524 *opt
= active
.opt
; /* when FOUNDATION */
531 fprintf (stderr
, ":");
532 raw_mode(0); /* turn on echo */
533 fgets (buf
, 256, stdin
);
536 case 'q': return CMD_QUIT
;
537 case 'n': return CMD_NEW
;
538 case 'r': return CMD_AGAIN
;
539 default: return CMD_INVAL
;
541 case 'J': return CMD_JOIN
;
542 case 'K': /* fallthrough */
543 case '?': return CMD_HINT
;
544 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
545 default: return CMD_INVAL
;
547 inactive
.pile
= *from
; /* for direct addressing highlighting */
548 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
551 to_l
: print_table(&active
, &inactive
);
555 case 'h': cursor_left (&active
); goto to_l
;
556 case 'j': cursor_down (&active
); goto to_l
;
557 case 'k': cursor_up (&active
); goto to_l
;
558 case 'l': cursor_right(&active
); goto to_l
;
559 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
560 case 'L': cursor_to(&active
,TAB_MAX
);goto to_l
;
561 case 'J': /* fallthrough; key makes no sense on destination */
564 break; /* continues with the foundation/empty tableu check */
565 case 'K': /* fallthrough */
566 case '?': return CMD_HINT
;
567 case 'G'&0x1f: return CMD_NONE
; /* cancel move with ^G */
568 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
570 if (t
< '0' || t
> '9') return CMD_INVAL
;
583 if (*from
== FOUNDATION
) {
584 int top
= find_top(f
.t
[*to
]);
585 if (top
< 0) return CMD_INVAL
;
586 int color
= get_color(f
.t
[*to
][top
]);
587 int choice_1
= 1-color
; /* selects piles of */
588 int choice_2
= 2+color
; /* the opposite color */
589 int top_c1
= find_top(f
.f
[choice_1
]);
590 int top_c2
= find_top(f
.f
[choice_2
]);
592 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
593 && top_c1
>= 0 ) << 0
594 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
595 && top_c2
>= 0 ) << 1) {
596 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
597 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
598 case (1<<1 | 1<<0): /* both, ask user which to pick from */
599 printf ("take from (1-4): "); fflush (stdout
);
600 *opt
= getchar() - '1';
601 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
603 default: return CMD_INVAL
; /* none matched */
605 /* `opt` is the foundation index (0..3) */
608 /* moving to empty tableu? */
609 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
610 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
611 int bottom
= first_movable(f
.t
[*from
]) + inactive
.opt
;
612 *opt
= get_rank(f
.t
[*from
][bottom
]);
615 int top
= find_top(f
.t
[*from
]);
616 if (top
< 0) return CMD_INVAL
;
617 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
618 *opt
= get_rank(f
.t
[*from
][top
]);
619 } else { /* only ask the user if it's unclear: */
620 printf ("\rup to (a23456789xjqk): ");
623 case 'a': case 'A': *opt
= RANK_A
; break;
624 case '0': /* fallthrough */
625 case 'x': case 'X': *opt
= RANK_X
; break;
626 case 'j': case 'J': *opt
= RANK_J
; break;
627 case 'q': case 'Q': *opt
= RANK_Q
; break;
628 case 'k': case 'K': *opt
= RANK_K
; break;
629 default: *opt
-= '1';
631 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
633 /* `opt` is the rank of the highest card to move */
640 f
= (const struct playfield
){0}; /* clear playfield */
641 card_t deck
[DECK_SIZE
*NUM_DECKS
];
642 int avail
= DECK_SIZE
*NUM_DECKS
;
643 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
645 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
646 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
647 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
648 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
651 srandom (time(NULL
));
652 long seed
= time(NULL
);
654 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
655 int j
= random() % (i
+1);
656 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
660 for (int i
= 0; i
< NUM_PILES
; i
++) {
662 int closed
= i
; /* pile n has n closed cards, then 1 open */
664 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
666 /* face down cards are negated: */
667 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
668 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
670 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
671 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
672 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
675 int is_movable(card_t
* pile
, int n
) {
677 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
679 int top
= find_top(pile
);
680 for (int i
= top
; i
>= 0; i
--) {
681 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
682 if (!is_consecutive(pile
, i
)) return 0;
683 if (i
== n
) return 1; /* card reached, must be movable */
688 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
689 printf ("%s%s%s%s%s%s%s",
690 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
692 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
694 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) { //{{{
695 printf("\033[2J\033[H"); /* clear screen, reset cursor */
697 /* print stock, waste and foundation: */
698 for (int line
= 0; line
< op
.s
->height
; line
++) {
700 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
701 (f
.w
< f
.z
-1)?op
.s
->facedown
702 :op
.s
->placeholder
)[line
]);
704 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
705 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
706 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
707 :op
.s
->placeholder
)[line
]);
708 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
710 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
711 int card
= find_top(f
.f
[pile
]);
712 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
713 inactive
->pile
==FOUNDATION
&& (
714 /* cursor addr. || direct addr. */
715 inactive
->opt
==pile
|| inactive
->opt
< 0
717 (card
< 0)?op
.s
->placeholder
[line
]
718 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
724 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
725 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
726 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
727 int spacer_to
= NUM_PILES
*op
.s
->width
-
728 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
729 for (int line
= 0; line
< op
.s
->height
; line
++) {
730 /* available stock: */
731 for (int i
= f
.z
/10; i
; i
--) {
732 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
733 else printf ("%s", op
.s
->halfstack
[line
]);
736 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
737 /* foundation (overlapping): */
738 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
739 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
740 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
741 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
748 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
751 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
752 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
753 && offset[pile] > cursor->opt
754 #define INC_OFFSET if (movable) offset[pile]++
756 /* print tableu piles: */
757 int row
[NUM_PILES
] = {0};
758 int line
[NUM_PILES
]= {0};
759 int label
[NUM_PILES
]={0};
761 int did_placeholders
= 0;
764 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
765 card_t card
= f
.t
[pile
][row
[pile
]];
766 card_t next
= f
.t
[pile
][row
[pile
]+1];
767 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
768 int empty
= !card
&& row
[pile
] == 0;
770 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
771 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
772 :(card
<0)?op
.s
->facedown
776 int extreme_overlap
= op
.v
&& find_top(f
.t
[pile
])>10;
777 /* normal overlap: */
778 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
779 /* extreme overlap on closed cards: */
780 || (extreme_overlap
&&
782 f
.t
[pile
][row
[pile
]] < 0 &&
783 f
.t
[pile
][row
[pile
]+1] <0)
784 /* extreme overlap on sequences: */
785 || (extreme_overlap
&&
786 line
[pile
] >= 1 && row
[pile
] > 0 &&
787 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
788 is_consecutive (f
.t
[pile
], row
[pile
]) &&
789 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
790 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
797 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
799 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
801 line_had_card
|= !!card
;
802 did_placeholders
|= row
[pile
] > 0;
805 } while (line_had_card
|| !did_placeholders
);
808 void visbell (void) {
809 printf ("\033[?5h"); fflush (stdout
);
811 printf ("\033[?5l"); fflush (stdout
);
814 void append_undo (int n
, int f
, int t
) {
815 (void)n
;(void)f
;(void)t
;
816 //check if we have to free redo buffer (.next)
819 //TODO: undo; needs operations to be written by x2y()
822 void screen_setup (int enable
) {
825 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
826 printf ("\033[H\033[J"); /* reset cursor, clear screen */
827 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
829 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
830 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
835 void raw_mode(int enable
) {
836 static struct termios saved_term_mode
;
837 struct termios raw_term_mode
;
840 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
841 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
842 raw_term_mode
= saved_term_mode
;
843 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
844 raw_term_mode
.c_cc
[VMIN
] = 1 ;
845 raw_term_mode
.c_cc
[VTIME
] = 0;
846 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
848 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
852 void signal_handler (int signum
) {
857 print_table(NO_HI
, NO_HI
);
863 void signal_setup(void) {
864 struct sigaction saction
;
866 saction
.sa_handler
= signal_handler
;
867 sigemptyset(&saction
.sa_mask
);
868 saction
.sa_flags
= 0;
869 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
873 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
879 //vim: foldmethod=marker