]>
git.gir.st - solVItaire.git/blob - sol.c
1 #define _DEFAULT_SOURCE
13 #define MAX_HIDDEN 6 /*how many cards are turned over at most in a tableu pile*/
14 #define MAX_STOCK 24 /*how many cards can be in the stock at most (=@start)*/
16 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
20 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
22 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
25 #define get_suit(card) \
26 ((card-1) % NUM_SUITS)
27 #define get_rank(card) \
28 ((card-1) / NUM_SUITS)
29 #define get_color(card) \
30 ((get_suit(card) ^ get_suit(card)>>1) & 1)
33 //TODO: stock and waste are incompatible with undo{}
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 (XXX@spider:complete set gets put on seperate pile, so undo is easy) */
38 card_t t
[NUM_PILES
][PILE_SIZE
]; /* tableu piles */
40 int from
; /* pile cards were taken from */
41 int to
; /* pile cards were moved to */
42 int n
; /* number of cards moved */
49 int m
; /* difficulty mode */
51 const struct scheme
* s
;
56 /* stores a function pointer for every takeable action; called by game loop */
57 int (*action
[10][10])(int,int) = {
58 /*fnd 1 2 3 4 5 6 7 stk wst*/
59 /*fnd*/ { nop
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
},
60 /* 1 */ { t2f
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
61 /* 2 */ { t2f
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
62 /* 3 */ { t2f
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
63 /* 4 */ { t2f
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, t2t
, nop
, nop
},
64 /* 5 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, t2t
, nop
, nop
},
65 /* 6 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, t2t
, nop
, nop
},
66 /* 7 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2f
, nop
, nop
},
67 /*stk*/ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
},
68 /*wst*/ { w2f
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, w2f
},
71 int (*action
[11][10])(int,int) = {
72 /* 0 1 2 3 4 5 6 7 8 9 */
73 /* 0 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
74 /* 1 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
75 /* 2 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
76 /* 3 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
77 /* 4 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
78 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
79 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
80 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
81 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
82 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
83 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
88 int main(int argc
, char** 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;
112 printf ("\033[7mYOU WON!\n");
113 //return; //TODO: do something nice
116 case CMD_QUIT
: return;
122 int find_top(card_t
* pile
) {
124 for(i
=MAX_HIDDEN
+NUM_RANKS
-1; i
>=0 && !pile
[i
]; i
--);
127 void turn_over(card_t
* pile
) {
128 int top
= find_top(pile
);
129 if (pile
[top
] < 0) pile
[top
] *= -1;
131 int check_won(void) {
132 for (int pile
= 0; pile
< NUM_DECKS
*NUM_COLORS
; pile
++)
133 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
137 // takeable actions {{{
139 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
140 card_t card
= f
.s
[f
.w
];
141 /* move stack one over, so there are no gaps in it: */
142 for (int i
= f
.w
; i
< f
.z
-1; i
++)
145 f
.w
--; /* make previous card visible again */
148 int t2f(int from
, int to
) { /* tableu to foundation */
149 from
--; //remove off-by-one
150 int top_from
= find_top(f
.t
[from
]);
151 to
= get_suit(f
.t
[from
][top_from
]);
152 int top_to
= find_top(f
.f
[to
]);
153 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
154 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
155 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
156 f
.t
[from
][top_from
] = NO_CARD
;
157 turn_over(f
.t
[from
]);
158 if (check_won()) return WON
;
162 int w2f(int from
, int to
) { /* waste to foundation */
163 if (f
.w
< 0) return ERR
;
164 to
= get_suit(f
.s
[f
.w
]);
165 int top_to
= find_top(f
.f
[to
]);
166 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
167 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
168 f
.f
[to
][top_to
+1] = stack_take();
169 if (check_won()) return WON
;
174 int s2w(int from
, int to
) { /* stock to waste */
175 if (f
.z
== 0) return ERR
;
177 if (f
.w
== f
.z
) f
.w
= -1;
180 int w2s(int from
, int to
) { /* waste to stock (undoes stock to waste) */
181 if (f
.z
== 0) return ERR
;
183 if (f
.w
< -1) f
.w
= f
.z
-1;
186 int f2t(int from
, int to
) { /* foundation to tableu */
187 to
--; //remove off-by-one
188 int top_to
= find_top(f
.t
[to
]);
189 printf ("take from (1-4): "); fflush (stdout
);
190 from
= getchar() - '0';
191 if (from
> 4 || from
< 1) return ERR
;
192 from
--; //remove off-by-one
193 int top_from
= find_top(f
.f
[from
]);
195 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
196 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
197 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
198 f
.f
[from
][top_from
] = NO_CARD
;
202 int w2t(int from
, int to
) { //waste to tableu
203 to
--; //remove off-by-one
204 int top_to
= find_top(f
.t
[to
]);
205 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
206 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
207 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
208 f
.t
[to
][top_to
+1] = stack_take();
212 int t2t(int from
, int to
) {
213 from
--; to
--; //remove off-by-one
214 int top_to
= find_top(f
.t
[to
]);
215 int top_from
= find_top(f
.t
[from
]);
216 for (int i
= top_from
; i
>=0; i
--) {
217 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
218 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
219 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
220 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
221 /* move cards [i..top_from] to their destination */
222 for (;i
<= top_from
; i
++) {
224 f
.t
[to
][top_to
] = f
.t
[from
][i
];
225 f
.t
[from
][i
] = NO_CARD
;
227 turn_over(f
.t
[from
]);
231 return ERR
; /* no such move possible */
234 void remove_if_complete (card_t
* pile
) { //TODO: cleanup
235 /* test if K...A complete; move to foundation if so */
236 int new_top_to
= find_top(pile
);
237 for (int i
= new_top_to
; i
>=0; i
--) {
238 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last? XXX: copied from t2t()--make function
239 && (get_rank(pile
[i
+1]) != get_rank(pile
[i
])-1) //cards not consecutive?
243 if ((i
+1 < PILE_SIZE
&& pile
[i
+1] != NO_CARD
) // card below or last?
244 && (get_suit(pile
[i
+1]) != get_suit(pile
[i
])) //cards not same suit?
249 if (get_rank(pile
[new_top_to
+RANK_K
]) != RANK_K
) return;
250 if (get_rank(pile
[new_top_to
]) != RANK_A
) return;
251 int j
= 0; //XXX: to seperate foundation piles!
252 for (int i
= new_top_to
; i
>= new_top_to
-13; i
--) {//TODO: magic value 13
253 f
.f
[0][j
++] = pile
[i
];
258 int t2t(int from
, int to
) { //TODO: in dire need of cleanup
259 //TODO: segfaulted once on large column
260 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
261 from
--; to
--; //remove off-by-one
262 (from
< 0) && (from
= 9); /* '0' is tenth ([9]) pile */
263 (to
< 0) && (to
= 9); /* ditto */
265 int top_from
= find_top(f
.t
[from
]);
266 int top_to
= find_top(f
.t
[to
]);
267 int empty_to
= 0; //awful name :/
268 if (top_to
< 0) { /* empty pile? */
269 printf ("\rup to (a23456789xjqk): ");
270 empty_to
= getchar();
272 case 'a': case 'A': empty_to
= RANK_A
; break;
273 case '0': /* fallthrough */
274 case 'x': case 'X': empty_to
= RANK_X
; break;
275 case 'j': case 'J': empty_to
= RANK_J
; break;
276 case 'q': case 'Q': empty_to
= RANK_Q
; break;
277 case 'k': case 'K': empty_to
= RANK_K
; break;
278 default: empty_to
-= '0'+1; /* NOTE@+1: ace == 0, not 1 */
280 if (empty_to
< RANK_A
|| empty_to
> RANK_K
) return -1;
282 for (int i
= top_from
; i
>= 0; i
--) {
283 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
284 && (get_rank(f
.t
[from
][i
+1]) != get_rank(f
.t
[from
][i
])-1) //cards not consecutive?
288 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
289 && (get_suit(f
.t
[from
][i
+1]) != get_suit(f
.t
[from
][i
])) //cards not same suit?
294 if ((get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1) // consecutive?
295 || (empty_to
> 0 && get_rank(f
.t
[from
][i
]) == empty_to
)) { //to empty pile and rank ok?
296 for (;i
<= top_from
; i
++) {
298 f
.t
[to
][top_to
] = f
.t
[from
][i
];
299 f
.t
[from
][i
] = NO_CARD
;
301 turn_over(f
.t
[from
]);
302 remove_if_complete (f
.t
[to
]);
303 if (check_won()) return WON
;
308 return ERR
; /* no such move possible */
310 int s2t(int from
, int to
) {
311 if (f
.z
<= 0) return ERR
; /* stack out of cards */
312 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
313 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
314 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
315 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
320 int nop(int from
, int to
) { return ERR
; }
323 int get_cmd (int* from
, int* to
) {
324 //returns 0 on success or an error code indicating game quit, new game,...
325 //TODO: check validity, escape sequences (mouse, cursor keys)
327 printf ("from: "); fflush (stdout
);
330 if (f
=='\n') { //TODO: also in klondike
337 case 'q': return CMD_QUIT
;
338 case 'r': return CMD_NEW
;
339 case 'h': return CMD_HINT
;
340 case '?': return CMD_HELP
;
341 default: if (f
< '0' || f
> '9') return CMD_INVAL
;
343 printf ("\r \rto: "); fflush (stdout
);
355 f
= (const struct playfield
){0}; /* clear playfield */
356 card_t deck
[DECK_SIZE
*NUM_DECKS
];
357 int avail
= DECK_SIZE
*NUM_DECKS
;
358 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
360 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
361 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
362 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
363 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
366 //XXX srandom (time(NULL));
367 /*XXX*/ long seed
= time(NULL
);
368 /*XXX*/ srandom (seed
);
369 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { //fisher-yates
370 int j
= random() % (i
+1);
371 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
373 ///////////////////////////////////////////////XXX
374 //sometimes we see duplicate cards. this tries to catch that
375 int count
[_NUM_CARDS_internal
] = {0};
376 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++)
378 for (int i
= 0; i
< _NUM_CARDS_internal
; i
++){ //0 is NO_CARD
380 int x
= op
.m
==MEDIUM
?2:op
.m
==EASY
?4:1;
384 if (deck
[i
] == NO_CARD
) continue;
385 if (count
[deck
[i
]] != NUM_DECKS
*x
) {
387 printf ("found duplicate card with seed %lx!\n", seed
);
388 for (int i
= 1; i
< _NUM_CARDS_internal
; i
++)
389 printf ("%3d of %2d\n", count
[deck
[i
]], deck
[i
]);
393 ///////////////////////////////////////////////XXX
396 for (int i
= 0; i
< NUM_PILES
; i
++) {
398 int closed
= i
; /* pile n has n closed cards, then 1 open */
400 int closed
= i
<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
402 /* face down cards are negated: */
403 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
];
404 f
.t
[i
][closed
] = deck
[--avail
]; /* the face-up card */
406 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
407 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
408 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
411 #define print_hi(test, str) /*for highlighting during get_cmd() */ \
412 printf ("%s%s%s", test?"\033[7m":"", str, test?"\033[27m":"") //TODO
413 void print_table(void) { //{{{
414 printf("\033[2J\033[H"); /* clear screen, reset cursor */
416 /* print stock, waste and foundation: */
417 for (int line
= 0; line
< op
.s
->height
; line
++) {
418 printf ("%s", ( /* stock */
419 (f
.w
< f
.z
-1)?op
.s
->facedown
420 :op
.s
->placeholder
)[line
]);
421 printf ("%s", ( /* waste */
422 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
423 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
424 :op
.s
->placeholder
)[line
]);
425 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
427 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
428 int card
= find_top(f
.f
[pile
]);
430 (card
< 0)?op
.s
->placeholder
[line
]
431 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
437 /* print tableu piles: */
438 int row
[NUM_PILES
] = {0};
439 int line
[NUM_PILES
]= {0};
440 int label
[NUM_PILES
]={0};// :|
441 int line_had_card
; // :|
444 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
445 card_t card
= f
.t
[pile
][row
[pile
]];
446 card_t next
= f
.t
[pile
][row
[pile
]+1];
448 (card
<0)?op
.s
->facedown
452 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
) //normal overlap
454 || (line
[pile
] >= 1 &&
455 f
.t
[pile
][row
[pile
]] < 0 &&
456 f
.t
[pile
][row
[pile
]+1] <0) //extreme overlap on closed
457 || (0) //extreme overlap on sequence TODO
463 if(!card
&& !label
[pile
]) { /* tableu labels: */
465 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
467 line_had_card
|= !!card
;
470 } while (line_had_card
);
473 void visbell (void) {
474 printf ("\033[?5h"); fflush (stdout
);
476 printf ("\033[?5l"); fflush (stdout
);
479 void append_undo (int n
, int f
, int t
) {
480 //check if we have to free redo buffer (.next)
486 void screen_setup (int enable
) {
489 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
490 printf ("\033[H\033[J"); /* reset cursor, clear screen */
491 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
493 printf (op
.s
->init_seq
); /*swich charset, if necessary*/
496 printf (op
.s
->reset_seq
);/*reset charset, if necessary*/
497 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
498 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
503 void raw_mode(int enable
) { //{{{
504 static struct termios saved_term_mode
;
505 struct termios raw_term_mode
;
508 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
509 raw_term_mode
= saved_term_mode
;
510 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
511 raw_term_mode
.c_cc
[VMIN
] = 1 ;
512 raw_term_mode
.c_cc
[VTIME
] = 0;
513 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
515 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
519 //vim: foldmethod=marker