1 #define _DEFAULT_SOURCE
2 #define _POSIX_C_SOURCE /* for sigaction */
19 /* stores a function pointer for every takeable action; called by game loop */
20 int (*action
[NUM_PLACES
][10])(int,int,int) = {
22 /* 1 2 3 4 5 6 7 stk wst fnd*/
23 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
24 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
25 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
26 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
27 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
28 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
29 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
30 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
31 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
32 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
34 /* 1 2 3 4 5 6 7 8 9 10*/
35 /* 1 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
36 /* 2 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
37 /* 3 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
38 /* 4 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
39 /* 5 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
40 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
41 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
42 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
43 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
44 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
45 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
50 // argv parsing, game loops, cleanup {{{
51 int main(int argc
, char** argv
) {
52 /* opinionated defaults: */
53 op
.s
= &unicode_large_color
;
60 opterr
= 0; /* don't print message on unrecognized option */
61 while ((optget
= getopt (argc
, argv
, "+:hd:o:s:")) != -1) {
64 case 'd': /* difficulty */
65 if(!strcmp(optarg
, "easy")) op
.m
= EASY
;
66 if(!strcmp(optarg
, "medium")) op
.m
= MEDIUM
;
67 if(!strcmp(optarg
, "hard")) op
.m
= NORMAL
;
70 case 'o': /* misc. options */
71 if(!strcmp(optarg
, "consv")) op
.v
= 1;
73 case 's': /* scheme */
74 if(!strcmp(optarg
,"color")) op
.s
= &unicode_large_color
;
75 if(!strcmp(optarg
, "mono")) op
.s
= &unicode_large_mono
;
76 if(!strcmp(optarg
,"small")) op
.s
= &unicode_small_mono
;
79 case ':': //missing optarg
81 fprintf (stderr
, SHORTHELP LONGHELP KEYHELP
, argv
[0]);
93 case GAME_NEW
: goto newgame
;
95 print_table(NO_HI
, NO_HI
);
97 if (getchar()=='q') return 0;
99 case GAME_QUIT
: return 0;
107 print_table(NO_HI
, NO_HI
);
109 switch (get_cmd(&from
, &to
, &opt
)) {
111 switch (action
[from
][to
](from
,to
,opt
)) {
113 case ERR
: visbell(); break;
114 case WON
: return GAME_WON
;
117 case CMD_HINT
: //TODO: show a possible (and sensible) move
118 case CMD_JOIN
: //TODO: join any pile to here (longest if possible)
119 case CMD_INVAL
: visbell(); break;
120 case CMD_NEW
: return GAME_NEW
;
121 case CMD_AGAIN
: //TODO: restart with same seed
122 case CMD_QUIT
: return GAME_QUIT
;
124 print_table(NO_HI
, NO_HI
);
130 //TODO: free undo data structures
134 // card games helper functions {{{
135 #define get_suit(card) \
136 ((card-1) % NUM_SUITS)
137 #define get_rank(card) \
138 ((card-1) / NUM_SUITS)
139 #define get_color(card) \
140 ((get_suit(card) ^ get_suit(card)>>1) & 1)
142 #define is_tableu(where) (where <= TAB_MAX)
144 int find_top(card_t
* pile
) {
146 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
149 int first_movable(card_t
* pile
) {
151 for (;pile
[i
] && !is_movable(pile
, i
); i
++);
154 int turn_over(card_t
* pile
) {
155 int top
= find_top(pile
);
161 int check_won(void) {
162 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
163 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
167 int rank_next (card_t a
, card_t b
) {
168 return get_rank(a
) == get_rank(b
)-1;
170 int is_consecutive (card_t
* pile
, int pos
) {
171 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
172 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
175 /* ranks consecutive? */
176 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
177 /* color opposite? */
178 if (get_color(pile
[pos
+1]) == get_color(pile
[pos
])) return 0;
180 /* ranks consecutive? */
181 if (!rank_next(pile
[pos
+1], pile
[pos
])) return 0;
183 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0;
189 int is_movable(card_t
* pile
, int n
) {
191 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
193 int top
= find_top(pile
);
194 for (int i
= top
; i
>= 0; i
--) {
195 if (pile
[i
] <= NO_CARD
) return 0; /*no card or card face down?*/
196 if (!is_consecutive(pile
, i
)) return 0;
197 if (i
== n
) return 1; /* card reached, must be movable */
204 // takeable actions {{{
206 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
207 card_t card
= f
.s
[f
.w
];
208 /* move stack one over, so there are no gaps in it: */
209 for (int i
= f
.w
; i
< f
.z
-1; i
++)
212 f
.w
--; /* make previous card visible again */
215 int t2f(int from
, int to
, int opt
) { /* tableu to foundation */
216 (void) to
; (void) opt
; /* don't need */
217 int top_from
= find_top(f
.t
[from
]);
218 to
= get_suit(f
.t
[from
][top_from
]);
219 int top_to
= find_top(f
.f
[to
]);
220 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
221 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
],f
.t
[from
][top_from
]))) {
222 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
223 f
.t
[from
][top_from
] = NO_CARD
;
225 ?undo_push(from
, FOUNDATION
, -to
)
226 :undo_push(from
, FOUNDATION
, to
);
227 if (check_won()) return WON
;
231 int w2f(int from
, int to
, int opt
) { /* waste to foundation */
232 (void) from
; (void) to
; (void) opt
; /* don't need */
233 if (f
.w
< 0) return ERR
;
234 to
= get_suit(f
.s
[f
.w
]);
235 int top_to
= find_top(f
.f
[to
]);
236 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
237 || (top_to
>= 0 && rank_next(f
.f
[to
][top_to
], f
.s
[f
.w
]))) {
238 undo_push(WASTE
, FOUNDATION
, f
.w
| to
<<16); //ugly encoding :|
239 f
.f
[to
][top_to
+1] = stack_take();
240 if (check_won()) return WON
;
245 int s2w(int from
, int to
, int opt
) { /* stock to waste */
246 (void) from
; (void) to
; (void) opt
; /* don't need */
247 if (f
.z
== 0) return ERR
;
249 if (f
.w
== f
.z
) f
.w
= -1;
252 int w2s(int from
, int to
, int opt
) { /* waste to stock (undo stock to waste) */
253 (void) from
; (void) to
; (void) opt
; /* don't need */
254 if (f
.z
== 0) return ERR
;
256 if (f
.w
< -1) f
.w
= f
.z
-1;
259 int f2t(int from
, int to
, int opt
) { /* foundation to tableu */
260 (void) from
; /* don't need */
261 int top_to
= find_top(f
.t
[to
]);
263 int top_from
= find_top(f
.f
[from
]);
265 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
266 && (rank_next(f
.f
[from
][top_from
], f
.t
[to
][top_to
]))) {
267 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
268 f
.f
[from
][top_from
] = NO_CARD
;
269 undo_push(FOUNDATION
, to
, from
);
273 int w2t(int from
, int to
, int opt
) { /* waste to tableu */
274 (void) from
; (void) opt
; /* don't need */
275 int top_to
= find_top(f
.t
[to
]);
276 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
277 && (rank_next(f
.s
[f
.w
], f
.t
[to
][top_to
])))
278 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
279 undo_push(WASTE
, to
, f
.w
);
280 f
.t
[to
][top_to
+1] = stack_take();
284 int t2t(int from
, int to
, int opt
) { /* tableu to tableu */
285 (void) opt
; /* don't need */
286 int top_to
= find_top(f
.t
[to
]);
287 int top_from
= find_top(f
.t
[from
]);
288 int count
= 0; //NOTE: could probably be factored out
289 for (int i
= top_from
; i
>=0; i
--) {
290 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
291 && (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
]))
292 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
293 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
294 /* move cards [i..top_from] to their destination */
295 for (;i
<= top_from
; i
++) {
297 f
.t
[to
][top_to
] = f
.t
[from
][i
];
298 f
.t
[from
][i
] = NO_CARD
;
302 ?undo_push(from
, to
, -count
)
303 :undo_push(from
, to
, count
);
307 return ERR
; /* no such move possible */
310 void remove_if_complete (int pileno
) { //cleanup!
311 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
312 card_t
* pile
= f
.t
[pileno
];
313 /* test if K...A complete; move to foundation if so */
314 int top_from
= find_top(pile
);
315 if (get_rank(pile
[top_from
]) != RANK_A
) return;
316 for (int i
= top_from
; i
>=0; i
--) {
317 if (!is_consecutive (pile
, i
)) return;
318 if (i
+RANK_K
== top_from
/* if ace to king: remove it */
319 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) {
320 for(int i
=top_from
, j
=0; i
>top_from
-NUM_RANKS
; i
--,j
++){
321 f
.f
[foundation
][j
] = pile
[i
];
325 ?undo_push(pileno
, FOUNDATION
, -foundation
)
326 :undo_push(pileno
, FOUNDATION
, foundation
);
332 int t2t(int from
, int to
, int opt
) { //in dire need of cleanup
333 int top_from
= find_top(f
.t
[from
]);
334 int top_to
= find_top(f
.t
[to
]);
335 int empty_to
= (top_to
< 0)? opt
: -1; /* empty pile? */
336 int count
= 0; //NOTE: could probably be factored out
338 for (int i
= top_from
; i
>= 0; i
--) {
339 if (!is_consecutive(f
.t
[from
], i
)) break;
341 /* is consecutive OR to empty pile and rank ok? */
342 if (rank_next(f
.t
[from
][i
], f
.t
[to
][top_to
])
343 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) {
344 for (;i
<= top_from
; i
++) {
346 f
.t
[to
][top_to
] = f
.t
[from
][i
];
347 f
.t
[from
][i
] = NO_CARD
;
351 ?undo_push(from
, to
, -count
)
352 :undo_push(from
, to
, count
);
353 remove_if_complete(to
);
354 if (check_won()) return WON
;
359 return ERR
; /* no such move possible */
361 int s2t(int from
, int to
, int opt
) {
362 (void) from
; (void) to
; (void) opt
; /* don't need */
363 if (f
.z
<= 0) return ERR
; /* stack out of cards */
364 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
365 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
366 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
367 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
368 remove_if_complete(pile
);
369 if (check_won()) return WON
;
371 undo_push(STOCK
, TABLEU
, 1); /*NOTE: puts 1 card on each tableu pile*/
375 int nop(int from
, int to
, int opt
) { (void)from
;(void)to
;(void)opt
;return ERR
; }
378 // keyboard input handling {{{
379 // cursor functions{{{
380 #pragma GCC diagnostic ignored "-Wswitch" //not ideal :|
382 void cursor_left (struct cursor
* cursor
) {
383 if (is_tableu(cursor
->pile
)) {
384 if (cursor
->pile
> 0) cursor
->pile
--;
386 } else { /* stock/waste/foundation*/
387 switch (cursor
->pile
) {
388 case WASTE
: cursor
->pile
= STOCK
; cursor
->opt
= 0; break;
390 if (cursor
->opt
<= 0)
391 cursor
->pile
= WASTE
;
397 void cursor_down (struct cursor
* cursor
) {
398 if (!is_tableu(cursor
->pile
)) {
399 switch (cursor
->pile
) {
400 case STOCK
: cursor
->pile
= TAB_1
; break;
401 case WASTE
: cursor
->pile
= TAB_2
; break;
403 cursor
->pile
= TAB_4
+ cursor
->opt
;
408 void cursor_up (struct cursor
* cursor
) {
409 if (is_tableu(cursor
->pile
)) {
410 switch (cursor
->pile
) { //ugly :|
411 case TAB_1
: cursor
->pile
= STOCK
; break;
412 case TAB_2
: cursor
->pile
= WASTE
; break;
413 case TAB_3
: cursor
->pile
= WASTE
; break;
414 case TAB_4
: case TAB_5
: case TAB_6
: case TAB_7
:
415 cursor
->opt
=cursor
->pile
-TAB_4
;
416 cursor
->pile
= FOUNDATION
;
421 void cursor_right (struct cursor
* cursor
) {
422 if (is_tableu(cursor
->pile
)) {
423 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
425 switch (cursor
->pile
) {
426 case STOCK
: cursor
->pile
= WASTE
; break;
427 case WASTE
: cursor
->pile
= FOUNDATION
;cursor
->opt
= 0; break;
429 if (cursor
->opt
< NUM_DECKS
*NUM_SUITS
)
435 /*NOTE: one can't highlight the stock due to me being too lazy to implement it*/
436 void cursor_left (struct cursor
* cursor
) {
437 if (cursor
->pile
> 0) cursor
->pile
--;
440 void cursor_down (struct cursor
* cursor
) {
441 int first
= first_movable(f
.t
[cursor
->pile
]);
442 int top
= find_top(f
.t
[cursor
->pile
]);
443 if (first
+ cursor
->opt
< top
)
446 void cursor_up (struct cursor
* cursor
) {
447 if (cursor
->opt
> 0) cursor
->opt
--;
449 void cursor_right (struct cursor
* cursor
) {
450 if (cursor
->pile
< TAB_MAX
) cursor
->pile
++;
454 void cursor_to (struct cursor
* cursor
, int pile
) {
458 #pragma GCC diagnostic pop
460 int get_cmd (int* from
, int* to
, int* opt
) {
461 //TODO: escape sequences (mouse, cursor keys)
463 struct cursor inactive
= {-1,-1};
464 static struct cursor active
= {0,0};
465 active
.opt
= 0; /* always reset offset, but keep pile */
468 from_l
: print_table(&active
, &inactive
);
472 /* direct addressing: */
473 case '1': *from
= TAB_1
; break;
474 case '2': *from
= TAB_2
; break;
475 case '3': *from
= TAB_3
; break;
476 case '4': *from
= TAB_4
; break;
477 case '5': *from
= TAB_5
; break;
478 case '6': *from
= TAB_6
; break;
479 case '7': *from
= TAB_7
; break;
481 case '8': *from
= TAB_8
; break;
482 case '9': *from
= TAB_9
; break;
483 case '0': *from
= TAB_10
;break;
484 #elif defined KLONDIKE
485 case '9': *from
= WASTE
; break;
486 case '0': *from
= FOUNDATION
; break;
487 case '8': /* fallthrough */
489 case '\n': /* shortcut for dealing from stock */
493 /* cursor keys addressing: */
494 case 'h': cursor_left (&active
); goto from_l
;
495 case 'j': cursor_down (&active
); goto from_l
;
496 case 'k': cursor_up (&active
); goto from_l
;
497 case 'l': cursor_right(&active
); goto from_l
;
498 case 'H': cursor_to(&active
,TAB_1
); goto from_l
; /* leftmost tableu */
499 case 'L': cursor_to(&active
,TAB_MAX
);goto from_l
; /* rigthmost tableu */
500 //TODO: real cursor keys, home/end
501 case ' ': /* continue with second cursor */
503 if (*from
== STOCK
) {
508 *opt
= active
.opt
; /* when FOUNDATION */
515 fprintf (stderr
, ":");
516 raw_mode(0); /* turn on echo */
517 fgets (buf
, 256, stdin
);
520 case 'q': return CMD_QUIT
;
521 case 'n': return CMD_NEW
;
522 case 'r': return CMD_AGAIN
;
523 default: return CMD_INVAL
;
525 case 'J': return CMD_JOIN
;
526 case 'K': /* fallthrough */
527 case '?': return CMD_HINT
;
528 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
529 default: return CMD_INVAL
;
531 inactive
.pile
= *from
; /* for direct addressing highlighting */
532 if (is_tableu(*from
) && f
.t
[*from
][0] == NO_CARD
) return CMD_INVAL
;
535 to_l
: print_table(&active
, &inactive
);
539 case 'h': cursor_left (&active
); goto to_l
;
540 case 'j': cursor_down (&active
); goto to_l
;
541 case 'k': cursor_up (&active
); goto to_l
;
542 case 'l': cursor_right(&active
); goto to_l
;
543 case 'H': cursor_to(&active
,TAB_1
); goto to_l
;
544 case 'L': cursor_to(&active
,TAB_MAX
);goto to_l
;
545 case 'J': /* fallthrough; key makes no sense on destination */
548 break; /* continues with the foundation/empty tableu check */
549 case 'K': /* fallthrough */
550 case '?': return CMD_HINT
;
551 case 'G'&0x1f: return CMD_NONE
; /* cancel move with ^G */
552 case EOF
: return CMD_NONE
; /* sent by SIGCONT */
554 if (t
< '0' || t
> '9') return CMD_INVAL
;
567 if (*from
== FOUNDATION
) {
568 int top
= find_top(f
.t
[*to
]);
569 if (top
< 0) return CMD_INVAL
;
570 int color
= get_color(f
.t
[*to
][top
]);
571 int choice_1
= 1-color
; /* selects piles of */
572 int choice_2
= 2+color
; /* the opposite color */
573 int top_c1
= find_top(f
.f
[choice_1
]);
574 int top_c2
= find_top(f
.f
[choice_2
]);
576 switch ((rank_next(f
.f
[choice_1
][top_c1
], f
.t
[*to
][top
])
577 && top_c1
>= 0 ) << 0
578 |(rank_next(f
.f
[choice_2
][top_c2
], f
.t
[*to
][top
])
579 && top_c2
>= 0 ) << 1) {
580 case ( 1<<0): *opt
= choice_1
; break; /* choice_1 only */
581 case (1<<1 ): *opt
= choice_2
; break; /* choice_2 only */
582 case (1<<1 | 1<<0): /* both, ask user which to pick from */
583 printf ("take from (1-4): "); fflush (stdout
);
584 *opt
= getchar() - '1';
585 if (*opt
< 0 || *opt
> 3) return CMD_INVAL
;
587 default: return CMD_INVAL
; /* none matched */
589 /* `opt` is the foundation index (0..3) */
592 /* moving to empty tableu? */
593 if (is_tableu(*to
) && f
.t
[*to
][0] == NO_CARD
) {
594 if (inactive
.opt
>= 0) { /*if from was cursor addressed: */
595 int bottom
= first_movable(f
.t
[*from
]) + inactive
.opt
;
596 *opt
= get_rank(f
.t
[*from
][bottom
]);
599 int top
= find_top(f
.t
[*from
]);
600 if (top
< 0) return CMD_INVAL
;
601 if (top
>= 0 && !is_movable(f
.t
[*from
], top
-1)) {
602 *opt
= get_rank(f
.t
[*from
][top
]);
603 } else { /* only ask the user if it's unclear: */
604 printf ("\rup to (a23456789xjqk): ");
607 case 'a': case 'A': *opt
= RANK_A
; break;
608 case '0': /* fallthrough */
609 case 'x': case 'X': *opt
= RANK_X
; break;
610 case 'j': case 'J': *opt
= RANK_J
; break;
611 case 'q': case 'Q': *opt
= RANK_Q
; break;
612 case 'k': case 'K': *opt
= RANK_K
; break;
613 default: *opt
-= '1';
615 if (*opt
< RANK_A
|| *opt
> RANK_K
) return ERR
;
617 /* `opt` is the rank of the highest card to move */
624 // shuffling and dealing {{{
626 f
= (const struct playfield
){0}; /* clear playfield */
627 card_t deck
[DECK_SIZE
*NUM_DECKS
];
628 int avail
= DECK_SIZE
*NUM_DECKS
;
629 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
631 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
632 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
633 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
634 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
637 srandom (time(NULL
));
638 long seed
= time(NULL
);
640 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { /* fisher-yates */
641 int j
= random() % (i
+1);
642 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
646 for (int i
= 0; i
< NUM_PILES
; i
++) {
648 int closed
= i
; /* pile n has n closed cards, then 1 open */
650 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
652 /* face down cards are negated: */
653 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
654 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
656 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
657 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
658 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
662 // screen drawing routines {{{
663 void print_hi(int invert
, int grey_bg
, int bold
, char* str
) {
664 printf ("%s%s%s%s%s%s%s",
665 bold
?"\033[1m":"", invert
?"\033[7m":"", grey_bg
?"\033[100m":"",
667 grey_bg
?"\033[49m":"", invert
?"\033[27m":"",bold
?"\033[22m":"");
669 void print_table(const struct cursor
* active
, const struct cursor
* inactive
) {
670 printf("\033[2J\033[H"); /* clear screen, reset cursor */
672 /* print stock, waste and foundation: */
673 for (int line
= 0; line
< op
.s
->height
; line
++) {
675 print_hi (active
->pile
== STOCK
, inactive
->pile
== STOCK
, 1, (
676 (f
.w
< f
.z
-1)?op
.s
->facedown
677 :op
.s
->placeholder
)[line
]);
679 print_hi (active
->pile
== WASTE
, inactive
->pile
== WASTE
, 1, (
680 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
681 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
682 :op
.s
->placeholder
)[line
]);
683 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
685 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
686 int card
= find_top(f
.f
[pile
]);
687 print_hi (active
->pile
==FOUNDATION
&& active
->opt
==pile
,
688 inactive
->pile
==FOUNDATION
&& (
689 /* cursor addr. || direct addr. */
690 inactive
->opt
==pile
|| inactive
->opt
< 0
692 (card
< 0)?op
.s
->placeholder
[line
]
693 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
699 int fdone
; for (fdone
= NUM_DECKS
*NUM_SUITS
; fdone
; fdone
--)
700 if (f
.f
[fdone
-1][RANK_K
]) break; /*number of completed stacks*/
701 int spacer_from
= f
.z
?(f
.z
/10-1) * op
.s
->halfwidth
[0] + op
.s
->width
:0;
702 int spacer_to
= NUM_PILES
*op
.s
->width
-
703 ((fdone
?(fdone
-1) * op
.s
->halfwidth
[1]:0)+op
.s
->width
);
704 for (int line
= 0; line
< op
.s
->height
; line
++) {
705 /* available stock: */
706 for (int i
= f
.z
/10; i
; i
--) {
707 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
708 else printf ("%s", op
.s
->halfstack
[line
]);
711 for (int i
= spacer_from
; i
< spacer_to
; i
++) printf (" ");
712 /* foundation (overlapping): */
713 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
714 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
715 if (f
.f
[i
][RANK_K
]) printf ("%.*s", op
.s
->halfwidth
[2],
716 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
723 #define DO_HI(cursor) cursor->pile == pile && (movable || empty)
726 int offset
[NUM_PILES
]={1,1,1,1,1,1,1,1,1,1}; // :|
727 #define DO_HI(cursor) cursor->pile == pile && (movable || empty) \
728 && offset[pile] > cursor->opt
729 #define INC_OFFSET if (movable) offset[pile]++
731 /* print tableu piles: */
732 int row
[NUM_PILES
] = {0};
733 int line
[NUM_PILES
]= {0};
734 int label
[NUM_PILES
]={0};
736 int did_placeholders
= 0;
739 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
740 card_t card
= f
.t
[pile
][row
[pile
]];
741 card_t next
= f
.t
[pile
][row
[pile
]+1];
742 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
743 int empty
= !card
&& row
[pile
] == 0;
745 print_hi (DO_HI(active
), DO_HI(inactive
), movable
, (
746 (!card
&& row
[pile
] == 0)?op
.s
->placeholder
747 :(card
<0)?op
.s
->facedown
751 int extreme_overlap
= op
.v
&& find_top(f
.t
[pile
])>10;
752 /* normal overlap: */
753 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)
754 /* extreme overlap on closed cards: */
755 || (extreme_overlap
&&
757 f
.t
[pile
][row
[pile
]] < 0 &&
758 f
.t
[pile
][row
[pile
]+1] <0)
759 /* extreme overlap on sequences: */
760 || (extreme_overlap
&&
761 line
[pile
] >= 1 && row
[pile
] > 0 &&
762 f
.t
[pile
][row
[pile
]-1] > NO_CARD
&&
763 is_consecutive (f
.t
[pile
], row
[pile
]) &&
764 is_consecutive (f
.t
[pile
], row
[pile
]-1) &&
765 f
.t
[pile
][row
[pile
]+1] != NO_CARD
)
772 if(!card
&& !label
[pile
] && row
[pile
]>0&&line
[pile
]>0) {
774 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
776 line_had_card
|= !!card
;
777 did_placeholders
|= row
[pile
] > 0;
780 } while (line_had_card
|| !did_placeholders
);
783 void visbell (void) {
784 printf ("\033[?5h"); fflush (stdout
);
786 printf ("\033[?5l"); fflush (stdout
);
788 void win_anim(void) {
789 printf ("\033[?25l"); /* hide cursor */
791 /* set cursor to random location */
792 int row
= 1+random()%(24-op
.s
->width
);
793 int col
= 1+random()%(80-op
.s
->height
);
795 /* draw random card */
796 int face
= 1 + random() % 52;
797 for (int l
= 0; l
< op
.s
->height
; l
++) {
798 printf ("\033[%d;%dH", row
+l
, col
);
799 printf ("%s", op
.s
->card
[face
][l
]);
803 /* exit on keypress */
804 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
805 if (poll (&p
, 1, 80)) goto fin
;
808 printf ("\033[?25h"); /* show cursor */
814 void undo_push (int f
, int t
, int n
) {
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()
821 void undo_pop (struct undo
* u
) {
822 //TODO: undoes the operation pointed to by *u and moves the pointer one item back
824 void free_undo (struct undo
* u
) {
825 //TODO: frees the list from here to then end (keeping .prev intact)
826 // NOTE: this probably means we need to add a sentinel at the beginning (e.g. when deal()ing)
830 // initialization stuff {{{
831 void screen_setup (int enable
) {
834 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
835 printf ("\033[H\033[J"); /* reset cursor, clear screen */
836 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
838 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
839 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
844 void raw_mode(int enable
) {
845 static struct termios saved_term_mode
;
846 struct termios raw_term_mode
;
849 if (saved_term_mode
.c_lflag
== 0)/*don't overwrite stored mode*/
850 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
851 raw_term_mode
= saved_term_mode
;
852 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
853 raw_term_mode
.c_cc
[VMIN
] = 1 ;
854 raw_term_mode
.c_cc
[VTIME
] = 0;
855 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
857 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
861 void signal_handler (int signum
) {
866 print_table(NO_HI
, NO_HI
);
872 void signal_setup(void) {
873 struct sigaction saction
;
875 saction
.sa_handler
= signal_handler
;
876 sigemptyset(&saction
.sa_mask
);
877 saction
.sa_flags
= 0;
878 if (sigaction(SIGCONT
, &saction
, NULL
) < 0) {
882 if (sigaction(SIGINT
, &saction
, NULL
) < 0) {
889 //vim: foldmethod=marker