Debian Codegraph @

games/canfield.c

raw symbols SCIP
1 /* The Ace of Penguins - canfield.c
2 Copyright (C) 1999, 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
22 #include "table.h"
23 #include "imagelib.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 #define WIN_H H + M + 23*F
32 #define WIN_W 6*W + 6*M
33
34 /* number of cards in stock -- higher number gives more difficult game */
35 #define CARDS_IN_STOCK 10
36
37 static Picture *xlogo, *splash, *youwin, *youlose;
38 static Stack *hand, *talon, *stock, *tableau[4], *foundation[4];
39
40 static int base_rank;
41 static char base_mesg[14];
42
43 static int auto_move();
44
45 static void debug(char *s)
46 {
47 #ifdef DEBUG
48 printf("%s", s);
49 #endif
50 }
51
52
53 static void start_again()
54 {
55 int i, j;
56
57 for (i = 0; i < 4; i++)
58 stack_flip_stack(tableau[i], hand, 0);
59
60 for (i = 0; i < 4; i++)
61 stack_flip_stack(foundation[i], hand, 0);
62
63 stack_flip_stack(stock, hand, 0);
64
65 stack_shuffle(hand);
66 stack_shuffle(hand);
67 stack_shuffle(hand);
68
69 for (i = 0; i < CARDS_IN_STOCK; i++)
70 stack_flip_card(hand, stock, 0);
71
72 for (i = 0; i < 4; i++)
73 stack_flip_card(hand, tableau[i], 0);
74
75 stack_flip_card(hand, foundation[0], 0);
76
77 text(" ", 90, font_height+5);
78
79 base_rank = VALUE(stack_get_card(foundation[0], 0));
80 sprintf(base_mesg, "Base rank: %d", base_rank);
81 printf("%s\n", base_mesg);
82 redraw();
83
84 stack_undo_reset();
85 }
86
87
88 static void init()
89 {
90 int s, v;
91 Picture *empty;
92
93 stack_load_standard_deck();
94 empty = get_picture("empty");
95
96 set_centered_pic(splash);
97
98 for (s = 0; s < 4; s++) {
99 foundation[s] = stack_create(2*W + 5*M + s*W, M);
100 stack_set_empty_picture(foundation[s], empty);
101 }
102
103 for (s = 0; s < 4; s++) {
104 tableau[s] = stack_create(2*W+2*M+s*(M+W), H+2*M);
105 stack_set_offset(tableau[s], STACK_OFFSET_DOWN);
106 }
107
108 stock = stack_create(M, M);
109 hand = stack_create(M, 2*H+3*M);
110 talon = stack_create(M, H+2*M);
111 stack_set_empty_picture(talon, empty);
112
113 for (s = 0; s < 4; s++)
114 for (v = ACE; v <= KING; v++)
115 stack_add_card(hand, MAKE_CARD(s, v, FACEDOWN));
116
117 start_again();
118 }
119
120
121 static void redraw()
122 {
123 /* draw base_rank on background */
124
125 put_picture(xlogo, M+W/2-xlogo->w/2, M+H/2-xlogo->h/2,
126 0, 0, xlogo->h, xlogo->w);
127 put_picture(xlogo, M+W/2-xlogo->w/2, 3*M+2*H+H/2-xlogo->h/2,
128 0, 0, xlogo->h, xlogo->w);
129 text(base_mesg, 90, font_height+5);
130
131 stack_redraw();
132 }
133
134
135 extern char unnamed_help[];
136
137 static void key(int k, int x, int y)
138 {
139 switch (k) {
140 case 3: case 27: case 'q':
141 exit(0);
142
143 case KEY_F(1): case 'h':
144 set_centered_pic(0);
145 /* help("canfield.html", canfield_help);*/
146 return;
147
148 case KEY_F(2): case 'r':
149 set_centered_pic(0);
150 start_again();
151
152 while (auto_move())
153 ;
154
155 break;
156
157 case 8: case 127: case KEY_DELETE:
158 set_centered_pic(0);
159 stack_undo();
160 }
161 }
162
163
164 static char *card_string(int c)
165 {
166 static char names[5][5];
167 static int n=0;
168 static char suits[] = "HDCS";
169 static char face[] = "A234567890JQK";
170
171 n = (n+1)%5;
172 sprintf(names[n], "%c%c", face[VALUE(c)], suits[SUIT(c)]);
173
174 return names[n];
175 }
176
177
178 static Stack *src_stack = 0;
179 static int src_n = 0;
180
181 static Stack *dest_stack;
182 static int last_n = -1;
183
184 static int n_droppable_s(Stack *dest_stack)
185 {
186 int src_top, dest_top, src_count, dest_count, i;
187
188 src_count = stack_count_cards(src_stack);
189 dest_count = stack_count_cards(dest_stack);
190 dest_top = stack_get_card(dest_stack, dest_count-1);
191 src_top = stack_get_card(src_stack, src_count-1);
192
193 if (dest_stack == src_stack)
194 return src_n;
195
196 for (i = 0; i < 4; i++)
197 if (dest_stack == foundation[i]) {
198 if (dest_count == 0 && VALUE(src_top) == base_rank)
199 return src_count - 1;
200 if (dest_count > 0 && SUIT(src_top) == SUIT(dest_top)
201 && ((VALUE(src_top) == VALUE(dest_top) + 1) ||
202 (VALUE(src_top) == KING && VALUE(dest_top) == ACE)))
203 return src_count - 1;
204 return src_count;
205 }
206
207 for (i = 0; i < 4; i++)
208 if (dest_stack == tableau[i]) {
209 if (0 == src_n && 0 == dest_count)
210 return 0;
211
212 src_top = stack_get_card(src_stack, src_n);
213
214 if (FACEDOWN == FACEDOWNP(src_top))
215 return src_count;
216
217 if (0 == dest_count) {
218 if (stock == src_stack)
219 return src_n;
220
221 return src_count;
222 }
223
224 if (COLOR(src_top) != COLOR(dest_top)
225 && (VALUE(src_top) == VALUE(dest_top)-1 ||
226 (VALUE(src_top) == KING && VALUE(dest_top) == ACE)))
227 return src_n;
228
229 break;
230 }
231
232 return src_count;
233 }
234
235 static int n_droppable(int x, int y)
236 {
237 if (!src_stack)
238 return 0;
239
240 if (!stack_drag_find(x, y, &dest_stack)) {
241 dest_stack = src_stack;
242
243 return last_n != -1 ? last_n : src_n;
244 }
245
246 return n_droppable_s(dest_stack);
247 }
248
249 static void hand_to_talon(int flag) {
250 stack_flip_card(hand, talon, flag);
251 stack_flip_card(hand, talon, 1);
252 stack_flip_card(hand, talon, 1);
253 }
254
255 static int auto_move()
256 {
257 int i, j, c, f, m = 0;
258
259 for (i = 0; i < 4; ++i)
260 if (!stack_count_cards(tableau[i]))
261 if (stack_count_cards(stock))
262 stack_animate(stock, tableau[i], 1);
263 else if (stack_count_cards(talon))
264 stack_animate(talon, tableau[i], 1);
265
266 if (!stack_count_cards(talon)) {
267 if (stack_count_cards(hand)) {
268 hand_to_talon(1);
269 m = 1;
270 }
271 } else {
272 c = stack_get_card(talon, stack_count_cards(talon) - 1);
273
274 if (base_rank == (VALUE(c))) {
275 for (j = 0; stack_count_cards(foundation[j]); ++j)
276 ;
277
278 stack_animate(talon, foundation[j], 1);
279 m = 1;
280 }
281
282 for (i = 0; i < 4; ++i) {
283 f = stack_get_card(foundation[i],
284 stack_count_cards(foundation[i])-1);
285
286 if (SUIT(f) == SUIT(c) &&
287 (VALUE(f) == VALUE(c) - 1 ||
288 (VALUE(f) == KING && VALUE(c) == ACE))) {
289 stack_animate(talon, foundation[i], 1);
290 m = 1;
291 break;
292 }
293 }
294 }
295
296 if (stack_count_cards(stock)) {
297 c = stack_get_card(stock, stack_count_cards(stock)-1);
298
299 if (base_rank == VALUE(c)) {
300 for (j = 0; stack_count_cards(foundation[j]); ++j)
301 ;
302
303 stack_animate(stock, foundation[j], 1);
304 m = 1;
305 }
306
307 for (i = 0; i < 4; ++i) {
308 f = stack_get_card(foundation[i], stack_count_cards(foundation[i])-1);
309
310 if (SUIT(f) == SUIT(c) &&
311 (VALUE(f) == VALUE(c) - 1 ||
312 (VALUE(f) == KING && VALUE(c) == ACE))) {
313 stack_animate(stock, foundation[i], 1);
314 m = 1;
315 break;
316 }
317 }
318 }
319
320 for (i = 0; i < 4; ++i) {
321 c = stack_get_card(tableau[i], stack_count_cards(tableau[i])-1);
322
323 if (base_rank == VALUE(c)) {
324 for (j = 0; stack_count_cards(foundation[j]); ++j)
325 ;
326
327 stack_animate(tableau[i], foundation[j], 1);
328 m = 1;
329 continue;
330 }
331
332 for (j = 0; j < 4; ++j) {
333 f = stack_get_card(foundation[j], stack_count_cards(foundation[j])-1);
334
335 if (SUIT(f) == SUIT(c) &&
336 (VALUE(f) == VALUE(c) - 1 ||
337 (VALUE(f) == KING && VALUE(c) == ACE))) {
338 stack_animate(tableau[i], foundation[j], 1);
339 m = 1;
340 break;
341 }
342 }
343 }
344
345 return m;
346 }
347
348
349 static void check_for_end_of_game()
350 {
351 int i, j, k, c, f;
352
353 /* presumes that auto_move() is called */
354
355 if (stack_count_cards(foundation[0]) == 13 &&
356 stack_count_cards(foundation[1]) == 13 &&
357 stack_count_cards(foundation[2]) == 13 &&
358 stack_count_cards(foundation[3]) == 13) {
359 set_centered_pic(youwin);
360 return;
361 }
362
363 /* for (i = 0; i < 8; ++i) {
364 j = stack_count_cards(tableau[i]);
365
366 while (j-- > 0) {
367 f = stack_get_card(tableau[i], j);
368
369 printf("VALUE(f) = %d\n", VALUE(f));
370
371 if (FACEDOWN == FACEDOWNP(f))
372 break;
373 printf("hei\n");
374
375 for (k = 0; k < 8; ++k) {
376 if (k == i)
377 continue;
378
379 c = stack_get_card(tableau[k],
380 stack_count_cards(tableau[k]-1))-1;
381 printf("VALUE(c) = %d\n", VALUE(c));
382 if (SUIT(c) == SUIT(j) && VALUE(c) == VALUE(j) - 1)
383 return;
384 }
385 }
386 }
387
388 set_centered_pic(youlose);*/
389 }
390
391
392 static void click(int x, int y, int b)
393 {
394 int c, f;
395
396 Picture *cp = get_centered_pic();
397
398 if ((cp == youlose || cp == youwin)
399 && (x > table_width/2-cp->w/2
400 && x < table_width/2+cp->w/2
401 && y > table_height/2-cp->h/2
402 && y < table_height/2+cp->h/2)) {
403 set_centered_pic(0);
404 start_again();
405
406 while (auto_move())
407 ;
408
409 return;
410 }
411
412 if (cp == splash) {
413 set_centered_pic(0);
414
415 /* while (auto_move())
416 ;*/
417
418 return;
419 }
420
421 src_stack = 0;
422 if (!(f = stack_find(x, y, &src_stack, &src_n)))
423 return;
424
425 if (b > 1) {
426 stack_peek_card(src_stack, src_n, 1);
427
428 return;
429 }
430
431 last_n = -1;
432
433 for (c = 0; c < 4; c++)
434 if (src_stack == foundation[c])
435 return;
436
437 if (src_stack == hand) {
438 if (0 == stack_count_cards(hand)) {
439 stack_flip_stack(talon, hand, 0);
440
441 return;
442 }
443
444 hand_to_talon(0);
445
446 return;
447 }
448
449 if (f && stack_count_cards(src_stack) > 0)
450 stack_begin_drag(src_stack, src_n, x, y);
451 else
452 src_stack = 0;
453 }
454
455 static void double_click(int x, int y, int b)
456 {
457 int c, f, i;
458
459 src_stack = 0;
460
461 if (!(stack_find(x, y, &src_stack, &src_n)))
462 return;
463
464 if (src_stack == hand) {
465 click(x, y, b);
466 return;
467 }
468
469 if (b > 1)
470 return;
471
472 c = stack_get_card(src_stack, stack_count_cards(src_stack)-1);
473
474 if (VALUE(c) == base_rank) {
475 for (i = 0; i < 4; ++i)
476 if (stack_count_cards(foundation[i]) == 0)
477 break;
478
479 stack_animate(src_stack, foundation[i], 0);
480 return;
481 }
482
483 for (i = 0; i < 4; ++i) {
484 f = stack_get_card(foundation[i], stack_count_cards(foundation[i])-1);
485
486 if (SUIT(f) == SUIT(c) && VALUE(f) == VALUE(c) - 1) {
487 stack_animate(src_stack, foundation[i], 0);
488 return;
489 }
490 }
491
492 while (auto_move())
493 ;
494
495 }
496
497 static void drag(int x, int y, int b)
498 {
499 if (b > 1)
500 return;
501
502 last_n = n_droppable(x, y);
503
504 stack_continue_drag(last_n, x, y);
505 }
506
507 static void drop(int x, int y, int b)
508 {
509 last_n = n_droppable(x, y); /* also sets dest_stack */
510
511 if (b > 1) {
512 if (src_stack)
513 stack_peek_card(src_stack, src_n, 0);
514
515 return;
516 }
517
518 stack_drop(dest_stack, last_n, 0);
519
520 while (auto_move())
521 ;
522
523 check_for_end_of_game();
524 }
525
526 static FunctionMapping fmap[] = {
527 { "click", (void *)click },
528 { "double_click", (void *)double_click },
529 { "drag", (void *)drag },
530 { "drop", (void *)drop },
531 { "init", (void *)init },
532 { "key", (void *)key },
533 { "redraw", (void *)redraw },
534 { 0, 0 }
535 };
536
537 int main(int argc, char **argv)
538 {
539 register_imagelib(appimglib_imagelib);
540 init_ace(argc, argv, fmap);
541
542 xlogo = get_picture("xemboss");
543 splash = get_picture("canfield");
544 youwin = get_picture("youwin");
545 youlose = get_picture("youlose");
546
547 init_table(WIN_W, WIN_H);
548 table_loop();
549
550 return 0;
551 }