Debian Codegraph @

games/merlin.c

raw symbols SCIP
1 /* The Ace of Penguins - merlin.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 <time.h>
20 #include <math.h>
21
22 #include "table.h"
23 #include "imagelib.h"
24 #include "cards.h"
25
26 static Picture *xlogo, *splash, *youwin;
27 static Picture *cell, *blank;
28
29 static char grid[9];
30
31 static char affects[9][9] = {
32 "XX-XX----",
33 "XXX------",
34 "-XX-XX---",
35 "X--X--X--",
36 "-X-XXX-X-",
37 "--X--X--X",
38 "---XX-XX-",
39 "------XXX",
40 "----XX-XX"
41 };
42
43 static void
44 start_again()
45 {
46 int i;
47 for (i=0; i<9; i++)
48 grid[i] = rand() & 1;
49 }
50
51 static void
52 init()
53 {
54 set_centered_pic(splash);
55 start_again();
56 }
57
58 static void
59 show(int i)
60 {
61 int x = (i/3)*100;
62 int y = (i%3)*100;
63 if (grid[i])
64 put_picture(cell, x, y, 0, 0, 100, 100);
65 else
66 put_picture(blank, x, y, 0, 0, 100, 100);
67 }
68
69 static void
70 redraw()
71 {
72 int i;
73 for (i=0; i<9; i++)
74 show(i);
75 }
76
77 static void
78 click(int x, int y, int b)
79 {
80 int i;
81 if (get_centered_pic() == youwin)
82 {
83 set_centered_pic(0);
84 start_again();
85 redraw();
86 return;
87 }
88 if (get_centered_pic())
89 {
90 set_centered_pic(0);
91 return;
92 }
93
94 x /= 100;
95 y /= 100;
96 i = x*3+y;
97 for (x=0; x<9; x++)
98 if (affects[i][x] == 'X')
99 {
100 grid[x] ^= 1;
101 show(x);
102 }
103 for (x=0; x<9; x++)
104 if (grid[x] == 0)
105 return;
106 set_centered_pic(youwin);
107 }
108
109 extern char merlin_help[];
110
111 static void
112 key(int k, int x, int y)
113 {
114 int i;
115 if (k == 3 || k == 27 || k == 'q')
116 exit(0);
117 set_centered_pic(0);
118 if (k == KEY_F(1) || k == 'h')
119 {
120 set_centered_pic(0);
121 help("merlin.html", merlin_help);
122 return;
123 }
124 if (k == KEY_F(2) || k == 'r')
125 {
126 start_again();
127 redraw();
128 }
129 }
130
131 static FunctionMapping fmap[] = {
132 { "click", (void *)click },
133 { "init", (void *)init },
134 { "key", (void *)key },
135 { "redraw", (void *)redraw },
136 { 0, 0 }
137 };
138
139 int
140 main(int argc, char **argv)
141 {
142 register_imagelib(appimglib_imagelib);
143 init_ace(argc, argv, fmap);
144
145 cell = get_picture("merlin-c");
146 blank = get_picture("merlin-b");
147 splash = get_picture("merlin");
148 youwin = get_picture("youwin");
149 xlogo = get_picture("xemboss");
150
151 init_table(300, 300);
152 table_loop();
153 }