| 1 | /* |
| 2 | * unequal.c |
| 3 | * |
| 4 | * Implementation of 'Futoshiki', a puzzle featured in the Guardian. |
| 5 | * |
| 6 | * TTD: |
| 7 | * add multiple-links-on-same-col/row solver nous |
| 8 | * Optimise set solver to use bit operations instead |
| 9 | * |
| 10 | * Guardian puzzles of note: |
| 11 | * #1: 5:0,0L,0L,0,0,0R,0,0L,0D,0L,0R,0,2,0D,0,0,0,0,0,0,0U,0,0,0,0U, |
| 12 | * #2: 5:0,0,0,4L,0L,0,2LU,0L,0U,0,0,0U,0,0,0,0,0D,0,3LRUD,0,0R,3,0L,0,0, |
| 13 | * #3: (reprint of #2) |
| 14 | * #4: |
| 15 | * #5: 5:0,0,0,0,0,0,2,0U,3U,0U,0,0,3,0,0,0,3,0D,4,0,0,0L,0R,0,0, |
| 16 | * #6: 5:0D,0L,0,0R,0,0,0D,0,3,0D,0,0R,0,0R,0D,0U,0L,0,1,2,0,0,0U,0,0L, |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <assert.h> |
| 23 | #include <ctype.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include "puzzles.h" |
| 27 | #include "latin.h" /* contains typedef for digit */ |
| 28 | |
| 29 | /* ---------------------------------------------------------- |
| 30 | * Constant and structure definitions |
| 31 | */ |
| 32 | |
| 33 | #define FLASH_TIME 0.4F |
| 34 | |
| 35 | #define PREFERRED_TILE_SIZE 32 |
| 36 | |
| 37 | #define TILE_SIZE (ds->tilesize) |
| 38 | #define GAP_SIZE (TILE_SIZE/2) |
| 39 | #define SQUARE_SIZE (TILE_SIZE + GAP_SIZE) |
| 40 | |
| 41 | #define BORDER (TILE_SIZE / 2) |
| 42 | |
| 43 | #define COORD(x) ( (x) * SQUARE_SIZE + BORDER ) |
| 44 | #define FROMCOORD(x) ( ((x) - BORDER + SQUARE_SIZE) / SQUARE_SIZE - 1 ) |
| 45 | |
| 46 | #define GRID(p,w,x,y) ((p)->w[((y)*(p)->order)+(x)]) |
| 47 | #define GRID3(p,w,x,y,z) ((p)->w[ (((x)*(p)->order+(y))*(p)->order+(z)) ]) |
| 48 | #define HINT(p,x,y,n) GRID3(p, hints, x, y, n) |
| 49 | |
| 50 | enum { |
| 51 | COL_BACKGROUND, |
| 52 | COL_GRID, |
| 53 | COL_TEXT, COL_GUESS, COL_ERROR, COL_PENCIL, |
| 54 | COL_HIGHLIGHT, COL_LOWLIGHT, |
| 55 | NCOLOURS |
| 56 | }; |
| 57 | |
| 58 | struct game_params { |
| 59 | int order, diff; |
| 60 | }; |
| 61 | |
| 62 | #define F_IMMUTABLE 1 /* passed in as game description */ |
| 63 | #define F_GT_UP 2 |
| 64 | #define F_GT_RIGHT 4 |
| 65 | #define F_GT_DOWN 8 |
| 66 | #define F_GT_LEFT 16 |
| 67 | #define F_ERROR 32 |
| 68 | #define F_ERROR_UP 64 |
| 69 | #define F_ERROR_RIGHT 128 |
| 70 | #define F_ERROR_DOWN 256 |
| 71 | #define F_ERROR_LEFT 512 |
| 72 | |
| 73 | #define F_ERROR_MASK (F_ERROR|F_ERROR_UP|F_ERROR_RIGHT|F_ERROR_DOWN|F_ERROR_LEFT) |
| 74 | |
| 75 | struct game_state { |
| 76 | int order, completed, cheated; |
| 77 | digit *nums; /* actual numbers (size order^2) */ |
| 78 | unsigned char *hints; /* remaining possiblities (size order^3) */ |
| 79 | unsigned int *flags; /* flags (size order^2) */ |
| 80 | }; |
| 81 | |
| 82 | /* ---------------------------------------------------------- |
| 83 | * Game parameters and presets |
| 84 | */ |
| 85 | |
| 86 | /* Steal the method from map.c for difficulty levels. */ |
| 87 | #define DIFFLIST(A) \ |
| 88 | A(LATIN,Trivial,t) \ |
| 89 | A(EASY,Easy,e) \ |
| 90 | A(SET,Tricky,k) \ |
| 91 | A(EXTREME,Extreme,x) \ |
| 92 | A(RECURSIVE,Recursive,r) |
| 93 | |
| 94 | #define ENUM(upper,title,lower) DIFF_ ## upper, |
| 95 | #define TITLE(upper,title,lower) #title, |
| 96 | #define ENCODE(upper,title,lower) #lower |
| 97 | #define CONFIG(upper,title,lower) ":" #title |
| 98 | enum { DIFFLIST(ENUM) DIFF_IMPOSSIBLE = diff_impossible, DIFF_AMBIGUOUS = diff_ambiguous, DIFF_UNFINISHED = diff_unfinished }; |
| 99 | static char const *const unequal_diffnames[] = { DIFFLIST(TITLE) }; |
| 100 | static char const unequal_diffchars[] = DIFFLIST(ENCODE); |
| 101 | #define DIFFCOUNT lenof(unequal_diffchars) |
| 102 | #define DIFFCONFIG DIFFLIST(CONFIG) |
| 103 | |
| 104 | #define DEFAULT_PRESET 0 |
| 105 | |
| 106 | const static struct game_params unequal_presets[] = { |
| 107 | { 4, DIFF_EASY }, |
| 108 | { 5, DIFF_EASY }, |
| 109 | { 5, DIFF_SET }, |
| 110 | { 5, DIFF_EXTREME }, |
| 111 | { 6, DIFF_EASY }, |
| 112 | { 6, DIFF_SET }, |
| 113 | { 6, DIFF_EXTREME }, |
| 114 | { 7, DIFF_SET }, |
| 115 | { 7, DIFF_EXTREME }, |
| 116 | }; |
| 117 | |
| 118 | static int game_fetch_preset(int i, char **name, game_params **params) |
| 119 | { |
| 120 | game_params *ret; |
| 121 | char buf[80]; |
| 122 | |
| 123 | if (i < 0 || i >= lenof(unequal_presets)) |
| 124 | return FALSE; |
| 125 | |
| 126 | ret = snew(game_params); |
| 127 | *ret = unequal_presets[i]; /* structure copy */ |
| 128 | |
| 129 | sprintf(buf, "%dx%d %s", ret->order, ret->order, |
| 130 | unequal_diffnames[ret->diff]); |
| 131 | |
| 132 | *name = dupstr(buf); |
| 133 | *params = ret; |
| 134 | return TRUE; |
| 135 | } |
| 136 | |
| 137 | static game_params *default_params(void) |
| 138 | { |
| 139 | game_params *ret; |
| 140 | char *name; |
| 141 | |
| 142 | if (!game_fetch_preset(DEFAULT_PRESET, &name, &ret)) return NULL; |
| 143 | sfree(name); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | static void free_params(game_params *params) |
| 148 | { |
| 149 | sfree(params); |
| 150 | } |
| 151 | |
| 152 | static game_params *dup_params(game_params *params) |
| 153 | { |
| 154 | game_params *ret = snew(game_params); |
| 155 | *ret = *params; /* structure copy */ |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void decode_params(game_params *ret, char const *string) |
| 160 | { |
| 161 | char const *p = string; |
| 162 | |
| 163 | ret->order = atoi(p); |
| 164 | while (*p && isdigit((unsigned char)*p)) p++; |
| 165 | |
| 166 | if (*p == 'd') { |
| 167 | int i; |
| 168 | p++; |
| 169 | ret->diff = DIFFCOUNT+1; /* ...which is invalid */ |
| 170 | if (*p) { |
| 171 | for (i = 0; i < DIFFCOUNT; i++) { |
| 172 | if (*p == unequal_diffchars[i]) |
| 173 | ret->diff = i; |
| 174 | } |
| 175 | p++; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static char *encode_params(game_params *params, int full) |
| 181 | { |
| 182 | char ret[80]; |
| 183 | |
| 184 | sprintf(ret, "%d", params->order); |
| 185 | if (full) |
| 186 | sprintf(ret + strlen(ret), "d%c", unequal_diffchars[params->diff]); |
| 187 | |
| 188 | return dupstr(ret); |
| 189 | } |
| 190 | |
| 191 | static config_item *game_configure(game_params *params) |
| 192 | { |
| 193 | config_item *ret; |
| 194 | char buf[80]; |
| 195 | |
| 196 | ret = snewn(3, config_item); |
| 197 | |
| 198 | ret[0].name = "Size (s*s)"; |
| 199 | ret[0].type = C_STRING; |
| 200 | sprintf(buf, "%d", params->order); |
| 201 | ret[0].sval = dupstr(buf); |
| 202 | ret[0].ival = 0; |
| 203 | |
| 204 | ret[1].name = "Difficulty"; |
| 205 | ret[1].type = C_CHOICES; |
| 206 | ret[1].sval = DIFFCONFIG; |
| 207 | ret[1].ival = params->diff; |
| 208 | |
| 209 | ret[2].name = NULL; |
| 210 | ret[2].type = C_END; |
| 211 | ret[2].sval = NULL; |
| 212 | ret[2].ival = 0; |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static game_params *custom_params(config_item *cfg) |
| 218 | { |
| 219 | game_params *ret = snew(game_params); |
| 220 | |
| 221 | ret->order = atoi(cfg[0].sval); |
| 222 | ret->diff = cfg[1].ival; |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static char *validate_params(game_params *params, int full) |
| 228 | { |
| 229 | if (params->order < 3 || params->order > 32) |
| 230 | return "Order must be between 3 and 32"; |
| 231 | if (params->diff >= DIFFCOUNT) |
| 232 | return "Unknown difficulty rating"; |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | /* ---------------------------------------------------------- |
| 237 | * Various utility functions |
| 238 | */ |
| 239 | |
| 240 | static const struct { unsigned int f, fo, fe; int dx, dy; char c; } gtthan[] = { |
| 241 | { F_GT_UP, F_GT_DOWN, F_ERROR_UP, 0, -1, '^' }, |
| 242 | { F_GT_RIGHT, F_GT_LEFT, F_ERROR_RIGHT, 1, 0, '>' }, |
| 243 | { F_GT_DOWN, F_GT_UP, F_ERROR_DOWN, 0, 1, 'v' }, |
| 244 | { F_GT_LEFT, F_GT_RIGHT, F_ERROR_LEFT, -1, 0, '<' } |
| 245 | }; |
| 246 | |
| 247 | static game_state *blank_game(int order) |
| 248 | { |
| 249 | game_state *state = snew(game_state); |
| 250 | int o2 = order*order, o3 = o2*order; |
| 251 | |
| 252 | state->order = order; |
| 253 | state->completed = state->cheated = 0; |
| 254 | |
| 255 | state->nums = snewn(o2, digit); |
| 256 | state->hints = snewn(o3, unsigned char); |
| 257 | state->flags = snewn(o2, unsigned int); |
| 258 | |
| 259 | memset(state->nums, 0, o2 * sizeof(digit)); |
| 260 | memset(state->hints, 0, o3); |
| 261 | memset(state->flags, 0, o2 * sizeof(unsigned int)); |
| 262 | |
| 263 | return state; |
| 264 | } |
| 265 | |
| 266 | static game_state *dup_game(game_state *state) |
| 267 | { |
| 268 | game_state *ret = blank_game(state->order); |
| 269 | int o2 = state->order*state->order, o3 = o2*state->order; |
| 270 | |
| 271 | memcpy(ret->nums, state->nums, o2 * sizeof(digit)); |
| 272 | memcpy(ret->hints, state->hints, o3); |
| 273 | memcpy(ret->flags, state->flags, o2 * sizeof(unsigned int)); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static void free_game(game_state *state) |
| 279 | { |
| 280 | sfree(state->nums); |
| 281 | sfree(state->hints); |
| 282 | sfree(state->flags); |
| 283 | sfree(state); |
| 284 | } |
| 285 | |
| 286 | #define CHECKG(x,y) grid[(y)*o+(x)] |
| 287 | |
| 288 | /* Returns 0 if it finds an error, 1 otherwise. */ |
| 289 | static int check_gt(digit *grid, game_state *state, |
| 290 | int x, int y, int dx, int dy) |
| 291 | { |
| 292 | int o = state->order; |
| 293 | int n = CHECKG(x,y), dn = CHECKG(x+dx, y+dy); |
| 294 | |
| 295 | assert(n != 0); |
| 296 | if (dn == 0) return 1; |
| 297 | |
| 298 | if (n <= dn) { |
| 299 | debug(("check_gt error (%d,%d) (%d,%d)", x, y, x+dx, y+dy)); |
| 300 | return 0; |
| 301 | } |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | /* Returns 0 if it finds an error, 1 otherwise. */ |
| 306 | static int check_num_gt(digit *grid, game_state *state, |
| 307 | int x, int y, int me) |
| 308 | { |
| 309 | unsigned int f = GRID(state, flags, x, y); |
| 310 | int ret = 1, i; |
| 311 | |
| 312 | for (i = 0; i < 4; i++) { |
| 313 | if ((f & gtthan[i].f) && |
| 314 | !check_gt(grid, state, x, y, gtthan[i].dx, gtthan[i].dy)) { |
| 315 | if (me) GRID(state, flags, x, y) |= gtthan[i].fe; |
| 316 | ret = 0; |
| 317 | } |
| 318 | } |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* Returns 0 if it finds an error, 1 otherwise. */ |
| 323 | static int check_num_error(digit *grid, game_state *state, |
| 324 | int x, int y, int mark_errors) |
| 325 | { |
| 326 | int o = state->order; |
| 327 | int xx, yy, val = CHECKG(x,y), ret = 1; |
| 328 | |
| 329 | assert(val != 0); |
| 330 | |
| 331 | /* check for dups in same column. */ |
| 332 | for (yy = 0; yy < state->order; yy++) { |
| 333 | if (yy == y) continue; |
| 334 | if (CHECKG(x,yy) == val) ret = 0; |
| 335 | } |
| 336 | |
| 337 | /* check for dups in same row. */ |
| 338 | for (xx = 0; xx < state->order; xx++) { |
| 339 | if (xx == x) continue; |
| 340 | if (CHECKG(xx,y) == val) ret = 0; |
| 341 | } |
| 342 | |
| 343 | if (!ret) { |
| 344 | debug(("check_num_error (%d,%d) duplicate %d", x, y, val)); |
| 345 | if (mark_errors) GRID(state, flags, x, y) |= F_ERROR; |
| 346 | } |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | /* Returns: -1 for 'wrong' |
| 351 | * 0 for 'incomplete' |
| 352 | * 1 for 'complete and correct' |
| 353 | */ |
| 354 | static int check_complete(digit *grid, game_state *state, int mark_errors) |
| 355 | { |
| 356 | int x, y, ret = 1, o = state->order; |
| 357 | |
| 358 | if (mark_errors) |
| 359 | assert(grid == state->nums); |
| 360 | |
| 361 | for (x = 0; x < state->order; x++) { |
| 362 | for (y = 0; y < state->order; y++) { |
| 363 | if (mark_errors) |
| 364 | GRID(state, flags, x, y) &= ~F_ERROR_MASK; |
| 365 | if (grid[y*o+x] == 0) { |
| 366 | ret = 0; |
| 367 | } else { |
| 368 | if (!check_num_error(grid, state, x, y, mark_errors)) ret = -1; |
| 369 | if (!check_num_gt(grid, state, x, y, mark_errors)) ret = -1; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | if (ret == 1 && latin_check(grid, o)) |
| 374 | ret = -1; |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static char n2c(digit n, int order) { |
| 379 | if (n == 0) return ' '; |
| 380 | if (order < 10) { |
| 381 | if (n < 10) return '0' + n; |
| 382 | } else { |
| 383 | if (n < 11) return '0' + n-1; |
| 384 | n -= 11; |
| 385 | if (n <= 26) return 'A' + n; |
| 386 | } |
| 387 | return '?'; |
| 388 | } |
| 389 | |
| 390 | /* should be 'digit', but includes -1 for 'not a digit'. |
| 391 | * Includes keypresses (0 especially) for interpret_move. */ |
| 392 | static int c2n(int c, int order) { |
| 393 | if (c < 0 || c > 0xff) |
| 394 | return -1; |
| 395 | if (c == ' ' || c == '\010' || c == '\177') |
| 396 | return 0; |
| 397 | if (order < 10) { |
| 398 | if (c >= '1' && c <= '9') |
| 399 | return (int)(c - '0'); |
| 400 | } else { |
| 401 | if (c >= '0' && c <= '9') |
| 402 | return (int)(c - '0' + 1); |
| 403 | if (c >= 'A' && c <= 'Z') |
| 404 | return (int)(c - 'A' + 11); |
| 405 | if (c >= 'a' && c <= 'z') |
| 406 | return (int)(c - 'a' + 11); |
| 407 | } |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | static int game_can_format_as_text_now(game_params *params) |
| 412 | { |
| 413 | return TRUE; |
| 414 | } |
| 415 | |
| 416 | static char *game_text_format(game_state *state) |
| 417 | { |
| 418 | int x, y, len, n; |
| 419 | char *ret, *p; |
| 420 | |
| 421 | len = (state->order*2) * (state->order*2-1) + 1; |
| 422 | ret = snewn(len, char); |
| 423 | p = ret; |
| 424 | |
| 425 | for (y = 0; y < state->order; y++) { |
| 426 | for (x = 0; x < state->order; x++) { |
| 427 | n = GRID(state, nums, x, y); |
| 428 | *p++ = n > 0 ? n2c(n, state->order) : '.'; |
| 429 | |
| 430 | if (x < (state->order-1)) { |
| 431 | if (GRID(state, flags, x, y) & F_GT_RIGHT) |
| 432 | *p++ = '>'; |
| 433 | else if (GRID(state, flags, x+1, y) & F_GT_LEFT) |
| 434 | *p++ = '<'; |
| 435 | else |
| 436 | *p++ = ' '; |
| 437 | } |
| 438 | } |
| 439 | *p++ = '\n'; |
| 440 | |
| 441 | if (y < (state->order-1)) { |
| 442 | for (x = 0; x < state->order; x++) { |
| 443 | if (GRID(state, flags, x, y) & F_GT_DOWN) |
| 444 | *p++ = 'v'; |
| 445 | else if (GRID(state, flags, x, y+1) & F_GT_UP) |
| 446 | *p++ = '^'; |
| 447 | else |
| 448 | *p++ = ' '; |
| 449 | |
| 450 | if (x < state->order-1) |
| 451 | *p++ = ' '; |
| 452 | } |
| 453 | *p++ = '\n'; |
| 454 | } |
| 455 | } |
| 456 | *p++ = '\0'; |
| 457 | |
| 458 | assert(p - ret == len); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | #ifdef STANDALONE_SOLVER |
| 463 | static void game_debug(game_state *state) |
| 464 | { |
| 465 | char *dbg = game_text_format(state); |
| 466 | printf("%s", dbg); |
| 467 | sfree(dbg); |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | /* ---------------------------------------------------------- |
| 472 | * Solver. |
| 473 | */ |
| 474 | |
| 475 | struct solver_link { |
| 476 | int len, gx, gy, lx, ly; |
| 477 | }; |
| 478 | |
| 479 | typedef struct game_solver { |
| 480 | struct latin_solver latin; /* keep first in struct! */ |
| 481 | |
| 482 | game_state *state; |
| 483 | |
| 484 | int nlinks, alinks; |
| 485 | struct solver_link *links; |
| 486 | } game_solver; |
| 487 | |
| 488 | #if 0 |
| 489 | static void solver_debug(game_solver *solver, int wide) |
| 490 | { |
| 491 | #ifdef STANDALONE_SOLVER |
| 492 | if (solver_show_working) { |
| 493 | if (!wide) |
| 494 | game_debug(solver->state); |
| 495 | else |
| 496 | latin_solver_debug(solver->latin.cube, solver->latin.o); |
| 497 | } |
| 498 | #endif |
| 499 | } |
| 500 | #endif |
| 501 | |
| 502 | static void solver_add_link(game_solver *solver, |
| 503 | int gx, int gy, int lx, int ly, int len) |
| 504 | { |
| 505 | if (solver->alinks < solver->nlinks+1) { |
| 506 | solver->alinks = solver->alinks*2 + 1; |
| 507 | /*debug(("resizing solver->links, new size %d", solver->alinks));*/ |
| 508 | solver->links = sresize(solver->links, solver->alinks, struct solver_link); |
| 509 | } |
| 510 | solver->links[solver->nlinks].gx = gx; |
| 511 | solver->links[solver->nlinks].gy = gy; |
| 512 | solver->links[solver->nlinks].lx = lx; |
| 513 | solver->links[solver->nlinks].ly = ly; |
| 514 | solver->links[solver->nlinks].len = len; |
| 515 | solver->nlinks++; |
| 516 | /*debug(("Adding new link: len %d (%d,%d) < (%d,%d), nlinks now %d", |
| 517 | len, lx, ly, gx, gy, solver->nlinks));*/ |
| 518 | } |
| 519 | |
| 520 | static game_solver *new_solver(digit *grid, game_state *state) |
| 521 | { |
| 522 | game_solver *solver = snew(game_solver); |
| 523 | int o = state->order; |
| 524 | int i, x, y; |
| 525 | unsigned int f; |
| 526 | |
| 527 | latin_solver_alloc(&solver->latin, grid, o); |
| 528 | |
| 529 | solver->nlinks = solver->alinks = 0; |
| 530 | solver->links = NULL; |
| 531 | |
| 532 | for (x = 0; x < o; x++) { |
| 533 | for (y = 0; y < o; y++) { |
| 534 | f = GRID(state, flags, x, y); |
| 535 | for (i = 0; i < 4; i++) { |
| 536 | if (f & gtthan[i].f) |
| 537 | solver_add_link(solver, x, y, x+gtthan[i].dx, y+gtthan[i].dy, 1); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return solver; |
| 543 | } |
| 544 | |
| 545 | static void free_solver(game_solver *solver) |
| 546 | { |
| 547 | if (solver->links) sfree(solver->links); |
| 548 | latin_solver_free(&solver->latin); |
| 549 | sfree(solver); |
| 550 | } |
| 551 | |
| 552 | static void solver_nminmax(game_solver *usolver, |
| 553 | int x, int y, int *min_r, int *max_r, |
| 554 | unsigned char **ns_r) |
| 555 | { |
| 556 | struct latin_solver *solver = &usolver->latin; |
| 557 | int o = usolver->latin.o, min = o, max = 0, n; |
| 558 | unsigned char *ns; |
| 559 | |
| 560 | assert(x >= 0 && y >= 0 && x < o && y < o); |
| 561 | |
| 562 | ns = solver->cube + cubepos(x,y,1); |
| 563 | |
| 564 | if (grid(x,y) > 0) { |
| 565 | min = max = grid(x,y)-1; |
| 566 | } else { |
| 567 | for (n = 0; n < o; n++) { |
| 568 | if (ns[n]) { |
| 569 | if (n > max) max = n; |
| 570 | if (n < min) min = n; |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | if (min_r) *min_r = min; |
| 575 | if (max_r) *max_r = max; |
| 576 | if (ns_r) *ns_r = ns; |
| 577 | } |
| 578 | |
| 579 | static int solver_links(game_solver *usolver) |
| 580 | { |
| 581 | int i, j, lmin, gmax, nchanged = 0; |
| 582 | unsigned char *gns, *lns; |
| 583 | struct solver_link *link; |
| 584 | struct latin_solver *solver = &usolver->latin; |
| 585 | |
| 586 | for (i = 0; i < usolver->nlinks; i++) { |
| 587 | link = &usolver->links[i]; |
| 588 | solver_nminmax(usolver, link->gx, link->gy, NULL, &gmax, &gns); |
| 589 | solver_nminmax(usolver, link->lx, link->ly, &lmin, NULL, &lns); |
| 590 | |
| 591 | for (j = 0; j < solver->o; j++) { |
| 592 | /* For the 'greater' end of the link, discount all numbers |
| 593 | * too small to satisfy the inequality. */ |
| 594 | if (gns[j]) { |
| 595 | if (j < (lmin+link->len)) { |
| 596 | #ifdef STANDALONE_SOLVER |
| 597 | if (solver_show_working) { |
| 598 | printf("%*slink elimination, (%d,%d) > (%d,%d):\n", |
| 599 | solver_recurse_depth*4, "", |
| 600 | link->gx, link->gy, link->lx, link->ly); |
| 601 | printf("%*s ruling out %d at (%d,%d)\n", |
| 602 | solver_recurse_depth*4, "", |
| 603 | j+1, link->gx, link->gy); |
| 604 | } |
| 605 | #endif |
| 606 | cube(link->gx, link->gy, j+1) = FALSE; |
| 607 | nchanged++; |
| 608 | } |
| 609 | } |
| 610 | /* For the 'lesser' end of the link, discount all numbers |
| 611 | * too large to satisfy inequality. */ |
| 612 | if (lns[j]) { |
| 613 | if (j > (gmax-link->len)) { |
| 614 | #ifdef STANDALONE_SOLVER |
| 615 | if (solver_show_working) { |
| 616 | printf("%*slink elimination, (%d,%d) > (%d,%d):\n", |
| 617 | solver_recurse_depth*4, "", |
| 618 | link->gx, link->gy, link->lx, link->ly); |
| 619 | printf("%*s ruling out %d at (%d,%d)\n", |
| 620 | solver_recurse_depth*4, "", |
| 621 | j+1, link->lx, link->ly); |
| 622 | } |
| 623 | #endif |
| 624 | cube(link->lx, link->ly, j+1) = FALSE; |
| 625 | nchanged++; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | return nchanged; |
| 631 | } |
| 632 | |
| 633 | static int solver_grid(digit *grid, int o, int maxdiff, void *ctx) |
| 634 | { |
| 635 | game_state *state = (game_state *)ctx; |
| 636 | game_solver *solver; |
| 637 | struct latin_solver *lsolver; |
| 638 | struct latin_solver_scratch *scratch; |
| 639 | int ret, diff = DIFF_LATIN; |
| 640 | |
| 641 | assert(maxdiff <= DIFF_RECURSIVE); |
| 642 | |
| 643 | assert(state->order == o); |
| 644 | solver = new_solver(grid, state); |
| 645 | |
| 646 | lsolver = &solver->latin; |
| 647 | scratch = latin_solver_new_scratch(lsolver); |
| 648 | |
| 649 | while (1) { |
| 650 | cont: |
| 651 | ret = latin_solver_diff_simple(lsolver); |
| 652 | if (ret < 0) { |
| 653 | diff = DIFF_IMPOSSIBLE; |
| 654 | goto got_result; |
| 655 | } else if (ret > 0) { |
| 656 | diff = max(diff, DIFF_LATIN); |
| 657 | goto cont; |
| 658 | } |
| 659 | |
| 660 | if (maxdiff <= DIFF_LATIN) |
| 661 | break; |
| 662 | |
| 663 | /* This bit is unequal-specific */ |
| 664 | ret = solver_links(solver); |
| 665 | if (ret < 0) { |
| 666 | diff = DIFF_IMPOSSIBLE; |
| 667 | goto got_result; |
| 668 | } else if (ret > 0) { |
| 669 | diff = max(diff, DIFF_EASY); |
| 670 | goto cont; |
| 671 | } |
| 672 | |
| 673 | if (maxdiff <= DIFF_EASY) |
| 674 | break; |
| 675 | |
| 676 | /* Row- and column-wise set elimination */ |
| 677 | ret = latin_solver_diff_set(lsolver, scratch, 0); |
| 678 | if (ret < 0) { |
| 679 | diff = DIFF_IMPOSSIBLE; |
| 680 | goto got_result; |
| 681 | } else if (ret > 0) { |
| 682 | diff = max(diff, DIFF_SET); |
| 683 | goto cont; |
| 684 | } |
| 685 | |
| 686 | if (maxdiff <= DIFF_SET) |
| 687 | break; |
| 688 | |
| 689 | ret = latin_solver_diff_set(lsolver, scratch, 1); |
| 690 | if (ret < 0) { |
| 691 | diff = DIFF_IMPOSSIBLE; |
| 692 | goto got_result; |
| 693 | } else if (ret > 0) { |
| 694 | diff = max(diff, DIFF_EXTREME); |
| 695 | goto cont; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Forcing chains. |
| 700 | */ |
| 701 | if (latin_solver_forcing(lsolver, scratch)) { |
| 702 | diff = max(diff, DIFF_EXTREME); |
| 703 | goto cont; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * If we reach here, we have made no deductions in this |
| 708 | * iteration, so the algorithm terminates. |
| 709 | */ |
| 710 | break; |
| 711 | } |
| 712 | /* |
| 713 | * Last chance: if we haven't fully solved the puzzle yet, try |
| 714 | * recursing based on guesses for a particular square. We pick |
| 715 | * one of the most constrained empty squares we can find, which |
| 716 | * has the effect of pruning the search tree as much as |
| 717 | * possible. |
| 718 | */ |
| 719 | if (maxdiff == DIFF_RECURSIVE) { |
| 720 | int nsol = latin_solver_recurse(lsolver, DIFF_RECURSIVE, solver_grid, ctx); |
| 721 | if (nsol < 0) diff = DIFF_IMPOSSIBLE; |
| 722 | else if (nsol == 1) diff = DIFF_RECURSIVE; |
| 723 | else if (nsol > 1) diff = DIFF_AMBIGUOUS; |
| 724 | /* if nsol == 0 then we were complete anyway |
| 725 | * (and thus don't need to change diff) */ |
| 726 | } else { |
| 727 | int cc = check_complete(grid, state, 0); |
| 728 | if (cc == -1) diff = DIFF_IMPOSSIBLE; |
| 729 | if (cc == 0) diff = DIFF_UNFINISHED; |
| 730 | } |
| 731 | |
| 732 | got_result: |
| 733 | |
| 734 | #ifdef STANDALONE_SOLVER |
| 735 | if (solver_show_working) |
| 736 | printf("%*s%s found\n", |
| 737 | solver_recurse_depth*4, "", |
| 738 | diff == DIFF_IMPOSSIBLE ? "no solution (impossible)" : |
| 739 | diff == DIFF_UNFINISHED ? "no solution (unfinished)" : |
| 740 | diff == DIFF_AMBIGUOUS ? "multiple solutions" : |
| 741 | "one solution"); |
| 742 | #endif |
| 743 | |
| 744 | latin_solver_free_scratch(scratch); |
| 745 | memcpy(state->hints, solver->latin.cube, o*o*o); |
| 746 | free_solver(solver); |
| 747 | |
| 748 | return diff; |
| 749 | } |
| 750 | |
| 751 | static int solver_state(game_state *state, int maxdiff) |
| 752 | { |
| 753 | int diff = solver_grid(state->nums, state->order, maxdiff, (void*)state); |
| 754 | |
| 755 | if (diff == DIFF_IMPOSSIBLE) |
| 756 | return -1; |
| 757 | if (diff == DIFF_UNFINISHED) |
| 758 | return 0; |
| 759 | if (diff == DIFF_AMBIGUOUS) |
| 760 | return 2; |
| 761 | return 1; |
| 762 | } |
| 763 | |
| 764 | static game_state *solver_hint(game_state *state, int *diff_r, int mindiff, int maxdiff) |
| 765 | { |
| 766 | game_state *ret = dup_game(state); |
| 767 | int diff, r = 0; |
| 768 | |
| 769 | for (diff = mindiff; diff <= maxdiff; diff++) { |
| 770 | r = solver_state(ret, diff); |
| 771 | debug(("solver_state after %s %d", unequal_diffnames[diff], r)); |
| 772 | if (r != 0) goto done; |
| 773 | } |
| 774 | |
| 775 | done: |
| 776 | if (diff_r) *diff_r = (r > 0) ? diff : -1; |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | /* ---------------------------------------------------------- |
| 781 | * Game generation. |
| 782 | */ |
| 783 | |
| 784 | static char *latin_desc(digit *sq, size_t order) |
| 785 | { |
| 786 | int o2 = order*order, i; |
| 787 | char *soln = snewn(o2+2, char); |
| 788 | |
| 789 | soln[0] = 'S'; |
| 790 | for (i = 0; i < o2; i++) |
| 791 | soln[i+1] = n2c(sq[i], order); |
| 792 | soln[o2+1] = '\0'; |
| 793 | |
| 794 | return soln; |
| 795 | } |
| 796 | |
| 797 | /* returns non-zero if it placed (or could have placed) clue. */ |
| 798 | static int gg_place_clue(game_state *state, int ccode, digit *latin, int checkonly) |
| 799 | { |
| 800 | int loc = ccode / 5, which = ccode % 5; |
| 801 | int x = loc % state->order, y = loc / state->order; |
| 802 | |
| 803 | assert(loc < state->order*state->order); |
| 804 | |
| 805 | if (which == 4) { /* add number */ |
| 806 | if (state->nums[loc] != 0) { |
| 807 | #ifdef STANDALONE_SOLVER |
| 808 | if (state->nums[loc] != latin[loc]) { |
| 809 | printf("inconsistency for (%d,%d): state %d latin %d\n", |
| 810 | x, y, state->nums[loc], latin[loc]); |
| 811 | } |
| 812 | #endif |
| 813 | assert(state->nums[loc] == latin[loc]); |
| 814 | return 0; |
| 815 | } |
| 816 | if (!checkonly) { |
| 817 | state->nums[loc] = latin[loc]; |
| 818 | } |
| 819 | } else { /* add flag */ |
| 820 | int lx, ly, lloc; |
| 821 | |
| 822 | if (state->flags[loc] & gtthan[which].f) |
| 823 | return 0; /* already has flag. */ |
| 824 | |
| 825 | lx = x + gtthan[which].dx; |
| 826 | ly = y + gtthan[which].dy; |
| 827 | if (lx < 0 || ly < 0 || lx >= state->order || ly >= state->order) |
| 828 | return 0; /* flag compares to off grid */ |
| 829 | |
| 830 | lloc = loc + gtthan[which].dx + gtthan[which].dy*state->order; |
| 831 | if (latin[loc] <= latin[lloc]) |
| 832 | return 0; /* flag would be incorrect */ |
| 833 | |
| 834 | if (!checkonly) { |
| 835 | state->flags[loc] |= gtthan[which].f; |
| 836 | } |
| 837 | } |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | /* returns non-zero if it removed (or could have removed) the clue. */ |
| 842 | static int gg_remove_clue(game_state *state, int ccode, int checkonly) |
| 843 | { |
| 844 | int loc = ccode / 5, which = ccode % 5; |
| 845 | #ifdef STANDALONE_SOLVER |
| 846 | int x = loc % state->order, y = loc / state->order; |
| 847 | #endif |
| 848 | |
| 849 | assert(loc < state->order*state->order); |
| 850 | |
| 851 | if (which == 4) { /* remove number. */ |
| 852 | if (state->nums[loc] == 0) return 0; |
| 853 | if (!checkonly) { |
| 854 | #ifdef STANDALONE_SOLVER |
| 855 | if (solver_show_working) |
| 856 | printf("gg_remove_clue: removing %d at (%d,%d)", |
| 857 | state->nums[loc], x, y); |
| 858 | #endif |
| 859 | state->nums[loc] = 0; |
| 860 | } |
| 861 | } else { /* remove flag */ |
| 862 | if (!(state->flags[loc] & gtthan[which].f)) return 0; |
| 863 | if (!checkonly) { |
| 864 | #ifdef STANDALONE_SOLVER |
| 865 | if (solver_show_working) |
| 866 | printf("gg_remove_clue: removing %c at (%d,%d)", |
| 867 | gtthan[which].c, x, y); |
| 868 | #endif |
| 869 | state->flags[loc] &= ~gtthan[which].f; |
| 870 | } |
| 871 | } |
| 872 | return 1; |
| 873 | } |
| 874 | |
| 875 | static int gg_best_clue(game_state *state, int *scratch, digit *latin) |
| 876 | { |
| 877 | int ls = state->order * state->order * 5; |
| 878 | int maxposs = 0, minclues = 5, best = -1, i, j; |
| 879 | int nposs, nclues, loc, x, y; |
| 880 | |
| 881 | #ifdef STANDALONE_SOLVER |
| 882 | if (solver_show_working) { |
| 883 | game_debug(state); |
| 884 | latin_solver_debug(state->hints, state->order); |
| 885 | } |
| 886 | #endif |
| 887 | |
| 888 | for (i = ls; i-- > 0 ;) { |
| 889 | if (!gg_place_clue(state, scratch[i], latin, 1)) continue; |
| 890 | |
| 891 | loc = scratch[i] / 5; |
| 892 | x = loc % state->order; y = loc / state->order; |
| 893 | for (j = nposs = 0; j < state->order; j++) { |
| 894 | if (state->hints[loc*state->order + j]) nposs++; |
| 895 | } |
| 896 | for (j = nclues = 0; j < 4; j++) { |
| 897 | if (state->flags[loc] & gtthan[j].f) nclues++; |
| 898 | } |
| 899 | if ((nposs > maxposs) || |
| 900 | (nposs == maxposs && nclues < minclues)) { |
| 901 | best = i; maxposs = nposs; minclues = nclues; |
| 902 | #ifdef STANDALONE_SOLVER |
| 903 | if (solver_show_working) |
| 904 | printf("gg_best_clue: b%d (%d,%d) new best [%d poss, %d clues].\n", |
| 905 | best, x, y, nposs, nclues); |
| 906 | #endif |
| 907 | } |
| 908 | } |
| 909 | /* if we didn't solve, we must have 1 clue to place! */ |
| 910 | assert(best != -1); |
| 911 | return best; |
| 912 | } |
| 913 | |
| 914 | #ifdef STANDALONE_SOLVER |
| 915 | int maxtries; |
| 916 | #define MAXTRIES maxtries |
| 917 | #else |
| 918 | #define MAXTRIES 50 |
| 919 | #endif |
| 920 | int gg_solved; |
| 921 | |
| 922 | static int game_assemble(game_state *new, int *scratch, digit *latin, |
| 923 | int difficulty) |
| 924 | { |
| 925 | game_state *copy = dup_game(new); |
| 926 | int best; |
| 927 | |
| 928 | if (difficulty >= DIFF_RECURSIVE) { |
| 929 | /* We mustn't use any solver that might guess answers; |
| 930 | * if it guesses wrongly but solves, gg_place_clue will |
| 931 | * get mighty confused. We will always trim clues down |
| 932 | * (making it more difficult) in game_strip, which doesn't |
| 933 | * have this problem. */ |
| 934 | difficulty = DIFF_RECURSIVE-1; |
| 935 | } |
| 936 | |
| 937 | #ifdef STANDALONE_SOLVER |
| 938 | if (solver_show_working) { |
| 939 | game_debug(new); |
| 940 | latin_solver_debug(new->hints, new->order); |
| 941 | } |
| 942 | #endif |
| 943 | |
| 944 | while(1) { |
| 945 | gg_solved++; |
| 946 | if (solver_state(copy, difficulty) == 1) break; |
| 947 | |
| 948 | best = gg_best_clue(copy, scratch, latin); |
| 949 | gg_place_clue(new, scratch[best], latin, 0); |
| 950 | gg_place_clue(copy, scratch[best], latin, 0); |
| 951 | } |
| 952 | free_game(copy); |
| 953 | #ifdef STANDALONE_SOLVER |
| 954 | if (solver_show_working) { |
| 955 | char *dbg = game_text_format(new); |
| 956 | printf("game_assemble: done, %d solver iterations:\n%s\n", gg_solved, dbg); |
| 957 | sfree(dbg); |
| 958 | } |
| 959 | #endif |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static void game_strip(game_state *new, int *scratch, digit *latin, |
| 964 | int difficulty) |
| 965 | { |
| 966 | int o = new->order, o2 = o*o, lscratch = o2*5, i; |
| 967 | game_state *copy = blank_game(new->order); |
| 968 | |
| 969 | /* For each symbol (if it exists in new), try and remove it and |
| 970 | * solve again; if we couldn't solve without it put it back. */ |
| 971 | for (i = 0; i < lscratch; i++) { |
| 972 | if (!gg_remove_clue(new, scratch[i], 0)) continue; |
| 973 | |
| 974 | memcpy(copy->nums, new->nums, o2 * sizeof(digit)); |
| 975 | memcpy(copy->flags, new->flags, o2 * sizeof(unsigned int)); |
| 976 | gg_solved++; |
| 977 | if (solver_state(copy, difficulty) != 1) { |
| 978 | /* put clue back, we can't solve without it. */ |
| 979 | int ret = gg_place_clue(new, scratch[i], latin, 0); |
| 980 | assert(ret == 1); |
| 981 | } else { |
| 982 | #ifdef STANDALONE_SOLVER |
| 983 | if (solver_show_working) |
| 984 | printf("game_strip: clue was redundant."); |
| 985 | #endif |
| 986 | } |
| 987 | } |
| 988 | free_game(copy); |
| 989 | #ifdef STANDALONE_SOLVER |
| 990 | if (solver_show_working) { |
| 991 | char *dbg = game_text_format(new); |
| 992 | debug(("game_strip: done, %d solver iterations.", gg_solved)); |
| 993 | debug(("%s", dbg)); |
| 994 | sfree(dbg); |
| 995 | } |
| 996 | #endif |
| 997 | } |
| 998 | |
| 999 | static char *new_game_desc(game_params *params, random_state *rs, |
| 1000 | char **aux, int interactive) |
| 1001 | { |
| 1002 | digit *sq = NULL; |
| 1003 | int i, x, y, retlen, k, nsol; |
| 1004 | int o2 = params->order * params->order, ntries = 1; |
| 1005 | int *scratch, lscratch = o2*5; |
| 1006 | char *ret, buf[80]; |
| 1007 | game_state *state = blank_game(params->order); |
| 1008 | |
| 1009 | /* Generate a list of 'things to strip' (randomised later) */ |
| 1010 | scratch = snewn(lscratch, int); |
| 1011 | /* Put the numbers (4 mod 5) before the inequalities (0-3 mod 5) */ |
| 1012 | for (i = 0; i < lscratch; i++) scratch[i] = (i%o2)*5 + 4 - (i/o2); |
| 1013 | |
| 1014 | generate: |
| 1015 | #ifdef STANDALONE_SOLVER |
| 1016 | if (solver_show_working) |
| 1017 | printf("new_game_desc: generating %s puzzle, ntries so far %d", |
| 1018 | unequal_diffnames[params->diff], ntries); |
| 1019 | #endif |
| 1020 | if (sq) sfree(sq); |
| 1021 | sq = latin_generate(params->order, rs); |
| 1022 | latin_debug(sq, params->order); |
| 1023 | /* Separately shuffle the numeric and inequality clues */ |
| 1024 | shuffle(scratch, lscratch/5, sizeof(int), rs); |
| 1025 | shuffle(scratch+lscratch/5, 4*lscratch/5, sizeof(int), rs); |
| 1026 | |
| 1027 | memset(state->nums, 0, o2 * sizeof(digit)); |
| 1028 | memset(state->flags, 0, o2 * sizeof(unsigned int)); |
| 1029 | |
| 1030 | gg_solved = 0; |
| 1031 | if (game_assemble(state, scratch, sq, params->diff) < 0) |
| 1032 | goto generate; |
| 1033 | game_strip(state, scratch, sq, params->diff); |
| 1034 | |
| 1035 | if (params->diff > 0) { |
| 1036 | game_state *copy = dup_game(state); |
| 1037 | nsol = solver_state(copy, params->diff-1); |
| 1038 | free_game(copy); |
| 1039 | if (nsol > 0) { |
| 1040 | #ifdef STANDALONE_SOLVER |
| 1041 | if (solver_show_working) |
| 1042 | printf("game_assemble: puzzle as generated is too easy.\n"); |
| 1043 | #endif |
| 1044 | if (ntries < MAXTRIES) { |
| 1045 | ntries++; |
| 1046 | goto generate; |
| 1047 | } |
| 1048 | #ifdef STANDALONE_SOLVER |
| 1049 | if (solver_show_working) |
| 1050 | printf("Unable to generate %s %dx%d after %d attempts.\n", |
| 1051 | unequal_diffnames[params->diff], |
| 1052 | params->order, params->order, MAXTRIES); |
| 1053 | #endif |
| 1054 | params->diff--; |
| 1055 | } |
| 1056 | } |
| 1057 | #ifdef STANDALONE_SOLVER |
| 1058 | if (solver_show_working) |
| 1059 | printf("new_game_desc: generated %s puzzle; %d attempts (%d solver).\n", |
| 1060 | unequal_diffnames[params->diff], ntries, gg_solved); |
| 1061 | #endif |
| 1062 | |
| 1063 | ret = NULL; retlen = 0; |
| 1064 | for (y = 0; y < params->order; y++) { |
| 1065 | for (x = 0; x < params->order; x++) { |
| 1066 | unsigned int f = GRID(state, flags, x, y); |
| 1067 | k = sprintf(buf, "%d%s%s%s%s,", |
| 1068 | GRID(state, nums, x, y), |
| 1069 | (f & F_GT_UP) ? "U" : "", |
| 1070 | (f & F_GT_RIGHT) ? "R" : "", |
| 1071 | (f & F_GT_DOWN) ? "D" : "", |
| 1072 | (f & F_GT_LEFT) ? "L" : ""); |
| 1073 | |
| 1074 | ret = sresize(ret, retlen + k + 1, char); |
| 1075 | strcpy(ret + retlen, buf); |
| 1076 | retlen += k; |
| 1077 | } |
| 1078 | } |
| 1079 | *aux = latin_desc(sq, params->order); |
| 1080 | |
| 1081 | free_game(state); |
| 1082 | sfree(sq); |
| 1083 | sfree(scratch); |
| 1084 | |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | static game_state *load_game(game_params *params, char *desc, |
| 1089 | char **why_r) |
| 1090 | { |
| 1091 | game_state *state = blank_game(params->order); |
| 1092 | char *p = desc; |
| 1093 | int i = 0, n, o = params->order, x, y; |
| 1094 | char *why = NULL; |
| 1095 | |
| 1096 | while (*p) { |
| 1097 | while (*p >= 'a' && *p <= 'z') { |
| 1098 | i += *p - 'a' + 1; |
| 1099 | p++; |
| 1100 | } |
| 1101 | if (i >= o*o) { |
| 1102 | why = "Too much data to fill grid"; goto fail; |
| 1103 | } |
| 1104 | |
| 1105 | if (*p < '0' && *p > '9') { |
| 1106 | why = "Expecting number in game description"; goto fail; |
| 1107 | } |
| 1108 | n = atoi(p); |
| 1109 | if (n < 0 || n > o) { |
| 1110 | why = "Out-of-range number in game description"; goto fail; |
| 1111 | } |
| 1112 | state->nums[i] = (digit)n; |
| 1113 | while (*p >= '0' && *p <= '9') p++; /* skip number */ |
| 1114 | |
| 1115 | if (state->nums[i] != 0) |
| 1116 | state->flags[i] |= F_IMMUTABLE; /* === number set by game description */ |
| 1117 | |
| 1118 | while (*p == 'U' || *p == 'R' || *p == 'D' || *p == 'L') { |
| 1119 | switch (*p) { |
| 1120 | case 'U': state->flags[i] |= F_GT_UP; break; |
| 1121 | case 'R': state->flags[i] |= F_GT_RIGHT; break; |
| 1122 | case 'D': state->flags[i] |= F_GT_DOWN; break; |
| 1123 | case 'L': state->flags[i] |= F_GT_LEFT; break; |
| 1124 | default: why = "Expecting flag URDL in game description"; goto fail; |
| 1125 | } |
| 1126 | p++; |
| 1127 | } |
| 1128 | i++; |
| 1129 | if (i < o*o && *p != ',') { |
| 1130 | why = "Missing separator"; goto fail; |
| 1131 | } |
| 1132 | if (*p == ',') p++; |
| 1133 | } |
| 1134 | if (i < o*o) { |
| 1135 | why = "Not enough data to fill grid"; goto fail; |
| 1136 | } |
| 1137 | i = 0; |
| 1138 | for (y = 0; y < o; y++) { |
| 1139 | for (x = 0; x < o; x++) { |
| 1140 | for (n = 0; n < 4; n++) { |
| 1141 | if (GRID(state, flags, x, y) & gtthan[n].f) { |
| 1142 | int nx = x + gtthan[n].dx; |
| 1143 | int ny = y + gtthan[n].dy; |
| 1144 | /* a flag must not point us off the grid. */ |
| 1145 | if (nx < 0 || ny < 0 || nx >= o || ny >= o) { |
| 1146 | why = "Flags go off grid"; goto fail; |
| 1147 | } |
| 1148 | /* if one cell is GT another, the other must not also |
| 1149 | * be GT the first. */ |
| 1150 | if (GRID(state, flags, nx, ny) & gtthan[n].fo) { |
| 1151 | why = "Flags contradicting each other"; goto fail; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | return state; |
| 1159 | |
| 1160 | fail: |
| 1161 | free_game(state); |
| 1162 | if (why_r) *why_r = why; |
| 1163 | return NULL; |
| 1164 | } |
| 1165 | |
| 1166 | static game_state *new_game(midend *me, game_params *params, char *desc) |
| 1167 | { |
| 1168 | game_state *state = load_game(params, desc, NULL); |
| 1169 | if (!state) { |
| 1170 | assert("Unable to load ?validated game."); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | return state; |
| 1174 | } |
| 1175 | |
| 1176 | static char *validate_desc(game_params *params, char *desc) |
| 1177 | { |
| 1178 | char *why = NULL; |
| 1179 | game_state *dummy = load_game(params, desc, &why); |
| 1180 | if (dummy) { |
| 1181 | free_game(dummy); |
| 1182 | assert(!why); |
| 1183 | } else |
| 1184 | assert(why); |
| 1185 | return why; |
| 1186 | } |
| 1187 | |
| 1188 | static char *solve_game(game_state *state, game_state *currstate, |
| 1189 | char *aux, char **error) |
| 1190 | { |
| 1191 | game_state *solved; |
| 1192 | int r; |
| 1193 | char *ret = NULL; |
| 1194 | |
| 1195 | if (aux) return dupstr(aux); |
| 1196 | |
| 1197 | solved = dup_game(state); |
| 1198 | for (r = 0; r < state->order*state->order; r++) { |
| 1199 | if (!(solved->flags[r] & F_IMMUTABLE)) |
| 1200 | solved->nums[r] = 0; |
| 1201 | } |
| 1202 | r = solver_state(solved, DIFFCOUNT); |
| 1203 | if (r > 0) ret = latin_desc(solved->nums, solved->order); |
| 1204 | free_game(solved); |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | /* ---------------------------------------------------------- |
| 1209 | * Game UI input processing. |
| 1210 | */ |
| 1211 | |
| 1212 | struct game_ui { |
| 1213 | int hx, hy; /* as for solo.c, highlight pos */ |
| 1214 | int hshow, hpencil, hcursor; /* show state, type, and ?cursor. */ |
| 1215 | }; |
| 1216 | |
| 1217 | static game_ui *new_ui(game_state *state) |
| 1218 | { |
| 1219 | game_ui *ui = snew(game_ui); |
| 1220 | |
| 1221 | ui->hx = ui->hy = 0; |
| 1222 | ui->hpencil = ui->hshow = ui->hcursor = 0; |
| 1223 | |
| 1224 | return ui; |
| 1225 | } |
| 1226 | |
| 1227 | static void free_ui(game_ui *ui) |
| 1228 | { |
| 1229 | sfree(ui); |
| 1230 | } |
| 1231 | |
| 1232 | static char *encode_ui(game_ui *ui) |
| 1233 | { |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | |
| 1237 | static void decode_ui(game_ui *ui, char *encoding) |
| 1238 | { |
| 1239 | } |
| 1240 | |
| 1241 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
| 1242 | game_state *newstate) |
| 1243 | { |
| 1244 | /* See solo.c; if we were pencil-mode highlighting and |
| 1245 | * somehow a square has just been properly filled, cancel |
| 1246 | * pencil mode. */ |
| 1247 | if (ui->hshow && ui->hpencil && !ui->hcursor && |
| 1248 | GRID(newstate, nums, ui->hx, ui->hy) != 0) { |
| 1249 | ui->hshow = 0; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | struct game_drawstate { |
| 1254 | int tilesize, order, started; |
| 1255 | digit *nums; /* copy of nums, o^2 */ |
| 1256 | unsigned char *hints; /* copy of hints, o^3 */ |
| 1257 | unsigned int *flags; /* o^2 */ |
| 1258 | |
| 1259 | int hx, hy, hshow, hpencil; /* as for game_ui. */ |
| 1260 | int hflash; |
| 1261 | }; |
| 1262 | |
| 1263 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
| 1264 | int ox, int oy, int button) |
| 1265 | { |
| 1266 | int x = FROMCOORD(ox), y = FROMCOORD(oy), n; |
| 1267 | char buf[80]; |
| 1268 | |
| 1269 | button &= ~MOD_MASK; |
| 1270 | |
| 1271 | if (x >= 0 && x < ds->order && ((ox - COORD(x)) <= TILE_SIZE) && |
| 1272 | y >= 0 && y < ds->order && ((oy - COORD(y)) <= TILE_SIZE)) { |
| 1273 | if (button == LEFT_BUTTON) { |
| 1274 | /* normal highlighting for non-immutable squares */ |
| 1275 | if (GRID(state, flags, x, y) & F_IMMUTABLE) |
| 1276 | ui->hshow = 0; |
| 1277 | else if (x == ui->hx && y == ui->hy && |
| 1278 | ui->hshow && ui->hpencil == 0) |
| 1279 | ui->hshow = 0; |
| 1280 | else { |
| 1281 | ui->hx = x; ui->hy = y; ui->hpencil = 0; |
| 1282 | ui->hshow = 1; |
| 1283 | } |
| 1284 | ui->hcursor = 0; |
| 1285 | return ""; |
| 1286 | } |
| 1287 | if (button == RIGHT_BUTTON) { |
| 1288 | /* pencil highlighting for non-filled squares */ |
| 1289 | if (GRID(state, nums, x, y) != 0) |
| 1290 | ui->hshow = 0; |
| 1291 | else if (x == ui->hx && y == ui->hy && |
| 1292 | ui->hshow && ui->hpencil) |
| 1293 | ui->hshow = 0; |
| 1294 | else { |
| 1295 | ui->hx = x; ui->hy = y; ui->hpencil = 1; |
| 1296 | ui->hshow = 1; |
| 1297 | } |
| 1298 | ui->hcursor = 0; |
| 1299 | return ""; |
| 1300 | } |
| 1301 | } |
| 1302 | if (button == 'H' || button == 'h') |
| 1303 | return dupstr("H"); |
| 1304 | if (button == 'M' || button == 'm') |
| 1305 | return dupstr("M"); |
| 1306 | |
| 1307 | if (IS_CURSOR_MOVE(button)) { |
| 1308 | move_cursor(button, &ui->hx, &ui->hy, ds->order, ds->order, 0); |
| 1309 | ui->hshow = ui->hcursor = 1; |
| 1310 | return ""; |
| 1311 | } |
| 1312 | if (ui->hshow && IS_CURSOR_SELECT(button)) { |
| 1313 | ui->hpencil = 1 - ui->hpencil; |
| 1314 | ui->hcursor = 1; |
| 1315 | return ""; |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | if (ui->hshow) { |
| 1320 | debug(("button %d, cbutton %d", button, (int)((char)button))); |
| 1321 | n = c2n(button, state->order); |
| 1322 | |
| 1323 | debug(("n %d, h (%d,%d) p %d flags 0x%x nums %d", |
| 1324 | n, ui->hx, ui->hy, ui->hpencil, |
| 1325 | GRID(state, flags, ui->hx, ui->hy), |
| 1326 | GRID(state, nums, ui->hx, ui->hy))); |
| 1327 | |
| 1328 | if (n < 0 || n > ds->order) |
| 1329 | return NULL; /* out of range */ |
| 1330 | if (GRID(state, flags, ui->hx, ui->hy) & F_IMMUTABLE) |
| 1331 | return NULL; /* can't edit immutable square (!) */ |
| 1332 | if (ui->hpencil && GRID(state, nums, ui->hx, ui->hy) > 0) |
| 1333 | return NULL; /* can't change hints on filled square (!) */ |
| 1334 | |
| 1335 | |
| 1336 | sprintf(buf, "%c%d,%d,%d", |
| 1337 | (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n); |
| 1338 | |
| 1339 | if (!ui->hcursor) ui->hshow = 0; |
| 1340 | |
| 1341 | return dupstr(buf); |
| 1342 | } |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | |
| 1346 | static game_state *execute_move(game_state *state, char *move) |
| 1347 | { |
| 1348 | game_state *ret = NULL; |
| 1349 | int x, y, n, i, rc; |
| 1350 | |
| 1351 | debug(("execute_move: %s", move)); |
| 1352 | |
| 1353 | if ((move[0] == 'P' || move[0] == 'R') && |
| 1354 | sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 && |
| 1355 | x >= 0 && x < state->order && y >= 0 && y < state->order && |
| 1356 | n >= 0 && n <= state->order) { |
| 1357 | ret = dup_game(state); |
| 1358 | if (move[0] == 'P' && n > 0) |
| 1359 | HINT(ret, x, y, n-1) = !HINT(ret, x, y, n-1); |
| 1360 | else { |
| 1361 | GRID(ret, nums, x, y) = n; |
| 1362 | for (i = 0; i < state->order; i++) |
| 1363 | HINT(ret, x, y, i) = 0; |
| 1364 | |
| 1365 | /* real change to grid; check for completion */ |
| 1366 | if (!ret->completed && check_complete(ret->nums, ret, 1) > 0) |
| 1367 | ret->completed = TRUE; |
| 1368 | } |
| 1369 | return ret; |
| 1370 | } else if (move[0] == 'S') { |
| 1371 | char *p; |
| 1372 | |
| 1373 | ret = dup_game(state); |
| 1374 | ret->completed = ret->cheated = TRUE; |
| 1375 | |
| 1376 | p = move+1; |
| 1377 | for (i = 0; i < state->order*state->order; i++) { |
| 1378 | n = c2n((int)*p, state->order); |
| 1379 | if (!*p || n <= 0 || n > state->order) |
| 1380 | goto badmove; |
| 1381 | ret->nums[i] = n; |
| 1382 | p++; |
| 1383 | } |
| 1384 | if (*p) goto badmove; |
| 1385 | rc = check_complete(ret->nums, ret, 1); |
| 1386 | assert(rc > 0); |
| 1387 | return ret; |
| 1388 | } else if (move[0] == 'M') { |
| 1389 | ret = dup_game(state); |
| 1390 | for (x = 0; x < state->order; x++) { |
| 1391 | for (y = 0; y < state->order; y++) { |
| 1392 | for (n = 0; n < state->order; n++) { |
| 1393 | HINT(ret, x, y, n) = 1; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | return ret; |
| 1398 | } else if (move[0] == 'H') { |
| 1399 | return solver_hint(state, NULL, DIFF_EASY, DIFF_EASY); |
| 1400 | } |
| 1401 | |
| 1402 | badmove: |
| 1403 | if (ret) free_game(ret); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
| 1407 | /* ---------------------------------------------------------------------- |
| 1408 | * Drawing/printing routines. |
| 1409 | */ |
| 1410 | |
| 1411 | #define DRAW_SIZE (TILE_SIZE*ds->order + GAP_SIZE*(ds->order-1) + BORDER*2) |
| 1412 | |
| 1413 | static void game_compute_size(game_params *params, int tilesize, |
| 1414 | int *x, int *y) |
| 1415 | { |
| 1416 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
| 1417 | struct { int tilesize, order; } ads, *ds = &ads; |
| 1418 | ads.tilesize = tilesize; |
| 1419 | ads.order = params->order; |
| 1420 | |
| 1421 | *x = *y = DRAW_SIZE; |
| 1422 | } |
| 1423 | |
| 1424 | static void game_set_size(drawing *dr, game_drawstate *ds, |
| 1425 | game_params *params, int tilesize) |
| 1426 | { |
| 1427 | ds->tilesize = tilesize; |
| 1428 | } |
| 1429 | |
| 1430 | static float *game_colours(frontend *fe, int *ncolours) |
| 1431 | { |
| 1432 | float *ret = snewn(3 * NCOLOURS, float); |
| 1433 | int i; |
| 1434 | |
| 1435 | game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); |
| 1436 | |
| 1437 | for (i = 0; i < 3; i++) { |
| 1438 | ret[COL_TEXT * 3 + i] = 0.0F; |
| 1439 | ret[COL_GRID * 3 + i] = 0.5F; |
| 1440 | } |
| 1441 | |
| 1442 | /* Lots of these were taken from solo.c. */ |
| 1443 | ret[COL_GUESS * 3 + 0] = 0.0F; |
| 1444 | ret[COL_GUESS * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1]; |
| 1445 | ret[COL_GUESS * 3 + 2] = 0.0F; |
| 1446 | |
| 1447 | ret[COL_ERROR * 3 + 0] = 1.0F; |
| 1448 | ret[COL_ERROR * 3 + 1] = 0.0F; |
| 1449 | ret[COL_ERROR * 3 + 2] = 0.0F; |
| 1450 | |
| 1451 | ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
| 1452 | ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
| 1453 | ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2]; |
| 1454 | |
| 1455 | *ncolours = NCOLOURS; |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
| 1460 | { |
| 1461 | struct game_drawstate *ds = snew(struct game_drawstate); |
| 1462 | int o2 = state->order*state->order, o3 = o2*state->order; |
| 1463 | |
| 1464 | ds->tilesize = 0; |
| 1465 | ds->order = state->order; |
| 1466 | |
| 1467 | ds->nums = snewn(o2, digit); |
| 1468 | ds->hints = snewn(o3, unsigned char); |
| 1469 | ds->flags = snewn(o2, unsigned int); |
| 1470 | memset(ds->nums, 0, o2*sizeof(digit)); |
| 1471 | memset(ds->hints, 0, o3); |
| 1472 | memset(ds->flags, 0, o2*sizeof(unsigned int)); |
| 1473 | |
| 1474 | ds->hx = ds->hy = 0; |
| 1475 | ds->started = ds->hshow = ds->hpencil = ds->hflash = 0; |
| 1476 | |
| 1477 | return ds; |
| 1478 | } |
| 1479 | |
| 1480 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
| 1481 | { |
| 1482 | sfree(ds->nums); |
| 1483 | sfree(ds->hints); |
| 1484 | sfree(ds->flags); |
| 1485 | sfree(ds); |
| 1486 | } |
| 1487 | |
| 1488 | static void draw_gt(drawing *dr, int ox, int oy, |
| 1489 | int dx1, int dy1, int dx2, int dy2, int col) |
| 1490 | { |
| 1491 | draw_line(dr, ox, oy, ox+dx1, oy+dy1, col); |
| 1492 | draw_line(dr, ox+dx1, oy+dy1, ox+dx1+dx2, oy+dy1+dy2, col); |
| 1493 | } |
| 1494 | |
| 1495 | static void draw_gts(drawing *dr, game_drawstate *ds, int ox, int oy, |
| 1496 | unsigned int f, int col, int needsupdate) |
| 1497 | { |
| 1498 | int g = GAP_SIZE, g2 = (g+1)/2, g4 = (g+1)/4; |
| 1499 | |
| 1500 | if (f & F_GT_UP) { |
| 1501 | draw_gt(dr, ox+g2, oy-g4, g2, -g2, g2, g2, |
| 1502 | (f & F_ERROR_UP) ? COL_ERROR : col); |
| 1503 | if (needsupdate) draw_update(dr, ox, oy-g, TILE_SIZE, g); |
| 1504 | } |
| 1505 | if (f & F_GT_RIGHT) { |
| 1506 | draw_gt(dr, ox+TILE_SIZE+g4, oy+g2, g2, g2, -g2, g2, |
| 1507 | (f & F_ERROR_RIGHT) ? COL_ERROR : col); |
| 1508 | if (needsupdate) draw_update(dr, ox+TILE_SIZE, oy, g, TILE_SIZE); |
| 1509 | } |
| 1510 | if (f & F_GT_DOWN) { |
| 1511 | draw_gt(dr, ox+g2, oy+TILE_SIZE+g4, g2, g2, g2, -g2, |
| 1512 | (f & F_ERROR_DOWN) ? COL_ERROR : col); |
| 1513 | if (needsupdate) draw_update(dr, ox, oy+TILE_SIZE, TILE_SIZE, g); |
| 1514 | } |
| 1515 | if (f & F_GT_LEFT) { |
| 1516 | draw_gt(dr, ox-g4, oy+g2, -g2, g2, g2, g2, |
| 1517 | (f & F_ERROR_LEFT) ? COL_ERROR : col); |
| 1518 | if (needsupdate) draw_update(dr, ox-g, oy, g, TILE_SIZE); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | static void draw_furniture(drawing *dr, game_drawstate *ds, game_state *state, |
| 1523 | game_ui *ui, int x, int y, int hflash) |
| 1524 | { |
| 1525 | int ox = COORD(x), oy = COORD(y), bg, hon; |
| 1526 | unsigned int f = GRID(state, flags, x, y); |
| 1527 | |
| 1528 | bg = hflash ? COL_HIGHLIGHT : COL_BACKGROUND; |
| 1529 | |
| 1530 | hon = (ui->hshow && x == ui->hx && y == ui->hy); |
| 1531 | |
| 1532 | /* Clear square. */ |
| 1533 | draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, |
| 1534 | (hon && !ui->hpencil) ? COL_HIGHLIGHT : bg); |
| 1535 | |
| 1536 | /* Draw the highlight (pencil or full), if we're the highlight */ |
| 1537 | if (hon && ui->hpencil) { |
| 1538 | int coords[6]; |
| 1539 | coords[0] = ox; |
| 1540 | coords[1] = oy; |
| 1541 | coords[2] = ox + TILE_SIZE/2; |
| 1542 | coords[3] = oy; |
| 1543 | coords[4] = ox; |
| 1544 | coords[5] = oy + TILE_SIZE/2; |
| 1545 | draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
| 1546 | } |
| 1547 | |
| 1548 | /* Draw the square outline (which is the cursor, if we're the cursor). */ |
| 1549 | draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID); |
| 1550 | |
| 1551 | draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE); |
| 1552 | |
| 1553 | /* Draw the GT signs. */ |
| 1554 | draw_gts(dr, ds, ox, oy, f, COL_TEXT, 1); |
| 1555 | } |
| 1556 | |
| 1557 | static void draw_num(drawing *dr, game_drawstate *ds, int x, int y) |
| 1558 | { |
| 1559 | int ox = COORD(x), oy = COORD(y); |
| 1560 | unsigned int f = GRID(ds,flags,x,y); |
| 1561 | char str[2]; |
| 1562 | |
| 1563 | /* (can assume square has just been cleared) */ |
| 1564 | |
| 1565 | /* Draw number, choosing appropriate colour */ |
| 1566 | str[0] = n2c(GRID(ds, nums, x, y), ds->order); |
| 1567 | str[1] = '\0'; |
| 1568 | draw_text(dr, ox + TILE_SIZE/2, oy + TILE_SIZE/2, |
| 1569 | FONT_VARIABLE, 3*TILE_SIZE/4, ALIGN_VCENTRE | ALIGN_HCENTRE, |
| 1570 | (f & F_IMMUTABLE) ? COL_TEXT : (f & F_ERROR) ? COL_ERROR : COL_GUESS, str); |
| 1571 | } |
| 1572 | |
| 1573 | static void draw_hints(drawing *dr, game_drawstate *ds, int x, int y) |
| 1574 | { |
| 1575 | int ox = COORD(x), oy = COORD(y); |
| 1576 | int nhints, i, j, hw, hh, hmax, fontsz; |
| 1577 | char str[2]; |
| 1578 | |
| 1579 | /* (can assume square has just been cleared) */ |
| 1580 | |
| 1581 | /* Draw hints; steal ingenious algorithm (basically) |
| 1582 | * from solo.c:draw_number() */ |
| 1583 | for (i = nhints = 0; i < ds->order; i++) { |
| 1584 | if (HINT(ds, x, y, i)) nhints++; |
| 1585 | } |
| 1586 | |
| 1587 | for (hw = 1; hw * hw < nhints; hw++); |
| 1588 | if (hw < 3) hw = 3; |
| 1589 | hh = (nhints + hw - 1) / hw; |
| 1590 | if (hh < 2) hh = 2; |
| 1591 | hmax = max(hw, hh); |
| 1592 | fontsz = TILE_SIZE/(hmax*(11-hmax)/8); |
| 1593 | |
| 1594 | for (i = j = 0; i < ds->order; i++) { |
| 1595 | if (HINT(ds,x,y,i)) { |
| 1596 | int hx = j % hw, hy = j / hw; |
| 1597 | |
| 1598 | str[0] = n2c(i+1, ds->order); |
| 1599 | str[1] = '\0'; |
| 1600 | draw_text(dr, |
| 1601 | ox + (4*hx+3) * TILE_SIZE / (4*hw+2), |
| 1602 | oy + (4*hy+3) * TILE_SIZE / (4*hh+2), |
| 1603 | FONT_VARIABLE, fontsz, |
| 1604 | ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str); |
| 1605 | j++; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
| 1611 | game_state *state, int dir, game_ui *ui, |
| 1612 | float animtime, float flashtime) |
| 1613 | { |
| 1614 | int x, y, i, hchanged = 0, stale, hflash = 0; |
| 1615 | |
| 1616 | debug(("highlight old (%d,%d), new (%d,%d)", ds->hx, ds->hy, ui->hx, ui->hy)); |
| 1617 | |
| 1618 | if (flashtime > 0 && |
| 1619 | (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3)) |
| 1620 | hflash = 1; |
| 1621 | |
| 1622 | if (!ds->started) { |
| 1623 | draw_rect(dr, 0, 0, DRAW_SIZE, DRAW_SIZE, COL_BACKGROUND); |
| 1624 | draw_update(dr, 0, 0, DRAW_SIZE, DRAW_SIZE); |
| 1625 | } |
| 1626 | if (ds->hx != ui->hx || ds->hy != ui->hy || |
| 1627 | ds->hshow != ui->hshow || ds->hpencil != ui->hpencil) |
| 1628 | hchanged = 1; |
| 1629 | |
| 1630 | for (x = 0; x < ds->order; x++) { |
| 1631 | for (y = 0; y < ds->order; y++) { |
| 1632 | if (!ds->started) |
| 1633 | stale = 1; |
| 1634 | else if (hflash != ds->hflash) |
| 1635 | stale = 1; |
| 1636 | else |
| 1637 | stale = 0; |
| 1638 | |
| 1639 | if (hchanged) { |
| 1640 | if ((x == ui->hx && y == ui->hy) || |
| 1641 | (x == ds->hx && y == ds->hy)) |
| 1642 | stale = 1; |
| 1643 | } |
| 1644 | |
| 1645 | if (GRID(state, nums, x, y) != GRID(ds, nums, x, y)) { |
| 1646 | GRID(ds, nums, x, y) = GRID(state, nums, x, y); |
| 1647 | stale = 1; |
| 1648 | } |
| 1649 | if (GRID(state, flags, x, y) != GRID(ds, flags, x, y)) { |
| 1650 | GRID(ds, flags, x, y) = GRID(state, flags, x, y); |
| 1651 | stale = 1; |
| 1652 | } |
| 1653 | if (GRID(ds, nums, x, y) == 0) { |
| 1654 | /* We're not a number square (therefore we might |
| 1655 | * display hints); do we need to update? */ |
| 1656 | for (i = 0; i < ds->order; i++) { |
| 1657 | if (HINT(state, x, y, i) != HINT(ds, x, y, i)) { |
| 1658 | HINT(ds, x, y, i) = HINT(state, x, y, i); |
| 1659 | stale = 1; |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | if (stale) { |
| 1664 | draw_furniture(dr, ds, state, ui, x, y, hflash); |
| 1665 | if (GRID(ds, nums, x, y) > 0) |
| 1666 | draw_num(dr, ds, x, y); |
| 1667 | else |
| 1668 | draw_hints(dr, ds, x, y); |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | ds->hx = ui->hx; ds->hy = ui->hy; |
| 1673 | ds->hshow = ui->hshow; |
| 1674 | ds->hpencil = ui->hpencil; |
| 1675 | |
| 1676 | ds->started = 1; |
| 1677 | ds->hflash = hflash; |
| 1678 | } |
| 1679 | |
| 1680 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
| 1681 | int dir, game_ui *ui) |
| 1682 | { |
| 1683 | return 0.0F; |
| 1684 | } |
| 1685 | |
| 1686 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
| 1687 | int dir, game_ui *ui) |
| 1688 | { |
| 1689 | if (!oldstate->completed && newstate->completed && |
| 1690 | !oldstate->cheated && !newstate->cheated) |
| 1691 | return FLASH_TIME; |
| 1692 | return 0.0F; |
| 1693 | } |
| 1694 | |
| 1695 | static int game_timing_state(game_state *state, game_ui *ui) |
| 1696 | { |
| 1697 | return TRUE; |
| 1698 | } |
| 1699 | |
| 1700 | static void game_print_size(game_params *params, float *x, float *y) |
| 1701 | { |
| 1702 | int pw, ph; |
| 1703 | |
| 1704 | /* 10mm squares by default, roughly the same as Grauniad. */ |
| 1705 | game_compute_size(params, 1000, &pw, &ph); |
| 1706 | *x = pw / 100.0F; |
| 1707 | *y = ph / 100.0F; |
| 1708 | } |
| 1709 | |
| 1710 | static void game_print(drawing *dr, game_state *state, int tilesize) |
| 1711 | { |
| 1712 | int ink = print_mono_colour(dr, 0); |
| 1713 | int x, y, o = state->order, ox, oy, n; |
| 1714 | char str[2]; |
| 1715 | |
| 1716 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
| 1717 | game_drawstate ads, *ds = &ads; |
| 1718 | game_set_size(dr, ds, NULL, tilesize); |
| 1719 | |
| 1720 | print_line_width(dr, 2 * TILE_SIZE / 40); |
| 1721 | |
| 1722 | /* Squares, numbers, gt signs */ |
| 1723 | for (y = 0; y < o; y++) { |
| 1724 | for (x = 0; x < o; x++) { |
| 1725 | ox = COORD(x); oy = COORD(y); |
| 1726 | n = GRID(state, nums, x, y); |
| 1727 | |
| 1728 | draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, ink); |
| 1729 | |
| 1730 | str[0] = n ? n2c(n, state->order) : ' '; |
| 1731 | str[1] = '\0'; |
| 1732 | draw_text(dr, ox + TILE_SIZE/2, oy + TILE_SIZE/2, |
| 1733 | FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE, |
| 1734 | ink, str); |
| 1735 | |
| 1736 | draw_gts(dr, ds, ox, oy, GRID(state, flags, x, y), ink, 1); |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* ---------------------------------------------------------------------- |
| 1742 | * Housekeeping. |
| 1743 | */ |
| 1744 | |
| 1745 | #ifdef COMBINED |
| 1746 | #define thegame unequal |
| 1747 | #endif |
| 1748 | |
| 1749 | const struct game thegame = { |
| 1750 | "Unequal", "games.unequal", "unequal", |
| 1751 | default_params, |
| 1752 | game_fetch_preset, |
| 1753 | decode_params, |
| 1754 | encode_params, |
| 1755 | free_params, |
| 1756 | dup_params, |
| 1757 | TRUE, game_configure, custom_params, |
| 1758 | validate_params, |
| 1759 | new_game_desc, |
| 1760 | validate_desc, |
| 1761 | new_game, |
| 1762 | dup_game, |
| 1763 | free_game, |
| 1764 | TRUE, solve_game, |
| 1765 | TRUE, game_can_format_as_text_now, game_text_format, |
| 1766 | new_ui, |
| 1767 | free_ui, |
| 1768 | encode_ui, |
| 1769 | decode_ui, |
| 1770 | game_changed_state, |
| 1771 | interpret_move, |
| 1772 | execute_move, |
| 1773 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
| 1774 | game_colours, |
| 1775 | game_new_drawstate, |
| 1776 | game_free_drawstate, |
| 1777 | game_redraw, |
| 1778 | game_anim_length, |
| 1779 | game_flash_length, |
| 1780 | TRUE, FALSE, game_print_size, game_print, |
| 1781 | FALSE, /* wants_statusbar */ |
| 1782 | FALSE, game_timing_state, |
| 1783 | REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */ |
| 1784 | }; |
| 1785 | |
| 1786 | /* ---------------------------------------------------------------------- |
| 1787 | * Standalone solver. |
| 1788 | */ |
| 1789 | |
| 1790 | #ifdef STANDALONE_SOLVER |
| 1791 | |
| 1792 | #include <time.h> |
| 1793 | #include <stdarg.h> |
| 1794 | |
| 1795 | const char *quis = NULL; |
| 1796 | |
| 1797 | #if 0 /* currently unused */ |
| 1798 | |
| 1799 | static void debug_printf(char *fmt, ...) |
| 1800 | { |
| 1801 | char buf[4096]; |
| 1802 | va_list ap; |
| 1803 | |
| 1804 | va_start(ap, fmt); |
| 1805 | vsprintf(buf, fmt, ap); |
| 1806 | puts(buf); |
| 1807 | va_end(ap); |
| 1808 | } |
| 1809 | |
| 1810 | static void game_printf(game_state *state) |
| 1811 | { |
| 1812 | char *dbg = game_text_format(state); |
| 1813 | printf("%s", dbg); |
| 1814 | sfree(dbg); |
| 1815 | } |
| 1816 | |
| 1817 | static void game_printf_wide(game_state *state) |
| 1818 | { |
| 1819 | int x, y, i, n; |
| 1820 | |
| 1821 | for (y = 0; y < state->order; y++) { |
| 1822 | for (x = 0; x < state->order; x++) { |
| 1823 | n = GRID(state, nums, x, y); |
| 1824 | for (i = 0; i < state->order; i++) { |
| 1825 | if (n > 0) |
| 1826 | printf("%c", n2c(n, state->order)); |
| 1827 | else if (HINT(state, x, y, i)) |
| 1828 | printf("%c", n2c(i+1, state->order)); |
| 1829 | else |
| 1830 | printf("."); |
| 1831 | } |
| 1832 | printf(" "); |
| 1833 | } |
| 1834 | printf("\n"); |
| 1835 | } |
| 1836 | printf("\n"); |
| 1837 | } |
| 1838 | |
| 1839 | #endif |
| 1840 | |
| 1841 | static void pdiff(int diff) |
| 1842 | { |
| 1843 | if (diff == DIFF_IMPOSSIBLE) |
| 1844 | printf("Game is impossible.\n"); |
| 1845 | else if (diff == DIFF_UNFINISHED) |
| 1846 | printf("Game has incomplete.\n"); |
| 1847 | else if (diff == DIFF_AMBIGUOUS) |
| 1848 | printf("Game has multiple solutions.\n"); |
| 1849 | else |
| 1850 | printf("Game has difficulty %s.\n", unequal_diffnames[diff]); |
| 1851 | } |
| 1852 | |
| 1853 | static int solve(game_params *p, char *desc, int debug) |
| 1854 | { |
| 1855 | game_state *st = new_game(NULL, p, desc); |
| 1856 | int diff; |
| 1857 | |
| 1858 | solver_show_working = debug; |
| 1859 | game_debug(st); |
| 1860 | |
| 1861 | diff = solver_grid(st->nums, st->order, DIFF_RECURSIVE, (void*)st); |
| 1862 | if (debug) pdiff(diff); |
| 1863 | |
| 1864 | game_debug(st); |
| 1865 | free_game(st); |
| 1866 | return diff; |
| 1867 | } |
| 1868 | |
| 1869 | static void check(game_params *p) |
| 1870 | { |
| 1871 | char *msg = validate_params(p, 1); |
| 1872 | if (msg) { |
| 1873 | fprintf(stderr, "%s: %s", quis, msg); |
| 1874 | exit(1); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | static int gen(game_params *p, random_state *rs, int debug) |
| 1879 | { |
| 1880 | char *desc, *aux; |
| 1881 | int diff; |
| 1882 | |
| 1883 | check(p); |
| 1884 | |
| 1885 | solver_show_working = debug; |
| 1886 | desc = new_game_desc(p, rs, &aux, 0); |
| 1887 | diff = solve(p, desc, debug); |
| 1888 | sfree(aux); |
| 1889 | sfree(desc); |
| 1890 | |
| 1891 | return diff; |
| 1892 | } |
| 1893 | |
| 1894 | static void soak(game_params *p, random_state *rs) |
| 1895 | { |
| 1896 | time_t tt_start, tt_now, tt_last; |
| 1897 | char *aux, *desc; |
| 1898 | game_state *st; |
| 1899 | int n = 0, neasy = 0, realdiff = p->diff; |
| 1900 | |
| 1901 | check(p); |
| 1902 | |
| 1903 | solver_show_working = 0; |
| 1904 | maxtries = 1; |
| 1905 | |
| 1906 | tt_start = tt_now = time(NULL); |
| 1907 | |
| 1908 | printf("Soak-generating a %dx%d grid, difficulty %s.\n", |
| 1909 | p->order, p->order, unequal_diffnames[p->diff]); |
| 1910 | |
| 1911 | while (1) { |
| 1912 | p->diff = realdiff; |
| 1913 | desc = new_game_desc(p, rs, &aux, 0); |
| 1914 | st = new_game(NULL, p, desc); |
| 1915 | solver_state(st, DIFF_RECURSIVE); |
| 1916 | free_game(st); |
| 1917 | sfree(aux); |
| 1918 | sfree(desc); |
| 1919 | |
| 1920 | n++; |
| 1921 | if (realdiff != p->diff) neasy++; |
| 1922 | |
| 1923 | tt_last = time(NULL); |
| 1924 | if (tt_last > tt_now) { |
| 1925 | tt_now = tt_last; |
| 1926 | printf("%d total, %3.1f/s; %d/%2.1f%% easy, %3.1f/s good.\n", |
| 1927 | n, (double)n / ((double)tt_now - tt_start), |
| 1928 | neasy, (double)neasy*100.0/(double)n, |
| 1929 | (double)(n - neasy) / ((double)tt_now - tt_start)); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | static void usage_exit(const char *msg) |
| 1935 | { |
| 1936 | if (msg) |
| 1937 | fprintf(stderr, "%s: %s\n", quis, msg); |
| 1938 | fprintf(stderr, "Usage: %s [--seed SEED] --soak <params> | [game_id [game_id ...]]\n", quis); |
| 1939 | exit(1); |
| 1940 | } |
| 1941 | |
| 1942 | int main(int argc, const char *argv[]) |
| 1943 | { |
| 1944 | random_state *rs; |
| 1945 | time_t seed = time(NULL); |
| 1946 | int do_soak = 0, diff; |
| 1947 | |
| 1948 | game_params *p; |
| 1949 | |
| 1950 | maxtries = 50; |
| 1951 | |
| 1952 | quis = argv[0]; |
| 1953 | while (--argc > 0) { |
| 1954 | const char *p = *++argv; |
| 1955 | if (!strcmp(p, "--soak")) |
| 1956 | do_soak = 1; |
| 1957 | else if (!strcmp(p, "--seed")) { |
| 1958 | if (argc == 0) |
| 1959 | usage_exit("--seed needs an argument"); |
| 1960 | seed = (time_t)atoi(*++argv); |
| 1961 | argc--; |
| 1962 | } else if (*p == '-') |
| 1963 | usage_exit("unrecognised option"); |
| 1964 | else |
| 1965 | break; |
| 1966 | } |
| 1967 | rs = random_new((void*)&seed, sizeof(time_t)); |
| 1968 | |
| 1969 | if (do_soak == 1) { |
| 1970 | if (argc != 1) usage_exit("only one argument for --soak"); |
| 1971 | p = default_params(); |
| 1972 | decode_params(p, *argv); |
| 1973 | soak(p, rs); |
| 1974 | } else if (argc > 0) { |
| 1975 | int i; |
| 1976 | for (i = 0; i < argc; i++) { |
| 1977 | const char *id = *argv++; |
| 1978 | char *desc = strchr(id, ':'), *err; |
| 1979 | p = default_params(); |
| 1980 | if (desc) { |
| 1981 | *desc++ = '\0'; |
| 1982 | decode_params(p, id); |
| 1983 | err = validate_desc(p, desc); |
| 1984 | if (err) { |
| 1985 | fprintf(stderr, "%s: %s\n", quis, err); |
| 1986 | exit(1); |
| 1987 | } |
| 1988 | solve(p, desc, 1); |
| 1989 | } else { |
| 1990 | decode_params(p, id); |
| 1991 | diff = gen(p, rs, 1); |
| 1992 | } |
| 1993 | } |
| 1994 | } else { |
| 1995 | while(1) { |
| 1996 | p = default_params(); |
| 1997 | p->order = random_upto(rs, 7) + 3; |
| 1998 | p->diff = random_upto(rs, 4); |
| 1999 | diff = gen(p, rs, 0); |
| 2000 | pdiff(diff); |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | #endif |
| 2008 | |
| 2009 | /* vim: set shiftwidth=4 tabstop=8: */ |