]> git.gir.st - solVItaire.git/blob - sol.c
overlap stack in spider, some scaffolding for overlapping spider foundation
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE
2 #include <poll.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <termios.h>
7 #include <unistd.h>
8
9 #include "sol.h"
10 #include "schemes.h"
11
12 #ifdef KLONDIKE
13 #define NUM_PILES 7
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)*/
16 #define NUM_DECKS 1
17 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
18 #elif defined SPIDER
19 #define MAX_HIDDEN 5
20 #define NUM_PILES 10
21 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
22 #define NUM_DECKS 2
23 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
24 #endif
25
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)
32
33 struct playfield {
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 */
39 struct undo {
40 /* NOTE: .f & .t are overloaded; 64+n=foundation; 128+n=stock */
41 int f; /* pile cards were taken from */
42 int t; /* pile cards were moved to */
43 int n; /* number of cards moved */
44 struct undo* prev;
45 struct undo* next;
46 }* u;
47 } f;
48 struct opts {
49 #ifdef SPIDER
50 int m; /* difficulty mode */
51 #endif
52 const struct scheme* s;
53 } op;
54
55 // action table {{{
56 /* stores a function pointer for every takeable action; called by game loop */
57 int (*action[NUM_PLACES][10])(int,int) = {
58 #ifdef KLONDIKE
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 },
70 #elif defined SPIDER
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 },
83 #endif
84 };
85 // }}}
86
87 int main(int argc, char** argv) {
88 (void) argc;(void) argv;
89 op.s = &unicode_large_color;
90 #ifdef SPIDER
91 op.m = MEDIUM; //TODO: make configurable
92 op.m = EASY;
93 #endif
94 screen_setup(1);
95 sol(); //TODO: restart, etc.
96 screen_setup(0);
97 }
98
99 void sol(void) {
100 deal();
101
102 int from, to;
103 print_table(NO_HI);
104 for(;;) {
105 switch (get_cmd(&from, &to)) {
106 case CMD_MOVE:
107 switch (action[from][to](from,to)) {
108 case OK: break;
109 case ERR: visbell(); break;
110 case WON:
111 print_table(NO_HI);
112 win_anim();
113 getchar(); /* consume char left by win_anim() */
114 return;
115 }
116 break;
117 case CMD_QUIT: return;
118 }
119 print_table(NO_HI);
120 }
121 }
122
123 int find_top(card_t* pile) {
124 int i;
125 for(i=PILE_SIZE-1; i>=0 && !pile[i]; i--);
126 return i;
127 }
128 void turn_over(card_t* pile) {
129 int top = find_top(pile);
130 if (pile[top] < 0) pile[top] *= -1;
131 }
132 int check_won(void) {
133 for (int pile = 0; pile < NUM_DECKS*NUM_SUITS; pile++)
134 if (f.f[pile][NUM_RANKS-1] == NO_CARD) return 0;
135
136 return 1;
137 }
138 void win_anim(void) {
139 printf ("\033[?25l"); /* hide cursor */
140 for (;;) {
141 /* set cursor to random location */
142 int row = 1+random()%(24-op.s->width);
143 int col = 1+random()%(80-op.s->height);
144
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]);
150 }
151 fflush (stdout);
152
153 /* exit on keypress */
154 struct pollfd p = {STDIN_FILENO, POLLIN, 0};
155 if (poll (&p, 1, 80)) goto fin;
156 }
157 fin:
158 printf ("\033[?25h"); /* show cursor */
159 return;
160 }
161 // takeable actions {{{
162 #ifdef KLONDIKE
163 card_t stack_take(void) { /*NOTE: assert(f.w >= 0) */
164 card_t card = f.s[f.w];
165 /* move stack one over, so there are no gaps in it: */
166 for (int i = f.w; i < f.z-1; i++)
167 f.s[i] = f.s[i+1];
168 f.z--;
169 f.w--; /* make previous card visible again */
170 return card;
171 }
172 int t2f(int from, int to) { /* tableu to foundation */
173 (void) to; //don't need
174 int top_from = find_top(f.t[from]);
175 to = get_suit(f.t[from][top_from]);
176 int top_to = find_top(f.f[to]);
177 if ((top_to < 0 && get_rank(f.t[from][top_from]) == RANK_A)
178 || (top_to >= 0 && get_rank(f.f[to][top_to]) == get_rank(f.t[from][top_from])-1)) {
179 f.f[to][top_to+1] = f.t[from][top_from];
180 f.t[from][top_from] = NO_CARD;
181 turn_over(f.t[from]);
182 if (check_won()) return WON;
183 return OK;
184 } else return ERR;
185 }
186 int w2f(int from, int to) { /* waste to foundation */
187 (void) from; (void) to; //don't need
188 if (f.w < 0) return ERR;
189 to = get_suit(f.s[f.w]);
190 int top_to = find_top(f.f[to]);
191 if ((top_to < 0 && get_rank(f.s[f.w]) == RANK_A)
192 || (top_to >= 0 && get_rank(f.f[to][top_to]) == get_rank(f.s[f.w])-1)) {
193 f.f[to][top_to+1] = stack_take();
194 if (check_won()) return WON;
195 return OK;
196 } else return ERR;
197
198 }
199 int s2w(int from, int to) { /* stock to waste */
200 (void) from; (void) to; //don't need
201 if (f.z == 0) return ERR;
202 f.w++;
203 if (f.w == f.z) f.w = -1;
204 return OK;
205 }
206 int w2s(int from, int to) { /* waste to stock (undoes stock to waste) */
207 (void) from; (void) to; //don't need
208 if (f.z == 0) return ERR;
209 f.w--;
210 if (f.w < -1) f.w = f.z-1;
211 return OK;
212 }
213 int f2t(int from, int to) { /* foundation to tableu */
214 int top_to = find_top(f.t[to]);
215 printf ("take from (1-4): "); fflush (stdout);
216 from = getchar() - '1';
217 if (from < 0 || from > 3) return ERR;
218 int top_from = find_top(f.f[from]);
219
220 if ((get_color(f.t[to][top_to]) != get_color(f.f[from][top_from]))
221 && (get_rank(f.t[to][top_to]) == get_rank(f.f[from][top_from])+1)) {
222 f.t[to][top_to+1] = f.f[from][top_from];
223 f.f[from][top_from] = NO_CARD;
224 return OK;
225 } else return ERR;
226 }
227 int w2t(int from, int to) { /* waste to tableu */
228 (void) from; //don't need
229 int top_to = find_top(f.t[to]);
230 if (((get_color(f.t[to][top_to]) != get_color(f.s[f.w]))
231 && (get_rank(f.t[to][top_to]) == get_rank(f.s[f.w])+1))
232 || (top_to < 0 && get_rank(f.s[f.w]) == RANK_K)) {
233 f.t[to][top_to+1] = stack_take();
234 return OK;
235 } else return ERR;
236 }
237 int t2t(int from, int to) { /* tableu to tableu */
238 int top_to = find_top(f.t[to]);
239 int top_from = find_top(f.t[from]);
240 for (int i = top_from; i >=0; i--) {
241 if (((get_color(f.t[to][top_to]) != get_color(f.t[from][i]))
242 && (get_rank(f.t[to][top_to]) == get_rank(f.t[from][i])+1)
243 && f.t[from][i] > NO_CARD) /* card face up? */
244 || (top_to < 0 && get_rank(f.t[from][i]) == RANK_K)) {
245 /* move cards [i..top_from] to their destination */
246 for (;i <= top_from; i++) {
247 top_to++;
248 f.t[to][top_to] = f.t[from][i];
249 f.t[from][i] = NO_CARD;
250 }
251 turn_over(f.t[from]);
252 return OK;
253 }
254 }
255 return ERR; /* no such move possible */
256 }
257 #elif defined SPIDER
258 int is_consecutive (card_t* pile, int pos) {
259 if (pos+1 >= PILE_SIZE) return 1; /* card is last */
260 if (pile[pos+1] == NO_CARD) return 1; /* card is first */
261
262 if (get_rank(pile[pos+1]) != get_rank(pile[pos])-1) return 0; /*rank consecutive?*/
263 if (get_suit(pile[pos+1]) != get_suit(pile[pos])) return 0; /*same suit?*/
264
265 return 1;
266 }
267 void remove_if_complete (card_t* pile) { //TODO: cleanup
268 static int foundation = 0; /* where to put pile onto (1 set per stack)*/
269 /* test if K...A complete; move to foundation if so */
270 int top_from = find_top(pile);
271 if (get_rank(pile[top_from]) != RANK_A) return;
272 for (int i = top_from; i>=0; i--) {
273 if (!is_consecutive (pile, i)) return;
274 if (i+RANK_K == top_from
275 && get_rank(pile[top_from-RANK_K]) == RANK_K) { //ace to king ok, remove it
276 for (int i = top_from, j = 0; i > top_from-NUM_RANKS; i--, j++) {
277 f.f[foundation][j] = pile[i];
278 pile[i] = NO_CARD;
279 }
280 foundation++;
281 turn_over(pile);
282 return;
283 }
284 }
285 }
286 int t2t(int from, int to) { //TODO: in dire need of cleanup
287 //TODO: segfaulted once on large column
288 //TODO: sometimes moving doesn't work (ERR when it should be OK) XXX
289
290 int top_from = find_top(f.t[from]);
291 int top_to = find_top(f.t[to]);
292 int empty_to = -1; //awful, nondescriptive name :/
293 if (top_to < 0) { /* empty pile? */
294 printf ("\rup to (a23456789xjqk): "); //TODO: automatically do it if only 1 card movable
295 empty_to = getchar();
296 switch (empty_to) {
297 case 'a': case 'A': empty_to = RANK_A; break;
298 case '0': /* fallthrough */
299 case 'x': case 'X': empty_to = RANK_X; break;
300 case 'j': case 'J': empty_to = RANK_J; break;
301 case 'q': case 'Q': empty_to = RANK_Q; break;
302 case 'k': case 'K': empty_to = RANK_K; break;
303 default: empty_to -= '1';
304 }
305 if (empty_to < RANK_A || empty_to > RANK_K) return ERR;
306 }
307 for (int i = top_from; i >= 0; i--) {
308 if (!is_consecutive(f.t[from], i)) break;
309
310 if ((get_rank(f.t[from][i]) == get_rank(f.t[to][top_to])-1) // consecutive?
311 || (empty_to >= RANK_A && get_rank(f.t[from][i]) == empty_to)) { //to empty pile and rank ok?
312 for (;i <= top_from; i++) {
313 top_to++;
314 f.t[to][top_to] = f.t[from][i];
315 f.t[from][i] = NO_CARD;
316 }
317 turn_over(f.t[from]);
318 remove_if_complete (f.t[to]);
319 if (check_won()) return WON;
320 return OK;
321 }
322 }
323
324 return ERR; /* no such move possible */
325 }
326 int s2t(int from, int to) { //TODO: check remove, won
327 (void) from; (void) to; //don't need
328 if (f.z <= 0) return ERR; /* stack out of cards */
329 for (int pile = 0; pile < NUM_PILES; pile++)
330 if (f.t[pile][0]==NO_CARD) return ERR; /*no piles may be empty*/
331 for (int pile = 0; pile < NUM_PILES; pile++) {
332 f.t[pile][find_top(f.t[pile])+1] = f.s[--f.z];
333 }
334 return OK;
335 }
336 #endif
337 int nop(int from, int to) { (void)from;(void)to; return ERR; }
338 // }}}
339
340 int get_cmd (int* from, int* to) {
341 //returns 0 on success or an error code indicating game quit, new game,...
342 //TODO: escape sequences (mouse, cursor keys)
343 //TODO: don't allow taking from empty piles
344 int f, t;
345 f = getchar();
346
347 switch (f) {
348 /* direct addressing: */
349 case '1': *from = TAB_1; break;
350 case '2': *from = TAB_2; break;
351 case '3': *from = TAB_3; break;
352 case '4': *from = TAB_4; break;
353 case '5': *from = TAB_5; break;
354 case '6': *from = TAB_6; break;
355 case '7': *from = TAB_7; break;
356 #ifdef SPIDER
357 case '8': *from = TAB_8; break;
358 case '9': *from = TAB_9; break;
359 case '0': *from = TAB_10; break;
360 #elif defined KLONDIKE
361 case '9': *from = WASTE; break;
362 case '0': *from = FOUNDATION; break;
363 case '8': /* fallthrough */
364 #endif
365 case '\n': /* shortcut for dealing from stock */
366 *from = STOCK;
367 *to = WASTE;
368 return CMD_MOVE;
369 /* cursor keys addressing: */
370 //TODO
371 case 'q': return CMD_QUIT;
372 case 'r': return CMD_NEW; //TODO
373 case 'h': return CMD_HINT; //TODO
374 case '?': return CMD_HELP; //TODO
375 case '/': return CMD_FIND; //TODO: highlight card of given rank (even non-movable)
376 case '\033': return CMD_INVAL; //TODO: cntlseq
377 default: return CMD_INVAL;
378 }
379 print_table(*from);
380
381 t = getchar();
382 if (t < '0' || t > '9') return CMD_INVAL;
383 if (t == '0')
384 #ifdef KLONDIKE
385 *to = FOUNDATION;
386 #elif defined SPIDER
387 *to = TAB_10;
388 #endif
389 else
390 *to = t-'1';
391 return CMD_MOVE;
392 }
393
394 void deal(void) {
395 f = (const struct playfield){0}; /* clear playfield */
396 card_t deck[DECK_SIZE*NUM_DECKS];
397 int avail = DECK_SIZE*NUM_DECKS;
398 for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) deck[i] = (i%DECK_SIZE)+1;
399 #ifdef SPIDER
400 if (op.m != NORMAL) for (int i = 0; i < DECK_SIZE*NUM_DECKS; i++) {
401 if (op.m == MEDIUM) deck[i] = 1+((deck[i]-1) | 2);
402 if (op.m == EASY) deck[i] = 1+((deck[i]-1) | 2 | 1);
403 /* the 1+ -1 dance gets rid of the offset created by NO_CARD */
404 }
405 #endif
406 srandom (time(NULL));
407 /*XXX*/ long seed = time(NULL);
408 /*XXX*/ srandom (seed);
409 for (int i = DECK_SIZE*NUM_DECKS-1; i > 0; i--) { //fisher-yates
410 int j = random() % (i+1);
411 if (j-i) deck[i]^=deck[j],deck[j]^=deck[i],deck[i]^=deck[j];
412 }
413
414 /* deal cards: */
415 for (int i = 0; i < NUM_PILES; i++) {
416 #ifdef KLONDIKE
417 int closed = i; /* pile n has n closed cards, then 1 open */
418 #elif defined SPIDER
419 int closed = i<4?5:4; /* pile 1-4 have 5, 5-10 have 4 closed */
420 #endif
421 /* face down cards are negated: */
422 for (int j = 0; j < closed; j++) f.t[i][j] = -deck[--avail];
423 f.t[i][closed] = deck[--avail]; /* the face-up card */
424 }
425 /* rest of the cards to the stock; NOTE: assert(avail==50) for spider */
426 for (f.z = 0; avail; f.z++) f.s[f.z] = deck[--avail];
427 f.w = -1; /* @start: nothing on waste (no waste in spider -> const) */
428 }
429
430 int is_movable(card_t* pile, int n) { //TODO cleanup, code deduplication, needs entry in sol.h
431 #ifdef KLONDIKE
432 return(pile[n] > NO_CARD); /*non-movable cards don't exist in klondike*/
433 #elif defined SPIDER
434 int top = find_top(pile);
435 for (int i = top; i >= 0; i--) {
436 if (pile[i] <= NO_CARD) return 0; //card face down?
437 if (!is_consecutive(pile, i)) return 0;
438 if (i == n) return 1; //card reached, must be movable
439 }
440 return 0;
441 #endif
442 }
443 #define print_hi(test, str) /* for highlighting during get_cmd() */ \
444 printf ("%s%s%s", (test)?"\033[7m":"", str, (test)?"\033[27m":"")
445 void print_table(int highlight) { //{{{
446 printf("\033[2J\033[H"); /* clear screen, reset cursor */
447 #ifdef KLONDIKE
448 /* print stock, waste and foundation: */
449 for (int line = 0; line < op.s->height; line++) {
450 /* stock: */
451 print_hi (highlight == STOCK, (
452 (f.w < f.z-1)?op.s->facedown
453 :op.s->placeholder)[line]);
454 /* waste: */
455 print_hi (highlight == WASTE, (
456 /* NOTE: cast, because f.w sometimes is (short)-1 !? */
457 ((short)f.w >= 0)?op.s->card[f.s[f.w]]
458 :op.s->placeholder)[line]);
459 printf ("%s", op.s->card[NO_CARD][line]); /* spacer */
460 /* foundation: */
461 for (int pile = 0; pile < NUM_SUITS; pile++) {
462 int card = find_top(f.f[pile]);
463 print_hi (highlight == FOUNDATION,
464 (card < 0)?op.s->placeholder[line]
465 :op.s->card[f.f[pile][card]][line]);
466 }
467 printf("\n");
468 }
469 printf("\n");
470 #elif SPIDER
471 for (int line = 0; line < op.s->height; line++) {
472 /* available stock: */
473 for (int i = f.z/10; i; i--) {
474 if (i==1) printf ("%s", op.s->facedown[line]);
475 else printf ("%s", op.s->halfstack[line]);
476 }
477 /* foundation (overlapping): */ //TODO: fill from the right
478 //TODO
479 printf("\n");
480 }
481 printf("\n");
482 #endif
483 /* print tableu piles: */
484 int row[NUM_PILES] = {0};
485 int line[NUM_PILES]= {0};
486 int label[NUM_PILES]={0};// :|
487 int line_had_card; // :|
488 do {
489 line_had_card = 0;
490 for (int pile = 0; pile < NUM_PILES; pile++) {
491 card_t card = f.t[pile][row[pile]];
492 card_t next = f.t[pile][row[pile]+1];
493 int movable = is_movable(f.t[pile], row[pile]);
494
495 if (movable) printf ("\033[1m"); /* bold */ //TODO: interferes with grey color!
496 print_hi (highlight == pile && movable, (
497 (card<0)?op.s->facedown
498 :op.s->card[card]
499 )[line[pile]]);
500 printf ("\033[22m"); /* normal intensity (no bold) */
501
502 if (++line[pile] >= (next?op.s->overlap:op.s->height) //normal overlap
503 #if 0 //XXX
504 || (line[pile] >= 1 &&
505 f.t[pile][row[pile]] < 0 &&
506 f.t[pile][row[pile]+1] <0) //extreme overlap on closed
507 || (0) //extreme overlap on sequence TODO
508 #endif
509 ) {
510 line[pile]=0;
511 row[pile]++;
512 }
513 if(!card && !label[pile]) { /* tableu labels: */
514 label[pile] = 1;
515 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
516 }
517 line_had_card |= !!card;
518 }
519 printf ("\n");
520 } while (line_had_card);
521 }//}}}
522
523 void visbell (void) {
524 printf ("\033[?5h"); fflush (stdout);
525 usleep (100000);
526 printf ("\033[?5l"); fflush (stdout);
527 }
528
529 void append_undo (int n, int f, int t) {
530 (void)n;(void)f;(void)t;
531 //check if we have to free redo buffer (.next)
532 //malloc
533 //update pointers
534 //TODO: undo; needs operations to be written by x2y()
535 }
536
537 void screen_setup (int enable) {
538 if (enable) {
539 raw_mode(1);
540 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
541 printf ("\033[H\033[J"); /* reset cursor, clear screen */
542 //TODO//printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
543 } else {
544 //TODO//printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
545 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
546 raw_mode(0);
547 }
548 }
549
550 void raw_mode(int enable) { //{{{
551 static struct termios saved_term_mode;
552 struct termios raw_term_mode;
553
554 if (enable) {
555 tcgetattr(STDIN_FILENO, &saved_term_mode);
556 raw_term_mode = saved_term_mode;
557 raw_term_mode.c_lflag &= ~(ICANON | ECHO);
558 raw_term_mode.c_cc[VMIN] = 1 ;
559 raw_term_mode.c_cc[VTIME] = 0;
560 tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
561 } else {
562 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
563 }
564 } //}}}
565
566 //vim: foldmethod=marker
Imprint / Impressum