| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "table.h" |
| 23 |
#include "imagelib.h" |
| 24 |
#include "cards.h" |
| 25 |
|
| 26 |
#define C_EMPTY 0 |
| 27 |
#define C_BOMB 9 |
| 28 |
#define C_RBOMB 10 |
| 29 |
#define C_FLAG 11 |
| 30 |
#define C_HUH 12 |
| 31 |
#define C_FILL 13 |
| 32 |
#define C_WRONG 14 |
| 33 |
#define C_UNKNOWN 15 |
| 34 |
|
| 35 |
#define TD_W 17 |
| 36 |
#define TD_H 26 |
| 37 |
#define TD_PX 175 |
| 38 |
#define TD_PW 31 |
| 39 |
#define TD_PH 26 |
| 40 |
|
| 41 |
#define PENGUIN_NORMAL 0 |
| 42 |
#define PENGUIN_SHOCK 1 |
| 43 |
#define PENGUIN_DEAD 2 |
| 44 |
#define PENGUIN_COOL 3 |
| 45 |
|
| 46 |
static int num_bombs[4] = { 6, 20, 45, 200 }; |
| 47 |
static int count[4] = { 8, 11, 16, 32 }; |
| 48 |
static int sizes[4] = { 48, 36, 24, 12 }; |
| 49 |
static Picture *cells[4]; |
| 50 |
|
| 51 |
static char grid[32][32]; |
| 52 |
static char bomb[32][32]; |
| 53 |
static char neighbors[32][32]; |
| 54 |
|
| 55 |
|
| 56 |
#define TW 401 |
| 57 |
#define TM 26 |
| 58 |
#define TH (401+TM) |
| 59 |
|
| 60 |
static int level=0, x0, y0, sz, ct, sc; |
| 61 |
static Picture *splash, *title, *xlogo; |
| 62 |
|
| 63 |
static int untagged=0, penguin=0; |
| 64 |
|
| 65 |
static int auto_play_mode = 0; |
| 66 |
|
| 67 |
static int title_y=0; |
| 68 |
|
| 69 |
static inline int |
| 70 |
BOMB(int x, int y) |
| 71 |
{ |
| 72 |
if (x < 0 || y < 0 || x >= ct || y >= ct) |
| 73 |
return 0; |
| 74 |
return bomb[x][y]; |
| 75 |
} |
| 76 |
|
| 77 |
static inline int |
| 78 |
GRID(int x, int y) |
| 79 |
{ |
| 80 |
if (x < 0 || y < 0 || x >= ct || y >= ct) |
| 81 |
return C_UNKNOWN; |
| 82 |
return grid[x][y]; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
start_again() |
| 87 |
{ |
| 88 |
int fill[1024]; |
| 89 |
int i, j, x, y; |
| 90 |
|
| 91 |
sz = sizes[level]; |
| 92 |
ct = count[level]; |
| 93 |
sc = 4-level; |
| 94 |
|
| 95 |
x0 = y0 = (TW-count[level]*sizes[level]-sc)/2 + sc; |
| 96 |
y0 += TD_H; |
| 97 |
|
| 98 |
for (i=0; i<1024; i++) |
| 99 |
fill[i] = i; |
| 100 |
for (i=ct*ct-1; i>=0; i--) |
| 101 |
{ |
| 102 |
int t; |
| 103 |
j = rand() % (ct*ct); |
| 104 |
t = fill[i]; |
| 105 |
fill[i] = fill[j]; |
| 106 |
fill[j] = t; |
| 107 |
} |
| 108 |
for (x=0; x<ct; x++) |
| 109 |
for (y=0; y<ct; y++) |
| 110 |
{ |
| 111 |
i = x*ct+y; |
| 112 |
if (fill[i] < num_bombs[level]) |
| 113 |
bomb[x][y] = 1; |
| 114 |
else |
| 115 |
bomb[x][y] = 0; |
| 116 |
grid[x][y] = C_FILL; |
| 117 |
neighbors[x][y] = 0; |
| 118 |
} |
| 119 |
|
| 120 |
for (x=0; x<ct; x++) |
| 121 |
for (y=0; y<ct; y++) |
| 122 |
neighbors[x][y] = |
| 123 |
BOMB(x-1,y-1) + BOMB(x-1,y) + BOMB(x-1,y+1) |
| 124 |
+ BOMB(x,y-1) + BOMB(x,y+1) |
| 125 |
+ BOMB(x+1,y-1) + BOMB(x+1,y) + BOMB(x+1,y+1); |
| 126 |
|
| 127 |
untagged = num_bombs[level]; |
| 128 |
penguin = PENGUIN_NORMAL; |
| 129 |
title_y = (y0-sc-TD_PH+1)/2; |
| 130 |
invalidate(0, 0, table_width, table_height); |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
init() |
| 135 |
{ |
| 136 |
splash = get_picture("minesweeper"); |
| 137 |
title = get_picture("minesweeper-t"); |
| 138 |
xlogo = get_picture("minesweeper-x"); |
| 139 |
cells[0] = get_picture("minesweeper-c48"); |
| 140 |
cells[1] = get_picture("minesweeper-c36"); |
| 141 |
cells[2] = get_picture("minesweeper-c24"); |
| 142 |
cells[3] = get_picture("minesweeper-c12"); |
| 143 |
level = 3; |
| 144 |
start_again(); |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
pc(int dx, int dy, int sx, int sy, int w, int h) |
| 149 |
{ |
| 150 |
put_picture(cells[level], dx-sx+x0, dy-sy+y0, sx, sy, w, h); |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
set_grid(int x, int y, int c) |
| 155 |
{ |
| 156 |
int xx = x*sz; |
| 157 |
int yy = y*sz; |
| 158 |
grid[x][y] = c; |
| 159 |
pc(xx, yy, c*sz, 0, sz, sz); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
set_penguin(int p) |
| 164 |
{ |
| 165 |
int x, y; |
| 166 |
penguin = p; |
| 167 |
x = TD_PX+penguin*TD_PW; |
| 168 |
put_picture(title, table_width/2-TD_PW/2-x, title_y, x, 0, TD_PW, TD_PH); |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
show_untagged() |
| 173 |
{ |
| 174 |
char tmp[4]; |
| 175 |
int i, x; |
| 176 |
if (untagged < 0) |
| 177 |
strcpy(tmp, "000"); |
| 178 |
else |
| 179 |
sprintf(tmp, "%03d", untagged); |
| 180 |
for (i=0; i<3; i++) |
| 181 |
{ |
| 182 |
x = TD_W*(tmp[i]-'0'); |
| 183 |
put_picture(title, i*TD_W-x+x0, title_y, x, 0, TD_W, TD_H); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
extern char minesweeper_help[]; |
| 188 |
|
| 189 |
static void |
| 190 |
flood_fill(int x, int y) |
| 191 |
{ |
| 192 |
if (x<0 || y<0 || x>=ct || y>=ct) |
| 193 |
return; |
| 194 |
if (bomb[x][y] || grid[x][y] == C_FLAG) |
| 195 |
return; |
| 196 |
if (grid[x][y] < 9) |
| 197 |
return; |
| 198 |
set_grid(x, y, neighbors[x][y]); |
| 199 |
if (neighbors[x][y] == 0) |
| 200 |
{ |
| 201 |
flood_fill(x-1, y-1); |
| 202 |
flood_fill(x-1, y+1); |
| 203 |
flood_fill(x+1, y-1); |
| 204 |
flood_fill(x+1, y+1); |
| 205 |
flood_fill(x-1, y); |
| 206 |
flood_fill(x+1, y); |
| 207 |
flood_fill(x, y-1); |
| 208 |
flood_fill(x, y+1); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
check_for_win() |
| 214 |
{ |
| 215 |
int x, y; |
| 216 |
if (untagged != 0) |
| 217 |
return; |
| 218 |
for (x=0; x<ct; x++) |
| 219 |
for (y=0; y<ct; y++) |
| 220 |
if (grid[x][y] > 8 && grid[x][y] != C_FLAG) |
| 221 |
return; |
| 222 |
set_penguin(PENGUIN_COOL); |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
do_bomb(int x, int y) |
| 227 |
{ |
| 228 |
set_grid(x, y, C_RBOMB); |
| 229 |
set_penguin(PENGUIN_DEAD); |
| 230 |
for (x=0; x<ct; x++) |
| 231 |
for (y=0; y<ct; y++) |
| 232 |
{ |
| 233 |
if (bomb[x][y] && grid[x][y] == C_FILL) |
| 234 |
set_grid(x, y, C_BOMB); |
| 235 |
if (!bomb[x][y] && grid[x][y] == C_FLAG) |
| 236 |
set_grid(x, y, C_WRONG); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
static int |
| 241 |
autoclick(int x, int y) |
| 242 |
{ |
| 243 |
int dx, dy, nflags=0, nfills=0, rv=0; |
| 244 |
for (dx=-1; dx<=1; dx++) |
| 245 |
for (dy=-1; dy<=1; dy++) |
| 246 |
{ |
| 247 |
int g = GRID(x+dx,y+dy); |
| 248 |
if (g == C_FLAG) |
| 249 |
nflags++; |
| 250 |
if (g == C_FILL || g == C_HUH) |
| 251 |
nfills++; |
| 252 |
} |
| 253 |
if (nflags == neighbors[x][y]) |
| 254 |
{ |
| 255 |
for (dx=-1; dx<=1; dx++) |
| 256 |
for (dy=-1; dy<=1; dy++) |
| 257 |
{ |
| 258 |
int g = GRID(x+dx,y+dy); |
| 259 |
if (g == C_FILL || g == C_HUH) |
| 260 |
{ |
| 261 |
rv=1; |
| 262 |
if (bomb[x+dx][y+dy]) |
| 263 |
do_bomb(x+dx, y+dy); |
| 264 |
else if (neighbors[x+dx][y+dy] == 0) |
| 265 |
flood_fill(x+dx, y+dy); |
| 266 |
else |
| 267 |
set_grid(x+dx, y+dy, neighbors[x+dx][y+dy]); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
else if (nfills+nflags == neighbors[x][y]) |
| 272 |
{ |
| 273 |
for (dx=-1; dx<=1; dx++) |
| 274 |
for (dy=-1; dy<=1; dy++) |
| 275 |
{ |
| 276 |
int g = GRID(x+dx,y+dy); |
| 277 |
if (g == C_FILL || g == C_HUH) |
| 278 |
{ |
| 279 |
rv=1; |
| 280 |
set_grid(x+dx, y+dy, C_FLAG); |
| 281 |
untagged--; |
| 282 |
show_untagged(); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
return rv; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
autoplay() |
| 291 |
{ |
| 292 |
int x, y, c=1; |
| 293 |
set_penguin(PENGUIN_SHOCK); |
| 294 |
while (c && penguin == PENGUIN_SHOCK) |
| 295 |
{ |
| 296 |
c = 0; |
| 297 |
for (x=0; x<ct; x++) |
| 298 |
for (y=0; y<ct; y++) |
| 299 |
if (grid[x][y] < 9) |
| 300 |
{ |
| 301 |
c += autoclick(x, y); |
| 302 |
flush(); |
| 303 |
} |
| 304 |
} |
| 305 |
if (penguin == PENGUIN_SHOCK) |
| 306 |
set_penguin(PENGUIN_NORMAL); |
| 307 |
check_for_win(); |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
key(int k, int x, int y) |
| 312 |
{ |
| 313 |
if (k == 3 || k == 27 || k == 'q') |
| 314 |
exit(0); |
| 315 |
|
| 316 |
set_centered_pic(0); |
| 317 |
|
| 318 |
if (k == KEY_F(1) || k == 'h') |
| 319 |
{ |
| 320 |
help("minesweeper.html", minesweeper_help); |
| 321 |
} |
| 322 |
|
| 323 |
if (k == 'a' && penguin == PENGUIN_NORMAL) |
| 324 |
autoplay(); |
| 325 |
if (k == 'A' && penguin == PENGUIN_NORMAL) |
| 326 |
{ |
| 327 |
auto_play_mode = !auto_play_mode; |
| 328 |
autoplay(); |
| 329 |
} |
| 330 |
|
| 331 |
if (k == KEY_F(2) || k == 'r') |
| 332 |
start_again(); |
| 333 |
|
| 334 |
if (k >= '1' && k <= '4') |
| 335 |
{ |
| 336 |
level = k - '1'; |
| 337 |
start_again(); |
| 338 |
} |
| 339 |
|
| 340 |
if (k >= '6' && k <= '9') |
| 341 |
{ |
| 342 |
penguin = k - '6'; |
| 343 |
set_penguin(penguin); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
static void |
| 348 |
click(int x, int y, int b) |
| 349 |
{ |
| 350 |
if (y < y0-sc && x > table_width/2-TD_PW/2 && x < table_width/2+TD_PW/2) |
| 351 |
{ |
| 352 |
if (b > 1) |
| 353 |
{ |
| 354 |
if (penguin == PENGUIN_NORMAL) |
| 355 |
autoplay(); |
| 356 |
} |
| 357 |
else |
| 358 |
start_again(); |
| 359 |
} |
| 360 |
|
| 361 |
if (penguin == PENGUIN_DEAD || penguin == PENGUIN_COOL || y < y0-sc) |
| 362 |
return; |
| 363 |
|
| 364 |
set_penguin(PENGUIN_SHOCK); |
| 365 |
} |
| 366 |
|
| 367 |
static void |
| 368 |
drop(int x, int y, int b) |
| 369 |
{ |
| 370 |
if (penguin == PENGUIN_DEAD || penguin == PENGUIN_COOL) |
| 371 |
return; |
| 372 |
|
| 373 |
set_penguin(PENGUIN_NORMAL); |
| 374 |
|
| 375 |
if (y < y0 || x < x0) |
| 376 |
return; |
| 377 |
x = (x-x0)/sz; |
| 378 |
y = (y-y0)/sz; |
| 379 |
if (x>=ct || y>=ct) |
| 380 |
return; |
| 381 |
|
| 382 |
if (b > 1) |
| 383 |
{ |
| 384 |
if (grid[x][y] < 9) |
| 385 |
{ |
| 386 |
autoclick(x, y); |
| 387 |
} |
| 388 |
switch (grid[x][y]) |
| 389 |
{ |
| 390 |
case C_FILL: |
| 391 |
set_grid(x, y, C_FLAG); |
| 392 |
untagged--; |
| 393 |
show_untagged(); |
| 394 |
if (auto_play_mode) autoplay(); |
| 395 |
break; |
| 396 |
case C_FLAG: |
| 397 |
set_grid(x, y, C_HUH); |
| 398 |
untagged++; |
| 399 |
show_untagged(); |
| 400 |
break; |
| 401 |
case C_HUH: |
| 402 |
set_grid(x, y, C_FILL); |
| 403 |
break; |
| 404 |
} |
| 405 |
|
| 406 |
check_for_win(); |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
if (grid[x][y] != C_FILL |
| 411 |
&& grid[x][y] != C_FLAG |
| 412 |
&& grid[x][y] != C_HUH) |
| 413 |
return; |
| 414 |
|
| 415 |
if (grid[x][y] == C_FLAG) |
| 416 |
return; |
| 417 |
|
| 418 |
if (bomb[x][y]) |
| 419 |
{ |
| 420 |
do_bomb(x, y); |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
if (neighbors[x][y]) |
| 425 |
{ |
| 426 |
set_grid(x, y, neighbors[x][y]); |
| 427 |
if (auto_play_mode) autoplay(); |
| 428 |
check_for_win(); |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
flood_fill(x, y); |
| 433 |
if (auto_play_mode) autoplay(); |
| 434 |
check_for_win(); |
| 435 |
} |
| 436 |
|
| 437 |
static void |
| 438 |
put(int x, int y) |
| 439 |
{ |
| 440 |
int xx = x*sz; |
| 441 |
int yy = y*sz; |
| 442 |
int cx = grid[x][y] * sz; |
| 443 |
pc(xx, yy, cx, 0, sz, sz); |
| 444 |
} |
| 445 |
|
| 446 |
static void |
| 447 |
redraw() |
| 448 |
{ |
| 449 |
int x, y, i; |
| 450 |
pc(-sc, -sc, sz-sc, sz-sc, sc, sc); |
| 451 |
for (x=0; x<ct; x++) |
| 452 |
{ |
| 453 |
pc(x*sz, -sc, 0, sz-sc, sz, sc); |
| 454 |
pc(-sc, x*sz, sz-sc, 0, sc, sz); |
| 455 |
for (y=0; y<ct; y++) |
| 456 |
put(x, y); |
| 457 |
} |
| 458 |
set_penguin(penguin); |
| 459 |
show_untagged(); |
| 460 |
put_picture(xlogo, table_width-xlogo->w-x0, title_y, 0, 0, xlogo->w, xlogo->h); |
| 461 |
} |
| 462 |
|
| 463 |
static FunctionMapping fmap[] = { |
| 464 |
{ "click", (void *)click }, |
| 465 |
{ "drop", (void *)drop }, |
| 466 |
{ "init", (void *)init }, |
| 467 |
{ "key", (void *)key }, |
| 468 |
{ "redraw", (void *)redraw }, |
| 469 |
{ 0, 0 } |
| 470 |
}; |
| 471 |
|
| 472 |
int |
| 473 |
main(int argc, char **argv) |
| 474 |
{ |
| 475 |
register_imagelib(appimglib_imagelib); |
| 476 |
init_ace(argc, argv, fmap); |
| 477 |
init_table(TW, TH); |
| 478 |
table_loop(); |
| 479 |
} |