Debian Codegraph @

games/golf.c

raw symbols SCIP
1 /* The Ace of Penguins - golf.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 <X11/Xlib.h>
22
23 #include "table.h"
24 #include "imagelib.h"
25 #include "cards.h"
26
27 #define W CARD_WIDTH
28 #define H CARD_HEIGHT
29 #define M CARD_MARGIN
30 #define R CARD_FAN_TBRIGHT
31 #define D stack_fan_down
32
33 static int stack_fan_down;
34
35 static Picture *splash, *youwin, *youlose;
36 static Picture *arrow, *no_arrow;
37
38 static Stack *deck, *discard, *stacks[7];
39
40 static int supress_arrows = 0;
41
42 static int arrow_offset, arrow_delta;
43
44 static int arrows[7] = { 0, 0, 0, 0, 0, 0, 0 };
45
46 static void
47 set_arrow(int column, int on)
48 {
49 int x = column * arrow_delta + arrow_offset;
50 if (supress_arrows)
51 return;
52 if (arrows[column])
53 put_picture(no_arrow, x, arrows[column],
54 0, 0, no_arrow->w, no_arrow->h);
55 if (on)
56 {
57 arrows[column] = M+H+(stack_count_cards(stacks[column])-1)*D + 2;
58 put_picture(arrow, x, arrows[column],
59 0, 0, arrow->w, arrow->h);
60 }
61 else
62 arrows[column] = 0;
63 }
64
65 static void
66 redraw_arrows()
67 {
68 int i;
69 for (i=0; i<7; i++)
70 {
71 int x = i * arrow_delta + arrow_offset;
72 if (arrows[i])
73 put_picture(arrow, x, arrows[i],
74 0, 0, arrow->w, arrow->h);
75 }
76 }
77
78 static void
79 clear_arrows()
80 {
81 int i;
82 for (i=0; i<7; i++)
83 set_arrow(i, 0);
84 }
85
86 static void
87 set_arrows()
88 {
89 int i;
90 int top_discard = stack_get_card(discard, stack_count_cards(discard)-1);
91 for (i=0; i<7; i++)
92 {
93 Stack *s = stacks[i];
94 int top_stack = stack_get_card(s, stack_count_cards(s)-1);
95 if (VALUE(top_discard) != KING
96 && (VALUE(top_discard) == VALUE(top_stack)+1
97 || VALUE(top_discard) == VALUE(top_stack)-1))
98 set_arrow(i, 1);
99 else
100 set_arrow(i, 0);
101 }
102 }
103
104 static void
105 show_count()
106 {
107 char s[10];
108 int c = stack_count_cards(deck);
109 if (c > 0)
110 sprintf(s, "%2d", c);
111 else
112 strcpy(s, " ");
113 text(s, M*2, table_height-M-H-2);
114 }
115
116 static void
117 start_again()
118 {
119 int i, j;
120
121 clear_arrows();
122
123 stack_flip_stack(discard, deck, 0);
124 for (i=0; i<7; i++)
125 stack_flip_stack(stacks[i], deck, 0);
126
127 stack_shuffle(deck);
128 stack_shuffle(deck);
129 stack_shuffle(deck);
130
131 for (i=0; i<7; i++)
132 for (j=0; j<5; j++)
133 stack_flip_card(deck, stacks[i], 0);
134
135 stack_flip_card(deck, discard, 0);
136
137 stack_undo_reset();
138 show_count();
139 set_arrows();
140 }
141
142 static void
143 init()
144 {
145 int s, v;
146
147 arrow_offset = (table_width - 7 * W) / 8;
148 arrow_delta = arrow_offset + W;
149 arrow_offset = (table_width - 7*W - 6*arrow_offset)/2;
150
151 stack_load_standard_deck();
152 splash = get_picture("golf");
153 youwin = get_picture("youwin");
154 youlose = get_picture("youlose");
155 arrow = get_picture("golf-arrow");
156 no_arrow = get_picture("golf-noarrow");
157
158 set_centered_pic(splash);
159
160 for (s=0; s<7; s++)
161 {
162 stacks[s] = stack_create(arrow_offset+s*arrow_delta, M);
163 stack_set_offset(stacks[s], STACK_OFFSET_DOWN);
164 }
165
166 deck = stack_create(M, table_height-M-H);
167 discard = stack_create(M*2+W, table_height-M-H);
168 stack_set_offset(discard, STACK_OFFSET_TBRIGHT);
169
170 for (s=0; s<4; s++)
171 for (v=1; v<=13; v++)
172 stack_add_card(deck, MAKE_CARD(s, v, FACEDOWN));
173
174 arrow_offset = arrow_offset + (W - arrow->w)/2;
175
176 stack_get_fans(&stack_fan_down, NULL, NULL, NULL);
177
178 start_again();
179 }
180
181 static void
182 check_for_endgame()
183 {
184 int c, top_discard, top_stack;
185
186 if (stack_count_cards(discard) == 52)
187 {
188 set_centered_pic(youwin);
189 return;
190 }
191
192 if (stack_count_cards(deck) == 0)
193 {
194 top_discard = stack_get_card(discard, stack_count_cards(discard)-1);
195 if (VALUE(top_discard) != KING)
196 for (c=0; c<7; c++)
197 if (stack_count_cards(stacks[c]) > 0)
198 {
199 top_stack = stack_get_card(stacks[c], stack_count_cards(stacks[c])-1);
200 if (VALUE(top_discard) == VALUE(top_stack)+1
201 || VALUE(top_discard) == VALUE(top_stack)-1)
202 return;
203 }
204 set_centered_pic(youlose);
205 }
206 }
207
208 static void
209 redraw()
210 {
211 stack_redraw();
212 show_count();
213 redraw_arrows();
214 }
215
216 static void
217 click(int x, int y, int b)
218 {
219 int c, f, n;
220 Stack *s;
221 int top_discard, top_stack;
222 Picture *cp = get_centered_pic();
223
224 if ((cp == youlose || cp == youwin)
225 && (x > table_width/2-cp->w/2
226 && x < table_width/2+cp->w/2
227 && y > table_height/2-cp->h/2
228 && y < table_height/2+cp->h/2))
229 {
230 set_centered_pic(0);
231 start_again();
232 return;
233 }
234 if (cp == splash)
235 {
236 set_centered_pic(0);
237 return;
238 }
239
240 if (b > 1)
241 s = deck;
242 else
243 {
244 f = stack_find(x, y, &s, &n);
245 if (!f)
246 return;
247 }
248
249 if (s == deck)
250 {
251 stack_flip_card(deck, discard, 0);
252 show_count();
253 set_arrows();
254 check_for_endgame();
255 return;
256 }
257 else if (s == discard)
258 return;
259
260 if (stack_count_cards(s) == 0)
261 return;
262
263 top_discard = stack_get_card(discard, stack_count_cards(discard)-1);
264 top_stack = stack_get_card(s, stack_count_cards(s)-1);
265
266 if (VALUE(top_discard) == KING)
267 return;
268 if (VALUE(top_discard) != VALUE(top_stack)+1
269 && VALUE(top_discard) != VALUE(top_stack)-1)
270 return;
271
272 stack_move_card(s, discard, 0);
273 set_arrows();
274
275 check_for_endgame();
276 }
277
278 extern char golf_help[];
279
280 static void
281 key(int k, int x, int y)
282 {
283 if (k == 3 || k == 27 || k == 'q')
284 exit(0);
285 if (k == KEY_F(1) || k == 'h')
286 {
287 set_centered_pic(0);
288 help("golf.html", golf_help);
289 }
290 if (k == KEY_F(2) || k == 'r')
291 {
292 set_centered_pic(0);
293 start_again();
294 }
295 if (k == 8 || k == 127 || k == KEY_DELETE)
296 {
297 clear_arrows();
298 supress_arrows = 1;
299 set_centered_pic(0);
300 stack_undo();
301 show_count();
302 supress_arrows = 0;
303 set_arrows();
304 }
305 if (k == 'd')
306 {
307 int i;
308 for (i=0; i<7; i++)
309 printf("i=%d a=%d\n", i, arrows[i]);
310 }
311 }
312
313 static FunctionMapping fmap[] = {
314 { "click", (void *)click },
315 { "init", (void *)init },
316 { "key", (void *)key },
317 { "redraw", (void *)redraw },
318 { 0, 0 }
319 };
320
321 int
322 main(int argc, char **argv)
323 {
324 int alt_width = 3*M+2*W+51*R;
325 init_ace(argc, argv, fmap);
326 register_imagelib(appimglib_imagelib);
327 table_width = 8*M+7*W;
328 if (table_width < alt_width) table_width = alt_width;
329 table_height = 3*M+5*CARD_FAN_DOWN+2*H;
330 init_table(table_width, table_height);
331 table_loop();
332 }