Debian Codegraph @

games/freecell.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* The Ace of Penguins - freecell.c
2 Copyright (C) 1998, 2001 DJ Delorie
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <time.h>
22 #include "imagelib.h"
23 #include "table.h"
24 #include "cards.h"
25
26 #define W CARD_WIDTH
27 #define H CARD_HEIGHT
28 #define M CARD_MARGIN
29 #define F CARD_FAN_DOWN
30
31 static Picture *xlogo, *splash, *youwin, *youlose;
32
33 static Stack *deck;
34 static Stack *freecells[4];
35 static Stack *outcells[4];
36 static Stack *maincells[8];
37
38 static int auto_move();
39
40 static void
41 start_again()
42 {
43 int i;
44 for (i=0; i<4; i++)
45 {
46 stack_flip_stack(freecells[i], deck, 0);
47 stack_flip_stack(outcells[i], deck, 0);
48 }
49 for (i=0; i<8; i++)
50 stack_flip_stack(maincells[i], deck, 0);
51 stack_shuffle(deck);
52 stack_shuffle(deck);
53 stack_shuffle(deck);
54 for (i=0; i<52; i++)
55 stack_flip_card(deck, maincells[i%8], 0);
56
57 stack_undo_reset();
58 }
59
60 static void
61 init()
62 {
63 int s, v;
64 char name[30];
65 Picture *empty;
66
67 stack_load_standard_deck();
68 empty = get_picture("empty");
69
70 set_centered_pic(splash);
71
72 for (s=0; s<4; s++)
73 {
74 freecells[s] = stack_create(s*W, 0);
75 stack_set_empty_picture(freecells[s], empty);
76
77 outcells[s] = stack_create(4*W+9*M+s*W, 0);
78 stack_set_empty_picture(outcells[s], empty);
79 }
80
81 for (s=0; s<8; s++)
82 {
83 maincells[s] = stack_create(M+s*(M+W), H+M);
84 stack_set_offset(maincells[s], STACK_OFFSET_DOWN);
85 }
86
87 deck = stack_create(10*W, 0);
88 for (s=0; s<4; s++)
89 for (v=ACE; v<=KING; v++)
90 stack_add_card(deck, MAKE_CARD(s, v, FACEDOWN));
91
92 start_again();
93 }
94
95 static void
96 resize(int w, int h)
97 {
98 int margin, offset, cw, ch, s;
99 Picture *empty;
100
101 stack_set_card_size (w/9, w/9*4/3);
102 stack_get_card_size (&cw, &ch);
103
104 empty = (Picture *)get_image("empty", cw, ch, 0);
105
106 margin = (w - 8*cw) / 9;
107 offset = (w - margin*9 - cw*8) / 2 + margin;
108
109 for (s=0; s<4; s++)
110 {
111 stack_move(freecells[s], s*cw, 0);
112 stack_move(outcells[s], w - (4-s)*cw, 0);
113 stack_set_empty_picture(freecells[s], empty);
114 stack_set_empty_picture(outcells[s], empty);
115 }
116 for (s=0; s<8; s++)
117 stack_move(maincells[s], offset + s*(cw+margin), ch + (offset<0?0:offset));
118 }
119
120 static void
121 redraw()
122 {
123 int cw, ch;
124 stack_get_card_size (&cw, &ch);
125 if (xlogo->w < table_width - 8*cw && xlogo->h < ch)
126 put_picture(xlogo, table_width/2-xlogo->w/2, ch/2-xlogo->h/2,
127 0, 0, xlogo->h, xlogo->w);
128 stack_redraw();
129 }
130
131 extern char freecell_help[];
132
133 static void
134 key(int k, int x, int y)
135 {
136 Picture *p = get_centered_pic();
137 if (k == 3 || k == 27 || k == 'q' || k == 'Q')
138 exit(0);
139 set_centered_pic(0);
140 if (k == 8 || k == 127 || k == KEY_DELETE)
141 {
142 stack_undo();
143 return;
144 }
145 if (p == splash)
146 return;
147 if (k == KEY_F(1) || k == 'h' || k == 'H')
148 {
149 help("freecell.html", freecell_help);
150 return;
151 }
152 if (k == KEY_F(2) || p || k == 'r' || k == 'R')
153 {
154 start_again();
155 while (auto_move());
156 return;
157 }
158
159 if (k == ' ')
160 {
161 double_click(x, y, 1);
162 }
163 }
164
165 static char *card_string(int c)
166 {
167 static char names[5][5];
168 static int n=0;
169 static char suits[] = "HDCS";
170 static char face[] = "A234567890JQK";
171 n = (n+1)%5;
172 sprintf(names[n], "%c%c", face[VALUE(c)], suits[SUIT(c)]);
173 return names[n];
174 }
175
176 static Stack *src_stack = 0;
177 static int src_n = 0, src_maxn = 0;
178
179 static Stack *dest_stack;
180 static int last_n=-1;
181
182 static int
183 max_freecells(Stack *excl1, Stack *excl2)
184 {
185 int num, empty, i;
186 empty = 0;
187 num = 1;
188 for (i=0; i<4; i++)
189 if (stack_count_cards(freecells[i]) == 0)
190 num++;
191 for (i=0; i<8; i++)
192 if (stack_count_cards(maincells[i]) == 0
193 && maincells[i] != excl1
194 && maincells[i] != excl2)
195 num *= 2;
196 return num;
197 }
198
199 static int
200 n_droppable_s(Stack *dest_stack)
201 {
202 int src_top, dest_top, src_count, dest_count, i, max_free;
203 int src_value, src_suit, dest_value, dest_suit;
204 src_count = stack_count_cards(src_stack);
205 dest_count = stack_count_cards(dest_stack);
206 dest_top = stack_get_card(dest_stack, dest_count-1);
207 src_top = stack_get_card(src_stack, src_count-1);
208
209 if (dest_stack == src_stack)
210 return src_n;
211
212 max_free = max_freecells(dest_stack, src_stack);
213
214 for (i=0; i<4; i++)
215 if (dest_stack == freecells[i])
216 {
217 if (stack_count_cards(freecells[i]) == 0)
218 return src_count-1;
219 return src_count;
220 }
221
222 for (i=0; i<4; i++)
223 if (dest_stack == outcells[i])
224 {
225 if (dest_count == 0 && VALUE(src_top) == ACE)
226 return src_count-1;
227 if (dest_count > 0 && VALUE(src_top) == VALUE(dest_top)+1
228 && SUIT(src_top) == SUIT(dest_top))
229 return src_count-1;
230 return src_count;
231 }
232
233 for (i=0; i<8; i++)
234 if (dest_stack == maincells[i])
235 {
236 int n = src_count-1;
237
238 if (src_n == 0 && dest_count == 0)
239 return 0;
240
241 while (1)
242 {
243 int src_next;
244
245 src_top = stack_get_card(src_stack, n);
246 if ((COLOR(src_top) != COLOR(dest_top)
247 && VALUE(src_top) == VALUE(dest_top)-1)
248 || (n == src_n && dest_count == 0))
249 return n;
250
251 src_next = stack_get_card(src_stack, n-1);
252 if (n == 0 || COLOR(src_top) == COLOR(src_next)
253 || VALUE(src_top) != VALUE(src_next)-1)
254 {
255 if (dest_count == 0)
256 return n;
257 break;
258 }
259
260 if (n <= src_count - max_free)
261 break;
262
263 n--;
264 }
265 return src_count;
266 }
267
268 return src_count;
269 }
270
271 static int
272 n_droppable(int x, int y)
273 {
274 if (!src_stack)
275 return 0;
276 if (!stack_drag_find(x, y, &dest_stack))
277 {
278 dest_stack = src_stack;
279 return last_n != -1 ? last_n : src_n;
280 }
281 return n_droppable_s(dest_stack);
282 }
283
284 static int lowest[2];
285 static int pile_for[4];
286 static int top_card[4];
287
288 static int
289 auto_move_stack(Stack *s)
290 {
291 int i, c, n = stack_count_cards(s);
292 if (n == 0)
293 return 0;
294 c = stack_get_card(s, n-1);
295 if (FACEDOWNP(c))
296 {
297 stack_flip_card(s, s, 1);
298 c = stack_get_card(s, n-1);
299 return 1;
300 }
301 if (VALUE(c) == ACE)
302 {
303 for (i=0; i<4; i++)
304 if (stack_count_cards(outcells[i]) == 0)
305 {
306 stack_animate(s, outcells[i], 1);
307 return 1;
308 }
309 return 0; /* can't happen? */
310 }
311 if (pile_for[SUIT(c)] == -1)
312 return 0;
313 if (VALUE(c) == VALUE(top_card[pile_for[SUIT(c)]])+1
314 && VALUE(c) <= lowest[COLOR(c)?0:1] + 2
315 && VALUE(c) <= lowest[COLOR(c)?1:0] + 3)
316 {
317 stack_animate(s, outcells[pile_for[SUIT(c)]], 1);
318 return 1;
319 }
320 return 0;
321 }
322
323 static int
324 auto_move()
325 {
326 int i, something_moved = 0;
327 lowest[0] = lowest[1] = 99;
328 pile_for[0] = pile_for[1] = pile_for[2] = pile_for[3] = -1;
329 memset(top_card, 0, sizeof(top_card));
330 for (i=0; i<4; i++)
331 {
332 int c, n = stack_count_cards(outcells[i]);
333 if (n == 0) continue;
334 c = stack_get_card(outcells[i], n-1);
335 top_card[i] = c;
336 pile_for[SUIT(c)] = i;
337 if (lowest[COLOR(c)?1:0] > VALUE(c))
338 lowest[COLOR(c)?1:0] = VALUE(c);
339 }
340 for (i=0; i<4; i++)
341 if (pile_for[i] == -1)
342 lowest[COLOR(MAKE_CARD(i, ACE, FACEUP))?1:0] = 0;
343 for (i=0; i<4; i++)
344 something_moved += auto_move_stack(freecells[i]);
345 for (i=0; i<8; i++)
346 something_moved += auto_move_stack(maincells[i]);
347 return something_moved;
348 }
349
350 static void
351 check_for_end_of_game()
352 {
353 int i, j;
354 while (auto_move());
355 if (stack_count_cards(outcells[0]) == 13
356 && stack_count_cards(outcells[1]) == 13
357 && stack_count_cards(outcells[2]) == 13
358 && stack_count_cards(outcells[3]) == 13)
359 {
360 set_centered_pic(youwin);
361 }
362
363 /* look for losing configuration */
364 for (i=0; i<4; i++)
365 if (stack_count_cards(freecells[i]) == 0)
366 return;
367 for (i=0; i<8; i++)
368 if (stack_count_cards(maincells[i]) == 0)
369 return;
370 for (i=0; i<8; i++)
371 {
372 int in = stack_count_cards(maincells[i]);
373 int ic = stack_get_card(maincells[i], in-1);
374 #if 0
375 /* There ARE situations where you can unpeel a sorted stack onto
376 other stacks, one card at a time, and go on to win. Sigh. */
377 if (in > 1)
378 {
379 /* If this stack is already partially sorted, skip it */
380 int inc = stack_get_card(maincells[i], in-2);
381 if (COLOR(inc) != COLOR(ic) && VALUE(inc) == VALUE(ic)+1)
382 continue;
383 }
384 #endif
385 for (j=0; j<8; j++)
386 {
387 /* Can we move a card to another stack? */
388 int dc = stack_get_card(maincells[j], stack_count_cards(maincells[j])-1);
389 if (COLOR(dc) != COLOR(ic) && VALUE(dc) == VALUE(ic)+1)
390 return;
391 }
392 for (j=0; j<4; j++)
393 {
394 /* Can we move a card from a maincell to an outcell? */
395 int dc = stack_get_card(outcells[j], stack_count_cards(outcells[j])-1);
396 if (SUIT(dc) == SUIT(ic) && VALUE(dc) == VALUE(ic)-1)
397 return;
398 }
399 }
400 for (i=0; i<4; i++)
401 {
402 int in = stack_count_cards(freecells[i]);
403 int ic = stack_get_card(freecells[i], in-1);
404 for (j=0; j<8; j++)
405 {
406 /* Can we move a card from a freecell to a stack? */
407 int dc = stack_get_card(maincells[j], stack_count_cards(maincells[j])-1);
408 if (COLOR(dc) != COLOR(ic) && VALUE(dc) == VALUE(ic)+1)
409 return;
410 }
411 for (j=0; j<4; j++)
412 {
413 /* Can we move a card from a freecell to an outcell? */
414 int dc = stack_get_card(outcells[j], stack_count_cards(outcells[j])-1);
415 if (SUIT(dc) == SUIT(ic) && VALUE(dc) == VALUE(ic)-1)
416 return;
417 }
418 }
419 set_centered_pic(youlose);
420 }
421
422 static void
423 click(int x, int y, int b)
424 {
425 int c, f;
426 Picture *cp = get_centered_pic();
427
428 if (cp == splash)
429 {
430 set_centered_pic(0);
431 return;
432 }
433
434 if (cp)
435 {
436 set_centered_pic(0);
437 start_again();
438 return;
439 }
440
441 src_stack = 0;
442 f = stack_find(x, y, &src_stack, &src_n);
443 if (!f) return;
444
445 if (b > 1)
446 {
447 stack_peek_card(src_stack, src_n, 1);
448 return;
449 }
450 last_n = -1;
451
452 for (c=0; c<4; c++)
453 if (src_stack == outcells[c])
454 return;
455
456 if (f && stack_count_cards(src_stack) > 0)
457 stack_begin_drag(src_stack, src_n, x, y);
458 else
459 src_stack = 0;
460 }
461
462 static int
463 stack_complete(Stack *s)
464 {
465 int i;
466 for (i=stack_count_cards(s)-1; i>0; i--)
467 {
468 int c1 = stack_get_card(s, i);
469 int c2 = stack_get_card(s, i-1);
470 if (COLOR(c1) == COLOR(c2) || VALUE(c1) != VALUE(c2)-1)
471 return 0;
472 }
473 return 1;
474 }
475
476 static void
477 double_click_1(int x, int y, int b)
478 {
479 int f, i, n, cnt;
480 src_stack = 0;
481 f = stack_find(x, y, &src_stack, &src_n);
482 if (!f) return;
483
484 if (b > 1) return;
485
486 /* double-click-move choices:
487 1. main stack
488 2. empty main
489 3. empty free
490 4. outcell
491 */
492
493 #define TOPIF(s) int c, n = stack_count_cards(s); \
494 if (n==0) return; \
495 c = stack_get_card(s, n-1)
496 #define TOPOF(s) stack_get_card(s, stack_count_cards(s)-1)
497
498 for (i=0; i<8; i++)
499 if (src_stack == maincells[i])
500 {
501 TOPIF(maincells[i]);
502 src_stack = maincells[i];
503 src_n = 1;
504 cnt = stack_count_cards(maincells[i]);
505 for (f=0; f<8; f++)
506 {
507 if (!stack_complete(maincells[f]))
508 continue;
509 n = n_droppable_s(maincells[f]);
510 if (f != i && n < cnt && stack_count_cards(maincells[f]))
511 {
512 if (n == cnt-1)
513 stack_animate(maincells[i], maincells[f], 0);
514 else
515 stack_move_cards(maincells[i], maincells[f], cnt - n, 0);
516 return;
517 }
518 }
519 for (f=0; f<8; f++)
520 {
521 n = n_droppable_s(maincells[f]);
522 if (f != i && n < cnt &&
523 (stack_count_cards(maincells[f]) || n))
524 {
525 if (n == cnt-1)
526 stack_animate(maincells[i], maincells[f], 0);
527 else
528 stack_move_cards(maincells[i], maincells[f], cnt - n, 0);
529 return;
530 }
531 }
532 if (src_n > 1)
533 {
534 for (f=0; f<8; f++)
535 {
536 int c2 = TOPOF(maincells[f]);
537 if (f != i && stack_count_cards(maincells[f]) == 0)
538 {
539 stack_animate(maincells[i], maincells[f], 0);
540 return;
541 }
542 }
543 }
544 for (f=0; f<4; f++)
545 {
546 int c2 = TOPOF(freecells[f]);
547 if (stack_count_cards(freecells[f]) == 0)
548 {
549 stack_animate(maincells[i], freecells[f], 0);
550 return;
551 }
552 }
553 for (f=0; f<4; f++)
554 {
555 int c2 = TOPOF(outcells[f]);
556 if (SUIT(c2) == SUIT(c) && VALUE(c2) == VALUE(c)-1)
557 {
558 stack_animate(maincells[i], outcells[f], 0);
559 return;
560 }
561 }
562 }
563
564 for (i=0; i<4; i++)
565 if (src_stack == freecells[i])
566 {
567 TOPIF(freecells[i]);
568 for (f=0; f<8; f++)
569 {
570 int c2 = TOPOF(maincells[f]);
571 if (COLOR(c2) != COLOR(c) && VALUE(c2) == VALUE(c)+1)
572 {
573 stack_animate(freecells[i], maincells[f], 0);
574 return;
575 }
576 }
577 for (f=0; f<8; f++)
578 {
579 int c2 = TOPOF(maincells[f]);
580 if (stack_count_cards(maincells[f]) == 0)
581 {
582 stack_animate(freecells[i], maincells[f], 0);
583 return;
584 }
585 }
586 for (f=0; f<4; f++)
587 {
588 int c2 = TOPOF(outcells[f]);
589 if (SUIT(c2) == SUIT(c) && VALUE(c2) == VALUE(c)-1)
590 {
591 stack_animate(freecells[i], outcells[f], 0);
592 return;
593 }
594 }
595 }
596 }
597
598 static void
599 double_click(int x, int y, int b)
600 {
601 double_click_1(x, y, b);
602 while (auto_move());
603 check_for_end_of_game();
604 }
605
606 static void
607 drag(int x, int y, int b)
608 {
609 if (b > 1) return;
610 last_n = n_droppable(x, y);
611 stack_continue_drag(last_n, x, y);
612 }
613
614 static void
615 drop(int x, int y, int b)
616 {
617 last_n = n_droppable(x, y); /* also sets dest_stack */
618
619 if (b > 1)
620 {
621 if (src_stack)
622 stack_peek_card(src_stack, src_n, 0);
623 return;
624 }
625
626 stack_drop(dest_stack, last_n, 0);
627 check_for_end_of_game();
628 }
629
630 static FunctionMapping fmap[] = {
631 { "click", (void *)click },
632 { "double_click", (void *)double_click },
633 { "drag", (void *)drag },
634 { "drop", (void *)drop },
635 { "init", (void *)init },
636 { "key", (void *)key },
637 { "redraw", (void *)redraw },
638 { "resize", (void *)resize },
639 { 0, 0 }
640 };
641
642 int
643 main(int argc, char **argv)
644 {
645 register_imagelib(appimglib_imagelib);
646 init_ace(argc, argv, fmap);
647 xlogo = get_picture("xemboss");
648 splash = get_picture("freecell");
649 youwin = get_picture("youwin");
650 youlose = get_picture("youlose");
651 if (table_width == 0 || table_height == 0)
652 {
653 table_width = 640;
654 table_height = 480;
655 }
656 init_table(table_width, table_height);
657 table_loop();
658 }