| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 *splash, *youwin, *youlose; |
| 27 |
|
| 28 |
static Picture *colors[6], *bighole, *cover; |
| 29 |
static Picture *black, *white, *smallhole; |
| 30 |
|
| 31 |
|
| 32 |
static int px, py, pdy; |
| 33 |
|
| 34 |
static int gx, gy, gdx, gdy; |
| 35 |
|
| 36 |
static int rx, ry, rdx, rdy; |
| 37 |
|
| 38 |
static int sx, sy, sdx; |
| 39 |
|
| 40 |
|
| 41 |
#define BSZ 16 |
| 42 |
#define SSZ 10 |
| 43 |
#define WGAP 30 |
| 44 |
|
| 45 |
#define MOVES 10 |
| 46 |
|
| 47 |
static Picture *guesses[MOVES][4]; |
| 48 |
static Picture *ranks[MOVES][4]; |
| 49 |
static Picture *solution[4]; |
| 50 |
|
| 51 |
static int active_row, solution_shown; |
| 52 |
|
| 53 |
static int |
| 54 |
redraw_row(int row) |
| 55 |
{ |
| 56 |
int x; |
| 57 |
for (x=0; x<4; x++) |
| 58 |
{ |
| 59 |
if (guesses[row][x]) |
| 60 |
put_picture(guesses[row][x], gx+x*gdx, gy+row*gdy, 0, 0, BSZ, BSZ); |
| 61 |
if (ranks[row][x]) |
| 62 |
put_picture(ranks[row][x], rx+x*rdx, ry+row*rdy, 0, 0, SSZ, SSZ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
set_active_row(int row) |
| 68 |
{ |
| 69 |
int x; |
| 70 |
active_row = row; |
| 71 |
for (x=0; x<4; x++) |
| 72 |
guesses[active_row][x] = bighole; |
| 73 |
redraw_row(active_row); |
| 74 |
py = (gy+active_row*gdy)-(BSZ+1)*5/2; |
| 75 |
if (py < CARD_MARGIN) |
| 76 |
py = CARD_MARGIN; |
| 77 |
if (py > table_height-CARD_MARGIN-6*BSZ-5) |
| 78 |
py = table_height-CARD_MARGIN-6*BSZ-5; |
| 79 |
invalidate(0, 0, px+BSZ, table_height); |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
start_again() |
| 84 |
{ |
| 85 |
int x, y; |
| 86 |
for (x=0; x<4; x++) |
| 87 |
{ |
| 88 |
for (y=0; y<MOVES; y++) |
| 89 |
{ |
| 90 |
guesses[y][x] = 0; |
| 91 |
ranks[y][x] = 0; |
| 92 |
} |
| 93 |
solution[x] = colors[rand()%6]; |
| 94 |
} |
| 95 |
set_active_row(0); |
| 96 |
solution_shown = 0; |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
init() |
| 101 |
{ |
| 102 |
start_again(); |
| 103 |
set_centered_pic(splash); |
| 104 |
} |
| 105 |
|
| 106 |
static void |
| 107 |
show_solution() |
| 108 |
{ |
| 109 |
solution_shown = 1; |
| 110 |
invalidate(0, 0, table_width, table_height); |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
redraw() |
| 115 |
{ |
| 116 |
int x, y; |
| 117 |
for (y=0; y<MOVES; y++) |
| 118 |
redraw_row(y); |
| 119 |
for (x=0; x<4; x++) |
| 120 |
if (solution_shown) |
| 121 |
put_picture(solution[x], sx+x*sdx, sy, 0, 0, BSZ, BSZ); |
| 122 |
else |
| 123 |
put_picture(cover, sx+x*sdx, sy, 0, 0, BSZ, BSZ); |
| 124 |
if (! solution_shown) |
| 125 |
for (y=0; y<6; y++) |
| 126 |
put_picture(colors[y], px, py+y*pdy, 0, 0, BSZ, BSZ); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
show_rank(int row) |
| 131 |
{ |
| 132 |
int x, y; |
| 133 |
int b=0, w=0, ri=0; |
| 134 |
int gs[4], ss[4]; |
| 135 |
for (x=0; x<4; x++) |
| 136 |
gs[x] = ss[x] = 0; |
| 137 |
for (x=0; x<4; x++) |
| 138 |
if (guesses[active_row][x] == solution[x]) |
| 139 |
{ |
| 140 |
ranks[active_row][ri++] = black; |
| 141 |
b++; |
| 142 |
gs[x] = ss[x] = 1; |
| 143 |
} |
| 144 |
for (x=0; x<4; x++) |
| 145 |
if (! gs[x]) |
| 146 |
for (y=0; y<4; y++) |
| 147 |
if (! ss[y]) |
| 148 |
if (guesses[active_row][x] == solution[y]) |
| 149 |
{ |
| 150 |
ranks[active_row][ri++] = white; |
| 151 |
w++; |
| 152 |
gs[x] = 1; |
| 153 |
ss[y] = 1; |
| 154 |
break; |
| 155 |
} |
| 156 |
while (ri<4) |
| 157 |
ranks[active_row][ri++] = smallhole; |
| 158 |
if (b == 4) |
| 159 |
show_solution(); |
| 160 |
redraw_row(active_row); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
check_row() |
| 165 |
{ |
| 166 |
int x; |
| 167 |
for (x=0; x<4; x++) |
| 168 |
if (!guesses[active_row][x] || guesses[active_row][x] == bighole) |
| 169 |
return; |
| 170 |
show_rank(active_row); |
| 171 |
if (!solution_shown) |
| 172 |
{ |
| 173 |
if (active_row == (MOVES-1)) |
| 174 |
{ |
| 175 |
show_solution(); |
| 176 |
set_centered_pic(youlose); |
| 177 |
} |
| 178 |
else |
| 179 |
set_active_row(active_row+1); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
static void |
| 184 |
key_color(int c) |
| 185 |
{ |
| 186 |
int x; |
| 187 |
for (x=0; x<4; x++) |
| 188 |
if (guesses[active_row][x] == bighole) |
| 189 |
{ |
| 190 |
guesses[active_row][x] = colors[c]; |
| 191 |
redraw_row(active_row); |
| 192 |
return; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
extern char mastermind_help[]; |
| 197 |
|
| 198 |
static void |
| 199 |
key(int k, int x, int y) |
| 200 |
{ |
| 201 |
int i; |
| 202 |
Picture *p = get_centered_pic(); |
| 203 |
set_centered_pic(0); |
| 204 |
if (p == splash) |
| 205 |
return; |
| 206 |
if (p == youwin || p == youlose || solution_shown) |
| 207 |
{ |
| 208 |
start_again(); |
| 209 |
invalidate(0, 0, table_width, table_height); |
| 210 |
return; |
| 211 |
} |
| 212 |
if (k == 3 || k == 27 || k == 'q') |
| 213 |
exit(0); |
| 214 |
if (k == KEY_F(2)) |
| 215 |
{ |
| 216 |
start_again(); |
| 217 |
invalidate(0, 0, table_width, table_height); |
| 218 |
return; |
| 219 |
} |
| 220 |
if (k == KEY_F(1) || k == 'h') |
| 221 |
{ |
| 222 |
set_centered_pic(0); |
| 223 |
help("mastermind.html", mastermind_help); |
| 224 |
return; |
| 225 |
} |
| 226 |
if (k == 'r') key_color(0); |
| 227 |
if (k == 'o') key_color(1); |
| 228 |
if (k == 'y') key_color(2); |
| 229 |
if (k == 'g') key_color(3); |
| 230 |
if (k == 'b') key_color(4); |
| 231 |
if (k == 'p') key_color(5); |
| 232 |
if (k == '\r' || k == '\n') |
| 233 |
check_row(); |
| 234 |
} |
| 235 |
|
| 236 |
static Picture *color_dragged=0; |
| 237 |
static int drag_dx, drag_dy, drag_ox, drag_oy; |
| 238 |
|
| 239 |
static void |
| 240 |
click(int x, int y, int b) |
| 241 |
{ |
| 242 |
Picture *p = get_centered_pic(); |
| 243 |
set_centered_pic(0); |
| 244 |
if (p == splash) |
| 245 |
return; |
| 246 |
if (p == youwin || p == youlose || solution_shown) |
| 247 |
{ |
| 248 |
start_again(); |
| 249 |
invalidate(0, 0, table_width, table_height); |
| 250 |
return; |
| 251 |
} |
| 252 |
drag_ox = x; |
| 253 |
drag_oy = y; |
| 254 |
if (x >= px && x < px+BSZ && y >= py && y < py+6*pdy) |
| 255 |
{ |
| 256 |
color_dragged = colors[(y-py)/pdy]; |
| 257 |
drag_dx = x - px; |
| 258 |
drag_dy = y - (((int)((y-py)/pdy))*pdy+py); |
| 259 |
return; |
| 260 |
} |
| 261 |
if (x >= gx && x < gx+4*gdx && y < gy+BSZ && y > gy+(MOVES-1)*gdy) |
| 262 |
{ |
| 263 |
int ty; |
| 264 |
ty = ((gy+BSZ)-y)/(-gdy); |
| 265 |
color_dragged = guesses[ty][(x-gx)/gdx]; |
| 266 |
if (color_dragged == bighole) |
| 267 |
color_dragged = 0; |
| 268 |
drag_dx = x - (((int)((x-gx)/gdx))*gdx+gx); |
| 269 |
ty = gy+ty*gdy; |
| 270 |
drag_dy = y - ty; |
| 271 |
return; |
| 272 |
} |
| 273 |
color_dragged = 0; |
| 274 |
if (x > rx-WGAP/2) |
| 275 |
check_row(); |
| 276 |
} |
| 277 |
|
| 278 |
static void |
| 279 |
drag(int x, int y, int b) |
| 280 |
{ |
| 281 |
if (color_dragged == 0) return; |
| 282 |
invalidate_exposure(drag_ox-drag_dx, drag_oy-drag_dy, BSZ, BSZ, |
| 283 |
x-drag_dx, y-drag_oy, BSZ, BSZ); |
| 284 |
put_picture(color_dragged, x-drag_dx, y-drag_dy, 0, 0, BSZ, BSZ); |
| 285 |
drag_ox = x; |
| 286 |
drag_oy = y; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
drop(int x, int y, int b) |
| 291 |
{ |
| 292 |
if (color_dragged == 0) return; |
| 293 |
invalidate(drag_ox-drag_dx, drag_oy-drag_dy, BSZ, BSZ); |
| 294 |
x = x-drag_dx + BSZ/2; |
| 295 |
y = y-drag_dy + BSZ/2; |
| 296 |
|
| 297 |
if (x >= gx && x < gx+4*gdx |
| 298 |
&& y >= gy+active_row*gdy && y < gy+active_row*gdy+BSZ) |
| 299 |
{ |
| 300 |
int i = (x-gx)/gdx; |
| 301 |
guesses[active_row][i] = color_dragged; |
| 302 |
redraw_row(active_row); |
| 303 |
} |
| 304 |
color_dragged = 0; |
| 305 |
} |
| 306 |
|
| 307 |
static FunctionMapping fmap[] = { |
| 308 |
{ "click", (void *)click }, |
| 309 |
{ "drag", (void *)drag }, |
| 310 |
{ "drop", (void *)drop }, |
| 311 |
{ "init", (void *)init }, |
| 312 |
{ "key", (void *)key }, |
| 313 |
{ "redraw", (void *)redraw }, |
| 314 |
{ 0, 0 } |
| 315 |
}; |
| 316 |
|
| 317 |
int |
| 318 |
main(int argc, char **argv) |
| 319 |
{ |
| 320 |
register_imagelib(appimglib_imagelib); |
| 321 |
init_ace(argc, argv, fmap); |
| 322 |
|
| 323 |
splash = get_picture("mastermind"); |
| 324 |
youwin = get_picture("youwin"); |
| 325 |
youlose = get_picture("youlose"); |
| 326 |
|
| 327 |
colors[0] = get_picture("mastermind-r"); |
| 328 |
colors[1] = get_picture("mastermind-o"); |
| 329 |
colors[2] = get_picture("mastermind-y"); |
| 330 |
colors[3] = get_picture("mastermind-g"); |
| 331 |
colors[4] = get_picture("mastermind-b"); |
| 332 |
colors[5] = get_picture("mastermind-p"); |
| 333 |
bighole = get_picture("mastermind-eb"); |
| 334 |
cover = get_picture("mastermind-c"); |
| 335 |
|
| 336 |
black = get_picture("mastermind-k"); |
| 337 |
white = get_picture("mastermind-w"); |
| 338 |
smallhole = get_picture("mastermind-e"); |
| 339 |
|
| 340 |
px = WGAP; |
| 341 |
py = CARD_MARGIN; |
| 342 |
pdy = BSZ + 1; |
| 343 |
|
| 344 |
sx = 2*WGAP+BSZ; |
| 345 |
sy = CARD_MARGIN; |
| 346 |
sdx = BSZ + 3; |
| 347 |
|
| 348 |
gdx = BSZ + 3; |
| 349 |
gdy = -(BSZ + 3); |
| 350 |
gx = sx; |
| 351 |
gy = 2*CARD_MARGIN+BSZ - (MOVES-1)*gdy; |
| 352 |
|
| 353 |
rx = gx + 3*gdx + BSZ + WGAP; |
| 354 |
ry = gy + (BSZ-SSZ)/2; |
| 355 |
rdx = SSZ+3; |
| 356 |
rdy = gdy; |
| 357 |
|
| 358 |
table_width = rx + 3*rdx + SSZ + WGAP; |
| 359 |
table_height = gy + BSZ + CARD_MARGIN; |
| 360 |
|
| 361 |
init_table(table_width, table_height); |
| 362 |
table_loop(); |
| 363 |
} |