]>
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
94 sol(); //TODO: restart, etc.
104 switch (get_cmd(&from
, &to
)) {
106 switch (action
[from
][to
](from
,to
)) {
108 case ERR
: visbell(); break;
109 case WON
: return; //TODO: do something nice
112 case CMD_QUIT
: return;
118 int find_top(card_t
* pile
) {
120 for(i
=MAX_HIDDEN
+NUM_RANKS
-1; i
>=0 && !pile
[i
]; i
--);
123 void turn_over(card_t
* pile
) {
124 int top
= find_top(pile
);
125 if (pile
[top
] < 0) pile
[top
] *= -1;
127 int check_won(void) {
128 for (int pile
= 0; pile
< NUM_DECKS
*NUM_COLORS
; pile
++)
129 if (f
.f
[pile
][NUM_RANKS
-1] == NO_CARD
) return 0;
133 // takeable actions {{{
135 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
136 card_t card
= f
.s
[f
.w
];
137 /* move stack one over, so there are no gaps in it: */
138 for (int i
= f
.w
; i
< f
.z
-1; i
++)
141 f
.w
--; /* make previous card visible again */
144 int t2f(int from
, int to
) { /* tableu to foundation */
145 from
--; //remove off-by-one
146 int top_from
= find_top(f
.t
[from
]);
147 to
= get_suit(f
.t
[from
][top_from
]);
148 int top_to
= find_top(f
.f
[to
]);
149 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
150 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
151 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
152 f
.t
[from
][top_from
] = NO_CARD
;
153 turn_over(f
.t
[from
]);
154 if (check_won()) return WON
;
158 int w2f(int from
, int to
) { /* waste to foundation */
159 if (f
.w
< 0) return ERR
;
160 to
= get_suit(f
.s
[f
.w
]);
161 int top_to
= find_top(f
.f
[to
]);
162 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
163 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
164 f
.f
[to
][top_to
+1] = stack_take();
165 if (check_won()) return WON
;
170 int s2w(int from
, int to
) { /* stock to waste */
171 if (f
.z
== 0) return ERR
;
173 if (f
.w
== f
.z
) f
.w
= -1;
176 int w2s(int from
, int to
) { /* waste to stock (undoes stock to waste) */
177 if (f
.z
== 0) return ERR
;
179 if (f
.w
< -1) f
.w
= f
.z
-1;
182 int f2t(int from
, int to
) { /* foundation to tableu */
183 to
--; //remove off-by-one
184 int top_to
= find_top(f
.t
[to
]);
185 printf ("take from (1-4): "); fflush (stdout
);
186 from
= getchar() - '0';
187 if (from
> 4 || from
< 1) return ERR
;
188 from
--; //remove off-by-one
189 int top_from
= find_top(f
.f
[from
]);
191 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
192 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
193 f
.t
[to
][top_to
+1] = f
.f
[from
][top_from
];
194 f
.f
[from
][top_from
] = NO_CARD
;
198 int w2t(int from
, int to
) { //waste to tableu
199 to
--; //remove off-by-one
200 int top_to
= find_top(f
.t
[to
]);
201 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
202 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
203 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
204 f
.t
[to
][top_to
+1] = stack_take();
208 int t2t(int from
, int to
) {
209 from
--; to
--; //remove off-by-one
210 int top_to
= find_top(f
.t
[to
]);
211 int top_from
= find_top(f
.t
[from
]);
212 for (int i
= top_from
; i
>=0; i
--) {
213 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
214 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1)
215 && f
.t
[from
][i
] > NO_CARD
) /* card face up? */
216 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
217 /* move cards [i..top_from] to their destination */
218 for (;i
<= top_from
; i
++) {
220 f
.t
[to
][top_to
] = f
.t
[from
][i
];
221 f
.t
[from
][i
] = NO_CARD
;
223 turn_over(f
.t
[from
]);
227 return ERR
; /* no such move possible */
230 int t2t(int from
, int to
) { //TODO: in dire need of cleanup
231 from
--; to
--; //remove off-by-one
232 (from
< 0) && (from
= 9); // '0' is tenth ([9]) pile
233 (to
< 0) && (to
= 9); // ditto
235 int top_from
= find_top(f
.t
[from
]);
236 int top_to
= find_top(f
.t
[to
]);
237 for (int i
= top_from
; i
>= 0; i
--) {
238 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
239 && (get_rank(f
.t
[from
][i
+1]) != get_rank(f
.t
[from
][i
])-1) //cards not consecutive?
243 if ((i
+1 < PILE_SIZE
&& f
.t
[from
][i
+1] != NO_CARD
) // card below or last?
244 && (get_suit(f
.t
[from
][i
+1]) != get_suit(f
.t
[from
][i
])) //cards not same suit?
249 if(get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1) { //TODO: to empty pile
250 for (;i
<= top_from
; i
++) {
252 f
.t
[to
][top_to
] = f
.t
[from
][i
];
253 f
.t
[from
][i
] = NO_CARD
;
255 turn_over(f
.t
[from
]);
256 //TODO: test if k..a complete; move to foundation if so
257 #define x(n) (f.t[i][top_to-n])
270 && x(12)==RANK_K
) {//TODO: check suit
273 for (int i
= top_to
; i
>= top_to
-13; i
--) {
274 f
.f
[0][j
++] = f
.t
[to
][i
];
275 f
.t
[to
][i
] = NO_CARD
;
277 if (check_won()) return WON
;
283 return ERR
; /* no such move possible */
285 int s2t(int from
, int to
) {
286 if (f
.z
<= 0) return ERR
; /* stack out of cards */
287 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
288 if (f
.t
[pile
][0]==NO_CARD
) return ERR
; /*no piles may be empty*/
289 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
290 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
295 int nop(int from
, int to
) { return ERR
; }
298 int get_cmd (int* from
, int* to
) {
299 //returns 0 on success or an error code indicating game quit, new game,...
300 //TODO: check validity, escape sequences (mouse, cursor keys)
311 case 'q': return CMD_QUIT
;
312 case 'r': return CMD_NEW
;
313 default: if (f
< '0' || f
> '9') return CMD_INVAL
;
326 f
= (const struct playfield
){0}; /* clear playfield */
327 card_t deck
[DECK_SIZE
*NUM_DECKS
];
328 int avail
= DECK_SIZE
*NUM_DECKS
;
329 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
331 if (op
.m
!= NORMAL
) for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
332 if (op
.m
== MEDIUM
) deck
[i
] = 1+((deck
[i
]-1) | 2);
333 if (op
.m
== EASY
) deck
[i
] = 1+((deck
[i
]-1) | 2 | 1);
334 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
337 srandom (time(NULL
));
338 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { //fisher-yates
339 int j
= random() % (i
+1);
340 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
344 for (int i
= 0; i
< NUM_PILES
; i
++) {
346 int closed
= i
; // tableu pile n has n closed cards, then 1 open
348 int closed
= i
<4?5:4; //tableu pile 1-4 have 5, 5-10 have 4 closed cards
351 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
]; //face-down cards are negative
352 f
.t
[i
][closed
] = deck
[--avail
]; //the face-up card
354 //rest of the cards to the stock (should be 50 for spider):
355 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
356 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
359 #define print_hi(test, str) /*for highlighting during get_cmd() */ \
360 printf ("%s%s%s", test?"\033[7m":"", str, test?"\033[27m":"") //TODO
361 void print_table(void) { //{{{
362 printf("\033[2J\033[H"); /* clear screen, reset cursor */
364 /* print stock, waste and foundation: */
365 for (int line
= 0; line
< op
.s
->height
; line
++) {
366 printf ("%s", ( /* stock */
367 (f
.w
< f
.z
-1)?op
.s
->facedown
368 :op
.s
->placeholder
)[line
]);
369 printf ("%s", ( /* waste */
370 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
371 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]]
372 :op
.s
->placeholder
)[line
]);
373 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
375 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
376 int card
= find_top(f
.f
[pile
]);
378 (card
< 0)?op
.s
->placeholder
[line
]
379 :op
.s
->card
[f
.f
[pile
][card
]][line
]);
385 /* print tableu piles: */
386 int row
[NUM_PILES
] = {0};
387 int line
[NUM_PILES
]= {0};
388 int label
[NUM_PILES
]={0};// :|
389 int line_had_card
; // :|
392 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
393 card_t card
= f
.t
[pile
][row
[pile
]];
394 card_t next
= f
.t
[pile
][row
[pile
]+1];
396 (card
<0)?op
.s
->facedown
400 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
) //normal overlap
402 || (line
[pile
] >= 1 &&
403 f
.t
[pile
][row
[pile
]] < 0 &&
404 f
.t
[pile
][row
[pile
]+1] <0) //extreme overlap on closed
405 || (0) //extreme overlap on sequence TODO
411 if(!card
&& !label
[pile
]) { /* tableu labels: */
413 printf ("\b\b%d ", (pile
+1) % 10); //XXX: hack
415 line_had_card
|= !!card
;
418 } while (line_had_card
);
421 void visbell (void) {
422 printf ("\033[?5h"); fflush (stdout
);
424 printf ("\033[?5l"); fflush (stdout
);
427 void append_undo (int n
, int f
, int t
) {
428 //check if we have to free redo buffer (.next)
434 void screen_setup (int enable
) {
437 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
438 printf ("\033[H\033[J"); /* reset cursor, clear screen */
439 //XXX//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
441 printf (op
.s
->init_seq
); /*swich charset, if necessary*/
444 printf (op
.s
->reset_seq
);/*reset charset, if necessary*/
445 //XXX//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
446 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
451 void raw_mode(int enable
) { //{{{
452 static struct termios saved_term_mode
;
453 struct termios raw_term_mode
;
456 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
457 raw_term_mode
= saved_term_mode
;
458 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
459 raw_term_mode
.c_cc
[VMIN
] = 1 ;
460 raw_term_mode
.c_cc
[VTIME
] = 0;
461 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
463 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
467 //vim: foldmethod=marker