1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
15 // constants and data structures {{{
18 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
19 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
21 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
25 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
27 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
31 card_t s
[MAX_STOCK
]; /* stock */
32 int z
; /* stock size */
33 int w
; /* waste; index into stock (const -1 in spider) */
34 card_t f
[NUM_DECKS
*NUM_SUITS
][PILE_SIZE
]; /* foundation */
35 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
37 int f
; /* pile cards were taken from */
38 int t
; /* pile cards were moved to */
39 int n
; /* if tableu: number of cards moved */
40 /* else: index into stock/foundation */
47 int m
; /* difficulty mode */
49 int v
; /* conserve vertical space */
50 const struct scheme
* s
;
55 /* stores a function pointer for every takeable action; called by game loop */
56 int (*action
[NUM_PLACES
][10])(int,int,int) = {
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
},
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
},
86 // argv parsing, game loops, cleanup {{{
87 int main(int argc
, char** argv
) {
88 /* opinionated defaults: */
89 op
.s
= &unicode_large_color
;
96 opterr
= 0; /* don't print message on unrecognized option */
97 while ((optget
= getopt (argc
, argv
, "+:hd:o:s:")) != -1) {
100 case 'd': /* difficulty */
101 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
102 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
103 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
106 case 'o': /* misc. options */
107 if(!strcmp(optarg
, "consv")) op
.v
= 1;
109 case 's': /* scheme */
110 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
111 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
112 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
115 case ':': //missing optarg
117 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
118 return optget
!= 'h';
129 case GAME_NEW
: goto newgame
;
131 print_table(NO_HI
, NO_HI
);
133 if (getchar()=='q') return 0;
135 case GAME_QUIT
: return 0;
143 print_table(NO_HI
, NO_HI
);
145 switch (get_cmd(&from
, &to
, &opt
)) {
147 switch (action
[from
][to
](from
,to
,opt
)) {
149 case ERR
: visbell(); break;
150 case WON
: return GAME_WON
;
153 case CMD_HINT
: //TODO: show a possible (and sensible) move
154 case CMD_JOIN
: //TODO: join any pile to here (longest if possible)
155 case CMD_INVAL
: visbell(); break;
156 case CMD_NEW
: return GAME_NEW
;
157 case CMD_AGAIN
: //TODO: restart with same seed
158 case CMD_QUIT
: return GAME_QUIT
;
160 print_table(NO_HI
, NO_HI
);
166 //TODO: free undo data structures
170 // card games helper functions {{{
171 #define get_suit(card) \
172 ((card-1) % NUM_SUITS)
173 #define get_rank(card) \
174 ((card-1) / NUM_SUITS)
175 #define get_color(card) \
176 ((get_suit(card) ^ get_suit(card)>>1) & 1)
178 #define is_tableu(where) (where <= TAB_MAX)
180 int find_top(card_t
* pile
) {
182 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
185 int first_movable(card_t
* pile
) {
187 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
190 int turn_over(card_t
* pile
) {
191 int top
= find_top(pile
);
197 int check_won(void) {
198 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
199 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
203 int rank_next (card_t a
, card_t b
) {
204 return get_rank(a
) == get_rank(b
)-1;
206 int is_consecutive (card_t
* pile
, int pos
) {
207 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
208 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
211 /* ranks consecutive? */
212 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
213 /* color opposite? */
214 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
216 /* ranks consecutive? */
217 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
219 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
225 int is_movable(card_t
* pile
, int n
) {
227 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
229 int top
= find_top(pile
);
230 for (int i
= top
; i
>= 0; i
--) {
231 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
232 if (!is_consecutive(pile
, i
)) return 0;
233 if (i
== n
) return 1; /* card reached, must be movable */
240 // takeable actions {{{
242 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
243 card_t card
= f
.s
[f
.w
];
244 /* move stack one over, so there are no gaps in it: */
245 for (int i
= f
.w
; i
< f
.z
-1; i
++)
248 f
.w
--; /* make previous card visible again */
251 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
252 (void) to
; (void) opt
; /* don't need */
253 int top_from
= find_top(f
.t
[from
]);
254 to
= get_suit(f
.t
[from
][top_from
]);
255 int top_to
= find_top(f
.f
[to
]);
256 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
257 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
258 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
259 f
.t
[from
][top_from
] = NO_CARD
;
261 ?append_undo(from
, FOUNDATION
, -to
)
262 :append_undo(from
, FOUNDATION
, to
);
263 if (check_won()) return WON
;
267 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
268 (void) from
; (void) to
; (void) opt
; /* don't need */
269 if (f
.w
< 0) return ERR
;
270 to
= get_suit(f
.s
[f
.w
]);
271 int top_to
= find_top(f
.f
[to
]);
272 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
273 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
274 append_undo(WASTE
, FOUNDATION
, f
.w
| to
<<16); //ugly encoding :|
275 f
.f
[to
][top_to
+1] = stack_take();
276 if (check_won()) return WON
;
281 int s2w(int from
, int to
, int opt
) { /* stock to waste */
282 (void) from
; (void) to
; (void) opt
; /* don't need */
283 if (f
.z
== 0) return ERR
;
285 if (f
.w
== f
.z
) f
.w
= -1;
288 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
289 (void) from
; (void) to
; (void) opt
; /* don't need */
290 if (f
.z
== 0) return ERR
;
292 if (f
.w
< -1) f
.w
= f
.z
-1;
295 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
296 (void) from
; /* don't need */
297 int top_to
= find_top(f
.t
[to
]);
299 int top_from
= find_top(f
.f
[from
]);
301 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
302 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
303 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
304 f
.f
[from
][top_from
] = NO_CARD
;
305 append_undo(FOUNDATION
, to
, from
);
309 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
310 (void) from
; (void) opt
; /* don't need */
311 int top_to
= find_top(f
.t
[to
]);
312 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
313 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
314 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
315 append_undo(WASTE
, to
, f
.w
);
316 f
.t
[to
][top_to
+1] = stack_take();
320 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
321 (void) opt
; /* don't need */
322 int top_to
= find_top(f
.t
[to
]);
323 int top_from
= find_top(f
.t
[from
]);
324 int count
= 0; //NOTE: could probably be factored out
325 for (int i
= top_from
; i
>=0; i
--) {
326 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
327 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
328 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
329 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
330 /* move cards [i..top_from] to their destination */
331 for (;i
<= top_from
; i
++) {
333 f
.t
[to
][top_to
] = f
.t
[from
][i
];
334 f
.t
[from
][i
] = NO_CARD
;
338 ?append_undo(from
, to
, -count
)
339 :append_undo(from
, to
, count
);
343 return ERR
; /* no such move possible */
346 void remove_if_complete (int pileno
) { //cleanup!
347 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
348 card_t
* pile
= f
.t
[pileno
];
349 /* test if K...A complete; move to foundation if so */
350 int top_from
= find_top(pile
);
351 if (get_rank(pile
[top_from
]) != RANK_A
) return;
352 for (int i
= top_from
; i
>=0; i
--) {
353 if (!is_consecutive (pile
, i
)) return;
354 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
355 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
356 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
357 f
.f
[foundation
][j
] = pile
[i
];
361 ?append_undo(pileno
, FOUNDATION
, -foundation
)
362 :append_undo(pileno
, FOUNDATION
, foundation
);
368 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
369 int top_from
= find_top(f
.t
[from
]);
370 int top_to
= find_top(f
.t
[to
]);
371 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
372 int count
= 0; //NOTE: could probably be factored out
374 for (int i
= top_from
; i
>= 0; i
--) {
375 if (!is_consecutive(f
.t
[from
], i
)) break;
377 /* is consecutive OR to empty pile and rank ok? */
378 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
379 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
380 for (;i
<= top_from
; i
++) {
382 f
.t
[to
][top_to
] = f
.t
[from
][i
];
383 f
.t
[from
][i
] = NO_CARD
;
387 ?append_undo(from
, to
, -count
)
388 :append_undo(from
, to
, count
);
389 remove_if_complete(to
);
390 if (check_won()) return WON
;
395 return ERR
; /* no such move possible */
397 int s2t(int from
, int to
, int opt
) {
398 (void) from
; (void) to
; (void) opt
; /* don't need */
399 if (f
.z
<= 0) return ERR
; /* stack out of cards */
400 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
401 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
402 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
403 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
404 remove_if_complete(pile
);
405 if (check_won()) return WON
;
407 append_undo(STOCK
, TABLEU
, 1); /*NOTE: puts 1 card on each tableu pile*/
411 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
414 // keyboard input handling {{{
415 // cursor functions{{{
416 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
418 void cursor_left (struct cursor
* cursor
) {
419 if (is_tableu(cursor
->pile
)) {
420 if (cursor
->pile
> 0) cursor
->pile
--;
422 } else { /* stock/waste/foundation*/
423 switch (cursor
->pile
) {
424 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
426 if (cursor
->opt
<= 0)
427 cursor
->pile
= WASTE
;
433 void cursor_down (struct cursor
* cursor
) {
434 if (!is_tableu(cursor
->pile
)) {
435 switch (cursor
->pile
) {
436 case STOCK
: cursor
->pile
= TAB_1
; break;
437 case WASTE
: cursor
->pile
= TAB_2
; break;
439 cursor
->pile
= TAB_4
+ cursor
->opt
;
444 void cursor_up (struct cursor
* cursor
) {
445 if (is_tableu(cursor
->pile
)) {
446 switch (cursor
->pile
) { //ugly :|
447 case TAB_1
: cursor
->pile
= STOCK
; break;
448 case TAB_2
: cursor
->pile
= WASTE
; break;
449 case TAB_3
: cursor
->pile
= WASTE
; break;
450 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
451 cursor
->opt
=cursor
->pile
-TAB_4
;
452 cursor
->pile
= FOUNDATION
;
457 void cursor_right (struct cursor
* cursor
) {
458 if (is_tableu(cursor
->pile
)) {
459 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
461 switch (cursor
->pile
) {
462 case STOCK
: cursor
->pile
= WASTE
; break;
463 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
465 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
471 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
472 void cursor_left (struct cursor
* cursor
) {
473 if (cursor
->pile
> 0) cursor
->pile
--;
476 void cursor_down (struct cursor
* cursor
) {
477 int first
= first_movable(f
.t
[cursor
->pile
]);
478 int top
= find_top(f
.t
[cursor
->pile
]);
479 if (first
+ cursor
->opt
< top
)
482 void cursor_up (struct cursor
* cursor
) {
483 if (cursor
->opt
> 0) cursor
->opt
--;
485 void cursor_right (struct cursor
* cursor
) {
486 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
490 void cursor_to (struct cursor
* cursor
, int pile
) {
494 #pragma GCC diagnostic pop
496 int get_cmd (int* from
, int* to
, int* opt
) {
497 //TODO: escape sequences (mouse, cursor keys)
499 struct cursor inactive
= {-1,-1};
500 static struct cursor active
= {0,0};
501 active
.opt
= 0; /* always reset offset, but keep pile */
504 from_l
: print_table(&active
, &inactive
);
508 /* direct addressing: */
509 case '1': *from
= TAB_1
; break;
510 case '2': *from
= TAB_2
; break;
511 case '3': *from
= TAB_3
; break;
512 case '4': *from
= TAB_4
; break;
513 case '5': *from
= TAB_5
; break;
514 case '6': *from
= TAB_6
; break;
515 case '7': *from
= TAB_7
; break;
517 case '8': *from
= TAB_8
; break;
518 case '9': *from
= TAB_9
; break;
519 case '0': *from
= TAB_10
;break;
520 #elif defined KLONDIKE
521 case '9': *from
= WASTE
; break;
522 case '0': *from
= FOUNDATION
; break;
523 case '8': /* fallthrough */
525 case '\n': /* shortcut for dealing from stock */
529 /* cursor keys addressing: */
530 case 'h': cursor_left (&active
); goto from_l
;
531 case 'j': cursor_down (&active
); goto from_l
;
532 case 'k': cursor_up (&active
); goto from_l
;
533 case 'l': cursor_right(&active
); goto from_l
;
534 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
535 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
536 //TODO: real cursor keys, home/end
537 case ' ': /* continue with second cursor */
539 if (*from
== STOCK
) {
544 *opt
= active
.opt
; /* when FOUNDATION */
551 fprintf (stderr
, ":");
552 raw_mode(0); /* turn on echo */
553 fgets (buf
, 256, stdin
);
556 case 'q': return CMD_QUIT
;
557 case 'n': return CMD_NEW
;
558 case 'r': return CMD_AGAIN
;
559 default: return CMD_INVAL
;
561 case 'J': return CMD_JOIN
;
562 case 'K': /* fallthrough */
563 case '?': return CMD_HINT
;
564 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
565 default: return CMD_INVAL
;
567 inactive
.pile
= *from
; /* for direct addressing highlighting */
568 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
571 to_l
: print_table(&active
, &inactive
);
575 case 'h': cursor_left (&active
); goto to_l
;
576 case 'j': cursor_down (&active
); goto to_l
;
577 case 'k': cursor_up (&active
); goto to_l
;
578 case 'l': cursor_right(&active
); goto to_l
;
579 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
580 case 'L': cursor_to(&active
,TAB_MAX
);goto to_l
;
581 case 'J': /* fallthrough; key makes no sense on destination */
584 break; /* continues with the foundation/empty tableu check */
585 case 'K': /* fallthrough */
586 case '?': return CMD_HINT
;
587 case 'G'&0x1f: return CMD_NONE
; /* cancel move with ^G */
588 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
590 if (t
< '0' || t
> '9') return CMD_INVAL
;
603 if (*from
== FOUNDATION
) {
604 int top
= find_top(f
.t
[*to
]);
605 if (top
< 0) return CMD_INVAL
;
606 int color
= get_color(f
.t
[*to
][top
]);
607 int choice_1
= 1-color
; /* selects piles of */
608 int choice_2
= 2+color
; /* the opposite color */
609 int top_c1
= find_top(f
.f
[choice_1
]);
610 int top_c2
= find_top(f
.f
[choice_2
]);
612 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
613 && top_c1
>= 0 ) << 0
614 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
615 && top_c2
>= 0 ) << 1) {
616 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
617 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
618 case (1<<1 | 1<<0): /* both, ask user which to pick from */
619 printf ("take from (1-4): "); fflush (stdout
);
620 *opt
= getchar() - '1';
621 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
623 default: return CMD_INVAL
; /* none matched */
625 /* `opt` is the foundation index (0..3) */
628 /* moving to empty tableu? */
629 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
630 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
631 int bottom
= first_movable(f
.t
[*from
]) + inactive
.opt
;
632 *opt
= get_rank(f
.t
[*from
][bottom
]);
635 int top
= find_top(f
.t
[*from
]);
636 if (top
< 0) return CMD_INVAL
;
637 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
638 *opt
= get_rank(f
.t
[*from
][top
]);
639 } else { /* only ask the user if it's unclear: */
640 printf ("\rup to (a23456789xjqk): ");
643 case 'a': case 'A': *opt
= RANK_A
; break;
644 case '0': /* fallthrough */
645 case 'x': case 'X': *opt
= RANK_X
; break;
646 case 'j': case 'J': *opt
= RANK_J
; break;
647 case 'q': case 'Q': *opt
= RANK_Q
; break;
648 case 'k': case 'K': *opt
= RANK_K
; break;
649 default: *opt
-= '1';
651 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
653 /* `opt` is the rank of the highest card to move */
660 // shuffling and dealing {{{
662 f
= (const struct playfield
){0}; /* clear playfield */
663 card_t deck
[DECK_SIZE
*NUM_DECKS
];
664 int avail
= DECK_SIZE
*NUM_DECKS
;
665 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
667 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
668 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
669 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
670 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
673 srandom (time(NULL
));
674 long seed
= time(NULL
);
676 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
677 int j
= random() % (i
+1);
678 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
682 for (int i
= 0; i
< NUM_PILES
; i
++) {
684 int closed
= i
; /* pile n has n closed cards, then 1 open */
686 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
688 /* face down cards are negated: */
689 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
690 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
692 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
693 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
694 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
698 // screen drawing routines {{{
699 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
700 printf ("%s%s%s%s%s%s%s",
701 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
703 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
705 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
706 printf("\033[2J\033[H"); /* clear screen, reset cursor */
708 /* print stock, waste and foundation: */
709 for (int line
= 0; line
< op
.s
->height
; line
++) {
711 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
712 (f
.w
< f
.z
-1)?op
.s
->facedown
713 :op
.s
->placeholder
)[line
]);
715 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
716 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
717 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
718 :op
.s
->placeholder
)[line
]);
719 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
721 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
722 int card
= find_top(f
.f
[pile
]);
723 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
724 inactive
->pile
==FOUNDATION
&& (
725 /* cursor addr. || direct addr. */
726 inactive
->opt
==pile
|| inactive
->opt
< 0
728 (card
< 0)?op
.s
->placeholder
[line
]
729 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
735 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
736 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
737 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
738 int spacer_to
= NUM_PILES
*op
.s
->width
-
739 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
740 for (int line
= 0; line
< op
.s
->height
; line
++) {
741 /* available stock: */
742 for (int i
= f
.z
/10; i
; i
--) {
743 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
744 else printf ("%s", op
.s
->halfstack
[line
]);
747 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
748 /* foundation (overlapping): */
749 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
750 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
751 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
752 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
759 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
762 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
763 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
764 && offset[pile] > cursor->opt
765 #define INC_OFFSET if (movable) offset[pile]++
767 /* print tableu piles: */
768 int row
[NUM_PILES
] = {0};
769 int line
[NUM_PILES
]= {0};
770 int label
[NUM_PILES
]={0};
772 int did_placeholders
= 0;
775 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
776 card_t card
= f
.t
[pile
][row
[pile
]];
777 card_t next
= f
.t
[pile
][row
[pile
]+1];
778 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
779 int empty
= !card
&& row
[pile
] == 0;
781 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
782 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
783 :(card
<0)?op
.s
->facedown
787 int extreme_overlap
= op
.v
&& find_top(f
.t
[pile
])>10;
788 /* normal overlap: */
789 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
790 /* extreme overlap on closed cards: */
791 || (extreme_overlap
&&
793 f
.t
[pile
][row
[pile
]] < 0 &&
794 f
.t
[pile
][row
[pile
]+1] <0)
795 /* extreme overlap on sequences: */
796 || (extreme_overlap
&&
797 line
[pile
] >= 1 && row
[pile
] > 0 &&
798 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
799 is_consecutive (f
.t
[pile
], row
[pile
]) &&
800 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
801 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
808 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
810 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
812 line_had_card
|= !!card
;
813 did_placeholders
|= row
[pile
] > 0;
816 } while (line_had_card
|| !did_placeholders
);
819 void visbell (void) {
820 printf ("\033[?5h"); fflush (stdout
);
822 printf ("\033[?5l"); fflush (stdout
);
824 void win_anim(void) {
825 printf ("\033[?25l"); /* hide cursor */
827 /* set cursor to random location */
828 int row
= 1+random()%(24-op
.s
->width
);
829 int col
= 1+random()%(80-op
.s
->height
);
831 /* draw random card */
832 int face
= 1 + random() % 52;
833 for (int l
= 0; l
< op
.s
->height
; l
++) {
834 printf ("\033[%d;%dH", row
+l
, col
);
835 printf ("%s", op
.s
->card
[face
][l
]);
839 /* exit on keypress */
840 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
841 if (poll (&p
, 1, 80)) goto fin
;
844 printf ("\033[?25h"); /* show cursor */
850 void append_undo (int f
, int t
, int n
) {
851 (void)n
;(void)f
;(void)t
;
852 //check if we have to free redo buffer (.next)
855 //TODO: undo; needs operations to be written by x2y()
857 //TODO: free_undo(struct undo* l); //frees the list from here to then end (keeping .prev intact) // NOTE: this probably means we need to add a sentinel at the beginning (e.g. when deal()ing)
858 //TODO: apply_undo(struct undo* u); //undoes the operation pointed to by *u and moves the pointer one item back
859 //TODO: rename append->push, apply->pop (technically peek)
862 // initialization stuff {{{
863 void screen_setup (int enable
) {
866 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
867 printf ("\033[H\033[J"); /* reset cursor, clear screen */
868 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
870 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
871 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
876 void raw_mode(int enable
) {
877 static struct termios saved_term_mode
;
878 struct termios raw_term_mode
;
881 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
882 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
883 raw_term_mode
= saved_term_mode
;
884 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
885 raw_term_mode
.c_cc
[VMIN
] = 1 ;
886 raw_term_mode
.c_cc
[VTIME
] = 0;
887 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
889 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
893 void signal_handler (int signum
) {
898 print_table(NO_HI
, NO_HI
);
904 void signal_setup(void) {
905 struct sigaction saction
;
907 saction
.sa_handler
= signal_handler
;
908 sigemptyset(&saction
.sa_mask
);
909 saction
.sa_flags
= 0;
910 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
914 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
921 //vim: foldmethod=marker