]> git.gir.st - solVItaire.git/blob - sol.c
sol prototype complete, bunch of small stuff
[solVItaire.git] / sol.c
1 #define _DEFAULT_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <termios.h>
6 #include <unistd.h>
7
8 #include "sol.h"
9 #include "schemes.h"
10
11 #ifdef KLONDIKE
12 #define NUM_PILES 7
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)*/
15 #define NUM_DECKS 1
16 #define PILE_SIZE MAX_HIDDEN+NUM_RANKS
17 #elif defined SPIDER
18 #define MAX_HIDDEN 5
19 #define NUM_PILES 10
20 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
21 #define NUM_DECKS 2
22 #define PILE_SIZE DECK_SIZE*NUM_DECKS /* no maximum stack size in spider :/ */
23 #endif
24
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)
31
32 struct playfield {
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 */
39 struct undo {
40 int from; /* pile cards were taken from */
41 int to; /* pile cards were moved to */
42 int n; /* number of cards moved */
43 struct undo* prev;
44 struct undo* next;
45 } u;
46 } f;
47 struct opts {
48 #ifdef SPIDER
49 int m; /* difficulty mode */
50 #endif
51 const struct scheme* s;
52 } op;
53
54 // action table {{{
55 #ifdef KLONDIKE
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 },
69 };
70 #elif defined SPIDER
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 },
84 };
85 #endif
86 // }}}
87
88 int main(int argc, char** argv) {
89 op.s = &unicode_large_color;
90 #ifdef SPIDER
91 op.m = MEDIUM; //TODO: make configurable
92 #endif
93 screen_setup(1);
94 sol(); //TODO: restart, etc.
95 screen_setup(0);
96 }
97
98 void sol(void) {
99 deal();
100
101 int from, to;
102 print_table();
103 for(;;) {
104 switch (get_cmd(&from, &to)) {
105 case CMD_MOVE:
106 switch (action[from][to](from,to)) {
107 case OK: break;
108 case ERR: visbell(); break;
109 case WON: return; //TODO: do something nice
110 }
111 break;
112 case CMD_QUIT: return;
113 }
114 print_table();
115 }
116 }
117
118 int find_top(card_t* pile) {
119 int i;
120 for(i=MAX_HIDDEN+NUM_RANKS-1; i>=0 && !pile[i]; i--);
121 return i;
122 }
123 void turn_over(card_t* pile) {
124 int top = find_top(pile);
125 if (pile[top] < 0) pile[top] *= -1;
126 }
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;
130
131 return 1;
132 }
133 // takeable actions {{{
134 #ifdef KLONDIKE
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++)
139 f.s[i] = f.s[i+1];
140 f.z--;
141 f.w--; /* make previous card visible again */
142 return card;
143 }
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;
155 return OK;
156 } else return ERR;
157 }
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;
166 return OK;
167 } else return ERR;
168
169 }
170 int s2w(int from, int to) { /* stock to waste */
171 if (f.z == 0) return ERR;
172 f.w++;
173 if (f.w == f.z) f.w = -1;
174 return OK;
175 }
176 int w2s(int from, int to) { /* waste to stock (undoes stock to waste) */
177 if (f.z == 0) return ERR;
178 f.w--;
179 if (f.w < -1) f.w = f.z-1;
180 return OK;
181 }
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]);
190
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;
195 return OK;
196 } else return ERR;
197 }
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();
205 return OK;
206 } else return ERR;
207 }
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++) {
219 top_to++;
220 f.t[to][top_to] = f.t[from][i];
221 f.t[from][i] = NO_CARD;
222 }
223 turn_over(f.t[from]);
224 return OK;
225 }
226 }
227 return ERR; /* no such move possible */
228 }
229 #elif defined SPIDER
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
234
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?
240 ) {
241 break;
242 }
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?
245 ) {
246 break;
247 }
248
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++) {
251 top_to++;
252 f.t[to][top_to] = f.t[from][i];
253 f.t[from][i] = NO_CARD;
254 }
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])
258 if (x(0)==RANK_A
259 && x(1)==RANK_2
260 && x(2)==RANK_3
261 && x(3)==RANK_4
262 && x(4)==RANK_5
263 && x(5)==RANK_6
264 && x(6)==RANK_7
265 && x(7)==RANK_8
266 && x(8)==RANK_9
267 && x(9)==RANK_X
268 && x(10)==RANK_J
269 && x(11)==RANK_Q
270 && x(12)==RANK_K) {//TODO: check suit
271 #undef x
272 int j = 0;
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;
276 }
277 if (check_won()) return WON;
278 }
279 return OK;
280 }
281 }
282
283 return ERR; /* no such move possible */
284 }
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];
291 }
292 return OK;
293 }
294 #endif
295 int nop(int from, int to) { return ERR; }
296 // }}}
297
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)
301 char f, t;
302 f = getchar();
303 #ifdef SPIDER
304 if (f=='\n') {
305 *from = 10;
306 *to = 0;
307 return CMD_MOVE;
308 }
309 #endif
310 switch (f) {
311 case 'q': return CMD_QUIT;
312 case 'r': return CMD_NEW;
313 default: if (f < '0' || f > '9') return CMD_INVAL;
314 }
315 t =
316 #ifdef KLONDIKE
317 (f=='8')?'9':
318 #endif
319 getchar();
320 *from = f-'0';
321 *to = t-'0';
322 return CMD_MOVE;
323 }
324
325 void deal(void) {
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;
330 #ifdef SPIDER
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 */
335 }
336 #endif
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];
341 }
342
343 /* deal cards: */
344 for (int i = 0; i < NUM_PILES; i++) {
345 #ifdef KLONDIKE
346 int closed = i; // tableu pile n has n closed cards, then 1 open
347 #elif defined SPIDER
348 int closed = i<4?5:4; //tableu pile 1-4 have 5, 5-10 have 4 closed cards
349 #endif
350
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
353 }
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) */
357 }
358
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 */
363 #ifdef KLONDIKE
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 */
374 /* foundation: */
375 for (int pile = 0; pile < NUM_SUITS; pile++) {
376 int card = find_top(f.f[pile]);
377 printf ("%s",
378 (card < 0)?op.s->placeholder[line]
379 :op.s->card[f.f[pile][card]][line]);
380 }
381 printf("\n");
382 }
383 printf("\n");
384 #endif
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; // :|
390 do {
391 line_had_card = 0;
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];
395 printf ("%s", (
396 (card<0)?op.s->facedown
397 :op.s->card[card]
398 )[line[pile]]);
399
400 if (++line[pile] >= (next?op.s->overlap:op.s->height) //normal overlap
401 #if 0 //XXX
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
406 #endif
407 ) {
408 line[pile]=0;
409 row[pile]++;
410 }
411 if(!card && !label[pile]) { /* tableu labels: */
412 label[pile] = 1;
413 printf ("\b\b%d ", (pile+1) % 10); //XXX: hack
414 }
415 line_had_card |= !!card;
416 }
417 printf ("\n");
418 } while (line_had_card);
419 }//}}}
420
421 void visbell (void) {
422 printf ("\033[?5h"); fflush (stdout);
423 usleep (100000);
424 printf ("\033[?5l"); fflush (stdout);
425 }
426
427 void append_undo (int n, int f, int t) {
428 //check if we have to free redo buffer (.next)
429 //malloc
430 //update pointers
431 *NULL;
432 }
433
434 void screen_setup (int enable) {
435 if (enable) {
436 raw_mode(1);
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 */
440 if (op.s->init_seq)
441 printf (op.s->init_seq); /*swich charset, if necessary*/
442 } else {
443 if (op.s->reset_seq)
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 */
447 raw_mode(0);
448 }
449 }
450
451 void raw_mode(int enable) { //{{{
452 static struct termios saved_term_mode;
453 struct termios raw_term_mode;
454
455 if (enable) {
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);
462 } else {
463 tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
464 }
465 } //}}}
466
467 //vim: foldmethod=marker
Imprint / Impressum