Debian Codegraph @

games/thornq.c

raw symbols SCIP
1 /* The Ace of Penguins - thornq.c
2 Copyright (C) 1998, 2001 Martin Thornquist
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "imagelib.h"
22 #include "table.h"
23 #include "cards.h"
24
25 #define W CARD_WIDTH
26 #define H CARD_HEIGHT
27 #define M CARD_MARGIN
28 #define F CARD_FAN_DOWN
29
30 #define WIN_H H + M + 23*F
31 #define WIN_W 8*W + 9*M
32
33 static Picture *xlogo, *splash, *youwin, *youlose, *arrow, *no_arrow;
34 static Stack *deck, *outcells[4], *maincells[8];
35 static int hint_mode = 0;
36 static char values[] = " A23456789TJQK";
37 static char suits[] = "HDCS";
38
39 static int auto_move();
40 static void check_for_end_of_game();
41 static void set_arrows();
42
43 static int ax, adx, ay, ady;
44
45 static void
46 resize(int w, int h)
47 {
48 int margin, offset, cw, ch, s, fd;
49 Picture *empty;
50
51 stack_set_card_size (w/9, w/9*4/3);
52 stack_get_card_size (&cw, &ch);
53
54 empty = (Picture *)get_image("empty", cw, ch, 0);
55
56 margin = (w - 8*cw) / 9;
57 offset = (w - margin*9 - cw*8) / 2 + margin;
58
59 for (s=0; s<4; s++)
60 {
61 stack_move(outcells[s], w - (4-s)*cw, 0);
62 stack_set_empty_picture(outcells[s], empty);
63 }
64 for (s=0; s<8; s++)
65 stack_move(maincells[s], offset + s*(cw+margin), ch + (offset<0?0:offset));
66
67 stack_get_fans(&fd, NULL, NULL, NULL);
68
69 ax = offset + cw/2;
70 adx = margin + cw;
71 ay = offset + ch*2 + 2;
72 ady = fd;
73
74 set_arrows();
75 }
76
77 static int supress_arrows = 0;
78 static int arrowsx[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
79 static int arrowsy[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
80
81 static void
82 set_arrow(int column, int on)
83 {
84 int x = ax + adx*column;
85 if (supress_arrows)
86 return;
87 if (arrowsy[column])
88 put_picture(no_arrow, arrowsx[column]-no_arrow->w/2, arrowsy[column],
89 0, 0, no_arrow->w, no_arrow->h);
90 if (on)
91 {
92 arrowsx[column] = x;
93 arrowsy[column] = ay+ady*(stack_count_cards(maincells[column])-1);
94 put_picture(arrow, arrowsx[column]-arrow->w/2, arrowsy[column],
95 0, 0, arrow->w, arrow->h);
96 }
97 else
98 arrowsy[column] = 0;
99 }
100
101 static void
102 redraw_arrows()
103 {
104 int i;
105 for (i=0; i<8; i++)
106 {
107 if (arrowsy[i])
108 put_picture(arrow, arrowsx[i]-arrow->w/2, arrowsy[i],
109 0, 0, arrow->w, arrow->h);
110 }
111 }
112
113 static void
114 clear_arrows()
115 {
116 int i;
117 for (i=0; i<8; i++)
118 set_arrow(i, 0);
119 }
120
121 static void
122 set_arrows()
123 {
124 int i, j, k;
125 for (i=0; i<8; i++)
126 {
127 Stack *s = maincells[i];
128 int top_stack = stack_get_card(s, stack_count_cards(s)-1);
129 set_arrow(i, 0);
130 for (j=0; j<8; j++)
131 if (j != i)
132 for (k=stack_count_cards(maincells[j])-1; k>=0; k--)
133 {
134 int match = stack_get_card(maincells[j], k);
135 if (SUIT(match) == SUIT(top_stack)
136 && VALUE(match) == VALUE(top_stack)-1
137 && ! FACEDOWNP(match))
138 set_arrow(i,1);
139 }
140 }
141 }
142
143 static void
144 start_again()
145 {
146 int i, j;
147
148 clear_arrows();
149
150 for (i = 0; i < 4; i++)
151 stack_flip_stack(outcells[i], deck, 0);
152
153 for (i = 0; i < 8; i++)
154 stack_flip_move_stack(maincells[i], deck, 0);
155
156 stack_shuffle(deck);
157 stack_shuffle(deck);
158 stack_shuffle(deck);
159
160 for (i = 0; i < 4; i++) {
161 for (j = 0; j < 4; j++)
162 stack_move_card(deck, maincells[i], 0);
163 for (j = 0; j < 3; j++)
164 stack_flip_card(deck, maincells[i], 0);
165 }
166
167 for (i = 4; i < 8; i++)
168 for (j = 0; j < 6; j++)
169 stack_flip_card(deck, maincells[i], 0);
170
171 stack_undo_reset();
172 set_arrows();
173 }
174
175
176 static void
177 init()
178 {
179 int s, v;
180 Picture *empty;
181
182 stack_load_standard_deck();
183 empty = get_picture("empty");
184
185 set_centered_pic(splash);
186
187 for (s = 0; s < 4; s++) {
188 outcells[s] = stack_create(4*W + 9*M + s*W, 0);
189 stack_set_empty_picture(outcells[s], empty);
190 }
191
192 for (s = 0; s < 8; s++) {
193 maincells[s] = stack_create(M+s*(M+W), H+M);
194 stack_set_offset(maincells[s], STACK_OFFSET_DOWN);
195 }
196
197 deck = stack_create(10*W, 0);
198
199 for (s = 0; s < 4; s++)
200 for (v = ACE; v <= KING; v++)
201 stack_add_card(deck, MAKE_CARD(s, v, FACEDOWN));
202
203 start_again();
204 }
205
206
207 static void
208 redraw()
209 {
210 int cw, ch;
211 stack_get_card_size (&cw, &ch);
212 if (xlogo->w < table_width - 8*cw && xlogo->h < ch)
213 put_picture(xlogo, table_width/2-xlogo->w/2, ch/2-xlogo->h/2,
214 0, 0, xlogo->h, xlogo->w);
215 stack_redraw();
216 redraw_arrows();
217 }
218
219
220 extern char thornq_help[];
221
222 static void
223 key(int k, int x, int y)
224 {
225
226 Picture *cp = get_centered_pic();
227
228 switch (k) {
229 case 3: case 27: case 'q':
230 exit(0);
231
232 case KEY_F(1): case 'h':
233 set_centered_pic(0);
234 help("thornq.html", thornq_help);
235 return;
236
237 case KEY_F(2): case 'r':
238 set_centered_pic(0);
239 start_again();
240 while (auto_move());
241 return;
242
243 case 8: case 127: case KEY_DELETE:
244 set_centered_pic(0);
245 clear_arrows();
246 stack_undo();
247 set_arrows();
248 return;
249 }
250
251 if (cp == youlose || cp == youwin)
252 {
253 set_centered_pic(0);
254 start_again();
255 while (auto_move());
256 return;
257 }
258
259 switch (k) {
260 case 'a':
261 hint_mode = 1;
262 check_for_end_of_game();
263 hint_mode = 0;
264 break;
265 case 'A':
266 hint_mode = 1-hint_mode;
267 check_for_end_of_game();
268 break;
269
270 case ' ':
271 double_click(x, y, 1);
272 break;
273 }
274 }
275
276
277 static char *
278 card_string(int c)
279 {
280 static char names[5][5];
281 static int n=0;
282 static char suits[] = "HDCS";
283 static char face[] = "A234567890JQK";
284
285 n = (n+1)%5;
286 sprintf(names[n], "%c%c", face[VALUE(c)], suits[SUIT(c)]);
287
288 return names[n];
289 }
290
291
292 static Stack *src_stack = 0;
293 static int src_n = 0;
294
295 static Stack *dest_stack;
296 static int last_n = -1;
297
298 static int
299 n_droppable_s(Stack *dest_stack)
300 {
301 int src_top, dest_top, src_count, dest_count, i, j;
302
303 src_count = stack_count_cards(src_stack);
304 dest_count = stack_count_cards(dest_stack);
305 dest_top = stack_get_card(dest_stack, dest_count-1);
306 src_top = stack_get_card(src_stack, src_count-1);
307
308 if (dest_stack == src_stack)
309 return src_n;
310
311 #if 0
312 /* auto-move precludes the need for this */
313 for (i = 0; i < 4; i++)
314 if (dest_stack == outcells[i]) {
315 if (dest_count == 0 && VALUE(src_top) == ACE)
316 return src_count - 1;
317 if (dest_count > 0 && VALUE(src_top) == VALUE(dest_top) + 1
318 && SUIT(src_top) == SUIT(dest_top))
319 return src_count - 1;
320 return src_count;
321 }
322 #endif
323
324 for (i = 0; i < 8; i++)
325 if (dest_stack == maincells[i])
326 {
327 /* move entire stack to empty slot */
328 if (0 == src_n && 0 == dest_count)
329 return 0;
330
331 if (dest_count == 0)
332 {
333 /* look for kings */
334 for (j=src_n; j<src_count; j++)
335 {
336 src_top = stack_get_card(src_stack, j);
337 if (!FACEDOWNP(src_top) && VALUE(src_top) == KING)
338 return j;
339 }
340 for (j=src_n-1; j>=0; j--)
341 {
342 src_top = stack_get_card(src_stack, j);
343 if (!FACEDOWNP(src_top) && VALUE(src_top) == KING)
344 return j;
345 }
346 return 0;
347 }
348 else
349 {
350 /* Look for moveable cards */
351 for (j=0; j<src_count; j++)
352 {
353 src_top = stack_get_card(src_stack, j);
354 if (!FACEDOWNP(src_top)
355 && SUIT(src_top) == SUIT(dest_top)
356 && VALUE(src_top) == VALUE(dest_top)-1)
357 return j;
358 }
359 }
360
361 break;
362 }
363
364 return src_count;
365 }
366
367 static int
368 n_droppable(int x, int y)
369 {
370 if (!src_stack)
371 return 0;
372
373 if (!stack_drag_find(x, y, &dest_stack)) {
374 dest_stack = src_stack;
375
376 return last_n != -1 ? last_n : src_n;
377 }
378
379 return n_droppable_s(dest_stack);
380 }
381
382
383 static int
384 auto_move()
385 {
386 int i, j, c, f, m = 0;
387
388 for (i = 0; i < 8; ++i) {
389 if (stack_count_cards(maincells[i]) == 0)
390 continue;
391 c = stack_get_card(maincells[i], stack_count_cards(maincells[i])-1);
392
393 if (FACEDOWNP(c))
394 stack_flip_card(maincells[i], maincells[i], 1);
395 /*continue;*//* use this to turn over cards yourself */
396
397 if (ACE == VALUE(c)) {
398 for (j = 0; stack_count_cards(outcells[j]); ++j)
399 ;
400
401 stack_animate(maincells[i], outcells[j], 1);
402 m = 1;
403 continue;
404 }
405
406 for (j = 0; j < 4; ++j) {
407 f = stack_get_card(outcells[j], stack_count_cards(outcells[j])-1);
408
409 if (SUIT(f) == SUIT(c) &&
410 VALUE(f) == VALUE(c) - 1) {
411 stack_animate(maincells[i], outcells[j], 1);
412 m = 1;
413 break;
414 }
415 }
416 }
417
418 return m;
419 }
420
421
422 static void
423 check_for_end_of_game()
424 {
425 int i, j, k, available_moves=0;
426
427 set_arrows();
428
429 if (stack_count_cards(outcells[0]) == 13 &&
430 stack_count_cards(outcells[1]) == 13 &&
431 stack_count_cards(outcells[2]) == 13 &&
432 stack_count_cards(outcells[3]) == 13)
433 {
434 set_centered_pic(youwin);
435 return;
436 }
437
438 if (hint_mode)
439 printf("Available moves:\n");
440 for (i=0; i<8; i++)
441 {
442 Stack *d = maincells[i];
443 int dc = stack_count_cards(d);
444 int topcard = stack_get_card(d, dc-1);
445 for (j=0; j<8; j++)
446 if (j != i)
447 {
448 Stack *s = maincells[j];
449 for (k=stack_count_cards(s)-1; k>=0; k--)
450 {
451 int one = stack_get_card(s, k);
452 if (!FACEDOWNP(one))
453 {
454 if (dc == 0)
455 {
456 if (VALUE(one) == KING && k > 0)
457 {
458 if (!hint_mode)
459 return;
460 printf(" Move K%c from stack %d to stack %d\n",
461 suits[SUIT(one)], j+1, i+1);
462 available_moves++;
463 }
464 }
465 else
466 {
467 if (SUIT(one) == SUIT(topcard)
468 && VALUE(one) == VALUE(topcard)-1)
469 {
470 if (!hint_mode)
471 return;
472 printf(" move %c%c from stack %d onto stack %d\n",
473 values[VALUE(one)], suits[SUIT(one)],
474 j+1, i+1);
475 available_moves++;
476 }
477 }
478 }
479 }
480 }
481 }
482 if (!available_moves)
483 set_centered_pic(youlose);
484 }
485
486 static void
487 click(int x, int y, int b)
488 {
489 int c, f;
490
491 Picture *cp = get_centered_pic();
492
493 if (cp == youlose || cp == youwin)
494 {
495 set_centered_pic(0);
496 start_again();
497 while (auto_move());
498 return;
499 }
500
501 if (cp == splash) {
502 set_centered_pic(0);
503
504 while (auto_move())
505 ;
506
507 return;
508 }
509
510 src_stack = 0;
511 if (!(f = stack_find(x, y, &src_stack, &src_n)))
512 return;
513
514 if (FACEDOWNP(stack_get_card(src_stack, stack_count_cards(src_stack)-1))
515 == FACEDOWN) {
516 stack_flip_card(src_stack, src_stack, 0);
517 return;
518 }
519
520 if (b > 1) {
521 stack_peek_card(src_stack, src_n, 1);
522
523 return;
524 }
525
526 last_n = -1;
527
528 for (c=0; c<4; c++)
529 if (src_stack == outcells[c])
530 return;
531
532 if (f && stack_count_cards(src_stack) > 0)
533 {
534 clear_arrows();
535 stack_begin_drag(src_stack, src_n, x, y);
536 }
537 else
538 src_stack = 0;
539 }
540
541 static int
542 try_moving_to(int i)
543 {
544 int c = stack_get_card(src_stack, src_n);
545 for (i=0; i<8; i++)
546 if (maincells[i] != src_stack)
547 {
548 int j = stack_count_cards(maincells[i]) - 1;
549 int f = stack_get_card(maincells[i], j);
550 if (SUIT(f) == SUIT(c)
551 && VALUE(c) == VALUE(f) - 1
552 && ! FACEDOWNP(c)) {
553 clear_arrows();
554 stack_move_cards(src_stack, maincells[i], stack_count_cards(src_stack) - src_n, 0);
555 while (auto_move()) ;
556 set_arrows();
557 check_for_end_of_game();
558 return 1;
559 }
560 }
561 return 0;
562 }
563
564 static int
565 try_moving_from(int i)
566 {
567 int c = stack_get_card(src_stack, stack_count_cards(src_stack)-1);
568 for (i=0; i<8; i++)
569 if (maincells[i] != src_stack)
570 {
571 int j, n = stack_count_cards(maincells[i]);
572 for (j=0; j<n; j++)
573 {
574 int f = stack_get_card(maincells[i], j);
575 if (SUIT(f) == SUIT(c)
576 && VALUE(f) == VALUE(c) - 1
577 && ! FACEDOWNP(f)) {
578 clear_arrows();
579 if (j == n-1)
580 stack_animate(maincells[i], src_stack, 0);
581 else
582 stack_move_cards(maincells[i], src_stack, n - j, 0);
583 while (auto_move()) ;
584 set_arrows();
585 check_for_end_of_game();
586 return 1;
587 }
588 }
589 }
590 return 0;
591 }
592
593 static void
594 double_click(int x, int y, int b)
595 {
596 int c, f, i, j;
597
598 src_stack = 0;
599
600 if (!(stack_find(x, y, &src_stack, &src_n)))
601 return;
602
603 if (b > 1)
604 return;
605
606 /* verify that it's a main stack we double clicked on */
607 for (i=0; i<8; i++)
608 if (maincells[i] == src_stack)
609 break;
610 if (i == 8)
611 return;
612
613 if (stack_count_cards(src_stack) == 0)
614 {
615 /* find a king, and move it */
616 for (i=0; i<8; i++)
617 for (j=stack_count_cards(maincells[i])-1; j>0; j--)
618 {
619 c=stack_get_card(maincells[i], j);
620 if (!FACEDOWNP(c) && VALUE(c) == KING)
621 {
622 clear_arrows();
623 stack_move_cards(maincells[i], src_stack, stack_count_cards(maincells[i]) - j, 0);
624 while (auto_move()) ;
625 set_arrows();
626 check_for_end_of_game();
627 return;
628 }
629 }
630 }
631
632 /* clicked on top card or below it */
633 if (src_n >= stack_count_cards(src_stack) - 1 || src_n < 0)
634 if (try_moving_from(i))
635 return;
636
637 /* clicked on underlying card */
638 if (try_moving_to(i))
639 return;
640
641 /* this didn't make sense to me - DJ */
642 #if 0
643 /* oh well, try top card anyway */
644 try_moving_from(i);
645 #endif
646 }
647
648 static void
649 drag(int x, int y, int b)
650 {
651 if (b > 1)
652 return;
653
654 last_n = n_droppable(x, y);
655
656 stack_continue_drag(last_n, x, y);
657 }
658
659 static void
660 drop(int x, int y, int b)
661 {
662 last_n = n_droppable(x, y); /* also sets dest_stack */
663
664 if (b > 1) {
665 if (src_stack)
666 stack_peek_card(src_stack, src_n, 0);
667
668 return;
669 }
670
671 clear_arrows();
672 stack_drop(dest_stack, last_n, 0);
673
674 while (auto_move())
675 ;
676
677 set_arrows();
678 check_for_end_of_game();
679 }
680
681 static FunctionMapping fmap[] = {
682 { "click", (void *)click },
683 { "double_click", (void *)double_click },
684 { "drag", (void *)drag },
685 { "drop", (void *)drop },
686 { "init", (void *)init },
687 { "key", (void *)key },
688 { "redraw", (void *)redraw },
689 { "resize", (void *)resize },
690 { 0, 0 }
691 };
692
693 int
694 main(int argc, char **argv)
695 {
696 register_imagelib(appimglib_imagelib);
697 init_ace(argc, argv, fmap);
698
699 xlogo = get_picture("xemboss");
700 splash = get_picture("thornq");
701 youwin = get_picture("youwin");
702 youlose = get_picture("youlose");
703 arrow = get_picture("thornq-arrow");
704 no_arrow = get_picture("thornq-noarrow");
705
706 if (table_width == 0 || table_height == 0)
707 {
708 table_width = WIN_W;
709 table_height = WIN_H;
710 }
711 init_table(table_width, table_height);
712 table_loop();
713
714 return 0;
715 }