]>
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 card_t s
[MAX_STOCK
]; /* stock */
35 int z
; /* stock size */
36 int w
; /* waste; index into stock (const -1 in spider) */
37 card_t f
[NUM_DECKS
*NUM_SUITS
][PILE_SIZE
]; /* foundation */
38 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
40 int f
; /* pile cards were taken from */
41 int t
; /* pile cards were moved to */
42 int n
; /* number of cards moved (if tableu; else index of stock/foundation) */
49 int m
; /* difficulty mode */
51 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) = {
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 int main(int argc
, char** argv
) {
87 (void) argc
;(void) argv
;
88 op
.s
= &unicode_large_color
;
90 op
.m
= MEDIUM
; //TODO: make configurable
94 sol(); //TODO: restart, etc.
104 switch (get_cmd(&from
, &to
)) {
106 switch (action
[from
][to
](from
,to
)) {
108 case ERR
: visbell(); break;
112 getchar(); /* consume char left by win_anim() */
119 case CMD_QUIT
: return;
125 int find_top(card_t
* pile
) {
127 for(i
=PILE_SIZE
-1; i
>=0 && !pile
[i
]; i
--);
130 void turn_over(card_t
* pile
) {
131 int top
= find_top(pile
);
132 if (pile
[top
] < 0) pile
[top
] *= -1;
134 int check_won(void) {
135 for (int pile
= 0; pile
< NUM_DECKS
*NUM_SUITS
; pile
++)
136 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
140 void win_anim(void) {
141 printf ("\033[?25l"); /* hide cursor */
143 /* set cursor to random location */
144 int row
= 1+random()%(24-op
.s
->width
);
145 int col
= 1+random()%(80-op
.s
->height
);
147 /* draw random card */
148 int face
= 1 + random() % 52;
149 for (int l
= 0; l
< op
.s
->height
; l
++) {
150 printf ("\033[%d;%dH", row
+l
, col
);
151 printf ("%s", op
.s
->card
[face
][l
]);
155 /* exit on keypress */
156 struct pollfd p
= {STDIN_FILENO
, POLLIN
, 0};
157 if (poll (&p
, 1, 80)) goto fin
;
160 printf ("\033[?25h"); /* show cursor */
163 // takeable actions {{{
165 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
166 card_t card
= f
.s
[f
.w
];
167 /* move stack one over, so there are no gaps in it: */
168 for (int i
= f
.w
; i
< f
.z
-1; i
++)
171 f
.w
--; /* make previous card visible again */
174 int t2f(int from
, int to
) { /* tableu to foundation */
175 (void) to
; //don't need
176 int top_from
= find_top(f
.t
[from
]);
177 to
= get_suit(f
.t
[from
][top_from
]);
178 int top_to
= find_top(f
.f
[to
]);
179 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
180 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
181 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
182 f
.t
[from
][top_from
] = NO_CARD
;
183 turn_over(f
.t
[from
]);
184 if (check_won()) return WON
;
188 int w2f(int from
, int to
) { /* waste to foundation */
189 (void) from
; (void) to
; //don't need
190 if (f
.w
< 0) return ERR
;
191 to
= get_suit(f
.s
[f
.w
]);
192 int top_to
= find_top(f
.f
[to
]);
193 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
194 || (top_to
>= 0 && get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
195 f
.f
[to
][top_to
+1] = stack_take();
196 if (check_won()) return WON
;
201 int s2w(int from
, int to
) { /* stock to waste */
202 (void) from
; (void) to
; //don't need
203 if (f
.z
== 0) return ERR
;
205 if (f
.w
== f
.z
) f
.w
= -1;
208 int w2s(int from
, int to
) { /* waste to stock (undoes stock to waste) */
209 (void) from
; (void) to
; //don't need
210 if (f
.z
== 0) return ERR
;
212 if (f
.w
< -1) f
.w
= f
.z
-1;
215 int f2t(int from
, int to
) { /* foundation to tableu */
216 int top_to
= find_top(f
.t
[to
]);
217 printf ("take from (1-4): "); fflush (stdout
);
218 from
= getchar() - '1';
219 if (from
< 0 || from
> 3) return ERR
;
220 int top_from
= find_top(f
.f
[from
]);
222 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
223 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
224 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
225 f
.f
[from
][top_from
] = NO_CARD
;
229 int w2t(int from
, int to
) { /* waste to tableu */
230 (void) from
; //don't need
231 int top_to
= find_top(f
.t
[to
]);
232 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
233 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
234 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
235 f
.t
[to
][top_to
+1] = stack_take();
239 int t2t(int from
, int to
) { /* tableu to tableu */
240 int top_to
= find_top(f
.t
[to
]);
241 int top_from
= find_top(f
.t
[from
]);
242 for (int i
= top_from
; i
>=0; i
--) {
243 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
244 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
245 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
246 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
247 /* move cards [i..top_from] to their destination */
248 for (;i
<= top_from
; i
++) {
250 f
.t
[to
][top_to
] = f
.t
[from
][i
];
251 f
.t
[from
][i
] = NO_CARD
;
253 turn_over(f
.t
[from
]);
257 return ERR
; /* no such move possible */
260 int is_consecutive (card_t
* pile
, int pos
) {
261 if (pos
+1 >= PILE_SIZE
) return 1; /* card is last */
262 if (pile
[pos
+1] == NO_CARD
) return 1; /* card is first */
264 if (get_rank(pile
[pos
+1]) != get_rank(pile
[pos
])-1) return 0; /*rank consecutive?*/
265 if (get_suit(pile
[pos
+1]) != get_suit(pile
[pos
])) return 0; /*same suit?*/
269 void remove_if_complete (card_t
* pile
) { //TODO: cleanup
270 static int foundation
= 0; /* where to put pile onto (1 set per stack)*/
271 /* test if K...A complete; move to foundation if so */
272 int top_from
= find_top(pile
);
273 if (get_rank(pile
[top_from
]) != RANK_A
) return;
274 for (int i
= top_from
; i
>=0; i
--) {
275 if (!is_consecutive (pile
, i
)) return;
276 if (i
+RANK_K
== top_from
277 && get_rank(pile
[top_from
-RANK_K
]) == RANK_K
) { //ace to king ok, remove it
278 for (int i
= top_from
, j
= 0; i
> top_from
-NUM_RANKS
; i
--, j
++) {
279 f
.f
[foundation
][j
] = pile
[i
];
288 int t2t(int from
, int to
) { //TODO: in dire need of cleanup
289 //TODO: segfaulted once on large column
290 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
292 int top_from
= find_top(f
.t
[from
]);
293 int top_to
= find_top(f
.t
[to
]);
294 int empty_to
= -1; //awful, nondescriptive name :/
295 if (top_to
< 0) { /* empty pile? */
296 printf ("\rup to (a23456789xjqk): "); //TODO: automatically do it if only 1 card movable
297 empty_to
= getchar();
299 case 'a': case 'A': empty_to
= RANK_A
; break;
300 case '0': /* fallthrough */
301 case 'x': case 'X': empty_to
= RANK_X
; break;
302 case 'j': case 'J': empty_to
= RANK_J
; break;
303 case 'q': case 'Q': empty_to
= RANK_Q
; break;
304 case 'k': case 'K': empty_to
= RANK_K
; break;
305 default: empty_to
-= '1';
307 if (empty_to
< RANK_A
|| empty_to
> RANK_K
) return ERR
;
309 for (int i
= top_from
; i
>= 0; i
--) {
310 if (!is_consecutive(f
.t
[from
], i
)) break;
312 if ((get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1) // consecutive?
313 || (empty_to
>= RANK_A
&& get_rank(f
.t
[from
][i
]) == empty_to
)) { //to empty pile and rank ok?
314 for (;i
<= top_from
; i
++) {
316 f
.t
[to
][top_to
] = f
.t
[from
][i
];
317 f
.t
[from
][i
] = NO_CARD
;
319 turn_over(f
.t
[from
]);
320 remove_if_complete (f
.t
[to
]);
321 if (check_won()) return WON
;
326 return ERR
; /* no such move possible */
328 int s2t(int from
, int to
) { //TODO: check remove, won
329 (void) from
; (void) to
; //don't need
330 if (f
.z
<= 0) return ERR
; /* stack out of cards */
331 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
332 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
333 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
334 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
339 int nop(int from
, int to
) { (void)from
;(void)to
; return ERR
; }
342 int get_cmd (int* from
, int* to
) {
343 //returns 0 on success or an error code indicating game quit, new game,...
344 //TODO: escape sequences (mouse, cursor keys)
345 //TODO: don't allow taking from empty piles
350 /* direct addressing: */ //TODO: cleanup empty pile check
351 case '1': if (!f
.t
[0][0]) return CMD_INVAL
; *from
= TAB_1
; break;
352 case '2': if (!f
.t
[1][0]) return CMD_INVAL
; *from
= TAB_2
; break;
353 case '3': if (!f
.t
[2][0]) return CMD_INVAL
; *from
= TAB_3
; break;
354 case '4': if (!f
.t
[3][0]) return CMD_INVAL
; *from
= TAB_4
; break;
355 case '5': if (!f
.t
[4][0]) return CMD_INVAL
; *from
= TAB_5
; break;
356 case '6': if (!f
.t
[5][0]) return CMD_INVAL
; *from
= TAB_6
; break;
357 case '7': if (!f
.t
[6][0]) return CMD_INVAL
; *from
= TAB_7
; break;
359 case '8': if (!f
.t
[7][0]) return CMD_INVAL
; *from
= TAB_8
; break;
360 case '9': if (!f
.t
[8][0]) return CMD_INVAL
; *from
= TAB_9
; break;
361 case '0': if (!f
.t
[9][0]) return CMD_INVAL
; *from
= TAB_10
;break;
362 #elif defined KLONDIKE
363 case '9': if (f
.w
< 0) return CMD_INVAL
; *from
= WASTE
; break;
364 case '0': *from
= FOUNDATION
; break;
365 case '8': /* fallthrough */
367 case '\n': /* shortcut for dealing from stock */
371 /* cursor keys addressing: */
373 case 'q': return CMD_QUIT
;
374 case 'r': return CMD_NEW
; //TODO
375 case 'h': return CMD_HINT
; //TODO
376 case '?': return CMD_HELP
; //TODO
377 case '/': return CMD_FIND
; //TODO: highlight card of given rank (even non-movable)
378 case '\033': return CMD_INVAL
; //TODO: cntlseq
379 default: return CMD_INVAL
;
384 if (t
< '0' || t
> '9') return CMD_INVAL
;
397 f
= (const struct playfield
){0}; /* clear playfield */
398 card_t deck
[DECK_SIZE
*NUM_DECKS
];
399 int avail
= DECK_SIZE
*NUM_DECKS
;
400 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
402 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
403 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
404 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
405 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
408 srandom (time(NULL
));
409 /*XXX*/ long seed
= time(NULL
);
410 /*XXX*/ srandom (seed
);
411 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { //fisher-yates
412 int j
= random() % (i
+1);
413 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
417 for (int i
= 0; i
< NUM_PILES
; i
++) {
419 int closed
= i
; /* pile n has n closed cards, then 1 open */
421 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
423 /* face down cards are negated: */
424 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
425 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
427 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
428 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
429 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
432 int is_movable(card_t
* pile
, int n
) { //TODO cleanup, code deduplication, needs entry in sol.h
434 return(pile
[n
] > NO_CARD
); /*non-movable cards don't exist in klondike*/
436 int top
= find_top(pile
);
437 for (int i
= top
; i
>= 0; i
--) {
438 if (pile
[i
] <= NO_CARD
) return 0; //card face down?
439 if (!is_consecutive(pile
, i
)) return 0;
440 if (i
== n
) return 1; //card reached, must be movable
445 #define print_hi(invert, bold, str) /* for highlighting during get_cmd() */ \
446 printf ("%s%s%s%s%s", (bold)?"\033[1m":"", (invert)?"\033[7m":"", str, \
447 (invert)?"\033[27m":"", (bold)?"\033[22m":"")
448 void print_table(int highlight
) { //{{{
449 printf("\033[2J\033[H"); /* clear screen, reset cursor */
451 /* print stock, waste and foundation: */
452 for (int line
= 0; line
< op
.s
->height
; line
++) {
454 print_hi (highlight
== STOCK
, 1, (
455 (f
.w
< f
.z
-1)?op
.s
->facedown
456 :op
.s
->placeholder
)[line
]);
458 print_hi (highlight
== WASTE
, 1, (
459 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
460 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
461 :op
.s
->placeholder
)[line
]);
462 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
464 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
465 int card
= find_top(f
.f
[pile
]);
466 print_hi (highlight
== FOUNDATION
, 1,
467 (card
< 0)?op
.s
->placeholder
[line
]
468 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
474 for (int line
= 0; line
< op
.s
->height
; line
++) {
475 /* available stock: */
476 for (int i
= f
.z
/10; i
; i
--) {
477 if (i
==1) printf ("%s", op
.s
->facedown
[line
]);
478 else printf ("%s", op
.s
->halfstack
[line
]);
480 /* spacer: */ //TODO: urgh! cleanup!
481 int xx
= 0; for(int i
=0;i
<NUM_DECKS
*NUM_SUITS
;i
++)if(f
.f
[i
][RANK_K
])xx
++;
482 int HALFWIDTH
= 2; int RIGHTWIDTH
= 3;
483 for (int i
= f
.z
?(f
.z
/10-1)*HALFWIDTH
+ op
.s
->width
:0;
484 i
< NUM_PILES
*op
.s
->width
- ((xx
?(xx
-1)*RIGHTWIDTH
:0)+op
.s
->width
);
487 /* foundation (overlapping): */
488 for (int i
= 0; i
< NUM_DECKS
*NUM_SUITS
; i
++) {
489 int overlap
= i
? op
.s
->halfcard
[line
]: 0;
490 if (f
.f
[i
][RANK_K
]) printf ("%s",
491 op
.s
->card
[f
.f
[i
][RANK_K
]][line
]+overlap
);
497 /* print tableu piles: */
498 int row
[NUM_PILES
] = {0};
499 int line
[NUM_PILES
]= {0};
500 int label
[NUM_PILES
]={0};// :|
501 int line_had_card
; // :|
504 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
505 card_t card
= f
.t
[pile
][row
[pile
]];
506 card_t next
= f
.t
[pile
][row
[pile
]+1];
507 int movable
= is_movable(f
.t
[pile
], row
[pile
]);
509 print_hi (highlight
== pile
&& movable
, movable
, (
510 (card
<0)?op
.s
->facedown
514 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
) //normal overlap
516 || (line
[pile
] >= 1 &&
517 f
.t
[pile
][row
[pile
]] < 0 &&
518 f
.t
[pile
][row
[pile
]+1] <0) //extreme overlap on closed
519 || (0) //extreme overlap on sequence TODO
525 if(!card
&& !label
[pile
]) { /* tableu labels: */
527 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
529 line_had_card
|= !!card
;
532 } while (line_had_card
);
535 void visbell (void) {
536 printf ("\033[?5h"); fflush (stdout
);
538 printf ("\033[?5l"); fflush (stdout
);
541 void append_undo (int n
, int f
, int t
) {
542 (void)n
;(void)f
;(void)t
;
543 //check if we have to free redo buffer (.next)
546 //TODO: undo; needs operations to be written by x2y()
549 void screen_setup (int enable
) {
552 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
553 printf ("\033[H\033[J"); /* reset cursor, clear screen */
554 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
556 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
557 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
562 void raw_mode(int enable
) { //{{{
563 static struct termios saved_term_mode
;
564 struct termios raw_term_mode
;
567 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
568 raw_term_mode
= saved_term_mode
;
569 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
570 raw_term_mode
.c_cc
[VMIN
] = 1 ;
571 raw_term_mode
.c_cc
[VTIME
] = 0;
572 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
574 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
578 //vim: foldmethod=marker