]>
git.gir.st - solVItaire.git/blob - sol.c
1 #define _DEFAULT_SOURCE
14 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
15 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
17 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
21 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
23 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
26 #define get_suit(card) \
27 ((card-1) % NUM_SUITS)
28 #define get_rank(card) \
29 ((card-1) / NUM_SUITS)
30 #define get_color(card) \
31 ((get_suit(card) ^ get_suit(card)>>1) & 1)
34 //TODO: stock and waste are incompatible with undo{}
35 card_t s
[MAX_STOCK
]; /* stock */
36 int z
; /* stock size */
37 int w
; /* waste; index into stock (const -1 in spider) */
38 card_t f
[NUM_DECKS
*NUM_SUITS
][PILE_SIZE
]; /* foundation (XXX@spider:complete set gets put on seperate pile, so undo is easy) */
39 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
41 int from
; /* pile cards were taken from */
42 int to
; /* pile cards were moved to */
43 int n
; /* number of cards moved */
50 int m
; /* difficulty mode */
52 const struct scheme
* s
;
56 /* stores a function pointer for every takeable action; called by game loop */
57 int (*action
[NUM_PLACES
][10])(int,int) = {
59 /* 1 2 3 4 5 6 7 stk wst fnd*/
60 /* 1 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
61 /* 2 */ { t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
62 /* 3 */ { t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
63 /* 4 */ { t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
, t2f
},
64 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
, t2f
},
65 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
, t2f
},
66 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
, t2f
},
67 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
, nop
},
68 /*wst*/ { w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
, w2f
},
69 /*fnd*/ { f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
, nop
},
71 /* 1 2 3 4 5 6 7 8 9 10*/
72 /* 1 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
73 /* 2 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
74 /* 3 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
75 /* 4 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
76 /* 5 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
77 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
78 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
79 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
80 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
81 /*10 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
82 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
87 int main(int argc
, char** argv
) {
88 (void) argc
;(void) argv
;
89 op
.s
= &unicode_large_color
;
91 op
.m
= MEDIUM
; //TODO: make configurable
95 sol(); //TODO: restart, etc.
105 switch (get_cmd(&from
, &to
)) {
107 switch (action
[from
][to
](from
,to
)) {
109 case ERR
: visbell(); break;
113 getchar(); /* consume char left by win_anim() */
117 case CMD_QUIT
: return;
123 int find_top(card_t
* pile
) {
125 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
128 void turn_over(card_t
* pile
) {
129 int top
= find_top(pile
);
130 if (pile
[top
] < 0) pile
[top
] *= -1;
132 int check_won(void) {
133 for (int pile
= 0; pile
< NUM_DECKS
*NUM_COLORS
; pile
++)
134 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
138 void win_anim(void) {
139 printf ("\033[?25l"); /* hide cursor */
141 /* set cursor to random location */
142 int row
= 1+random()%(24-op
.s
->width
);
143 int col
= 1+random()%(80-op
.s
->height
);
145 /* draw random card */
146 int face
= 1 + random() % 52;
147 for (int l
= 0; l
< op
.s
->height
; l
++) {
148 printf ("\033[%d;%dH", row
+l
, col
);
149 printf ("%s", op
.s
->card
[face
][l
]);
153 /* exit on keypress */
154 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
155 if (poll (&p
, 1, 80)) return;
158 // takeable actions {{{
160 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
161 card_t card
= f
.s
[f
.w
];
162 /* move stack one over, so there are no gaps in it: */
163 for (int i
= f
.w
; i
< f
.z
-1; i
++)
166 f
.w
--; /* make previous card visible again */
169 int t2f(int from
, int to
) { /* tableu to foundation */
170 (void) to
; //don't need
171 int top_from
= find_top(f
.t
[from
]);
172 to
= get_suit(f
.t
[from
][top_from
]);
173 int top_to
= find_top(f
.f
[to
]);
174 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
175 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
176 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
177 f
.t
[from
][top_from
] = NO_CARD
;
178 turn_over(f
.t
[from
]);
179 if (check_won()) return WON
;
183 int w2f(int from
, int to
) { /* waste to foundation */
184 (void) from
; (void) to
; //don't need
185 if (f
.w
< 0) return ERR
;
186 to
= get_suit(f
.s
[f
.w
]);
187 int top_to
= find_top(f
.f
[to
]);
188 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
189 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
190 f
.f
[to
][top_to
+1] = stack_take();
191 if (check_won()) return WON
;
196 int s2w(int from
, int to
) { /* stock to waste */
197 (void) from
; (void) to
; //don't need
198 if (f
.z
== 0) return ERR
;
200 if (f
.w
== f
.z
) f
.w
= -1;
203 int w2s(int from
, int to
) { /* waste to stock (undoes stock to waste) */
204 (void) from
; (void) to
; //don't need
205 if (f
.z
== 0) return ERR
;
207 if (f
.w
< -1) f
.w
= f
.z
-1;
210 int f2t(int from
, int to
) { /* foundation to tableu */
211 int top_to
= find_top(f
.t
[to
]);
212 printf ("take from (1-4): "); fflush (stdout
);
213 from
= getchar() - '1';
214 if (from
< 0 || from
> 3) return ERR
;
215 int top_from
= find_top(f
.f
[from
]);
217 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
218 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
219 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
220 f
.f
[from
][top_from
] = NO_CARD
;
224 int w2t(int from
, int to
) { /* waste to tableu */
225 (void) from
; //don't need
226 int top_to
= find_top(f
.t
[to
]);
227 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
228 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
229 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
230 f
.t
[to
][top_to
+1] = stack_take();
234 int t2t(int from
, int to
) { /* tableu to tableu */
235 int top_to
= find_top(f
.t
[to
]);
236 int top_from
= find_top(f
.t
[from
]);
237 for (int i
= top_from
; i
>=0; i
--) {
238 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
239 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
240 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
241 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
242 /* move cards [i..top_from] to their destination */
243 for (;i
<= top_from
; i
++) {
245 f
.t
[to
][top_to
] = f
.t
[from
][i
];
246 f
.t
[from
][i
] = NO_CARD
;
248 turn_over(f
.t
[from
]);
252 return ERR
; /* no such move possible */
255 void remove_if_complete (card_t
* pile
) { //TODO: cleanup
256 static int foundation
= 0; /* foundation to put pile onto */
257 /* test if K...A complete; move to foundation if so */
258 int top_from
= find_top(pile
);
259 if (get_rank(pile
[top_from
]) != RANK_A
) return;
260 for (int i
= top_from
; i
>=0; i
--) {
261 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last? XXX: copied from t2t()--make function
262 && (get_rank(pile
[i
+1]) != get_rank(pile
[i
])-1) //cards not consecutive?
266 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last?
267 && (get_suit(pile
[i
+1]) != get_suit(pile
[i
])) //cards not same suit?
271 if (i
+RANK_K
== top_from
272 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) { //ace to king ok, remove it
273 for (int i
= top_from
, j
= 0; i
>= top_from
-NUM_RANKS
; i
--, j
++) {
274 f
.f
[foundation
][j
] = pile
[i
];
283 int t2t(int from
, int to
) { //TODO: in dire need of cleanup
284 //TODO: segfaulted once on large column
285 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
286 (from
< 0) && (from
= 9); /* '0' is tenth ([9]) pile */
287 (to
< 0) && (to
= 9); /* ditto */
289 int top_from
= find_top(f
.t
[from
]);
290 int top_to
= find_top(f
.t
[to
]);
291 int empty_to
= -1; //awful, nondescriptive name :/
292 if (top_to
< 0) { /* empty pile? */
293 printf ("\rup to (a23456789xjqk): "); //TODO: automatically do it if only 1 card movable XXX: cant move ace
294 empty_to
= getchar();
296 case 'a': case 'A': empty_to
= RANK_A
; break;
297 case '0': /* fallthrough */
298 case 'x': case 'X': empty_to
= RANK_X
; break;
299 case 'j': case 'J': empty_to
= RANK_J
; break;
300 case 'q': case 'Q': empty_to
= RANK_Q
; break;
301 case 'k': case 'K': empty_to
= RANK_K
; break;
302 default: empty_to
-= '1';
304 if (empty_to
< RANK_A
|| empty_to
> RANK_K
) return ERR
;
306 for (int i
= top_from
; i
>= 0; i
--) {
307 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
308 && (get_rank(f
.t
[from
][i
+1]) != get_rank(f
.t
[from
][i
])-1) //cards not consecutive?
312 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
313 && (get_suit(f
.t
[from
][i
+1]) != get_suit(f
.t
[from
][i
])) //cards not same suit?
318 if ((get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1) // consecutive?
319 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) { //to empty pile and rank ok?
320 for (;i
<= top_from
; i
++) {
322 f
.t
[to
][top_to
] = f
.t
[from
][i
];
323 f
.t
[from
][i
] = NO_CARD
;
325 turn_over(f
.t
[from
]);
326 remove_if_complete (f
.t
[to
]);
327 if (check_won()) return WON
;
332 return ERR
; /* no such move possible */
334 int s2t(int from
, int to
) { //TODO: check remove, won
335 (void) from
; (void) to
; //don't need
336 if (f
.z
<= 0) return ERR
; /* stack out of cards */
337 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
338 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
339 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
340 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
345 int nop(int from
, int to
) { (void)from
;(void)to
; return ERR
; }
348 int get_cmd (int* from
, int* to
) {
349 //returns 0 on success or an error code indicating game quit, new game,...
350 //TODO: check validity, escape sequences (mouse, cursor keys)
355 case '1': *from
= TAB_1
; break;
356 case '2': *from
= TAB_2
; break;
357 case '3': *from
= TAB_3
; break;
358 case '4': *from
= TAB_4
; break;
359 case '5': *from
= TAB_5
; break;
360 case '6': *from
= TAB_6
; break;
361 case '7': *from
= TAB_7
; break;
363 case '8': *from
= TAB_8
; break;
364 case '9': *from
= TAB_9
; break;
365 case '0': *from
= TAB_10
; break;
366 #elif defined KLONDIKE
367 case '9': *from
= WASTE
; break;
368 case '0': *from
= FOUNDATION
; break;
369 case '8': /* fallthrough */
371 case '\n': /* shortcut for dealing from stock */
375 case 'q': return CMD_QUIT
;
376 case 'r': return CMD_NEW
; //TODO
377 case 'h': return CMD_HINT
; //TODO
378 case '?': return CMD_HELP
; //TODO
379 case '/': return CMD_FIND
; //TODO: highlight card of given rank (even non-movable)
380 case '\033': return CMD_INVAL
; //TODO: cntlseq
381 default: return CMD_INVAL
;
386 if (t
< '0' || t
> '9') return CMD_INVAL
;
399 f
= (const struct playfield
){0}; /* clear playfield */
400 card_t deck
[DECK_SIZE
*NUM_DECKS
];
401 int avail
= DECK_SIZE
*NUM_DECKS
;
402 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
404 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
405 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
406 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
407 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
410 srandom (time(NULL
));
411 /*XXX*/ long seed
= time(NULL
);
412 /*XXX*/ srandom (seed
);
413 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { //fisher-yates
414 int j
= random() % (i
+1);
415 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
417 ///////////////////////////////////////////////XXX
418 //sometimes we see duplicate cards. this tries to catch that
419 int count
[_NUM_CARDS_internal
] = {0};
420 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++)
422 for (int i
= 0; i
< _NUM_CARDS_internal
; i
++){ //0 is NO_CARD
424 int x
= op
.m
==MEDIUM
?2:op
.m
==EASY
?4:1;
428 if (deck
[i
] == NO_CARD
) continue;
429 if (count
[deck
[i
]] != NUM_DECKS
*x
) {
431 printf ("found duplicate card with seed %lx!\n", seed
);
432 for (int i
= 1; i
< _NUM_CARDS_internal
; i
++)
433 printf ("%3d of %2d\n", count
[deck
[i
]], deck
[i
]);
437 ///////////////////////////////////////////////XXX
440 for (int i
= 0; i
< NUM_PILES
; i
++) {
442 int closed
= i
; /* pile n has n closed cards, then 1 open */
444 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
446 /* face down cards are negated: */
447 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
448 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
450 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
451 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
452 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
455 int is_movable(card_t
* pile
, int n
) { //TODO cleanup, code deduplication, needs entry in sol.h
457 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
459 int top
= find_top(pile
);
460 for (int i
= top
; i
; i
--) {
461 if (pile
[i
] <= NO_CARD
) return 0; //card face down?
462 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last? //COPIED FROM t2t
463 && (get_rank(pile
[i
+1]) != get_rank(pile
[i
])-1) //cards not consecutive?
467 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last?
468 && (get_suit(pile
[i
+1]) != get_suit(pile
[i
])) //cards not same suit?
472 if (i
== n
) return 1; //card reached, must be movable
477 #define print_hi(test, str) /* for highlighting during get_cmd() */ \
478 printf ("%s%s%s", (test)?"\033[7m":"", str, (test)?"\033[27m":"")
479 void print_table(int highlight
) { //{{{
480 printf("\033[2J\033[H"); /* clear screen, reset cursor */
482 /* print stock, waste and foundation: */
483 for (int line
= 0; line
< op
.s
->height
; line
++) {
485 print_hi (highlight
== STOCK
, (
486 (f
.w
< f
.z
-1)?op
.s
->facedown
487 :op
.s
->placeholder
)[line
]);
489 print_hi (highlight
== WASTE
, (
490 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
491 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
492 :op
.s
->placeholder
)[line
]);
493 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
495 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
496 int card
= find_top(f
.f
[pile
]);
497 print_hi (highlight
== FOUNDATION
,
498 (card
< 0)?op
.s
->placeholder
[line
]
499 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
505 /* print tableu piles: */
506 int row
[NUM_PILES
] = {0};
507 int line
[NUM_PILES
]= {0};
508 int label
[NUM_PILES
]={0};// :|
509 int line_had_card
; // :|
512 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
513 card_t card
= f
.t
[pile
][row
[pile
]];
514 card_t next
= f
.t
[pile
][row
[pile
]+1];
516 print_hi (highlight
== pile
&& is_movable(f
.t
[pile
],row
[pile
]), (
517 (card
<0)?op
.s
->facedown
521 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
) //normal overlap
523 || (line
[pile
] >= 1 &&
524 f
.t
[pile
][row
[pile
]] < 0 &&
525 f
.t
[pile
][row
[pile
]+1] <0) //extreme overlap on closed
526 || (0) //extreme overlap on sequence TODO
532 if(!card
&& !label
[pile
]) { /* tableu labels: */
534 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
536 line_had_card
|= !!card
;
539 } while (line_had_card
);
542 void visbell (void) {
543 printf ("\033[?5h"); fflush (stdout
);
545 printf ("\033[?5l"); fflush (stdout
);
548 void append_undo (int n
, int f
, int t
) {
549 (void)n
;(void)f
;(void)t
;
550 //check if we have to free redo buffer (.next)
553 //TODO: undo; needs operations to be written by x2y()
556 void screen_setup (int enable
) {
559 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
560 printf ("\033[H\033[J"); /* reset cursor, clear screen */
561 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
563 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
564 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
569 void raw_mode(int enable
) { //{{{
570 static struct termios saved_term_mode
;
571 struct termios raw_term_mode
;
574 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
575 raw_term_mode
= saved_term_mode
;
576 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
577 raw_term_mode
.c_cc
[VMIN
] = 1 ;
578 raw_term_mode
.c_cc
[VTIME
] = 0;
579 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
581 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
585 //vim: foldmethod=marker