Debian Codegraph @

games/taipei.c

raw symbols SCIP
1 /* The Ace of Penguins - taipei.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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "imagelib.h"
22 #include "table.h"
23 #include "cards.h"
24 #include "taipeilib.h"
25
26 static char *tile_names[] = {
27 0, 0, 0, 0,
28 "n1", "n1", "n1", "n1",
29 "n2", "n2", "n2", "n2",
30 "n3", "n3", "n3", "n3",
31 "n4", "n4", "n4", "n4",
32 "n5", "n5", "n5", "n5",
33 "n6", "n6", "n6", "n6",
34 "n7", "n7", "n7", "n7",
35 "n8", "n8", "n8", "n8",
36 "n9", "n9", "n9", "n9",
37
38 "p1", "p1", "p1", "p1",
39 "p2", "p2", "p2", "p2",
40 "p3", "p3", "p3", "p3",
41 "p4", "p4", "p4", "p4",
42 "p5", "p5", "p5", "p5",
43 "p6", "p6", "p6", "p6",
44 "p7", "p7", "p7", "p7",
45 "p8", "p8", "p8", "p8",
46 "p9", "p9", "p9", "p9",
47
48 "b1", "b1", "b1", "b1",
49 "b2", "b2", "b2", "b2",
50 "b3", "b3", "b3", "b3",
51 "b4", "b4", "b4", "b4",
52 "b5", "b5", "b5", "b5",
53 "b6", "b6", "b6", "b6",
54 "b7", "b7", "b7", "b7",
55 "b8", "b8", "b8", "b8",
56 "b9", "b9", "b9", "b9",
57
58 "xj", "xj", "xj", "xj",
59 "xa", "xa", "xa", "xa",
60 "xx", "xx", "xx", "xx",
61
62 "sh", "sh", "sh", "sh",
63 "sd", "sd", "sd", "sd",
64 "sc", "sc", "sc", "sc",
65 "ss", "ss", "ss", "ss",
66
67 "cr", "cg", "cb", "ck",
68 "du", "dd", "dl", "dr"
69 };
70
71 static signed char tp_standard[] = {
72 #include "taipei-standard.tp"
73 };
74
75 static signed char tp_bridge[] = {
76 #include "taipei-bridge.tp"
77 };
78
79 static signed char tp_castle[] = {
80 #include "taipei-castle.tp"
81 };
82
83 static signed char tp_cube[] = {
84 #include "taipei-cube.tp"
85 };
86
87 static signed char tp_glyph[] = {
88 #include "taipei-glyph.tp"
89 };
90
91 static signed char tp_pyramid[] = {
92 #include "taipei-pyramid.tp"
93 };
94
95 static signed char tp_spiral[] = {
96 #include "taipei-spiral.tp"
97 };
98
99 static signed char tp_simple[] = {
100 #include "taipei-simple.tp"
101 };
102
103 static signed char tp_smiley[] = {
104 #include "taipei-smiley.tp"
105 };
106
107 static signed char tp_jason[] = {
108 #include "taipei-jason.tp"
109 };
110
111 static signed char tp_rebecca[] = {
112 #include "taipei-rebecca.tp"
113 };
114
115 static signed char *layouts[] = {
116 tp_standard,
117 tp_bridge,
118 tp_castle,
119 tp_cube,
120 tp_glyph,
121 tp_pyramid,
122 tp_spiral,
123 tp_simple,
124 tp_smiley,
125 tp_jason,
126 tp_rebecca,
127 0
128 };
129
130 static int layout_number = 1;
131
132 #define NUM_TILES (sizeof(tile_names)/(sizeof(tile_names[0])))
133 #define NUM_TILE_SETS (NUM_TILES/4)
134
135 static Picture *tiles[NUM_TILES], *blank_tile;
136 static Picture *splash, *youwin, *youlose;
137
138 static unsigned char exposures[GRID_SX][GRID_SY][GRID_SZ];
139
140 static int selected_x=-1, selected_y=-1, selected_z=-1;
141 static int selected_tile = 0;
142
143 static int
144 calculate_exposures(int x, int y, int z)
145 {
146 int e = 0;
147 if (z == GRID_SZ-1)
148 return 0;
149 if (grid[x+1][y+1][z+1]) e++;
150 if (grid[x-1][y+1][z+1]) e++;
151 if (grid[x+1][y-1][z+1]) e++;
152 if (grid[x-1][y-1][z+1]) e++;
153 if (grid[x-1][y][z+1]) e+=2;
154 if (grid[x+1][y][z+1]) e+=2;
155 if (grid[x][y-1][z+1]) e+=2;
156 if (grid[x][y+1][z+1]) e+=2;
157 if (grid[x][y][z+1]) e+=4;
158 return e;
159 }
160
161 typedef struct XYZ {
162 unsigned char x, y, z, a;
163 } XYZ;
164
165 static XYZ xyz[NUM_TILES];
166 static int n_xyz, num_tiles;
167 static int shuffle[NUM_TILES];
168 static XYZ backup[NUM_TILES];
169 static int n_backup;
170
171 #define ixyz(i) (((int *)xyz)[i])
172
173 #define TRACE_PLACEMENT 1
174 #if TRACE_PLACEMENT
175 static int
176 show_grid()
177 {
178 int x, y;
179 for (y=0; y<GRID_SY; y++)
180 {
181 for (x=0; x<GRID_SX; x++)
182 {
183 if (grid[x][y][0] == 1)
184 putchar(183);
185 else if (grid[x][y][0] == 3)
186 putchar('X');
187 else if (grid[x][y][0] == 0)
188 putchar(' ');
189 else
190 putchar('O');
191 }
192 putchar('\n');
193 }
194 putchar('\n');
195 }
196 #endif
197
198 static int one_means_filled = 1;
199
200 static int
201 is_filled(int x, int y, int z)
202 {
203 if (x < 0 || x >= GRID_SX || y < 0 || y >= GRID_SY || z < 0 || z >= GRID_SZ)
204 return 0;
205 if (one_means_filled)
206 {
207 if (grid[x][y][z] == 1)
208 return 1;
209 }
210 else
211 {
212 if (grid[x][y][z] > 3)
213 return 1;
214 }
215 return 0;
216 }
217
218 static int
219 gather_usable_spots(XYZ *xyz)
220 {
221 int x, y, z;
222 int n_xyz = 0;
223 for (y=0; y<GRID_SY; y++)
224 for (x=0; x<GRID_SX; x++)
225 for (z=0; z<GRID_SZ; z++)
226 if (one_means_filled ? (grid[x][y][z] == 1) : (grid[x][y][z] > 3))
227 {
228 int blocked_over=0, blocked_left=0, blocked_right=0;
229 int ux, uy;
230 for (ux=-1; ux<=1; ux++)
231 for (uy=-1; uy<=1; uy++)
232 if (is_filled(x+ux, y+uy, z+1))
233 blocked_over=1;
234 for (uy=-1; uy<=1; uy++)
235 {
236 if (is_filled(x-2, y+uy, z))
237 blocked_left = 1;
238 if (is_filled(x+2, y+uy, z))
239 blocked_right = 1;
240 }
241 if (!blocked_over && (!blocked_left || !blocked_right))
242 {
243 xyz[n_xyz].x = x;
244 xyz[n_xyz].y = y;
245 xyz[n_xyz].z = z;
246 xyz[n_xyz].a = 0;
247 n_xyz++;
248 }
249 }
250 return n_xyz;
251 }
252
253 static int
254 place_tiles(int n)
255 {
256 XYZ xyz[NUM_TILES], txyz; /* maximum number of spots */
257 int n_xyz, i, j;
258
259 if (n >= num_tiles)
260 return 1;
261
262 n_xyz = gather_usable_spots(xyz);
263 for (i=0; i<n_xyz; i++)
264 {
265 j = rand() % n_xyz;
266 txyz = xyz[i];
267 xyz[i] = xyz[j];
268 xyz[j] = txyz;
269 }
270
271 for (i=0; i<n_xyz-1; i++)
272 {
273 grid[xyz[i].x][xyz[i].y][xyz[i].z] = shuffle[n];
274 for (j=i+1; j<n_xyz; j++)
275 {
276 grid[xyz[j].x][xyz[j].y][xyz[j].z] = shuffle[n+1];
277 if (place_tiles(n+2))
278 return 1;
279 grid[xyz[j].x][xyz[j].y][xyz[j].z] = 1;
280 }
281 grid[xyz[i].x][xyz[i].y][xyz[i].z] = 1;
282 }
283 return 0;
284 }
285
286 static int defer_redraw = 1;
287
288 static void
289 bi_load(signed char *data)
290 {
291 int x, y, z, i;
292 memset(grid, 0, sizeof(grid));
293 while (*data != 127)
294 {
295 x = *data++;
296 if (x <= 0)
297 {
298 x = -x;
299 y = *data++;
300 z = *data++;
301 }
302 grid[x][y][z] = 1;
303 }
304 }
305
306 static void
307 remove_pair(int x1, int y1, int z1, int x2, int y2, int z2)
308 {
309 selected_tile = 0;
310 selected_x = -1;
311
312 backup[n_backup].x = x1;
313 backup[n_backup].y = y1;
314 backup[n_backup].z = z1;
315 backup[n_backup].a = grid[x1][y1][z1];
316 n_backup ++;
317 grid[x1][y1][z1] = 0;
318 invalidate_tile(x1, y1, z1);
319
320 backup[n_backup].x = x2;
321 backup[n_backup].y = y2;
322 backup[n_backup].z = z2;
323 backup[n_backup].a = grid[x2][y2][z2];
324 n_backup ++;
325 grid[x2][y2][z2] = 0;
326 invalidate_tile(x2, y2, z2);
327
328 num_tiles -= 2;
329 }
330
331 static void
332 redraw_free_tile_count()
333 {
334 XYZ xyz[NUM_TILES], txyz; /* maximum number of spots */
335 int n = gather_usable_spots(xyz), i, j, c;
336 char tmp[10];
337 c = 0;
338 for (i=0; i<n; i++)
339 for (j=0; j<n; j++)
340 if (i != j
341 && ((grid[xyz[i].x][xyz[i].y][xyz[i].z] & ~3)
342 == (grid[xyz[j].x][xyz[j].y][xyz[j].z] & ~3)))
343 {
344 c ++;
345 break;
346 }
347 sprintf(tmp, " %3d %3d", num_tiles, c);
348 text(tmp, 2, table_height-2);
349 }
350
351 static void
352 shuffle_set(int *s)
353 {
354 int i, j, t;
355 for (i=0; i<4; i++)
356 for (j=0; j<4; j++)
357 if (rand() & 16)
358 {
359 t = s[i];
360 s[i] = s[j];
361 s[j] = t;
362 }
363 }
364
365 static void
366 start_again()
367 {
368 int i, x, y, z, n;
369
370 if (selected_tile)
371 {
372 int nx = selected_x;
373 selected_tile = 0;
374 selected_x = -1;
375 invalidate_tile(nx, selected_y, selected_z);
376 }
377
378 /* load the board into grid (each cell is zero or one) */
379 defer_redraw = 1;
380 if (layout_number)
381 bi_load(layouts[layout_number-1]);
382 else
383 load(0);
384 defer_redraw = 0;
385
386 /* count the number of tiles we need */
387 num_tiles = 0;
388 for (y=0; y<GRID_SY; y++)
389 for (x=0; x<GRID_SX; x++)
390 for (z=0; z<GRID_SZ; z++)
391 if (grid[x][y][z])
392 num_tiles++;
393
394 if (num_tiles > NUM_TILES-4 || num_tiles == 0 || num_tiles%2)
395 {
396 fprintf(stderr, "Invalid number of tiles: %d\n", num_tiles);
397 exit(0);
398 }
399
400 /* build a list of all tiles */
401 for (x=4; x<NUM_TILES; x++)
402 shuffle[x-4] = x;
403 /* randomize the colors and directions - the last two sets */
404 shuffle_set(shuffle+NUM_TILES-8);
405 shuffle_set(shuffle+NUM_TILES-12);
406
407 if (layout_number > 9)
408 {
409 /* special case for "learning" sets */
410 for (x=0; x<NUM_TILES-4; x+=2)
411 {
412 y = (rand() % (NUM_TILES-4)) & ~1;
413 z = shuffle[x];
414 shuffle[x] = shuffle[y];
415 shuffle[y] = z;
416 z = shuffle[x+1];
417 shuffle[x+1] = shuffle[y+1];
418 shuffle[y+1] = z;
419 }
420 }
421 else
422 {
423 /* Randomize in groups of four */
424 for (x=0; x<NUM_TILES-4; x+=4)
425 {
426 y = (rand() % (NUM_TILES-4)) & ~3;
427 for (n=0; n<4; n++)
428 {
429 z = shuffle[x+n];
430 shuffle[x+n] = shuffle[y+n];
431 shuffle[y+n] = z;
432 }
433 }
434
435 /* Now randomize the first N tiles in groups of two */
436 for (x=0; x<num_tiles; x+=2)
437 {
438 y = (rand() % num_tiles) & ~1;
439 z = shuffle[x];
440 shuffle[x] = shuffle[y];
441 shuffle[y] = z;
442 z = shuffle[x+1];
443 shuffle[x+1] = shuffle[y+1];
444 shuffle[y+1] = z;
445 }
446 }
447
448 /* Now place the tiles */
449
450 one_means_filled = 1;
451 if (! place_tiles(0))
452 {
453 fprintf(stderr, "Error: unable to find a way to lay out tiles.\n");
454 exit(0);
455 }
456 n_backup = 0;
457 one_means_filled = 0;
458
459 redraw_free_tile_count();
460 invalidate(0, 0, table_width, table_height);
461 }
462
463 static void
464 init()
465 {
466 int i;
467 memset(grid, 0, sizeof(grid));
468 for (i=4; i<NUM_TILES; i++)
469 {
470 char n[20];
471 sprintf(n, "%s", tile_names[i]);
472 tiles[i] = get_picture(n);
473 }
474 blank_tile = get_picture("bl");
475 youwin = get_picture("youwin");
476 youlose = get_picture("youlose");
477 splash = get_picture("taipei");
478 set_centered_pic(splash);
479 start_again();
480 }
481
482 static void
483 redraw_tile(int x, int y, int z)
484 {
485 Picture *p = tiles[grid[x][y][z]];
486 if (calculate_exposures(x, y, z) >= 4)
487 put_picture(blank_tile, gx2x(x, z), gy2y(y, z), 0, 0, TILE_SX, TILE_SY);
488 else if (x == selected_x && y == selected_y && z == selected_z)
489 put_picture_inverted(p, gx2x(x, z), gy2y(y, z), 0, 0, TILE_SX, TILE_SY);
490 else
491 put_picture(p, gx2x(x, z), gy2y(y, z), 0, 0, TILE_SX, TILE_SY);
492 }
493
494 static void
495 redraw()
496 {
497 int x, y, z;
498 if (defer_redraw)
499 return;
500 for (z=0; z<GRID_SZ; z++)
501 {
502 for (x=GRID_SX-1; x>=0; x--)
503 for (y=0; x+y<GRID_SX && y<GRID_SY; y++)
504 if (grid[x+y][y][z])
505 redraw_tile(x+y, y, z);
506 for (y=0; y<GRID_SY; y++)
507 for (x=0; x+y<GRID_SY && x<GRID_SX; x++)
508 if (grid[x][x+y][z])
509 redraw_tile(x, x+y, z);
510 }
511 redraw_free_tile_count();
512 }
513
514 extern char taipei_help[];
515
516 static void
517 key(int k, int x, int y)
518 {
519 if (k == 3 || k == 27 || k == 'q')
520 exit(0);
521 if (k == KEY_F(1) || k == 'h')
522 {
523 set_centered_pic(0);
524 help("taipei.html", taipei_help);
525 }
526 if (k == KEY_F(2))
527 {
528 set_centered_pic(0);
529 start_again();
530 }
531 if (k == 8 || k == 127 || k == KEY_DELETE)
532 {
533 int nx = selected_x;
534 selected_tile = 0;
535 set_centered_pic(0);
536 selected_x = -1;
537 invalidate_tile(nx, selected_y, selected_z);
538 if (n_backup > 1)
539 {
540 int i;
541 for (i=0; i<2; i++)
542 {
543 n_backup--;
544 grid[backup[n_backup].x][backup[n_backup].y][backup[n_backup].z]
545 = backup[n_backup].a;
546 invalidate_tile(backup[n_backup].x, backup[n_backup].y, backup[n_backup].z);
547 num_tiles++;
548 }
549 redraw_free_tile_count();
550 }
551 }
552
553 if (k >= '1' && k <= '9')
554 {
555 set_centered_pic(0);
556 layout_number = k - '0';
557 start_again();
558 }
559 if (k == 'j' || k == 'J')
560 {
561 set_centered_pic(0);
562 layout_number = 10;
563 start_again();
564 }
565 if (k == 'r' || k == 'R')
566 {
567 set_centered_pic(0);
568 layout_number = 11;
569 start_again();
570 }
571 if (k == '0' && filename)
572 {
573 set_centered_pic(0);
574 layout_number = 0;
575 start_again();
576 }
577 }
578
579 static int
580 tile_is_free(int x, int y, int z)
581 {
582 int dx, dy, bl=0, br=0;
583 if (z < GRID_SZ-1)
584 {
585 for (dx=-1; dx<=1; dx++)
586 for (dy=-1; dy<=1; dy++)
587 if (grid[x+dx][y+dy][z+1])
588 return 0;
589 }
590 for (dy=-1; dy<=1; dy++)
591 {
592 if (y+dy < 0 || y+dy >= GRID_DY)
593 continue;
594 if (x-2 >= 0 && grid[x-2][y+dy][z])
595 bl = 1;
596 if (x+2 < GRID_SX && grid[x+2][y+dy][z])
597 br = 1;
598 }
599 if (bl && br)
600 return 0;
601 return 1;
602 }
603
604 static void
605 remove_tile(int x, int y)
606 {
607 int nx, ny, nz;
608 for (nz=GRID_SZ-1; nz>=0; nz--)
609 for (nx=0; nx<GRID_SX; nx++)
610 if (x >= gx2x(nx, nz) && x < gx2x(nx, nz)+TILE_SX)
611 for (ny=0; ny<GRID_SY; ny++)
612 if (y >= gy2y(ny, nz) && y < gy2y(ny, nz)+TILE_SY)
613 if (grid[nx][ny][nz])
614 {
615 if (!tile_is_free(nx, ny, nz))
616 {
617 beep();
618 return;
619 }
620 if (selected_tile && selected_x == nx
621 && selected_y == ny && selected_z == nz)
622 {
623 selected_tile = 0;
624 selected_x = -1;
625 invalidate_tile(nx, ny, nz);
626 }
627 else if (selected_tile)
628 {
629 if ((grid[nx][ny][nz] & ~3)
630 != (selected_tile & ~3))
631 {
632 beep();
633 return;
634 }
635 remove_pair(selected_x, selected_y, selected_z, nx, ny, nz);
636 }
637 else
638 {
639 selected_x = nx;
640 selected_y = ny;
641 selected_z = nz;
642 selected_tile = grid[nx][ny][nz];
643 invalidate_tile(nx, ny, nz);
644 }
645 return;
646 }
647 }
648
649
650 static void
651 click(int x, int y, int b)
652 {
653 Picture *cp = get_centered_pic();
654
655 if ((cp == youlose || cp == youwin)
656 && (x > table_width/2-cp->w/2
657 && x < table_width/2+cp->w/2
658 && y > table_height/2-cp->h/2
659 && y < table_height/2+cp->h/2))
660 {
661 set_centered_pic(0);
662 start_again();
663 return;
664 }
665
666 if (cp == splash)
667 {
668 set_centered_pic(0);
669 return;
670 }
671
672 remove_tile(x, y);
673 if (num_tiles == 0)
674 set_centered_pic(youwin);
675 redraw_free_tile_count();
676 }
677
678 static FunctionMapping fmap[] = {
679 { "click", (void *)click },
680 { "init", (void *)init },
681 { "key", (void *)key },
682 { "redraw", (void *)redraw },
683 { 0, 0 }
684 };
685
686 int
687 main(int argc, char **argv)
688 {
689 register_imagelib(tile_images);
690 register_imagelib(appimglib_imagelib);
691 init_ace(argc, argv, fmap);
692 init_table(GRID_SX*GRID_DX+GRID_SZ*GRID_DZ+2*MARGIN,
693 GRID_SY*GRID_DY+GRID_SZ*GRID_DZ+2*MARGIN);
694 filename = argv[1];
695 if (filename)
696 layout_number = 0;
697 table_loop();
698 }