A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 2375 lines 71 kB view raw
1/* 2 * pattern.c: the pattern-reconstruction game known as `nonograms'. 3 */ 4 5#include <stdio.h> 6#include <stdlib.h> 7#include <string.h> 8#include <assert.h> 9#include <ctype.h> 10#include <limits.h> 11#ifdef NO_TGMATH_H 12# include <math.h> 13#else 14# include <tgmath.h> 15#endif 16 17#include "puzzles.h" 18 19enum { 20 COL_BACKGROUND, 21 COL_EMPTY, 22 COL_FULL, 23 COL_TEXT, 24 COL_UNKNOWN, 25 COL_GRID, 26 COL_CURSOR, 27 COL_ERROR, 28 COL_CURSOR_GUIDE, 29 NCOLOURS 30}; 31 32#define PREFERRED_TILE_SIZE 24 33#define TILE_SIZE (ds->tilesize) 34#define BORDER (3 * TILE_SIZE / 4) 35#define TLBORDER(d) ( (d) / 5 + 2 ) 36#define GUTTER (TILE_SIZE / 2) 37 38#define FROMCOORD(d, x) \ 39 ( ((x) - (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) - 1))) \ 40 / TILE_SIZE - 1) 41 42#define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d))) 43#define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d))) 44 45#define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x))) 46 47struct game_params { 48 int w, h; 49}; 50 51#define GRID_UNKNOWN 2 52#define GRID_FULL 1 53#define GRID_EMPTY 0 54 55typedef struct game_state_common { 56 /* Parts of the game state that don't change during play. */ 57 int w, h; 58 int rowsize; 59 int *rowdata, *rowlen; 60 bool *immutable; 61 int refcount; 62 enum { FS_SMALL, FS_LARGE } fontsize; 63} game_state_common; 64 65struct game_state { 66 game_state_common *common; 67 unsigned char *grid; 68 bool completed, cheated; 69}; 70 71#define FLASH_TIME 0.13F 72 73static game_params *default_params(void) 74{ 75 game_params *ret = snew(game_params); 76 77 ret->w = ret->h = 15; 78 79 return ret; 80} 81 82static const struct game_params pattern_presets[] = { 83 {10, 10}, 84 {15, 15}, 85 {20, 20}, 86#ifndef SLOW_SYSTEM 87 {25, 25}, 88 {30, 30}, 89#endif 90}; 91 92static bool game_fetch_preset(int i, char **name, game_params **params) 93{ 94 game_params *ret; 95 char str[80]; 96 97 if (i < 0 || i >= lenof(pattern_presets)) 98 return false; 99 100 ret = snew(game_params); 101 *ret = pattern_presets[i]; 102 103 sprintf(str, "%dx%d", ret->w, ret->h); 104 105 *name = dupstr(str); 106 *params = ret; 107 return true; 108} 109 110static void free_params(game_params *params) 111{ 112 sfree(params); 113} 114 115static game_params *dup_params(const game_params *params) 116{ 117 game_params *ret = snew(game_params); 118 *ret = *params; /* structure copy */ 119 return ret; 120} 121 122static void decode_params(game_params *ret, char const *string) 123{ 124 char const *p = string; 125 126 ret->w = atoi(p); 127 while (*p && isdigit((unsigned char)*p)) p++; 128 if (*p == 'x') { 129 p++; 130 ret->h = atoi(p); 131 while (*p && isdigit((unsigned char)*p)) p++; 132 } else { 133 ret->h = ret->w; 134 } 135} 136 137static char *encode_params(const game_params *params, bool full) 138{ 139 char ret[400]; 140 int len; 141 142 len = sprintf(ret, "%dx%d", params->w, params->h); 143 assert(len < lenof(ret)); 144 ret[len] = '\0'; 145 146 return dupstr(ret); 147} 148 149static config_item *game_configure(const game_params *params) 150{ 151 config_item *ret; 152 char buf[80]; 153 154 ret = snewn(3, config_item); 155 156 ret[0].name = "Width"; 157 ret[0].type = C_STRING; 158 sprintf(buf, "%d", params->w); 159 ret[0].u.string.sval = dupstr(buf); 160 161 ret[1].name = "Height"; 162 ret[1].type = C_STRING; 163 sprintf(buf, "%d", params->h); 164 ret[1].u.string.sval = dupstr(buf); 165 166 ret[2].name = NULL; 167 ret[2].type = C_END; 168 169 return ret; 170} 171 172static game_params *custom_params(const config_item *cfg) 173{ 174 game_params *ret = snew(game_params); 175 176 ret->w = atoi(cfg[0].u.string.sval); 177 ret->h = atoi(cfg[1].u.string.sval); 178 179 return ret; 180} 181 182static const char *validate_params(const game_params *params, bool full) 183{ 184 if (params->w <= 0 || params->h <= 0) 185 return "Width and height must both be greater than zero"; 186 if (params->w > INT_MAX - 1 || params->h > INT_MAX - 1 || 187 params->w > INT_MAX / params->h) 188 return "Puzzle must not be unreasonably large"; 189 if (params->w * params->h < 2) 190 return "Grid must contain at least two squares"; 191 return NULL; 192} 193 194/* ---------------------------------------------------------------------- 195 * Puzzle generation code. 196 * 197 * For this particular puzzle, it seemed important to me to ensure 198 * a unique solution. I do this the brute-force way, by having a 199 * solver algorithm alongside the generator, and repeatedly 200 * generating a random grid until I find one whose solution is 201 * unique. It turns out that this isn't too onerous on a modern PC 202 * provided you keep grid size below around 30. Any offers of 203 * better algorithms, however, will be very gratefully received. 204 * 205 * Another annoyance of this approach is that it limits the 206 * available puzzles to those solvable by the algorithm I've used. 207 * My algorithm only ever considers a single row or column at any 208 * one time, which means it's incapable of solving the following 209 * difficult example (found by Bella Image around 1995/6, when she 210 * and I were both doing maths degrees): 211 * 212 * 2 1 2 1 213 * 214 * +--+--+--+--+ 215 * 1 1 | | | | | 216 * +--+--+--+--+ 217 * 2 | | | | | 218 * +--+--+--+--+ 219 * 1 | | | | | 220 * +--+--+--+--+ 221 * 1 | | | | | 222 * +--+--+--+--+ 223 * 224 * Obviously this cannot be solved by a one-row-or-column-at-a-time 225 * algorithm (it would require at least one row or column reading 226 * `2 1', `1 2', `3' or `4' to get started). However, it can be 227 * proved to have a unique solution: if the top left square were 228 * empty, then the only option for the top row would be to fill the 229 * two squares in the 1 columns, which would imply the squares 230 * below those were empty, leaving no place for the 2 in the second 231 * row. Contradiction. Hence the top left square is full, and the 232 * unique solution follows easily from that starting point. 233 * 234 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case 235 * it's useful to anyone.) 236 */ 237 238#ifndef STANDALONE_PICTURE_GENERATOR 239static int float_compare(const void *av, const void *bv) 240{ 241 const float *a = (const float *)av; 242 const float *b = (const float *)bv; 243 if (*a < *b) 244 return -1; 245 else if (*a > *b) 246 return +1; 247 else 248 return 0; 249} 250 251static void generate(random_state *rs, int w, int h, unsigned char *retgrid) 252{ 253 float *fgrid; 254 float *fgrid2; 255 int step, i, j; 256 float threshold; 257 258 fgrid = snewn(w*h, float); 259 260 for (i = 0; i < h; i++) { 261 for (j = 0; j < w; j++) { 262 fgrid[i*w+j] = random_upto(rs, 100000000UL) / 100000000.F; 263 } 264 } 265 266 /* 267 * The above gives a completely random splattering of black and 268 * white cells. We want to gently bias this in favour of _some_ 269 * reasonably thick areas of white and black, while retaining 270 * some randomness and fine detail. 271 * 272 * So we evolve the starting grid using a cellular automaton. 273 * Currently, I'm doing something very simple indeed, which is 274 * to set each square to the average of the surrounding nine 275 * cells (or the average of fewer, if we're on a corner). 276 */ 277 for (step = 0; step < 1; step++) { 278 fgrid2 = snewn(w*h, float); 279 280 for (i = 0; i < h; i++) { 281 for (j = 0; j < w; j++) { 282 float sx, xbar; 283 int n, p, q; 284 285 /* 286 * Compute the average of the surrounding cells. 287 */ 288 n = 0; 289 sx = 0.F; 290 for (p = -1; p <= +1; p++) { 291 for (q = -1; q <= +1; q++) { 292 if (i+p < 0 || i+p >= h || j+q < 0 || j+q >= w) 293 continue; 294 /* 295 * An additional special case not mentioned 296 * above: if a grid dimension is 2xn then 297 * we do not average across that dimension 298 * at all. Otherwise a 2x2 grid would 299 * contain four identical squares. 300 */ 301 if ((h==2 && p!=0) || (w==2 && q!=0)) 302 continue; 303 n++; 304 sx += fgrid[(i+p)*w+(j+q)]; 305 } 306 } 307 xbar = sx / n; 308 309 fgrid2[i*w+j] = xbar; 310 } 311 } 312 313 sfree(fgrid); 314 fgrid = fgrid2; 315 } 316 317 fgrid2 = snewn(w*h, float); 318 memcpy(fgrid2, fgrid, w*h*sizeof(float)); 319 qsort(fgrid2, w*h, sizeof(float), float_compare); 320 /* Choose a threshold that makes half the pixels black. In case of 321 * an odd number of pixels, select randomly between just under and 322 * just over half. */ 323 { 324 int index = w * h / 2; 325 if (w & h & 1) 326 index += random_upto(rs, 2); 327 if (index < w*h) 328 threshold = fgrid2[index]; 329 else 330 threshold = fgrid2[w*h-1] + 1; 331 } 332 sfree(fgrid2); 333 334 for (i = 0; i < h; i++) { 335 for (j = 0; j < w; j++) { 336 retgrid[i*w+j] = (fgrid[i*w+j] >= threshold ? GRID_FULL : 337 GRID_EMPTY); 338 } 339 } 340 341 sfree(fgrid); 342} 343#endif 344 345static int compute_rowdata(int *ret, unsigned char *start, int len, int step) 346{ 347 int i, n; 348 349 n = 0; 350 351 for (i = 0; i < len; i++) { 352 if (start[i*step] == GRID_FULL) { 353 int runlen = 1; 354 while (i+runlen < len && start[(i+runlen)*step] == GRID_FULL) 355 runlen++; 356 ret[n++] = runlen; 357 i += runlen; 358 } 359 360 if (i < len && start[i*step] == GRID_UNKNOWN) 361 return -1; 362 } 363 364 return n; 365} 366 367#define UNKNOWN 0 368#define BLOCK 1 369#define DOT 2 370#define STILL_UNKNOWN 3 371 372#ifdef STANDALONE_SOLVER 373static bool verbose = false; 374#endif 375 376static bool do_recurse(unsigned char *known, unsigned char *deduced, 377 unsigned char *row, 378 unsigned char *minpos_done, unsigned char *maxpos_done, 379 unsigned char *minpos_ok, unsigned char *maxpos_ok, 380 int *data, int len, 381 int freespace, int ndone, int lowest) 382{ 383 int i, j, k; 384 385 386 /* This algorithm basically tries all possible ways the given rows of 387 * black blocks can be laid out in the row/column being examined. 388 * Special care is taken to avoid checking the tail of a row/column 389 * if the same conditions have already been checked during this recursion 390 * The algorithm also takes care to cut its losses as soon as an 391 * invalid (partial) solution is detected. 392 */ 393 if (data[ndone]) { 394 if (lowest >= minpos_done[ndone] && lowest <= maxpos_done[ndone]) { 395 if (lowest >= minpos_ok[ndone] && lowest <= maxpos_ok[ndone]) { 396 for (i=0; i<lowest; i++) 397 deduced[i] |= row[i]; 398 } 399 return lowest >= minpos_ok[ndone] && lowest <= maxpos_ok[ndone]; 400 } else { 401 if (lowest < minpos_done[ndone]) minpos_done[ndone] = lowest; 402 if (lowest > maxpos_done[ndone]) maxpos_done[ndone] = lowest; 403 } 404 for (i=0; i<=freespace; i++) { 405 j = lowest; 406 for (k=0; k<i; k++) { 407 if (known[j] == BLOCK) goto next_iter; 408 row[j++] = DOT; 409 } 410 for (k=0; k<data[ndone]; k++) { 411 if (known[j] == DOT) goto next_iter; 412 row[j++] = BLOCK; 413 } 414 if (j < len) { 415 if (known[j] == BLOCK) goto next_iter; 416 row[j++] = DOT; 417 } 418 if (do_recurse(known, deduced, row, minpos_done, maxpos_done, 419 minpos_ok, maxpos_ok, data, len, freespace-i, ndone+1, j)) { 420 if (lowest < minpos_ok[ndone]) minpos_ok[ndone] = lowest; 421 if (lowest + i > maxpos_ok[ndone]) maxpos_ok[ndone] = lowest + i; 422 if (lowest + i > maxpos_done[ndone]) maxpos_done[ndone] = lowest + i; 423 } 424 next_iter: 425 j++; 426 } 427 return lowest >= minpos_ok[ndone] && lowest <= maxpos_ok[ndone]; 428 } else { 429 for (i=lowest; i<len; i++) { 430 if (known[i] == BLOCK) return false; 431 row[i] = DOT; 432 } 433 for (i=0; i<len; i++) 434 deduced[i] |= row[i]; 435 return true; 436 } 437} 438 439 440static bool do_row(unsigned char *known, unsigned char *deduced, 441 unsigned char *row, 442 unsigned char *minpos_done, unsigned char *maxpos_done, 443 unsigned char *minpos_ok, unsigned char *maxpos_ok, 444 unsigned char *start, int len, int step, int *data, 445 unsigned int *changed 446#ifdef STANDALONE_SOLVER 447 , const char *rowcol, int index, int cluewid 448#endif 449 ) 450{ 451 int rowlen, i, freespace; 452 bool done_any; 453 454 assert(len >= 0); /* avoid compile warnings about the memsets below */ 455 456 freespace = len+1; 457 for (rowlen = 0; data[rowlen]; rowlen++) { 458 minpos_done[rowlen] = minpos_ok[rowlen] = len - 1; 459 maxpos_done[rowlen] = maxpos_ok[rowlen] = 0; 460 freespace -= data[rowlen]+1; 461 } 462 463 for (i = 0; i < len; i++) { 464 known[i] = start[i*step]; 465 deduced[i] = 0; 466 } 467 for (i = len - 1; i >= 0 && known[i] == DOT; i--) 468 freespace--; 469 470 if (rowlen == 0) { 471 memset(deduced, DOT, len); 472 } else if (rowlen == 1 && data[0] == len) { 473 memset(deduced, BLOCK, len); 474 } else { 475 do_recurse(known, deduced, row, minpos_done, maxpos_done, minpos_ok, 476 maxpos_ok, data, len, freespace, 0, 0); 477 } 478 479 done_any = false; 480 for (i=0; i<len; i++) 481 if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) { 482 start[i*step] = deduced[i]; 483 if (changed) changed[i]++; 484 done_any = true; 485 } 486#ifdef STANDALONE_SOLVER 487 if (verbose && done_any) { 488 char buf[80]; 489 int thiscluewid; 490 printf("%s %2d: [", rowcol, index); 491 for (thiscluewid = -1, i = 0; data[i]; i++) 492 thiscluewid += sprintf(buf, " %d", data[i]); 493 printf("%*s", cluewid - thiscluewid, ""); 494 for (i = 0; data[i]; i++) 495 printf(" %d", data[i]); 496 printf(" ] "); 497 for (i = 0; i < len; i++) 498 putchar(known[i] == BLOCK ? '#' : 499 known[i] == DOT ? '.' : '?'); 500 printf(" -> "); 501 for (i = 0; i < len; i++) 502 putchar(start[i*step] == BLOCK ? '#' : 503 start[i*step] == DOT ? '.' : '?'); 504 putchar('\n'); 505 } 506#endif 507 return done_any; 508} 509 510static bool solve_puzzle(const game_state *state, unsigned char *grid, 511 int w, int h, 512 unsigned char *matrix, unsigned char *workspace, 513 unsigned int *changed_h, unsigned int *changed_w, 514 int *rowdata 515#ifdef STANDALONE_SOLVER 516 , int cluewid 517#else 518 , int dummy 519#endif 520 ) 521{ 522 int i, j, max; 523 bool ok; 524 int max_h, max_w; 525 526 assert((state!=NULL && state->common->rowdata!=NULL) ^ (grid!=NULL)); 527 528 max = max(w, h); 529 530 memset(matrix, 0, w*h); 531 if (state) { 532 for (i=0; i<w*h; i++) { 533 if (state->common->immutable[i]) 534 matrix[i] = state->grid[i]; 535 } 536 } 537 538 /* For each column, compute how many squares can be deduced 539 * from just the row-data and initial clues. 540 * Later, changed_* will hold how many squares were changed 541 * in every row/column in the previous iteration 542 * Changed_* is used to choose the next rows / cols to re-examine 543 */ 544 for (i=0; i<h; i++) { 545 int freespace, rowlen; 546 if (state && state->common->rowdata) { 547 memcpy(rowdata, state->common->rowdata + state->common->rowsize*(w+i), max*sizeof(int)); 548 rowlen = state->common->rowlen[w+i]; 549 } else { 550 rowlen = compute_rowdata(rowdata, grid+i*w, w, 1); 551 } 552 rowdata[rowlen] = 0; 553 if (rowlen == 0) { 554 changed_h[i] = w; 555 } else { 556 for (j=0, freespace=w+1; rowdata[j]; j++) 557 freespace -= rowdata[j] + 1; 558 for (j=0, changed_h[i]=0; rowdata[j]; j++) 559 if (rowdata[j] > freespace) 560 changed_h[i] += rowdata[j] - freespace; 561 } 562 for (j = 0; j < w; j++) 563 if (matrix[i*w+j]) 564 changed_h[i]++; 565 } 566 for (i=0,max_h=0; i<h; i++) 567 if (changed_h[i] > max_h) 568 max_h = changed_h[i]; 569 for (i=0; i<w; i++) { 570 int freespace, rowlen; 571 if (state && state->common->rowdata) { 572 memcpy(rowdata, state->common->rowdata + state->common->rowsize*i, max*sizeof(int)); 573 rowlen = state->common->rowlen[i]; 574 } else { 575 rowlen = compute_rowdata(rowdata, grid+i, h, w); 576 } 577 rowdata[rowlen] = 0; 578 if (rowlen == 0) { 579 changed_w[i] = h; 580 } else { 581 for (j=0, freespace=h+1; rowdata[j]; j++) 582 freespace -= rowdata[j] + 1; 583 for (j=0, changed_w[i]=0; rowdata[j]; j++) 584 if (rowdata[j] > freespace) 585 changed_w[i] += rowdata[j] - freespace; 586 } 587 for (j = 0; j < h; j++) 588 if (matrix[j*w+i]) 589 changed_w[i]++; 590 } 591 for (i=0,max_w=0; i<w; i++) 592 if (changed_w[i] > max_w) 593 max_w = changed_w[i]; 594 595 /* Solve the puzzle. 596 * Process rows/columns individually. Deductions involving more than one 597 * row and/or column at a time are not supported. 598 * Take care to only process rows/columns which have been changed since they 599 * were previously processed. 600 * Also, prioritize rows/columns which have had the most changes since their 601 * previous processing, as they promise the greatest benefit. 602 * Extremely rectangular grids (e.g. 10x20, 15x40, etc.) are not treated specially. 603 */ 604 do { 605 for (; max_h && max_h >= max_w; max_h--) { 606 for (i=0; i<h; i++) { 607 if (changed_h[i] >= max_h) { 608 if (state && state->common->rowdata) { 609 memcpy(rowdata, state->common->rowdata + state->common->rowsize*(w+i), max*sizeof(int)); 610 rowdata[state->common->rowlen[w+i]] = 0; 611 } else { 612 rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0; 613 } 614 do_row(workspace, workspace+max, workspace+2*max, 615 workspace+3*max, workspace+4*max, 616 workspace+5*max, workspace+6*max, 617 matrix+i*w, w, 1, rowdata, changed_w 618#ifdef STANDALONE_SOLVER 619 , "row", i+1, cluewid 620#endif 621 ); 622 changed_h[i] = 0; 623 } 624 } 625 for (i=0,max_w=0; i<w; i++) 626 if (changed_w[i] > max_w) 627 max_w = changed_w[i]; 628 } 629 for (; max_w && max_w >= max_h; max_w--) { 630 for (i=0; i<w; i++) { 631 if (changed_w[i] >= max_w) { 632 if (state && state->common->rowdata) { 633 memcpy(rowdata, state->common->rowdata + state->common->rowsize*i, max*sizeof(int)); 634 rowdata[state->common->rowlen[i]] = 0; 635 } else { 636 rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0; 637 } 638 do_row(workspace, workspace+max, workspace+2*max, 639 workspace+3*max, workspace+4*max, 640 workspace+5*max, workspace+6*max, 641 matrix+i, h, w, rowdata, changed_h 642#ifdef STANDALONE_SOLVER 643 , "col", i+1, cluewid 644#endif 645 ); 646 changed_w[i] = 0; 647 } 648 } 649 for (i=0,max_h=0; i<h; i++) 650 if (changed_h[i] > max_h) 651 max_h = changed_h[i]; 652 } 653 } while (max_h>0 || max_w>0); 654 655 ok = true; 656 for (i=0; i<h; i++) { 657 for (j=0; j<w; j++) { 658 if (matrix[i*w+j] == UNKNOWN) 659 ok = false; 660 } 661 } 662 663 return ok; 664} 665 666#ifndef STANDALONE_PICTURE_GENERATOR 667static unsigned char *generate_soluble(random_state *rs, int w, int h) 668{ 669 int i, j, max; 670 bool ok; 671 unsigned char *grid, *matrix, *workspace; 672 unsigned int *changed_h, *changed_w; 673 int *rowdata; 674 675 max = max(w, h); 676 677 grid = snewn(w*h, unsigned char); 678 /* Allocate this here, to avoid having to reallocate it again for every geneerated grid */ 679 matrix = snewn(w*h, unsigned char); 680 workspace = snewn(max*7, unsigned char); 681 changed_h = snewn(max+1, unsigned int); 682 changed_w = snewn(max+1, unsigned int); 683 rowdata = snewn(max+1, int); 684 685 do { 686 generate(rs, w, h, grid); 687 688 /* 689 * The game is a bit too easy if any row or column is 690 * completely black or completely white. An exception is 691 * made for rows/columns that are under 3 squares, 692 * otherwise nothing will ever be successfully generated. 693 */ 694 ok = true; 695 if (w > 2) { 696 for (i = 0; i < h; i++) { 697 int colours = 0; 698 for (j = 0; j < w; j++) 699 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1); 700 if (colours != 3) 701 ok = false; 702 } 703 } 704 if (h > 2) { 705 for (j = 0; j < w; j++) { 706 int colours = 0; 707 for (i = 0; i < h; i++) 708 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1); 709 if (colours != 3) 710 ok = false; 711 } 712 } 713 if (!ok) 714 continue; 715 716 ok = solve_puzzle(NULL, grid, w, h, matrix, workspace, 717 changed_h, changed_w, rowdata, 0); 718 } while (!ok); 719 720 sfree(matrix); 721 sfree(workspace); 722 sfree(changed_h); 723 sfree(changed_w); 724 sfree(rowdata); 725 return grid; 726} 727#endif 728 729#ifdef STANDALONE_PICTURE_GENERATOR 730static unsigned char *picture; 731#endif 732 733static char *new_game_desc(const game_params *params, random_state *rs, 734 char **aux, bool interactive) 735{ 736 unsigned char *grid; 737 int i, j, max, rowlen, *rowdata; 738 char intbuf[80], *desc; 739 int desclen, descpos; 740#ifdef STANDALONE_PICTURE_GENERATOR 741 game_state *state; 742 int *index; 743#endif 744 745 max = max(params->w, params->h); 746 747#ifdef STANDALONE_PICTURE_GENERATOR 748 /* 749 * Fixed input picture. 750 */ 751 grid = snewn(params->w * params->h, unsigned char); 752 memcpy(grid, picture, params->w * params->h); 753 754 /* 755 * Now winnow the immutable square set as far as possible. 756 */ 757 state = snew(game_state); 758 state->grid = grid; 759 state->common = snew(game_state_common); 760 state->common->rowdata = NULL; 761 state->common->immutable = snewn(params->w * params->h, bool); 762 for (i = 0; i < params->w * params->h; i++) 763 state->common->immutable[i] = true; 764 765 index = snewn(params->w * params->h, int); 766 for (i = 0; i < params->w * params->h; i++) 767 index[i] = i; 768 shuffle(index, params->w * params->h, sizeof(*index), rs); 769 770 { 771 unsigned char *matrix = snewn(params->w*params->h, unsigned char); 772 unsigned char *workspace = snewn(max*7, unsigned char); 773 unsigned int *changed_h = snewn(max+1, unsigned int); 774 unsigned int *changed_w = snewn(max+1, unsigned int); 775 int *rowdata = snewn(max+1, int); 776 for (i = 0; i < params->w * params->h; i++) { 777 state->common->immutable[index[i]] = false; 778 if (!solve_puzzle(state, grid, params->w, params->h, 779 matrix, workspace, changed_h, changed_w, 780 rowdata, 0)) 781 state->common->immutable[index[i]] = true; 782 } 783 sfree(workspace); 784 sfree(changed_h); 785 sfree(changed_w); 786 sfree(rowdata); 787 sfree(matrix); 788 } 789#else 790 grid = generate_soluble(rs, params->w, params->h); 791#endif 792 rowdata = snewn(max, int); 793 794 /* 795 * Save the solved game in aux. 796 */ 797 if (aux) { 798 char *ai = snewn(params->w * params->h + 2, char); 799 800 /* 801 * String format is exactly the same as a solve move, so we 802 * can just dupstr this in solve_game(). 803 */ 804 805 ai[0] = 'S'; 806 807 for (i = 0; i < params->w * params->h; i++) 808 ai[i+1] = grid[i] ? '1' : '0'; 809 810 ai[params->w * params->h + 1] = '\0'; 811 812 *aux = ai; 813 } 814 815 /* 816 * Seed is a slash-separated list of row contents; each row 817 * contents section is a dot-separated list of integers. Row 818 * contents are listed in the order (columns left to right, 819 * then rows top to bottom). 820 * 821 * Simplest way to handle memory allocation is to make two 822 * passes, first computing the seed size and then writing it 823 * out. 824 */ 825 desclen = 0; 826 for (i = 0; i < params->w + params->h; i++) { 827 if (i < params->w) 828 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w); 829 else 830 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w, 831 params->w, 1); 832 if (rowlen > 0) { 833 for (j = 0; j < rowlen; j++) { 834 desclen += 1 + sprintf(intbuf, "%d", rowdata[j]); 835 } 836 } else { 837 desclen++; 838 } 839 } 840 desc = snewn(desclen, char); 841 descpos = 0; 842 for (i = 0; i < params->w + params->h; i++) { 843 if (i < params->w) 844 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w); 845 else 846 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w, 847 params->w, 1); 848 if (rowlen > 0) { 849 for (j = 0; j < rowlen; j++) { 850 int len = sprintf(desc+descpos, "%d", rowdata[j]); 851 if (j+1 < rowlen) 852 desc[descpos + len] = '.'; 853 else 854 desc[descpos + len] = '/'; 855 descpos += len+1; 856 } 857 } else { 858 desc[descpos++] = '/'; 859 } 860 } 861 assert(descpos == desclen); 862 assert(desc[desclen-1] == '/'); 863 desc[desclen-1] = '\0'; 864#ifdef STANDALONE_PICTURE_GENERATOR 865 for (i = 0; i < params->w * params->h; i++) 866 if (state->common->immutable[i]) 867 break; 868 if (i < params->w * params->h) { 869 /* 870 * At least one immutable square, so we need a suffix. 871 */ 872 int run; 873 874 desc = sresize(desc, desclen + params->w * params->h + 3, char); 875 desc[descpos-1] = ','; 876 877 run = 0; 878 for (i = 0; i < params->w * params->h; i++) { 879 if (!state->common->immutable[i]) { 880 run++; 881 if (run == 25) { 882 desc[descpos++] = 'z'; 883 run = 0; 884 } 885 } else { 886 desc[descpos++] = run + (grid[i] == GRID_FULL ? 'A' : 'a'); 887 run = 0; 888 } 889 } 890 if (run > 0) 891 desc[descpos++] = run + 'a'; 892 desc[descpos] = '\0'; 893 } 894 sfree(state->common->immutable); 895 sfree(state->common); 896 sfree(state); 897#endif 898 sfree(rowdata); 899 sfree(grid); 900 return desc; 901} 902 903static const char *validate_desc(const game_params *params, const char *desc) 904{ 905 int i, n, rowspace; 906 const char *p; 907 908 for (i = 0; i < params->w + params->h; i++) { 909 if (i < params->w) 910 rowspace = params->h + 1; 911 else 912 rowspace = params->w + 1; 913 914 if (*desc && isdigit((unsigned char)*desc)) { 915 do { 916 p = desc; 917 while (*desc && isdigit((unsigned char)*desc)) desc++; 918 n = atoi(p); 919 if (n <= 0) 920 return "all clues must be positive"; 921 if (n > INT_MAX - 1) 922 return "at least one clue is grossly excessive"; 923 rowspace -= n+1; 924 925 if (rowspace < 0) { 926 if (i < params->w) 927 return "at least one column contains more numbers than will fit"; 928 else 929 return "at least one row contains more numbers than will fit"; 930 } 931 } while (*desc++ == '.'); 932 } else { 933 desc++; /* expect a slash immediately */ 934 } 935 936 if (desc[-1] == '/') { 937 if (i+1 == params->w + params->h) 938 return "too many row/column specifications"; 939 } else if (desc[-1] == '\0' || desc[-1] == ',') { 940 if (i+1 < params->w + params->h) 941 return "too few row/column specifications"; 942 } else 943 return "unrecognised character in game specification"; 944 } 945 946 if (desc[-1] == ',') { 947 /* 948 * Optional extra piece of game description which fills in 949 * some grid squares as extra clues. 950 */ 951 i = 0; 952 while (i < params->w * params->h) { 953 int c = (unsigned char)*desc++; 954 if ((c >= 'a' && c <= 'z') || 955 (c >= 'A' && c <= 'Z')) { 956 int len = tolower(c) - 'a'; 957 i += len; 958 if (len < 25 && i < params->w*params->h) 959 i++; 960 if (i > params->w * params->h) { 961 return "too much data in clue-squares section"; 962 } 963 } else if (!c) { 964 return "too little data in clue-squares section"; 965 } else { 966 return "unrecognised character in clue-squares section"; 967 } 968 } 969 if (*desc) { 970 return "too much data in clue-squares section"; 971 } 972 } 973 974 return NULL; 975} 976 977static game_state *new_game(midend *me, const game_params *params, 978 const char *desc) 979{ 980 int i, j; 981 const char *p; 982 game_state *state = snew(game_state); 983 984 state->common = snew(game_state_common); 985 state->common->refcount = 1; 986 987 state->common->w = params->w; 988 state->common->h = params->h; 989 990 state->grid = snewn(state->common->w * state->common->h, unsigned char); 991 memset(state->grid, GRID_UNKNOWN, state->common->w * state->common->h); 992 993 state->common->immutable = snewn(state->common->w * state->common->h, 994 bool); 995 memset(state->common->immutable, 0, 996 state->common->w * state->common->h * sizeof(bool)); 997 998 state->common->rowsize = max(state->common->w, state->common->h); 999 state->common->rowdata = snewn(state->common->rowsize * (state->common->w + state->common->h), int); 1000 state->common->rowlen = snewn(state->common->w + state->common->h, int); 1001 1002 state->completed = state->cheated = false; 1003 1004 for (i = 0; i < params->w + params->h; i++) { 1005 state->common->rowlen[i] = 0; 1006 if (*desc && isdigit((unsigned char)*desc)) { 1007 do { 1008 p = desc; 1009 while (*desc && isdigit((unsigned char)*desc)) desc++; 1010 state->common->rowdata[state->common->rowsize * i + state->common->rowlen[i]++] = 1011 atoi(p); 1012 } while (*desc++ == '.'); 1013 } else { 1014 desc++; /* expect a slash immediately */ 1015 } 1016 } 1017 1018 /* 1019 * Choose a font size based on the clues. If any column clue is 1020 * more than one digit, switch to the smaller size. 1021 */ 1022 state->common->fontsize = FS_LARGE; 1023 for (i = 0; i < params->w; i++) 1024 for (j = 0; j < state->common->rowlen[i]; j++) 1025 if (state->common->rowdata[state->common->rowsize * i + j] >= 10) 1026 state->common->fontsize = FS_SMALL; 1027 /* 1028 * We might also need to use the small font if there are lots of 1029 * row clues. We assume that all clues are one digit and that a 1030 * single-digit clue takes up 1.5 tiles, of which the clue is 0.5 1031 * tiles and the space is 1.0 tiles. 1032 */ 1033 for (i = params->w; i < params->w + params->h; i++) 1034 if ((state->common->rowlen[i] * 3 - 2) > 1035 TLBORDER(state->common->w) * 2) 1036 state->common->fontsize = FS_SMALL; 1037 1038 if (desc[-1] == ',') { 1039 /* 1040 * Optional extra piece of game description which fills in 1041 * some grid squares as extra clues. 1042 */ 1043 i = 0; 1044 while (i < params->w * params->h) { 1045 int c = (unsigned char)*desc++; 1046 bool full = isupper(c); 1047 int len = tolower(c) - 'a'; 1048 i += len; 1049 if (len < 25 && i < params->w*params->h) { 1050 state->grid[i] = full ? GRID_FULL : GRID_EMPTY; 1051 state->common->immutable[i] = true; 1052 i++; 1053 } 1054 } 1055 } 1056 1057 return state; 1058} 1059 1060static game_state *dup_game(const game_state *state) 1061{ 1062 game_state *ret = snew(game_state); 1063 1064 ret->common = state->common; 1065 ret->common->refcount++; 1066 1067 ret->grid = snewn(ret->common->w * ret->common->h, unsigned char); 1068 memcpy(ret->grid, state->grid, ret->common->w * ret->common->h); 1069 1070 ret->completed = state->completed; 1071 ret->cheated = state->cheated; 1072 1073 return ret; 1074} 1075 1076static void free_game(game_state *state) 1077{ 1078 if (--state->common->refcount == 0) { 1079 sfree(state->common->rowdata); 1080 sfree(state->common->rowlen); 1081 sfree(state->common->immutable); 1082 sfree(state->common); 1083 } 1084 sfree(state->grid); 1085 sfree(state); 1086} 1087 1088static char *solve_game(const game_state *state, const game_state *currstate, 1089 const char *ai, const char **error) 1090{ 1091 unsigned char *matrix; 1092 int w = state->common->w, h = state->common->h; 1093 int i; 1094 char *ret; 1095 int max; 1096 bool ok; 1097 unsigned char *workspace; 1098 unsigned int *changed_h, *changed_w; 1099 int *rowdata; 1100 1101 /* 1102 * If we already have the solved state in ai, copy it out. 1103 */ 1104 if (ai) 1105 return dupstr(ai); 1106 1107 max = max(w, h); 1108 matrix = snewn(w*h, unsigned char); 1109 workspace = snewn(max*7, unsigned char); 1110 changed_h = snewn(max+1, unsigned int); 1111 changed_w = snewn(max+1, unsigned int); 1112 rowdata = snewn(max+1, int); 1113 1114 ok = solve_puzzle(state, NULL, w, h, matrix, workspace, 1115 changed_h, changed_w, rowdata, 0); 1116 1117 sfree(workspace); 1118 sfree(changed_h); 1119 sfree(changed_w); 1120 sfree(rowdata); 1121 1122 if (!ok) { 1123 sfree(matrix); 1124 *error = "Solving algorithm cannot complete this puzzle"; 1125 return NULL; 1126 } 1127 1128 ret = snewn(w*h+2, char); 1129 ret[0] = 'S'; 1130 for (i = 0; i < w*h; i++) { 1131 assert(matrix[i] == BLOCK || matrix[i] == DOT); 1132 ret[i+1] = (matrix[i] == BLOCK ? '1' : '0'); 1133 } 1134 ret[w*h+1] = '\0'; 1135 1136 sfree(matrix); 1137 1138 return ret; 1139} 1140 1141static bool game_can_format_as_text_now(const game_params *params) 1142{ 1143 return true; 1144} 1145 1146static char *game_text_format(const game_state *state) 1147{ 1148 int w = state->common->w, h = state->common->h, i, j; 1149 int left_gap = 0, top_gap = 0, ch = 2, cw = 1, limit = 1; 1150 1151 int len, topleft, lw, lh, gw, gh; /* {line,grid}_{width,height} */ 1152 char *board, *buf; 1153 1154 for (i = 0; i < w; ++i) { 1155 top_gap = max(top_gap, state->common->rowlen[i]); 1156 for (j = 0; j < state->common->rowlen[i]; ++j) 1157 while (state->common->rowdata[i*state->common->rowsize + j] >= limit) { 1158 ++cw; 1159 limit *= 10; 1160 } 1161 } 1162 for (i = 0; i < h; ++i) { 1163 int rowlen = 0; 1164 bool predecessors = false; 1165 for (j = 0; j < state->common->rowlen[i+w]; ++j) { 1166 int copy = state->common->rowdata[(i+w)*state->common->rowsize + j]; 1167 rowlen += predecessors; 1168 predecessors = true; 1169 do ++rowlen; while (copy /= 10); 1170 } 1171 left_gap = max(left_gap, rowlen); 1172 } 1173 1174 cw = max(cw, 3); 1175 1176 gw = w*cw + 2; 1177 gh = h*ch + 1; 1178 lw = gw + left_gap; 1179 lh = gh + top_gap; 1180 len = lw * lh; 1181 topleft = lw * top_gap + left_gap; 1182 1183 board = snewn(len + 1, char); 1184 memset(board, ' ', len); 1185 board[len] = '\0'; 1186 1187 for (i = 0; i < lh; ++i) { 1188 board[lw - 1 + i*lw] = '\n'; 1189 if (i < top_gap) continue; 1190 board[lw - 2 + i*lw] = ((i - top_gap) % ch ? '|' : '+'); 1191 } 1192 1193 for (i = 0; i < w; ++i) { 1194 for (j = 0; j < state->common->rowlen[i]; ++j) { 1195 int cell = topleft + i*cw + 1 + lw*(j - state->common->rowlen[i]); 1196 int nch = sprintf(board + cell, "%*d", cw - 1, 1197 state->common->rowdata[i*state->common->rowsize + j]); 1198 board[cell + nch] = ' '; /* de-NUL-ify */ 1199 } 1200 } 1201 1202 buf = snewn(left_gap + 1, char); 1203 for (i = 0; i < h; ++i) { 1204 char *p = buf, *start = board + top_gap*lw + left_gap + (i*ch+1)*lw; 1205 for (j = 0; j < state->common->rowlen[i+w]; ++j) { 1206 if (p > buf) *p++ = ' '; 1207 p += sprintf(p, "%d", state->common->rowdata[(i+w)*state->common->rowsize + j]); 1208 } 1209 memcpy(start - (p - buf), buf, p - buf); 1210 } 1211 1212 for (i = 0; i < h; ++i) { 1213 for (j = 0; j < w; ++j) { 1214 int cell = topleft + j*cw + i*ch*lw; 1215 int center = cell + cw/2 + (ch/2)*lw; 1216 int dx, dy; 1217 board[cell] = false ? center : '+'; 1218 for (dx = 1; dx < cw; ++dx) board[cell + dx] = '-'; 1219 for (dy = 1; dy < ch; ++dy) board[cell + dy*lw] = '|'; 1220 if (state->grid[i*w+j] == GRID_UNKNOWN) continue; 1221 for (dx = 1; dx < cw; ++dx) 1222 for (dy = 1; dy < ch; ++dy) 1223 board[cell + dx + dy*lw] = 1224 state->grid[i*w+j] == GRID_FULL ? '#' : '.'; 1225 } 1226 } 1227 1228 memcpy(board + topleft + h*ch*lw, board + topleft, gw - 1); 1229 1230 sfree(buf); 1231 1232 assert(board[len] == '\0' && "Overwrote the NUL"); 1233 return board; 1234} 1235 1236struct game_ui { 1237 bool dragging; 1238 int drag_start_x; 1239 int drag_start_y; 1240 int drag_end_x; 1241 int drag_end_y; 1242 int drag, release, state; 1243 int cur_x, cur_y; 1244 bool cur_visible; 1245}; 1246 1247static game_ui *new_ui(const game_state *state) 1248{ 1249 game_ui *ret; 1250 1251 ret = snew(game_ui); 1252 ret->dragging = false; 1253 ret->cur_x = ret->cur_y = 0; 1254 ret->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false); 1255 1256 return ret; 1257} 1258 1259static void free_ui(game_ui *ui) 1260{ 1261 sfree(ui); 1262} 1263 1264static void game_changed_state(game_ui *ui, const game_state *oldstate, 1265 const game_state *newstate) 1266{ 1267} 1268 1269static const char *current_key_label(const game_ui *ui, 1270 const game_state *state, int button) 1271{ 1272 if (IS_CURSOR_SELECT(button)) { 1273 if (!ui->cur_visible) return ""; 1274 switch (state->grid[ui->cur_y * state->common->w + ui->cur_x]) { 1275 case GRID_UNKNOWN: 1276 return button == CURSOR_SELECT ? "Black" : "White"; 1277 case GRID_FULL: 1278 return button == CURSOR_SELECT ? "White" : "Grey"; 1279 case GRID_EMPTY: 1280 return button == CURSOR_SELECT ? "Grey" : "Black"; 1281 } 1282 } 1283 return ""; 1284} 1285 1286struct game_drawstate { 1287 bool started; 1288 int w, h; 1289 int tilesize; 1290 unsigned char *visible, *numcolours; 1291 int cur_x, cur_y; 1292 char *strbuf; /* Used for formatting clues. */ 1293}; 1294 1295static char *interpret_move(const game_state *state, game_ui *ui, 1296 const game_drawstate *ds, 1297 int x, int y, int button) 1298{ 1299 bool control = button & MOD_CTRL, shift = button & MOD_SHFT; 1300 button = STRIP_BUTTON_MODIFIERS(button); 1301 1302 x = FROMCOORD(state->common->w, x); 1303 y = FROMCOORD(state->common->h, y); 1304 1305 if (x >= 0 && x < state->common->w && y >= 0 && y < state->common->h && 1306 (button == LEFT_BUTTON || button == RIGHT_BUTTON || 1307 button == MIDDLE_BUTTON)) { 1308#ifdef STYLUS_BASED 1309 int currstate = state->grid[y * state->common->w + x]; 1310#endif 1311 1312 ui->dragging = true; 1313 1314 if (button == LEFT_BUTTON) { 1315 ui->drag = LEFT_DRAG; 1316 ui->release = LEFT_RELEASE; 1317#ifdef STYLUS_BASED 1318 ui->state = (currstate + 2) % 3; /* FULL -> EMPTY -> UNKNOWN */ 1319#else 1320 ui->state = GRID_FULL; 1321#endif 1322 } else if (button == RIGHT_BUTTON) { 1323 ui->drag = RIGHT_DRAG; 1324 ui->release = RIGHT_RELEASE; 1325#ifdef STYLUS_BASED 1326 ui->state = (currstate + 1) % 3; /* EMPTY -> FULL -> UNKNOWN */ 1327#else 1328 ui->state = GRID_EMPTY; 1329#endif 1330 } else /* if (button == MIDDLE_BUTTON) */ { 1331 ui->drag = MIDDLE_DRAG; 1332 ui->release = MIDDLE_RELEASE; 1333 ui->state = GRID_UNKNOWN; 1334 } 1335 1336 ui->drag_start_x = ui->drag_end_x = x; 1337 ui->drag_start_y = ui->drag_end_y = y; 1338 ui->cur_visible = false; 1339 1340 return MOVE_UI_UPDATE; 1341 } 1342 1343 if (ui->dragging && button == ui->drag) { 1344 /* 1345 * There doesn't seem much point in allowing a rectangle 1346 * drag; people will generally only want to drag a single 1347 * horizontal or vertical line, so we make that easy by 1348 * snapping to it. 1349 * 1350 * Exception: if we're _middle_-button dragging to tag 1351 * things as UNKNOWN, we may well want to trash an entire 1352 * area and start over! 1353 */ 1354 if (ui->state != GRID_UNKNOWN) { 1355 if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y)) 1356 y = ui->drag_start_y; 1357 else 1358 x = ui->drag_start_x; 1359 } 1360 1361 if (x < 0) x = 0; 1362 if (y < 0) y = 0; 1363 if (x >= state->common->w) x = state->common->w - 1; 1364 if (y >= state->common->h) y = state->common->h - 1; 1365 1366 ui->drag_end_x = x; 1367 ui->drag_end_y = y; 1368 1369 return MOVE_UI_UPDATE; 1370 } 1371 1372 if (ui->dragging && button == ui->release) { 1373 int x1, x2, y1, y2, xx, yy; 1374 bool move_needed = false; 1375 1376 x1 = min(ui->drag_start_x, ui->drag_end_x); 1377 x2 = max(ui->drag_start_x, ui->drag_end_x); 1378 y1 = min(ui->drag_start_y, ui->drag_end_y); 1379 y2 = max(ui->drag_start_y, ui->drag_end_y); 1380 1381 for (yy = y1; yy <= y2; yy++) 1382 for (xx = x1; xx <= x2; xx++) 1383 if (!state->common->immutable[yy * state->common->w + xx] && 1384 state->grid[yy * state->common->w + xx] != ui->state) 1385 move_needed = true; 1386 1387 ui->dragging = false; 1388 1389 if (move_needed) { 1390 char buf[80]; 1391 sprintf(buf, "%c%d,%d,%d,%d", 1392 (char)(ui->state == GRID_FULL ? 'F' : 1393 ui->state == GRID_EMPTY ? 'E' : 'U'), 1394 x1, y1, x2-x1+1, y2-y1+1); 1395 return dupstr(buf); 1396 } else 1397 return MOVE_UI_UPDATE; 1398 } 1399 1400 if (IS_CURSOR_MOVE(button)) { 1401 int x = ui->cur_x, y = ui->cur_y, newstate; 1402 char buf[80], *ret; 1403 ret = move_cursor(button, &ui->cur_x, &ui->cur_y, 1404 state->common->w, state->common->h, false, 1405 &ui->cur_visible); 1406 if (!control && !shift) return ret; 1407 1408 newstate = control ? shift ? GRID_UNKNOWN : GRID_FULL : GRID_EMPTY; 1409 if (state->grid[y * state->common->w + x] == newstate && 1410 state->grid[ui->cur_y * state->common->w + ui->cur_x] == newstate) 1411 return ret; 1412 1413 sprintf(buf, "%c%d,%d,%d,%d", control ? shift ? 'U' : 'F' : 'E', 1414 min(x, ui->cur_x), min(y, ui->cur_y), 1415 abs(x - ui->cur_x) + 1, abs(y - ui->cur_y) + 1); 1416 return dupstr(buf); 1417 } 1418 1419 if (IS_CURSOR_SELECT(button)) { 1420 int currstate = state->grid[ui->cur_y * state->common->w + ui->cur_x]; 1421 int newstate; 1422 char buf[80]; 1423 1424 if (!ui->cur_visible) { 1425 ui->cur_visible = true; 1426 return MOVE_UI_UPDATE; 1427 } 1428 1429 if (button == CURSOR_SELECT2) 1430 newstate = currstate == GRID_UNKNOWN ? GRID_EMPTY : 1431 currstate == GRID_EMPTY ? GRID_FULL : GRID_UNKNOWN; 1432 else 1433 newstate = currstate == GRID_UNKNOWN ? GRID_FULL : 1434 currstate == GRID_FULL ? GRID_EMPTY : GRID_UNKNOWN; 1435 1436 sprintf(buf, "%c%d,%d,%d,%d", 1437 (char)(newstate == GRID_FULL ? 'F' : 1438 newstate == GRID_EMPTY ? 'E' : 'U'), 1439 ui->cur_x, ui->cur_y, 1, 1); 1440 return dupstr(buf); 1441 } 1442 1443 return NULL; 1444} 1445 1446static game_state *execute_move(const game_state *from, const char *move) 1447{ 1448 game_state *ret; 1449 int x1, x2, y1, y2, xx, yy; 1450 int val; 1451 1452 if (move[0] == 'S' && 1453 strlen(move) == from->common->w * from->common->h + 1) { 1454 int i; 1455 1456 ret = dup_game(from); 1457 1458 for (i = 0; i < ret->common->w * ret->common->h; i++) 1459 ret->grid[i] = (move[i+1] == '1' ? GRID_FULL : GRID_EMPTY); 1460 1461 ret->completed = ret->cheated = true; 1462 1463 return ret; 1464 } else if ((move[0] == 'F' || move[0] == 'E' || move[0] == 'U') && 1465 sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 && 1466 x1 >= 0 && x2 >= 0 && x1+x2 <= from->common->w && 1467 y1 >= 0 && y2 >= 0 && y1+y2 <= from->common->h) { 1468 1469 x2 += x1; 1470 y2 += y1; 1471 val = (move[0] == 'F' ? GRID_FULL : 1472 move[0] == 'E' ? GRID_EMPTY : GRID_UNKNOWN); 1473 1474 ret = dup_game(from); 1475 for (yy = y1; yy < y2; yy++) 1476 for (xx = x1; xx < x2; xx++) 1477 if (!ret->common->immutable[yy * ret->common->w + xx]) 1478 ret->grid[yy * ret->common->w + xx] = val; 1479 1480 /* 1481 * An actual change, so check to see if we've completed the 1482 * game. 1483 */ 1484 if (!ret->completed) { 1485 int *rowdata = snewn(ret->common->rowsize, int); 1486 int i, len; 1487 1488 ret->completed = true; 1489 1490 for (i=0; i<ret->common->w; i++) { 1491 len = compute_rowdata(rowdata, ret->grid+i, 1492 ret->common->h, ret->common->w); 1493 if (len != ret->common->rowlen[i] || 1494 memcmp(ret->common->rowdata+i*ret->common->rowsize, 1495 rowdata, len * sizeof(int))) { 1496 ret->completed = false; 1497 break; 1498 } 1499 } 1500 for (i=0; i<ret->common->h; i++) { 1501 len = compute_rowdata(rowdata, ret->grid+i*ret->common->w, 1502 ret->common->w, 1); 1503 if (len != ret->common->rowlen[i+ret->common->w] || 1504 memcmp(ret->common->rowdata + 1505 (i+ret->common->w)*ret->common->rowsize, 1506 rowdata, len * sizeof(int))) { 1507 ret->completed = false; 1508 break; 1509 } 1510 } 1511 1512 sfree(rowdata); 1513 } 1514 1515 return ret; 1516 } else 1517 return NULL; 1518} 1519 1520/* ---------------------------------------------------------------------- 1521 * Error-checking during gameplay. 1522 */ 1523 1524/* 1525 * The difficulty in error-checking Pattern is to make the error check 1526 * _weak_ enough. The most obvious way would be to check each row and 1527 * column by calling (a modified form of) do_row() to recursively 1528 * analyse the row contents against the clue set and see if the 1529 * GRID_UNKNOWNs could be filled in in any way that would end up 1530 * correct. However, this turns out to be such a strong error check as 1531 * to constitute a spoiler in many situations: you make a typo while 1532 * trying to fill in one row, and not only does the row light up to 1533 * indicate an error, but several columns crossed by the move also 1534 * light up and draw your attention to deductions you hadn't even 1535 * noticed you could make. 1536 * 1537 * So instead I restrict error-checking to 'complete runs' within a 1538 * row, by which I mean contiguous sequences of GRID_FULL bounded at 1539 * both ends by either GRID_EMPTY or the ends of the row. We identify 1540 * all the complete runs in a row, and verify that _those_ are 1541 * consistent with the row's clue list. Sequences of complete runs 1542 * separated by solid GRID_EMPTY are required to match contiguous 1543 * sequences in the clue list, whereas if there's at least one 1544 * GRID_UNKNOWN between any two complete runs then those two need not 1545 * be contiguous in the clue list. 1546 * 1547 * To simplify the edge cases, I pretend that the clue list for the 1548 * row is extended with a 0 at each end, and I also pretend that the 1549 * grid data for the row is extended with a GRID_EMPTY and a 1550 * zero-length run at each end. This permits the contiguity checker to 1551 * handle the fiddly end effects (e.g. if the first contiguous 1552 * sequence of complete runs in the grid matches _something_ in the 1553 * clue list but not at the beginning, this is allowable iff there's a 1554 * GRID_UNKNOWN before the first one) with minimal faff, since the end 1555 * effects just drop out as special cases of the normal inter-run 1556 * handling (in this code the above case is not 'at the end of the 1557 * clue list' at all, but between the implicit initial zero run and 1558 * the first nonzero one). 1559 * 1560 * We must also be a little careful about how we search for a 1561 * contiguous sequence of runs. In the clue list (1 1 2 1 2 3), 1562 * suppose we see a GRID_UNKNOWN and then a length-1 run. We search 1563 * for 1 in the clue list and find it at the very beginning. But now 1564 * suppose we find a length-2 run with no GRID_UNKNOWN before it. We 1565 * can't naively look at the next clue from the 1 we found, because 1566 * that'll be the second 1 and won't match. Instead, we must backtrack 1567 * by observing that the 2 we've just found must be contiguous with 1568 * the 1 we've already seen, so we search for the sequence (1 2) and 1569 * find it starting at the second 1. Now if we see a 3, we must 1570 * rethink again and search for (1 2 3). 1571 */ 1572 1573struct errcheck_state { 1574 /* 1575 * rowdata and rowlen point at the clue data for this row in the 1576 * game state. 1577 */ 1578 int *rowdata; 1579 int rowlen; 1580 /* 1581 * rowpos indicates the lowest position where it would be valid to 1582 * see our next run length. It might be equal to rowlen, 1583 * indicating that the next run would have to be the terminating 0. 1584 */ 1585 int rowpos; 1586 /* 1587 * ncontig indicates how many runs we've seen in a contiguous 1588 * block. This is taken into account when searching for the next 1589 * run we find, unless ncontig is zeroed out first by encountering 1590 * a GRID_UNKNOWN. 1591 */ 1592 int ncontig; 1593}; 1594 1595static bool errcheck_found_run(struct errcheck_state *es, int r) 1596{ 1597/* Macro to handle the pretence that rowdata has a 0 at each end */ 1598#define ROWDATA(k) ((k)<0 || (k)>=es->rowlen ? 0 : es->rowdata[(k)]) 1599 1600 /* 1601 * See if we can find this new run length at a position where it 1602 * also matches the last 'ncontig' runs we've seen. 1603 */ 1604 int i, newpos; 1605 for (newpos = es->rowpos; newpos <= es->rowlen; newpos++) { 1606 1607 if (ROWDATA(newpos) != r) 1608 goto notfound; 1609 1610 for (i = 1; i <= es->ncontig; i++) 1611 if (ROWDATA(newpos - i) != ROWDATA(es->rowpos - i)) 1612 goto notfound; 1613 1614 es->rowpos = newpos+1; 1615 es->ncontig++; 1616 return true; 1617 1618 notfound:; 1619 } 1620 1621 return false; 1622 1623#undef ROWDATA 1624} 1625 1626static bool check_errors(const game_state *state, int i) 1627{ 1628 int start, step, end, j; 1629 int val, runlen; 1630 struct errcheck_state aes, *es = &aes; 1631 1632 es->rowlen = state->common->rowlen[i]; 1633 es->rowdata = state->common->rowdata + state->common->rowsize * i; 1634 /* Pretend that we've already encountered the initial zero run */ 1635 es->ncontig = 1; 1636 es->rowpos = 0; 1637 1638 if (i < state->common->w) { 1639 start = i; 1640 step = state->common->w; 1641 end = start + step * state->common->h; 1642 } else { 1643 start = (i - state->common->w) * state->common->w; 1644 step = 1; 1645 end = start + step * state->common->w; 1646 } 1647 1648 runlen = -1; 1649 for (j = start - step; j <= end; j += step) { 1650 if (j < start || j == end) 1651 val = GRID_EMPTY; 1652 else 1653 val = state->grid[j]; 1654 1655 if (val == GRID_UNKNOWN) { 1656 runlen = -1; 1657 es->ncontig = 0; 1658 } else if (val == GRID_FULL) { 1659 if (runlen >= 0) 1660 runlen++; 1661 } else if (val == GRID_EMPTY) { 1662 if (runlen > 0) { 1663 if (!errcheck_found_run(es, runlen)) 1664 return true; /* error! */ 1665 } 1666 runlen = 0; 1667 } 1668 } 1669 1670 /* Signal end-of-row by sending errcheck_found_run the terminating 1671 * zero run, which will be marked as contiguous with the previous 1672 * run if and only if there hasn't been a GRID_UNKNOWN before. */ 1673 if (!errcheck_found_run(es, 0)) 1674 return true; /* error at the last minute! */ 1675 1676 return false; /* no error */ 1677} 1678 1679/* ---------------------------------------------------------------------- 1680 * Drawing routines. 1681 */ 1682 1683static void game_compute_size(const game_params *params, int tilesize, 1684 const game_ui *ui, int *x, int *y) 1685{ 1686 /* Ick: fake up `ds->tilesize' for macro expansion purposes */ 1687 struct { int tilesize; } ads, *ds = &ads; 1688 ads.tilesize = tilesize; 1689 1690 *x = SIZE(params->w); 1691 *y = SIZE(params->h); 1692} 1693 1694static void game_set_size(drawing *dr, game_drawstate *ds, 1695 const game_params *params, int tilesize) 1696{ 1697 ds->tilesize = tilesize; 1698} 1699 1700static float *game_colours(frontend *fe, int *ncolours) 1701{ 1702 float *ret = snewn(3 * NCOLOURS, float); 1703 int i; 1704 1705 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); 1706 1707 for (i = 0; i < 3; i++) { 1708 ret[COL_GRID * 3 + i] = 0.3F; 1709 ret[COL_UNKNOWN * 3 + i] = 0.5F; 1710 ret[COL_TEXT * 3 + i] = 0.0F; 1711 ret[COL_FULL * 3 + i] = 0.0F; 1712 ret[COL_EMPTY * 3 + i] = 1.0F; 1713 ret[COL_CURSOR_GUIDE * 3 + i] = 0.5F; 1714 } 1715 ret[COL_CURSOR * 3 + 0] = 1.0F; 1716 ret[COL_CURSOR * 3 + 1] = 0.25F; 1717 ret[COL_CURSOR * 3 + 2] = 0.25F; 1718 ret[COL_ERROR * 3 + 0] = 1.0F; 1719 ret[COL_ERROR * 3 + 1] = 0.0F; 1720 ret[COL_ERROR * 3 + 2] = 0.0F; 1721 1722 *ncolours = NCOLOURS; 1723 return ret; 1724} 1725 1726static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state) 1727{ 1728 struct game_drawstate *ds = snew(struct game_drawstate); 1729 1730 ds->started = false; 1731 ds->w = state->common->w; 1732 ds->h = state->common->h; 1733 ds->visible = snewn(ds->w * ds->h, unsigned char); 1734 ds->tilesize = 0; /* not decided yet */ 1735 memset(ds->visible, 255, ds->w * ds->h); 1736 ds->numcolours = snewn(ds->w + ds->h, unsigned char); 1737 memset(ds->numcolours, 255, ds->w + ds->h); 1738 ds->cur_x = ds->cur_y = 0; 1739 ds->strbuf = snewn(state->common->rowsize * 1740 MAX_DIGITS(*state->common->rowdata) + 1, char); 1741 1742 return ds; 1743} 1744 1745static void game_free_drawstate(drawing *dr, game_drawstate *ds) 1746{ 1747 sfree(ds->visible); 1748 sfree(ds->numcolours); 1749 sfree(ds->strbuf); 1750 sfree(ds); 1751} 1752 1753static void grid_square(drawing *dr, game_drawstate *ds, 1754 int y, int x, int state, bool cur) 1755{ 1756 int xl, xr, yt, yb, dx, dy, dw, dh; 1757 1758 draw_rect(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y), 1759 TILE_SIZE, TILE_SIZE, COL_GRID); 1760 1761 xl = (x % 5 == 0 ? 1 : 0); 1762 yt = (y % 5 == 0 ? 1 : 0); 1763 xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0); 1764 yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0); 1765 1766 dx = TOCOORD(ds->w, x) + 1 + xl; 1767 dy = TOCOORD(ds->h, y) + 1 + yt; 1768 dw = TILE_SIZE - xl - xr - 1; 1769 dh = TILE_SIZE - yt - yb - 1; 1770 1771 draw_rect(dr, dx, dy, dw, dh, 1772 (state == GRID_FULL ? COL_FULL : 1773 state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN)); 1774 if (cur) { 1775 draw_rect_outline(dr, dx, dy, dw, dh, COL_CURSOR); 1776 draw_rect_outline(dr, dx+1, dy+1, dw-2, dh-2, COL_CURSOR); 1777 } 1778 1779 draw_update(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y), 1780 TILE_SIZE, TILE_SIZE); 1781} 1782 1783/* 1784 * Draw the numbers for a single row or column. 1785 */ 1786static void draw_numbers( 1787 drawing *dr, game_drawstate *ds, const game_state *state, 1788 int i, bool erase, int colour) 1789{ 1790 int rowlen = state->common->rowlen[i]; 1791 int *rowdata = state->common->rowdata + state->common->rowsize * i; 1792 int nfit; 1793 int j; 1794 int rx, ry, rw, rh; 1795 int fontsize; 1796 1797 if (i < state->common->w) { 1798 rx = TOCOORD(state->common->w, i); 1799 ry = 0; 1800 rw = TILE_SIZE; 1801 rh = BORDER + TLBORDER(state->common->h) * TILE_SIZE; 1802 } else { 1803 rx = 0; 1804 ry = TOCOORD(state->common->h, i - state->common->w); 1805 rw = BORDER + TLBORDER(state->common->w) * TILE_SIZE; 1806 rh = TILE_SIZE; 1807 } 1808 1809 clip(dr, rx, ry, rw, rh); 1810 if (erase) 1811 draw_rect(dr, rx, ry, rw, rh, COL_BACKGROUND); 1812 1813 /* 1814 * Choose a font size that's suitable for the lengths of clue. 1815 * Only column clues are interesting because row clues can be 1816 * spaced out independent of the tile size. For column clues, we 1817 * want to go as large as practical while leaving decent space 1818 * between horizintally adjacent clues. We currently distinguish 1819 * two cases: FS_LARGE is when all column clues are single digits, 1820 * and FS_SMALL in all other cases. 1821 * 1822 * If we assume that a digit is about 0.6em wide, and we want 1823 * about that space between clues, then FS_LARGE should be 1824 * TILESIZE/1.2. If we also assume that clues are at most two 1825 * digits long then the case where adjacent clues are two digits 1826 * long requries FS_SMALL to be TILESIZE/1.8. 1827 */ 1828 fontsize = (TILE_SIZE + 0.5F) / 1829 (state->common->fontsize == FS_LARGE ? 1.2F : 1.8F); 1830 1831 /* 1832 * Normally I space the numbers out by the same distance as the 1833 * tile size. However, if there are more numbers than available 1834 * spaces, I have to squash them up a bit. 1835 */ 1836 if (i < state->common->w) 1837 nfit = TLBORDER(state->common->h); 1838 else 1839 nfit = TLBORDER(state->common->w); 1840 nfit = max(rowlen, nfit) - 1; 1841 assert(nfit > 0); 1842 1843 if (i < state->common->w) { 1844 for (j = 0; j < rowlen; j++) { 1845 int x, y; 1846 char str[MAX_DIGITS(*rowdata) + 1]; 1847 1848 x = rx; 1849 y = BORDER + TILE_SIZE * (TLBORDER(state->common->h)-1); 1850 y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->common->h)-1) / nfit; 1851 sprintf(str, "%d", rowdata[j]); 1852 draw_text(dr, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE, 1853 fontsize, ALIGN_HCENTRE | ALIGN_VCENTRE, colour, str); 1854 } 1855 } else { 1856 int x, y; 1857 size_t off = 0; 1858 const char *spaces = " "; 1859 1860 assert(rowlen <= state->common->rowsize); 1861 *ds->strbuf = '\0'; 1862 /* Squish up a bit if there are lots of clues. */ 1863 if (rowlen > TLBORDER(state->common->w)) spaces++; 1864 for (j = 0; j < rowlen; j++) 1865 off += sprintf(ds->strbuf + off, "%s%d", 1866 j ? spaces : "", rowdata[j]); 1867 y = ry; 1868 x = BORDER + TILE_SIZE * (TLBORDER(state->common->w)-1); 1869 draw_text(dr, x+TILE_SIZE, y+TILE_SIZE/2, FONT_VARIABLE, 1870 fontsize, ALIGN_HRIGHT | ALIGN_VCENTRE, colour, ds->strbuf); 1871 } 1872 1873 unclip(dr); 1874 draw_update(dr, rx, ry, rw, rh); 1875} 1876 1877static void game_redraw(drawing *dr, game_drawstate *ds, 1878 const game_state *oldstate, const game_state *state, 1879 int dir, const game_ui *ui, 1880 float animtime, float flashtime) 1881{ 1882 int i, j; 1883 int x1, x2, y1, y2; 1884 int cx, cy; 1885 bool cmoved; 1886 1887 if (!ds->started) { 1888 /* 1889 * Draw the grid outline. 1890 */ 1891 draw_rect(dr, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1, 1892 ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3, 1893 COL_GRID); 1894 1895 ds->started = true; 1896 1897 draw_update(dr, 0, 0, SIZE(ds->w), SIZE(ds->h)); 1898 } 1899 1900 if (ui->dragging) { 1901 x1 = min(ui->drag_start_x, ui->drag_end_x); 1902 x2 = max(ui->drag_start_x, ui->drag_end_x); 1903 y1 = min(ui->drag_start_y, ui->drag_end_y); 1904 y2 = max(ui->drag_start_y, ui->drag_end_y); 1905 } else { 1906 x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */ 1907 } 1908 1909 if (ui->cur_visible) { 1910 cx = ui->cur_x; cy = ui->cur_y; 1911 } else { 1912 cx = cy = -1; 1913 } 1914 cmoved = (cx != ds->cur_x || cy != ds->cur_y); 1915 1916 /* 1917 * Now draw any grid squares which have changed since last 1918 * redraw. 1919 */ 1920 for (i = 0; i < ds->h; i++) { 1921 for (j = 0; j < ds->w; j++) { 1922 int val; 1923 bool cc = false; 1924 1925 /* 1926 * Work out what state this square should be drawn in, 1927 * taking any current drag operation into account. 1928 */ 1929 if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2 && 1930 !state->common->immutable[i * state->common->w + j]) 1931 val = ui->state; 1932 else 1933 val = state->grid[i * state->common->w + j]; 1934 1935 if (cmoved) { 1936 /* the cursor has moved; if we were the old or 1937 * the new cursor position we need to redraw. */ 1938 if (j == cx && i == cy) cc = true; 1939 if (j == ds->cur_x && i == ds->cur_y) cc = true; 1940 } 1941 1942 /* 1943 * Briefly invert everything twice during a completion 1944 * flash. 1945 */ 1946 if (flashtime > 0 && 1947 (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) && 1948 val != GRID_UNKNOWN) 1949 val = (GRID_FULL ^ GRID_EMPTY) ^ val; 1950 1951 if (ds->visible[i * ds->w + j] != val || cc) { 1952 grid_square(dr, ds, i, j, val, 1953 (j == cx && i == cy)); 1954 ds->visible[i * ds->w + j] = val; 1955 } 1956 } 1957 } 1958 ds->cur_x = cx; ds->cur_y = cy; 1959 1960 /* 1961 * Redraw any numbers which have changed their colour due to error 1962 * indication. 1963 */ 1964 for (i = 0; i < state->common->w + state->common->h; i++) { 1965 int colour = check_errors(state, i) ? COL_ERROR : COL_TEXT; 1966 if (colour == COL_TEXT && ((cx >= 0 && i == cx) || (cy >= 0 && i == cy + ds->w))) { 1967 colour = COL_CURSOR_GUIDE; 1968 } 1969 if (ds->numcolours[i] != colour) { 1970 draw_numbers(dr, ds, state, i, true, colour); 1971 ds->numcolours[i] = colour; 1972 } 1973 } 1974} 1975 1976static float game_anim_length(const game_state *oldstate, 1977 const game_state *newstate, int dir, game_ui *ui) 1978{ 1979 return 0.0F; 1980} 1981 1982static float game_flash_length(const game_state *oldstate, 1983 const game_state *newstate, int dir, game_ui *ui) 1984{ 1985 if (!oldstate->completed && newstate->completed && 1986 !oldstate->cheated && !newstate->cheated) 1987 return FLASH_TIME; 1988 return 0.0F; 1989} 1990 1991static void game_get_cursor_location(const game_ui *ui, 1992 const game_drawstate *ds, 1993 const game_state *state, 1994 const game_params *params, 1995 int *x, int *y, int *w, int *h) 1996{ 1997 if(ui->cur_visible) { 1998 *x = TOCOORD(ds->w, ui->cur_x); 1999 *y = TOCOORD(ds->h, ui->cur_y); 2000 *w = *h = TILE_SIZE; 2001 } 2002} 2003 2004static int game_status(const game_state *state) 2005{ 2006 return state->completed ? +1 : 0; 2007} 2008 2009static void game_print_size(const game_params *params, const game_ui *ui, 2010 float *x, float *y) 2011{ 2012 int pw, ph; 2013 2014 /* 2015 * I'll use 5mm squares by default. 2016 */ 2017 game_compute_size(params, 500, ui, &pw, &ph); 2018 *x = pw / 100.0F; 2019 *y = ph / 100.0F; 2020} 2021 2022static void game_print(drawing *dr, const game_state *state, const game_ui *ui, 2023 int tilesize) 2024{ 2025 int w = state->common->w, h = state->common->h; 2026 int ink = print_mono_colour(dr, 0); 2027 int x, y, i; 2028 2029 /* 2030 * Make a game_drawstate, so that the TILE_SIZE macro will work in 2031 * this function, and so that draw_numbers can use it to format 2032 * the text for numeric clues. 2033 */ 2034 game_drawstate *ds = game_new_drawstate(dr, state); 2035 game_set_size(dr, ds, NULL, tilesize); 2036 2037 /* 2038 * Border. 2039 */ 2040 print_line_width(dr, TILE_SIZE / 16); 2041 draw_rect_outline(dr, TOCOORD(w, 0), TOCOORD(h, 0), 2042 w*TILE_SIZE, h*TILE_SIZE, ink); 2043 2044 /* 2045 * Grid. 2046 */ 2047 for (x = 1; x < w; x++) { 2048 print_line_width(dr, TILE_SIZE / (x % 5 ? 128 : 24)); 2049 draw_line(dr, TOCOORD(w, x), TOCOORD(h, 0), 2050 TOCOORD(w, x), TOCOORD(h, h), ink); 2051 } 2052 for (y = 1; y < h; y++) { 2053 print_line_width(dr, TILE_SIZE / (y % 5 ? 128 : 24)); 2054 draw_line(dr, TOCOORD(w, 0), TOCOORD(h, y), 2055 TOCOORD(w, w), TOCOORD(h, y), ink); 2056 } 2057 2058 /* 2059 * Clues. 2060 */ 2061 for (i = 0; i < state->common->w + state->common->h; i++) 2062 draw_numbers(dr, ds, state, i, false, ink); 2063 2064 /* 2065 * Solution. 2066 */ 2067 print_line_width(dr, TILE_SIZE / 128); 2068 for (y = 0; y < h; y++) 2069 for (x = 0; x < w; x++) { 2070 if (state->grid[y*w+x] == GRID_FULL) 2071 draw_rect(dr, TOCOORD(w, x), TOCOORD(h, y), 2072 TILE_SIZE, TILE_SIZE, ink); 2073 else if (state->grid[y*w+x] == GRID_EMPTY) 2074 draw_circle(dr, TOCOORD(w, x) + TILE_SIZE/2, 2075 TOCOORD(h, y) + TILE_SIZE/2, 2076 TILE_SIZE/12, ink, ink); 2077 } 2078 2079 game_free_drawstate(dr, ds); 2080} 2081 2082#ifdef COMBINED 2083#define thegame pattern 2084#endif 2085 2086const struct game thegame = { 2087 "Pattern", "games.pattern", "pattern", 2088 default_params, 2089 game_fetch_preset, NULL, 2090 decode_params, 2091 encode_params, 2092 free_params, 2093 dup_params, 2094 true, game_configure, custom_params, 2095 validate_params, 2096 new_game_desc, 2097 validate_desc, 2098 new_game, 2099 dup_game, 2100 free_game, 2101 true, solve_game, 2102 true, game_can_format_as_text_now, game_text_format, 2103 NULL, NULL, /* get_prefs, set_prefs */ 2104 new_ui, 2105 free_ui, 2106 NULL, /* encode_ui */ 2107 NULL, /* decode_ui */ 2108 NULL, /* game_request_keys */ 2109 game_changed_state, 2110 current_key_label, 2111 interpret_move, 2112 execute_move, 2113 PREFERRED_TILE_SIZE, game_compute_size, game_set_size, 2114 game_colours, 2115 game_new_drawstate, 2116 game_free_drawstate, 2117 game_redraw, 2118 game_anim_length, 2119 game_flash_length, 2120 game_get_cursor_location, 2121 game_status, 2122 true, false, game_print_size, game_print, 2123 false, /* wants_statusbar */ 2124 false, NULL, /* timing_state */ 2125 REQUIRE_RBUTTON, /* flags */ 2126}; 2127 2128#ifdef STANDALONE_SOLVER 2129 2130int main(int argc, char **argv) 2131{ 2132 game_params *p; 2133 game_state *s; 2134 char *id = NULL, *desc; 2135 const char *err; 2136 2137 while (--argc > 0) { 2138 char *p = *++argv; 2139 if (*p == '-') { 2140 if (!strcmp(p, "-v")) { 2141 verbose = true; 2142 } else { 2143 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); 2144 return 1; 2145 } 2146 } else { 2147 id = p; 2148 } 2149 } 2150 2151 if (!id) { 2152 fprintf(stderr, "usage: %s <game_id>\n", argv[0]); 2153 return 1; 2154 } 2155 2156 desc = strchr(id, ':'); 2157 if (!desc) { 2158 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); 2159 return 1; 2160 } 2161 *desc++ = '\0'; 2162 2163 p = default_params(); 2164 decode_params(p, id); 2165 err = validate_desc(p, desc); 2166 if (err) { 2167 fprintf(stderr, "%s: %s\n", argv[0], err); 2168 return 1; 2169 } 2170 s = new_game(NULL, p, desc); 2171 2172 { 2173 int w = p->w, h = p->h, i, j, max, cluewid = 0; 2174 unsigned char *matrix, *workspace; 2175 unsigned int *changed_h, *changed_w; 2176 int *rowdata; 2177 2178 matrix = snewn(w*h, unsigned char); 2179 max = max(w, h); 2180 workspace = snewn(max*7, unsigned char); 2181 changed_h = snewn(max+1, unsigned int); 2182 changed_w = snewn(max+1, unsigned int); 2183 rowdata = snewn(max+1, int); 2184 2185 if (verbose) { 2186 int thiswid; 2187 /* 2188 * Work out the maximum text width of the clue numbers 2189 * in a row or column, so we can print the solver's 2190 * working in a nicely lined up way. 2191 */ 2192 for (i = 0; i < (w+h); i++) { 2193 char buf[80]; 2194 for (thiswid = -1, j = 0; j < s->common->rowlen[i]; j++) 2195 thiswid += sprintf 2196 (buf, " %d", 2197 s->common->rowdata[s->common->rowsize*i+j]); 2198 if (cluewid < thiswid) 2199 cluewid = thiswid; 2200 } 2201 } 2202 2203 solve_puzzle(s, NULL, w, h, matrix, workspace, 2204 changed_h, changed_w, rowdata, cluewid); 2205 2206 for (i = 0; i < h; i++) { 2207 for (j = 0; j < w; j++) { 2208 int c = (matrix[i*w+j] == UNKNOWN ? '?' : 2209 matrix[i*w+j] == BLOCK ? '#' : 2210 matrix[i*w+j] == DOT ? '.' : 2211 '!'); 2212 putchar(c); 2213 } 2214 printf("\n"); 2215 } 2216 } 2217 2218 return 0; 2219} 2220 2221#endif 2222 2223#ifdef STANDALONE_PICTURE_GENERATOR 2224 2225/* 2226 * Main program for the standalone picture generator. To use it, 2227 * simply provide it with an XBM-format bitmap file (note XBM, not 2228 * XPM) on standard input, and it will output a game ID in return. 2229 * For example: 2230 * 2231 * $ ./patternpicture < calligraphic-A.xbm 2232 * 15x15:2/4/2/2/2/3/3/3.1/3.1/3.1/11/14/12/6/1/2/2/3/4/5/1.3/2.3/1.3/2.3/1.4/9/1.1.3/2.2.3/5.4/3.2 2233 * 2234 * That looks easy, of course - all the program has done is to count 2235 * up the clue numbers! But in fact, it's done more than that: it's 2236 * also checked that the result is uniquely soluble from just the 2237 * numbers. If it hadn't been, then it would have also left some 2238 * filled squares in the playing area as extra clues. 2239 * 2240 * $ ./patternpicture < cube.xbm 2241 * 15x15:10/2.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.10/1.1.1/1.1.1/1.1.1/2.1/10/10/1.2/1.1.1/1.1.1/1.1.1/10.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.2/10,TNINzzzzGNzw 2242 * 2243 * This enables a reasonably convenient design workflow for coming up 2244 * with pictorial Pattern puzzles which _are_ uniquely soluble without 2245 * those inelegant pre-filled squares. Fire up a bitmap editor (X11 2246 * bitmap(1) is good enough), save a trial .xbm, and then test it by 2247 * running a command along the lines of 2248 * 2249 * $ ./pattern $(./patternpicture < test.xbm) 2250 * 2251 * If the resulting window pops up with some pre-filled squares, then 2252 * that tells you which parts of the image are giving rise to 2253 * ambiguities, so try making tweaks in those areas, try the test 2254 * command again, and see if it helps. Once you have a design for 2255 * which the Pattern starting grid comes out empty, there's your game 2256 * ID. 2257 */ 2258 2259#include <time.h> 2260 2261int main(int argc, char **argv) 2262{ 2263 game_params *par; 2264 char *params, *desc; 2265 random_state *rs; 2266 time_t seed = time(NULL); 2267 char buf[4096]; 2268 int i; 2269 int x, y; 2270 2271 par = default_params(); 2272 if (argc > 1) 2273 decode_params(par, argv[1]); /* get difficulty */ 2274 par->w = par->h = -1; 2275 2276 /* 2277 * Now read an XBM file from standard input. This is simple and 2278 * hacky and will do very little error detection, so don't feed 2279 * it bogus data. 2280 */ 2281 picture = NULL; 2282 x = y = 0; 2283 while (fgets(buf, sizeof(buf), stdin)) { 2284 buf[strcspn(buf, "\r\n")] = '\0'; 2285 if (!strncmp(buf, "#define", 7)) { 2286 /* 2287 * Lines starting `#define' give the width and height. 2288 */ 2289 char *num = buf + strlen(buf); 2290 char *symend; 2291 2292 while (num > buf && isdigit((unsigned char)num[-1])) 2293 num--; 2294 symend = num; 2295 while (symend > buf && isspace((unsigned char)symend[-1])) 2296 symend--; 2297 2298 if (symend-5 >= buf && !strncmp(symend-5, "width", 5)) 2299 par->w = atoi(num); 2300 else if (symend-6 >= buf && !strncmp(symend-6, "height", 6)) 2301 par->h = atoi(num); 2302 } else { 2303 /* 2304 * Otherwise, break the string up into words and take 2305 * any word of the form `0x' plus hex digits to be a 2306 * byte. 2307 */ 2308 char *p, *wordstart; 2309 2310 if (!picture) { 2311 if (par->w < 0 || par->h < 0) { 2312 printf("failed to read width and height\n"); 2313 return 1; 2314 } 2315 picture = snewn(par->w * par->h, unsigned char); 2316 for (i = 0; i < par->w * par->h; i++) 2317 picture[i] = GRID_UNKNOWN; 2318 } 2319 2320 p = buf; 2321 while (*p) { 2322 while (*p && (*p == ',' || isspace((unsigned char)*p))) 2323 p++; 2324 wordstart = p; 2325 while (*p && !(*p == ',' || *p == '}' || 2326 isspace((unsigned char)*p))) 2327 p++; 2328 if (*p) 2329 *p++ = '\0'; 2330 2331 if (wordstart[0] == '0' && 2332 (wordstart[1] == 'x' || wordstart[1] == 'X') && 2333 !wordstart[2 + strspn(wordstart+2, 2334 "0123456789abcdefABCDEF")]) { 2335 unsigned long byte = strtoul(wordstart+2, NULL, 16); 2336 for (i = 0; i < 8; i++) { 2337 int bit = (byte >> i) & 1; 2338 if (y < par->h && x < par->w) 2339 picture[y * par->w + x] = 2340 bit ? GRID_FULL : GRID_EMPTY; 2341 x++; 2342 } 2343 2344 if (x >= par->w) { 2345 x = 0; 2346 y++; 2347 } 2348 } 2349 } 2350 } 2351 } 2352 2353 for (i = 0; i < par->w * par->h; i++) 2354 if (picture[i] == GRID_UNKNOWN) { 2355 fprintf(stderr, "failed to read enough bitmap data\n"); 2356 return 1; 2357 } 2358 2359 rs = random_new((void*)&seed, sizeof(time_t)); 2360 2361 desc = new_game_desc(par, rs, NULL, false); 2362 params = encode_params(par, false); 2363 printf("%s:%s\n", params, desc); 2364 2365 sfree(desc); 2366 sfree(params); 2367 free_params(par); 2368 random_free(rs); 2369 2370 return 0; 2371} 2372 2373#endif 2374 2375/* vim: set shiftwidth=4 tabstop=8: */