]>
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)*/
19 #define MAX_STOCK 50 /*how many cards can be dealt onto the piles*/
23 #define get_suit(card) \
24 ((card-1) % NUM_SUITS)
25 #define get_rank(card) \
26 ((card-1) / NUM_SUITS)
27 #define get_color(card) \
28 ((get_suit(card) ^ get_suit(card)>>1) & 1)
31 //TODO: stock and waste are incompatible with undo{}
32 card_t s
[MAX_STOCK
]; /* stock */
33 int z
; /* stock size */
34 int w
; /* waste; index into stock (const -1 in spider) */
36 card_t f
[NUM_SUITS
][MAX_HIDDEN
+NUM_RANKS
]; /* foundation (oversized to ease find_top()) */
37 card_t t
[NUM_PILES
][MAX_HIDDEN
+NUM_RANKS
]; /* tableu piles */
39 card_t f
[NUM_DECKS
*NUM_COLORS
][MAX_HIDDEN
+NUM_RANKS
]; //each completed set gets put on its own pile, so undo is possible
40 card_t t
[NUM_PILES
][MAX_HIDDEN
+NUM_RANKS
];
43 int from
; /* pile cards were taken from */
44 int to
; /* pile cards were moved to */
45 int n
; /* number of cards moved */
51 const struct scheme
* s
;
55 //declare action as array 10 of array 10 of pointer to function (int,int) returning int
56 // this stores a function pointer for every takeable action that is then called by execute()
57 //lines = from, cols = to
58 int (*action
[10][10])(int,int) = {
59 /* 0 1 2 3 4 5 6 7 8 9 */
60 /* 0 */ { nop
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, f2t
, nop
, nop
},
61 /* 1 */ { t2f
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
62 /* 2 */ { t2f
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
63 /* 3 */ { t2f
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, nop
, nop
},
64 /* 4 */ { t2f
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, nop
, nop
},
65 /* 5 */ { t2f
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, nop
, nop
},
66 /* 6 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, nop
, nop
},
67 /* 7 */ { t2f
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, nop
, nop
},
68 /* 8 */ { nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, nop
, s2w
},
69 /* 9 */ { w2f
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2t
, w2s
, nop
},
72 int (*action
[11][10])(int,int) = {
73 //piles are zero-indexed in spider; stk=stock
74 /* 0 1 2 3 4 5 6 7 8 9 */
75 /* 0 */ { nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
76 /* 1 */ { t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
77 /* 2 */ { t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
78 /* 3 */ { t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
},
79 /* 4 */ { t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
, t2t
},
80 /* 5 */ { t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
, t2t
},
81 /* 6 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
, t2t
},
82 /* 7 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
, t2t
},
83 /* 8 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
, t2t
},
84 /* 9 */ { t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, t2t
, nop
},
85 /*stk*/ { s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
, s2t
},
89 int main(int argc
, char** argv
) {
90 //op.s = &unicode_small_mono;
91 //op.s = &unicode_large_mono;
92 op
.s
= &unicode_large_color
;
94 sol(); //TODO: restart, etc.
104 switch (get_cmd(&from
, &to
)) {
106 if (action
[from
][to
](from
,to
))
109 case CMD_QUIT
: return;
115 int find_top(card_t
* pile
) {
117 for(i
=MAX_HIDDEN
+NUM_RANKS
-1; i
>=0 && !pile
[i
]; i
--);
120 void turn_over(card_t
* pile
) {
121 int top
= find_top(pile
);
122 if (pile
[top
] < 0) pile
[top
] *= -1;
125 card_t
stack_take(void) { /*NOTE: assert(f.w >= 0) */
126 card_t card
= f
.s
[f
.w
];
127 /* move stack one over, so there are no gaps in it: */
128 for (int i
= f
.w
; i
< f
.z
-1; i
++)
131 f
.w
--; /* make previous card visible again */
134 int t2f(int from
, int to
) { /* tableu to foundation */
135 from
--; //remove off-by-one
136 int top_from
= find_top(f
.t
[from
]);
137 to
= get_suit(f
.t
[from
][top_from
]);
138 int top_to
= find_top(f
.f
[to
]);
139 if ((top_to
< 0 && get_rank(f
.t
[from
][top_from
]) == RANK_A
)
140 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.t
[from
][top_from
])-1)) {
141 f
.f
[to
][top_to
+1] = f
.t
[from
][top_from
];
142 f
.t
[from
][top_from
] = NO_CARD
;
143 turn_over(f
.t
[from
]);
147 int w2f(int from
, int to
) { /* waste to foundation */
148 if (f
.w
< 0) return 1;
149 to
= get_suit(f
.s
[f
.w
]);
150 int top_to
= find_top(f
.f
[to
]);
151 if ((top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_A
)
152 || (get_rank(f
.f
[to
][top_to
]) == get_rank(f
.s
[f
.w
])-1)) {
153 f
.f
[to
][top_to
+1] = stack_take();
158 int s2w(int from
, int to
) { /* stock to waste */
159 if (f
.z
== 0) return 1;
161 if (f
.w
== f
.z
) f
.w
= -1;
164 int w2s(int from
, int to
) { /* waste to stock (undoes stock to waste) */
165 if (f
.z
== 0) return 1;
167 if (f
.w
< -1) f
.w
= f
.z
-1;
170 int f2t(int from
, int to
) { /* foundation to tableu */
171 //TODO: there are two possible cards one can take. choosing one isn't implemented yet!
172 to
--; //remove off-by-one
173 int top_to
= find_top(f
.t
[to
]);
174 from
= get_color(f
.t
[to
][top_to
]);
175 int top_from
= find_top(f
.f
[from
]);
176 if ((get_color(f
.t
[to
][top_to
]) != get_color(f
.f
[from
][top_from
]))
177 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.f
[from
][top_from
])+1)) {
178 f
.t
[to
][top_to
+1] = stack_take(); //XXX: not stack_take!
182 int w2t(int from
, int to
) { //waste to tableu
183 to
--; //remove off-by-one
184 int top_to
= find_top(f
.t
[to
]);
185 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.s
[f
.w
]))
186 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.s
[f
.w
])+1))
187 || (top_to
< 0 && get_rank(f
.s
[f
.w
]) == RANK_K
)) {
188 f
.t
[to
][top_to
+1] = stack_take();
192 int t2t(int from
, int to
) {
193 from
--; to
--; //remove off-by-one
194 int top_to
= find_top(f
.t
[to
]);
195 int top_from
= find_top(f
.t
[from
]);
196 for (int i
= top_from
; i
>=0; i
--) {
197 //TODO: check that we aren't moving facedown cards!
198 if (((get_color(f
.t
[to
][top_to
]) != get_color(f
.t
[from
][i
]))
199 && (get_rank(f
.t
[to
][top_to
]) == get_rank(f
.t
[from
][i
])+1))
200 || (top_to
< 0 && get_rank(f
.t
[from
][i
]) == RANK_K
)) {
201 /* move cards [i..top_from] to their destination */
202 for (;i
<= top_from
; i
++) {
204 f
.t
[to
][top_to
] = f
.t
[from
][i
];
205 f
.t
[from
][i
] = NO_CARD
;
207 turn_over(f
.t
[from
]);
211 return 1; /* no such move possible */
214 int t2t(int from
, int to
) { //TODO: in dire need of cleanup
215 from
--; to
--; //remove off-by-one
216 (from
< 0) && (from
= 9); // '0' is tenth ([9]) pile
217 (to
< 0) && (to
= 9); // ditto
219 int top_from
= find_top(f
.t
[from
]);
220 int top_to
= find_top(f
.t
[to
]);
221 for (int i
= top_from
; i
>= 0; i
--) {
222 if ((f
.t
[from
][i
+1] != NO_CARD
) // if there is a card below (TODO: check maximum)
223 && (get_rank(f
.t
[from
][i
+1]) != get_rank(f
.t
[from
][i
])-1) //and cards not consecutive?
227 if ((f
.t
[from
][i
+1] != NO_CARD
) // if there is a card below (TODO: check maximum)
228 && (get_suit(f
.t
[from
][i
+1]) != get_suit(f
.t
[from
][i
])) //and cards not same suit?
233 if(get_rank(f
.t
[from
][i
]) == get_rank(f
.t
[to
][top_to
])-1) { //TODO: to empty pile
234 for (;i
<= top_from
; i
++) {
236 f
.t
[to
][top_to
] = f
.t
[from
][i
];
237 f
.t
[from
][i
] = NO_CARD
;
239 turn_over(f
.t
[from
]);
240 //TODO: test if k..a complete; move to foundation if so
245 return 1; /* no such move possible */
247 int s2t(int from
, int to
) {
248 if (f
.z
<= 0) return 1; /* stack out of cards */
249 for (int pile
= 0; pile
< NUM_PILES
; pile
++)
250 if (f
.t
[pile
][0] == NO_CARD
) return 1; /*no piles may be empty*/
251 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
252 f
.t
[pile
][find_top(f
.t
[pile
])+1] = f
.s
[--f
.z
];
257 int nop(int from
, int to
) { return 1; }
259 int get_cmd (int* from
, int* to
) {
260 //returns 0 on success or an error code indicating game quit, new game,...
261 //TODO: check validity
272 case 'q': return CMD_QUIT
;
273 case 'r': return CMD_NEW
;
274 default: if (f
< '0' || f
> '9') return CMD_INVAL
;
287 f
= (const struct playfield
){0}; /* clear playfield */
288 card_t deck
[DECK_SIZE
*NUM_DECKS
];
289 int avail
= DECK_SIZE
*NUM_DECKS
;
290 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) deck
[i
] = (i
%DECK_SIZE
)+1;
292 //if spider-mode easy/medium:
293 for (int i
= 0; i
< DECK_SIZE
*NUM_DECKS
; i
++) {
295 int medium
= 1; //XXX
296 if (easy
||medium
) deck
[i
] = 1+((deck
[i
]-1) | 2);
297 if (easy
) deck
[i
] = 1+((deck
[i
]-1) | 1);
298 // the 1+ -1 dance gets rid of the offset created by NO_CARD
301 srandom (time(NULL
));
302 for (int i
= DECK_SIZE
*NUM_DECKS
-1; i
> 0; i
--) { //fisher-yates
303 int j
= random() % (i
+1);
304 if (j
-i
) deck
[i
]^=deck
[j
],deck
[j
]^=deck
[i
],deck
[i
]^=deck
[j
];
308 for (int i
= 0; i
< NUM_PILES
; i
++) {
310 int closed
= i
; // tableu pile n has n closed cards, then 1 open
312 int closed
= i
<4?5:4; //tableu pile 1-4 have 5, 5-10 have 4 closed cards
315 for (int j
= 0; j
< closed
; j
++) f
.t
[i
][j
] = -deck
[--avail
]; //face-down cards are negative
316 f
.t
[i
][closed
] = deck
[--avail
]; //the face-up card
318 //rest of the cards to the stock (should be 50 for spider):
319 for (f
.z
= 0; avail
; f
.z
++) f
.s
[f
.z
] = deck
[--avail
];
320 f
.w
= -1; /* @start: nothing on waste (no waste in spider -> const) */
323 void print_table(void) { //{{{
324 printf("\033[2J\033[H");//TEMP XXX -- use raw term mode
326 //print stock, waste and foundation:
327 for (int line
= 0; line
< op
.s
->height
; line
++) {
328 printf ("%s", ( /* stock */
329 (f
.w
< f
.z
-1)?op
.s
->facedown
330 :op
.s
->placeholder
)[line
]);
331 printf ("%s", ( /* waste */
332 ((short)f
.w
>= 0)?op
.s
->card
[f
.s
[f
.w
]] //TODO: sometimes segfaults because f.w == (int)((short)-1)
333 :op
.s
->placeholder
)[line
]);
334 printf ("%s", op
.s
->card
[NO_CARD
][line
]); /* spacer */
336 for (int pile
= 0; pile
< NUM_SUITS
; pile
++) {
337 for (int i
= NUM_RANKS
-1; i
>=0; i
--) {
339 printf ("%s", op
.s
->card
[f
.f
[pile
][i
]][line
]);
343 printf ("%s", op
.s
->placeholder
[line
]);
350 //print tableu piles:
351 int row
[NUM_PILES
] = {0};
352 int line
[NUM_PILES
]= {0};
353 int line_had_card
; // :|
354 int label
[NUM_PILES
] = {0};//XXX
357 for (int pile
= 0; pile
< NUM_PILES
; pile
++) {
358 card_t card
= f
.t
[pile
][row
[pile
]];
359 card_t next
= f
.t
[pile
][row
[pile
]+1];
361 (card
<0)?op
.s
->facedown
365 if (++line
[pile
] >= (next
?op
.s
->overlap
:op
.s
->height
)) {
366 //TODO: allow extreme overlap on sequences
370 if(!card
&& !label
[pile
]) label
[pile
] = 1, printf ("\b\b%d ", (pile
+1) % 10);//XXX: print tableu labels
371 line_had_card
|= !!card
;
374 } while (line_had_card
);
377 void visbell (void) {
378 printf ("\033[?5h"); fflush (stdout
);
380 printf ("\033[?5l"); fflush (stdout
);
383 void append_undo (int n
, int f
, int t
) {
384 //check if we have to free redo buffer (.next)
390 void screen_setup (int enable
) {
393 printf ("\033[s\033[?47h"); /* save cursor, alternate screen */
394 printf ("\033[H\033[J"); /* reset cursor, clear screen */
395 printf ("\033[?1000h\033[?25l"); /* enable mouse, hide cursor */
397 printf (op
.s
->init_seq
); /*swich charset, if necessary*/
400 printf (op
.s
->reset_seq
);/*reset charset, if necessary*/
401 printf ("\033[?9l\033[?25h"); /* disable mouse, show cursor */
402 printf ("\033[?47l\033[u"); /* primary screen, restore cursor */
407 void raw_mode(int enable
) { //{{{
408 static struct termios saved_term_mode
;
409 struct termios raw_term_mode
;
412 tcgetattr(STDIN_FILENO
, &saved_term_mode
);
413 raw_term_mode
= saved_term_mode
;
414 raw_term_mode
.c_lflag
&= ~(ICANON
| ECHO
);
415 raw_term_mode
.c_cc
[VMIN
] = 1 ;
416 raw_term_mode
.c_cc
[VTIME
] = 0;
417 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_term_mode
);
419 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &saved_term_mode
);
423 //vim: foldmethod=marker