Debian Codegraph @

games/solitaire.c

raw symbols SCIP
1 /* The Ace of Penguins - solitaire.c
2 Copyright (C) 1998, 2001, 2002 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 <math.h>
23 #include "imagelib.h"
24 #include "table.h"
25 #include "cards.h"
26
27 #define W CARD_WIDTH
28 #define H CARD_HEIGHT
29 #define M CARD_MARGIN
30 #define F CARD_FAN_DOWN
31
32 static Picture *xlogo, *splash, *youwin, *youlose;
33
34 static Stack *deck, *hole;
35 static Stack *outcells[4];
36 static Stack *maincells[7];
37
38 static int auto_move();
39 static void update_status_text(int redraw);
40 static void check_for_end_of_game();
41
42 static int no_auto = 0; /* boolean */
43 static int use_auto_moves = 1; /* boolean */
44 static int drag_active = 0; /* boolean - set if card is being dragged */
45 static int flip_3s = 0; /* boolean - zero means flip 1 at a time */
46 /* non-zero means flip 3 at a time */
47 static int vegas = 0; /* boolean - in vegas style, you pay $52 to play */
48 /* and you win $5 for each card that you */
49 /* place in the outdeck. The deck is only */
50 /* turned 3 times. flip_3s is implied to */
51 /* be true. */
52 static int winnings = 0;
53 static int vegas_deck_count = 0;
54 static int display_vegas_xlogo = 0; /* set to 1 to display xlogo in pile area at */
55 /* end of vegas game. */
56 static OptionDesc new_options[] = {
57 { "-noauto", OPTION_BOOLEAN, &no_auto },
58 { "-flip3s", OPTION_BOOLEAN, &flip_3s },
59 { "-vegas", OPTION_BOOLEAN, &vegas },
60 { 0, 0, 0 }
61 };
62
63 OptionDesc *app_options = new_options;
64
65 static void
66 start_again()
67 {
68 int i, p, pc;
69 stack_flip_stack(hole, deck, 0);
70 for (i=0; i<4; i++)
71 stack_flip_stack(outcells[i], deck, 0);
72 for (i=0; i<7; i++)
73 stack_flip_move_stack(maincells[i], deck, 0);
74
75 stack_shuffle(deck);
76 stack_shuffle(deck);
77 stack_shuffle(deck);
78
79 for (p=0; p<7; p++)
80 for (pc=0; pc<=p; pc++)
81 stack_move_card(deck, maincells[p], 0);
82 for (p=0; p<7; p++)
83 stack_flip_card(maincells[p], maincells[p], 0);
84
85 stack_undo_reset();
86
87 winnings -= 52;
88 update_status_text(1);
89 vegas_deck_count = 0;
90 display_vegas_xlogo = 0;
91 }
92
93 static int xlogo_x;
94 static int xlogo_y;
95
96 static int vegas_xlogo_x;
97 static int vegas_xlogo_y;
98
99 static void
100 init()
101 {
102 int s, v;
103 Picture *empty;
104
105 stack_load_standard_deck();
106 empty = get_picture("empty");
107 xlogo = get_picture("xemboss");
108 splash = get_picture("solitaire");
109 youwin = get_picture("youwin");
110 youlose = get_picture("youlose");
111
112 xlogo_x = 3*M+2*W+W/2-xlogo->w/2;
113 xlogo_y = M+H/2-xlogo->h/2;
114
115 vegas_xlogo_x = M+W/2-xlogo->w/2;
116 vegas_xlogo_y = M+H/2-xlogo->h/2;
117
118 set_centered_pic(splash);
119
120 for (s=0; s<4; s++)
121 {
122 outcells[s] = stack_create(3*W+4*M+s*(W+M), M);
123 stack_set_empty_picture(outcells[s], empty);
124 }
125
126 for (s=0; s<7; s++)
127 {
128 maincells[s] = stack_create(M+s*(M+W), H+2*M);
129 stack_set_offset(maincells[s], STACK_OFFSET_DOWN);
130 }
131
132 deck = stack_create(M, M);
133 hole = stack_create(2*M+W, M);
134
135 for (s=0; s<4; s++)
136 for (v=1; v<=13; v++)
137 stack_add_card(deck, MAKE_CARD(s, v, FACEDOWN));
138 start_again();
139 }
140
141 static void
142 resize(int w, int h)
143 {
144 int margin, offset, cw, ch, s;
145 Picture *empty;
146
147 stack_set_card_size (w/8, w/8*4/3);
148 stack_get_card_size (&cw, &ch);
149
150 empty = (Picture *)get_image("empty", cw, ch, 0);
151
152 margin = (w - 7*cw) / 8;
153 offset = (w - margin*8 - cw*7) / 2 + margin;
154
155 for (s=0; s<4; s++)
156 {
157 stack_move(outcells[s], w - (4-s)*(cw+margin), margin);
158 stack_set_empty_picture(outcells[s], empty);
159 }
160 for (s=0; s<7; s++)
161 stack_move(maincells[s], offset + s*(cw+margin), ch + (offset<0?0:offset) + margin);
162
163 stack_move(deck, offset, margin);
164 stack_move(hole, offset+margin+cw, margin);
165
166 if (xlogo->w > cw)
167 xlogo_x = xlogo_y = vegas_xlogo_x = vegas_xlogo_y = 0;
168 else
169 {
170 xlogo_x = 3 * margin + 2 * cw + cw/2 - xlogo->w/2;
171 xlogo_y = margin + ch/2 - xlogo->h/2;
172 vegas_xlogo_x = margin + cw/2 - xlogo->w/2;
173 vegas_xlogo_y = margin + ch/2 - xlogo->h/2;
174 }
175 }
176
177 static void
178 redraw()
179 {
180 if (xlogo_x || xlogo_y)
181 put_picture(xlogo, xlogo_x, xlogo_y, 0, 0, xlogo->h, xlogo->w);
182
183 if (display_vegas_xlogo && (vegas_xlogo_x || vegas_xlogo_y))
184 put_picture(xlogo, vegas_xlogo_x, vegas_xlogo_y, 0, 0, xlogo->h, xlogo->w);
185 update_status_text(0);
186
187 stack_redraw();
188 }
189
190 static void check_for_end_of_game();
191
192 extern char solitaire_help[];
193
194 static void
195 key(int k, int x, int y)
196 {
197 if (k == 3 || k == 27 || k == 'q' || k == 'Q')
198 exit(0);
199 if (k == KEY_F(1) || k == 'h' || k == 'H')
200 {
201 set_centered_pic(0);
202 help("solitaire.html", solitaire_help);
203 }
204 if (k == ' ')
205 {
206 double_click(x, y, 1);
207 check_for_end_of_game();
208 }
209
210 if (k == 'w')
211 set_centered_pic(youwin);
212 if (k == 'l')
213 set_centered_pic(youlose);
214
215 if (k == KEY_F(2) || k == 'r' || k == 'R')
216 {
217 set_centered_pic(0);
218 start_again();
219
220 if (use_auto_moves)
221 while (auto_move());
222
223 }
224 if ((k == 8 || k == 127 || k == KEY_DELETE))
225 {
226 stack_undo();
227 }
228 }
229
230 static Stack *src_stack = 0;
231 static int src_n = 0, src_maxn = 0;
232
233 static Stack *dest_stack;
234 static int last_n=-1;
235
236 static int
237 n_droppable_s(Stack *dest_stack)
238 {
239 int src_top, dest_top, src_count, dest_count, i;
240 int src_value, src_suit, dest_value, dest_suit;
241 src_count = stack_count_cards(src_stack);
242 dest_count = stack_count_cards(dest_stack);
243 dest_top = stack_get_card(dest_stack, dest_count-1);
244 src_top = stack_get_card(src_stack, src_count-1);
245
246 if (dest_stack == src_stack)
247 return src_n;
248
249 if (dest_stack == deck || dest_stack == hole)
250 return src_count;
251
252 for (i=0; i<4; i++)
253 if (dest_stack == outcells[i])
254 {
255 if (dest_count == 0 && VALUE(src_top) == ACE)
256 return src_count-1;
257 if (dest_count > 0 && VALUE(src_top) == VALUE(dest_top)+1
258 && SUIT(src_top) == SUIT(dest_top))
259 return src_count-1;
260 return src_count;
261 }
262
263 for (i=0; i<7; i++)
264 if (dest_stack == maincells[i])
265 {
266 int n = src_count-1;
267
268 if (src_stack != hole && src_n == 0 && dest_count == 0)
269 return 0;
270
271 if (dest_count && FACEDOWNP(dest_top))
272 return src_count;
273
274 while (1)
275 {
276 int src_next;
277 src_top = stack_get_card(src_stack, n);
278 if ((COLOR(src_top) != COLOR(dest_top)
279 && VALUE(src_top) == VALUE(dest_top)-1)
280 || VALUE(src_top)==13 && dest_count == 0)
281 return n;
282
283 src_next = stack_get_card(src_stack, n-1);
284 if (n == 0 || COLOR(src_top) == COLOR(src_next)
285 || VALUE(src_top) != VALUE(src_next)-1
286 || FACEDOWNP(src_next)
287 || src_stack == hole)
288 break;
289
290 n--;
291 }
292 return src_count;
293 }
294
295 return src_count;
296 }
297
298 static int
299 n_droppable(int x, int y)
300 {
301 if (!src_stack)
302 return 0;
303 if (!stack_drag_find(x, y, &dest_stack))
304 {
305 dest_stack = src_stack;
306 return last_n != -1 ? last_n : src_n;
307 }
308 return n_droppable_s(dest_stack);
309 }
310
311 static int lowest[2];
312 static int pile_for[4];
313 static int top_card[4];
314
315 static int
316 auto_move_stack(Stack *s)
317 {
318 int i, c, n = stack_count_cards(s);
319 if (n == 0)
320 return 0;
321 c = stack_get_card(s, n-1);
322 if (FACEDOWNP(c))
323 {
324 stack_flip_card(s, s, 1);
325 c = stack_get_card(s, n-1);
326 return 1;
327 }
328 if (VALUE(c) == ACE)
329 {
330 for (i=0; i<4; i++)
331 if (stack_count_cards(outcells[i]) == 0)
332 {
333 stack_animate(s, outcells[i], 1);
334
335 winnings += 5;
336 update_status_text(1);
337
338 return 1;
339 }
340 return 0; /* can't happen? */
341 }
342 if (pile_for[SUIT(c)] == -1)
343 return 0;
344 if (VALUE(c) == VALUE(top_card[pile_for[SUIT(c)]])+1
345 && VALUE(c) <= lowest[COLOR(c)?0:1] + 2
346 && VALUE(c) <= lowest[COLOR(c)?1:0] + 3)
347 {
348 stack_animate(s, outcells[pile_for[SUIT(c)]], 1);
349
350 winnings += 5;
351 update_status_text(1);
352
353 return 1;
354 }
355 return 0;
356 }
357
358 static int
359 auto_move()
360 {
361 int i, something_moved = 0;
362 lowest[0] = lowest[1] = 99;
363 pile_for[0] = pile_for[1] = pile_for[2] = pile_for[3] = -1;
364 memset(top_card, 0, sizeof(top_card));
365 for (i=0; i<4; i++)
366 {
367 int c, n = stack_count_cards(outcells[i]);
368 if (n == 0) continue;
369 c = stack_get_card(outcells[i], n-1);
370 top_card[i] = c;
371 pile_for[SUIT(c)] = i;
372 if (lowest[COLOR(c)?1:0] > VALUE(c))
373 lowest[COLOR(c)?1:0] = VALUE(c);
374 }
375 for (i=0; i<4; i++)
376 if (pile_for[i] == -1)
377 lowest[COLOR(MAKE_CARD(i, ACE, FACEUP))?1:0] = 0;
378 if (stack_count_cards(hole) == 0 && stack_count_cards(deck) > 0)
379 {
380 stack_flip_card(deck, hole, 1);
381
382 if (flip_3s)
383 {
384 for (i=0; i<2; i++)
385 if (stack_count_cards(deck))
386 stack_flip_card(deck, hole, 1);
387 }
388
389 something_moved += 1;
390 }
391 something_moved += auto_move_stack(hole);
392 for (i=0; i<7; i++)
393 something_moved += auto_move_stack(maincells[i]);
394 return something_moved;
395 }
396
397 static void
398 check_for_end_of_game()
399 {
400 if (use_auto_moves)
401 while (auto_move());
402
403 if (stack_count_cards(outcells[0]) == 13
404 && stack_count_cards(outcells[1]) == 13
405 && stack_count_cards(outcells[2]) == 13
406 && stack_count_cards(outcells[3]) == 13)
407 {
408 set_centered_pic(youwin);
409 }
410 }
411
412 static void
413 click(int x, int y, int b)
414 {
415 int c, f;
416
417 Picture *cp = get_centered_pic();
418 src_stack = 0;
419
420 // abort drag on click of other mousebutton
421 if (drag_active) {
422 stack_drop(dest_stack, last_n, 0);
423 drag_active = 0;
424 return;
425 };
426
427 if ((cp == youlose || cp == youwin)
428 && (x > table_width/2-cp->w/2
429 && x < table_width/2+cp->w/2
430 && y > table_height/2-cp->h/2
431 && y < table_height/2+cp->h/2))
432 {
433 set_centered_pic(0);
434 start_again();
435 return;
436 }
437
438 if (cp == splash)
439 {
440 set_centered_pic(0);
441 return;
442 }
443 f = stack_find(x, y, &src_stack, &src_n);
444 if (!f)
445 return;
446
447 if (b > 1)
448 {
449 if (src_stack && src_n>=0 && src_n <= stack_count_cards(src_stack)-1)
450 stack_peek_card(src_stack, src_n, 1);
451 return;
452 }
453
454 last_n = -1;
455
456 if (src_stack == deck)
457 {
458 if (stack_count_cards(deck) == 0)
459 {
460 if (vegas)
461 {
462 vegas_deck_count++;
463 if (vegas_deck_count >= 3)
464 {
465 start_again();
466 return;
467 }
468 }
469 stack_flip_stack(hole, deck, 0);
470 }
471 else
472 {
473 stack_flip_card(deck, hole, 0);
474 if (flip_3s)
475 for (c=0; c<2; c++)
476 if (stack_count_cards(deck) != 0)
477 stack_flip_card(deck, hole, 1);
478 if (vegas && vegas_deck_count == 2 && stack_count_cards(deck) == 0)
479 {
480 display_vegas_xlogo = 1;
481 invalidate (vegas_xlogo_x, vegas_xlogo_y,
482 xlogo->w, xlogo->h);
483 }
484 }
485 return;
486 }
487
488 if (vegas)
489 for (c=0; c<4; c++)
490 if (src_stack == outcells[c])
491 {
492 winnings -= 5;
493 update_status_text(1);
494 }
495
496 if (src_n>=0 && stack_count_cards(src_stack) > 0)
497 {
498 c = stack_get_card(src_stack, src_n);
499 if (FACEDOWNP(c))
500 stack_flip_card(src_stack, src_stack, 0);
501
502 stack_begin_drag(src_stack, src_n, x, y);
503 }
504 }
505
506 static void
507 double_click(int x, int y, int b)
508 {
509 int c, f, n, sc;
510 Stack *dest_stack=0;
511 int dest_n;
512 src_stack = 0;
513 /*printf("double click\n");*/
514
515 f = stack_find(x, y, &src_stack, &src_n);
516 if (!f)
517 return;
518
519 if (b > 1) return;
520
521 if (src_stack == deck)
522 {
523 click(x, y, b);
524 return;
525 }
526
527 sc = stack_count_cards(src_stack);
528
529 if (VALUE(stack_get_card(src_stack, sc-1)) > ACE)
530 for (c=0; c<7; c++)
531 {
532 if (src_stack == maincells[c]) continue;
533 n = n_droppable_s(maincells[c]);
534 if (stack_count_cards(maincells[c]) == 0
535 && (VALUE(stack_get_card(src_stack, n)) != KING
536 || FACEDOWNP(stack_get_card(src_stack, n))
537 || (src_stack != hole && n == 0)))
538 continue;
539 /*printf("- maincell %d = %d\n", c, n);*/
540 if (stack_count_cards(maincells[c]) == 0)
541 {
542 if (n < sc)
543 {
544 dest_stack = maincells[c];
545 dest_n = n;
546 /*break;*/
547 }
548 continue;
549 }
550 stack_card_posn(maincells[c], 0, &x, &y);
551 if (n < sc)
552 {
553 if (dest_stack)
554 if (dest_n < n || (dest_n == n && stack_count_cards(dest_stack)<stack_count_cards(maincells[c])))
555 break;
556 dest_stack = maincells[c];
557 dest_n = n;
558 }
559 }
560
561 if (dest_stack)
562 {
563 /*printf("- move 0x%08x %d 0x%08x\n", src_stack, dest_n, dest_stack);*/
564 stack_move_cards(src_stack, dest_stack, stack_count_cards(src_stack) - dest_n, 0);
565 check_for_end_of_game();
566 return;
567 }
568
569 /*printf("- checking stacks, sc=%d\n", sc);*/
570 for (c=0; c<4; c++)
571 {
572 if (src_stack == outcells[c]) return;
573 stack_card_posn(outcells[c], 0, &x, &y);
574 n = n_droppable_s(outcells[c]);
575 /*printf("- outcell %d = %d\n", c, n);*/
576 if (n < sc)
577 {
578 dest_stack = outcells[c];
579 dest_n = n;
580 break; /* we don't care which outcell it is */
581 }
582 }
583
584 if (dest_stack)
585 {
586 /*printf("- move 0x%08x %d 0x%08x\n", src_stack, dest_n, dest_stack);*/
587 stack_move_cards(src_stack, dest_stack, stack_count_cards(src_stack) - dest_n, 0);
588 check_for_end_of_game();
589 return;
590 }
591
592 check_for_end_of_game();
593 }
594
595 static void
596 drag(int x, int y, int b)
597 {
598 drag_active = 1;
599 if (b > 1) return;
600 drag_active = 1;
601 last_n = n_droppable(x, y);
602 stack_continue_drag(last_n, x, y);
603 }
604
605 static void
606 drop(int x, int y, int b)
607 {
608 int i;
609
610 last_n = n_droppable(x, y); /* also sets dest_stack */
611
612 if (b > 1)
613 {
614 if (src_stack && src_n>=0 && src_n <= stack_count_cards(src_stack)-1)
615 stack_peek_card(src_stack, src_n, 0);
616 return;
617 }
618
619 if (vegas)
620 for (i=0; i<4; i++)
621 if (dest_stack == outcells[i])
622 {
623 winnings += (stack_count_cards(src_stack) - last_n) * 5;
624 update_status_text(1);
625 }
626
627 drag_active = 0;
628
629 stack_drop(dest_stack, last_n, 0);
630
631 drag_active = 0;
632
633 check_for_end_of_game();
634 }
635
636 static void
637 update_status_text(int redraw)
638 {
639 char buffer[10] = "";
640
641 if (vegas)
642 {
643 sprintf (buffer, "$%d", winnings);
644 text (buffer, M, table_height - M);
645 if (redraw)
646 invalidate (M, table_height - M - font_height,
647 10 * font_width, font_height);
648 }
649 }
650
651 static FunctionMapping fmap[] = {
652 { "click", (void *)click },
653 { "double_click", (void *)double_click },
654 { "drag", (void *)drag },
655 { "drop", (void *)drop },
656 { "init", (void *)init },
657 { "key", (void *)key },
658 { "redraw", (void *)redraw },
659 { "resize", (void *)resize },
660 { 0, 0 }
661 };
662
663 int
664 main(int argc, char **argv)
665 {
666 register_imagelib(appimglib_imagelib);
667 init_ace(argc, argv, fmap);
668
669 use_auto_moves = !no_auto;
670 if (vegas)
671 flip_3s = 1;
672
673 if (table_width == 0 || table_height == 0)
674 {
675 table_width = W*7+M*8;
676 table_height = H+3*M+19*F+font_height;
677 }
678 init_table(table_width, table_height);
679 table_loop();
680 }