Debian Codegraph @

games/pegged.c

raw symbols SCIP
1 /* The Ace of Penguins - pegged.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 <math.h>
23
24 #include <X11/keysym.h>
25
26 #include "table.h"
27 #include "imagelib.h"
28 #include "cards.h"
29
30 static Picture *xlogo, *splash, *youwin, *youlose;
31 static Picture *hole, *peg;
32 #define RADIUS (peg->w/2)
33
34 #define RULES_VH 1
35 #define RULES_TRI 2
36
37 static char *layout_cross[] = {
38 " . . . ",
39 " . * . ",
40 ". . * * * . .",
41 ". . . * . . .",
42 ". . . * . . .",
43 " . . . ",
44 " . . . ",
45 };
46
47 static char *layout_plus[] = {
48 " . . . ",
49 " . * . ",
50 ". . . * . . .",
51 ". * * * * * .",
52 ". . . * . . .",
53 " . * . ",
54 " . . . ",
55 };
56
57 static char *layout_fireplace[] = {
58 " * * * ",
59 " * * * ",
60 ". . * * * . .",
61 ". . * . * . .",
62 ". . . . . . .",
63 " . . . ",
64 " . . . ",
65 };
66
67 static char *layout_uparrow[] = {
68 " . * . ",
69 " * * * ",
70 ". * * * * * .",
71 ". . . * . . .",
72 ". . . * . . .",
73 " * * * ",
74 " * * * ",
75 };
76
77 static char *layout_pyramid[] = {
78 " . . . ",
79 " . * . ",
80 ". . * * * . .",
81 ". * * * * * .",
82 "* * * * * * *",
83 " . . . ",
84 " . . . ",
85 };
86
87 static char *layout_diamond[] = {
88 " . * . ",
89 " * * * ",
90 ". * * * * * .",
91 "* * * . * * *",
92 ". * * * * * .",
93 " * * * ",
94 " . * . ",
95 };
96
97 static char *layout_solitaire[] = {
98 " * * * ",
99 " * * * ",
100 "* * * * * * *",
101 "* * * . * * *",
102 "* * * * * * *",
103 " * * * ",
104 " * * * ",
105 };
106
107 static char *layout_triangle5[] = {
108 "* ",
109 "* * ",
110 "* * * ",
111 "* * * * ",
112 "* * * * *",
113 };
114
115 static char *layout_triangle7[] = {
116 "* ",
117 "* * ",
118 "* * * ",
119 "* * * * ",
120 "* * * * * ",
121 "* * * * * * ",
122 "* * * * * * *",
123 };
124
125 static struct {
126 int key, width, height, rules;
127 char **layout;
128 } layouts[] = {
129 '1', 7, 7, RULES_VH, layout_cross,
130 '2', 7, 7, RULES_VH, layout_plus,
131 '3', 7, 7, RULES_VH, layout_fireplace,
132 '4', 7, 7, RULES_VH, layout_uparrow,
133 '5', 7, 7, RULES_VH, layout_pyramid,
134 '6', 7, 7, RULES_VH, layout_diamond,
135 '7', 7, 7, RULES_VH, layout_solitaire,
136 '8', 5, 5, RULES_TRI, layout_triangle5,
137 '9', 7, 7, RULES_TRI, layout_triangle7,
138 0, 0
139 };
140 #define INITIAL_LAYOUT 6
141
142 static int layout_number = 0;
143
144 #define MAXX 7
145 #define MAXY 7
146
147 #define GRID_HOLE 1
148 #define GRID_PEG 2
149
150 #define GRID_XX_TRI 40
151 #define GRID_YX_TRI 20
152 #define GRID_YY_TRI 34
153
154 static char grid[MAXX+1][MAXY+1];
155 static int gr, gw, gh;
156
157 static int gx[MAXX+1][MAXY+1];
158 static int gy[MAXX+1][MAXY+1];
159
160 static struct UndoInfo {
161 unsigned short initial_pick:1;
162 unsigned short from_x:3, from_y:3;
163 unsigned short to_x:3, to_y:3;
164 } undo_list[49];
165 static int next_undo = 0;
166
167 static void
168 start_again()
169 {
170 int x, y, cx, cy;
171 gr = layouts[layout_number].rules;
172 gw = layouts[layout_number].width;
173 gh = layouts[layout_number].height;
174
175 switch (gr)
176 {
177 case RULES_VH:
178 cx = table_width/2 - (gw-1)*20;
179 cy = table_height/2 - (gh-1)*20;
180 break;
181 case RULES_TRI:
182 cx = table_width/2 - (gw-1)*20;
183 cy = table_height/2 - (gh-1)*GRID_YY_TRI/2;
184 break;
185 default:
186 printf("no rules!\n");
187 exit(1);
188 }
189 for (x=0; x<gw; x++)
190 for (y=0; y<gh; y++)
191 {
192 switch (layouts[layout_number].layout[y][x*2])
193 {
194 case ' ':
195 grid[x][y] = 0;
196 break;
197 case '.':
198 grid[x][y] = GRID_HOLE;
199 break;
200 case '*':
201 grid[x][y] = GRID_PEG;
202 break;
203 }
204 switch (gr)
205 {
206 case RULES_VH:
207 gx[x][y] = cx+x*40;
208 gy[x][y] = cy+y*40;
209 break;
210 case RULES_TRI:
211 gx[x][y] = cx + x*GRID_XX_TRI + (gh-y-1)*GRID_YX_TRI;
212 gy[x][y] = cy + y*GRID_YY_TRI;
213 }
214 }
215 invalidate(0, 0, table_width, table_height);
216 next_undo = 0;
217 }
218
219 static void
220 init()
221 {
222 layout_number = INITIAL_LAYOUT;
223 set_centered_pic(splash);
224 start_again();
225 }
226
227 static void
228 xyput(int x, int y, Picture *p)
229 {
230 int px, py;
231 px = gx[x][y] - p->w/2;
232 py = gy[x][y] - p->h/2;
233 put_picture(p, px, py, 0, 0, p->w, p->h);
234 }
235
236 static void
237 redraw()
238 {
239 int x, y;
240 put_picture(xlogo, table_width-xlogo->w-4, 4, 0, 0, xlogo->w, xlogo->h);
241 for (x=0; x<gw; x++)
242 for (y=0; y<gh; y++)
243 switch (grid[x][y])
244 {
245 case GRID_HOLE:
246 xyput(x, y, hole);
247 break;
248 case GRID_PEG:
249 xyput(x, y, peg);
250 break;
251 }
252 }
253
254 static void
255 set_grid(int x, int y, int which)
256 {
257 if (grid[x][y] == which)
258 return;
259 grid[x][y] = which;
260 switch (which)
261 {
262 case GRID_HOLE:
263 /* the peg is bigger */
264 clear(gx[x][y]-RADIUS, gy[x][y]-RADIUS, RADIUS*2, RADIUS*2);
265 xyput(x, y, hole);
266 break;
267 case GRID_PEG:
268 xyput(x, y, peg);
269 break;
270 }
271 }
272
273 static int
274 find_hole(int x, int y, int *rx, int *ry)
275 {
276 int ix, iy;
277 for (ix=0; ix<gw; ix++)
278 for (iy=0; iy<gh; iy++)
279 if (x > gx[ix][iy]-RADIUS-5 && x < gx[ix][iy]+RADIUS+5
280 && y > gy[ix][iy]-RADIUS-5 && y < gy[ix][iy]+RADIUS+5)
281 {
282 *rx = ix;
283 *ry = iy;
284 return 1;
285 }
286 return 0;
287 }
288
289 static int holes_left, pegs_left;
290
291 static void
292 count_stuff()
293 {
294 int x, y;
295 holes_left=0;
296 pegs_left=0;
297 for (x=0; x<gw; x++)
298 for (y=0; y<gh; y++)
299 switch (grid[x][y])
300 {
301 case GRID_HOLE:
302 holes_left++;
303 break;
304 case GRID_PEG:
305 pegs_left++;
306 break;
307 }
308 }
309
310 static int
311 see_if_lose()
312 {
313 int x, y;
314 for (x=0; x<gw; x++)
315 for (y=0; y<gh; y++)
316 if (grid[x][y] == GRID_PEG)
317 {
318 if (x > 1 && grid[x-1][y] == GRID_PEG && grid[x-2][y] == GRID_HOLE)
319 return 0;
320 if (x < gw-2 && grid[x+1][y] == GRID_PEG && grid[x+2][y] == GRID_HOLE)
321 return 0;
322 if (y > 1 && grid[x][y-1] == GRID_PEG && grid[x][y-2] == GRID_HOLE)
323 return 0;
324 if (y < gh-2 && grid[x][y+1] == GRID_PEG && grid[x][y+2] == GRID_HOLE)
325 return 0;
326 if (gw == RULES_TRI)
327 {
328 if (x > 1 && y > 1 && grid[x-1][y-1] == GRID_PEG
329 && grid[x-2][y-2] == GRID_HOLE)
330 return 0;
331 if (x < gw-2 && y < gh-2 && grid[x+1][y+1] == GRID_PEG
332 && grid[x+2][y+2] == GRID_HOLE)
333 return 0;
334 }
335 }
336
337 set_centered_pic(youlose);
338 return 1;
339 }
340
341 static int click_x, click_y, ofs_x, ofs_y;
342 static int drag_x, drag_y, dragging;
343
344 static void
345 click(int mx, int my, int b)
346 {
347 int x, y;
348
349 Picture *cp = get_centered_pic();
350 set_centered_pic(0);
351 if (cp == youwin || cp == youlose)
352 start_again();
353
354 dragging = 0;
355 if (!find_hole(mx, my, &x, &y))
356 return;
357
358 count_stuff();
359
360 if (holes_left == 0 && grid[x][y] == GRID_PEG)
361 {
362 set_grid(x, y, GRID_HOLE);
363 undo_list[next_undo].initial_pick = 1;
364 undo_list[next_undo].from_x = x;
365 undo_list[next_undo].from_y = y;
366 next_undo++;
367 return;
368 }
369
370 if (grid[x][y] != GRID_PEG)
371 return;
372
373 grid[x][y] = GRID_HOLE;
374 click_x = x;
375 click_y = y;
376 ofs_x = mx - gx[x][y];
377 ofs_y = my - gy[x][y];
378 drag_x = gx[x][y];
379 drag_y = gy[x][y];
380 dragging = 1;
381 }
382
383 static void
384 drag(int x, int y, int b)
385 {
386 if (!dragging)
387 return;
388 invalidate(drag_x-RADIUS, drag_y-RADIUS, RADIUS*2, RADIUS*2);
389 drag_x = x - ofs_x;
390 drag_y = y - ofs_y;
391 put_picture(peg, drag_x-peg->w/2, drag_y-peg->h/2, 0, 0, peg->w, peg->h);
392 }
393
394 static void
395 drop(int x, int y, int b)
396 {
397 if (!dragging)
398 return;
399 invalidate(drag_x-RADIUS, drag_y-RADIUS, RADIUS*2, RADIUS*2);
400 dragging = 0;
401 if (find_hole(x-ofs_x, y-ofs_y, &x, &y))
402 {
403 if (grid[x][y] == GRID_HOLE)
404 {
405 int jump_ok = 0;
406 if ((x == click_x+2 && y == click_y)
407 || (x == click_x-2 && y == click_y)
408 || (x == click_x && y == click_y+2)
409 || (x == click_x && y == click_y-2))
410 jump_ok = 1;
411 switch (gr)
412 {
413 case RULES_TRI:
414 if ((x == click_x+2 && y == click_y+2)
415 || (x == click_x-2 && y == click_y-2))
416 jump_ok = 1;
417 break;
418 }
419 if (jump_ok)
420 {
421 int jx, jy;
422 jx = (x+click_x)/2;
423 jy = (y+click_y)/2;
424 if (grid[jx][jy] == GRID_PEG)
425 {
426 set_grid(jx, jy, GRID_HOLE);
427 set_grid(x, y, GRID_PEG);
428 undo_list[next_undo].initial_pick = 0;
429 undo_list[next_undo].from_x = click_x;
430 undo_list[next_undo].from_y = click_y;
431 undo_list[next_undo].to_x = x;
432 undo_list[next_undo].to_y = y;
433 next_undo++;
434
435 count_stuff();
436
437 if (pegs_left == 1)
438 set_centered_pic(youwin);
439 else
440 see_if_lose();
441
442 return;
443 }
444 }
445 }
446 }
447 set_grid(click_x, click_y, GRID_PEG);
448 }
449
450 extern char pegged_help[];
451
452 static void
453 key(int k, int x, int y)
454 {
455 int i;
456 if (k == 3 || k == 27 || k == 'q')
457 exit(0);
458 set_centered_pic(0);
459 if (k == KEY_F(2) || k == 'r')
460 {
461 start_again();
462 return;
463 }
464 if (k == KEY_F(1) || k == 'h')
465 {
466 set_centered_pic(0);
467 help("pegged.html", pegged_help);
468 return;
469 }
470 if (k == 8 || k == 127 || k == KEY_DELETE)
471 {
472 if (next_undo > 0)
473 {
474 struct UndoInfo *u;
475 next_undo--;
476 u = undo_list+next_undo;
477 if (! u->initial_pick)
478 {
479 set_grid(u->to_x, u->to_y, GRID_HOLE);
480 set_grid((u->from_x+u->to_x)/2, (u->from_y+u->to_y)/2, GRID_PEG);
481 }
482 set_centered_pic(0);
483 set_grid(u->from_x, u->from_y, GRID_PEG);
484 }
485 return;
486 }
487 for (i=0; layouts[i].key; i++)
488 if (layouts[i].key == k)
489 {
490 layout_number = i;
491 start_again();
492 return;
493 }
494 }
495
496 static FunctionMapping fmap[] = {
497 { "click", (void *)click },
498 { "drag", (void *)drag },
499 { "drop", (void *)drop },
500 { "init", (void *)init },
501 { "key", (void *)key },
502 { "redraw", (void *)redraw },
503 { 0, 0 }
504 };
505
506 int
507 main(int argc, char **argv)
508 {
509 register_imagelib(appimglib_imagelib);
510 init_ace(argc, argv, fmap);
511 hole = get_picture("pegged-h");
512 peg = get_picture("pegged-p");
513 splash = get_picture("pegged");
514 youwin = get_picture("youwin");
515 youlose = get_picture("youlose");
516 xlogo = get_picture("xemboss");
517
518 init_table(320, 320);
519 table_loop();
520 }