A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 3910 lines 130 kB view raw
1/* 2 * loopy.c: 3 * 4 * An implementation of the Nikoli game 'Loop the loop'. 5 * (c) Mike Pinna, 2005, 2006 6 * Substantially rewritten to allowing for more general types of grid. 7 * (c) Lambros Lambrou 2008 8 * 9 * vim: set shiftwidth=4 :set textwidth=80: 10 */ 11 12/* 13 * Possible future solver enhancements: 14 * 15 * - There's an interesting deductive technique which makes use 16 * of topology rather than just graph theory. Each _face_ in 17 * the grid is either inside or outside the loop; you can tell 18 * that two faces are on the same side of the loop if they're 19 * separated by a LINE_NO (or, more generally, by a path 20 * crossing no LINE_UNKNOWNs and an even number of LINE_YESes), 21 * and on the opposite side of the loop if they're separated by 22 * a LINE_YES (or an odd number of LINE_YESes and no 23 * LINE_UNKNOWNs). Oh, and any face separated from the outside 24 * of the grid by a LINE_YES or a LINE_NO is on the inside or 25 * outside respectively. So if you can track this for all 26 * faces, you figure out the state of the line between a pair 27 * once their relative insideness is known. 28 * + The way I envisage this working is simply to keep a flip dsf 29 * of all _faces_, which indicates whether they're on 30 * opposite sides of the loop from one another. We also 31 * include a special entry in the dsf for the infinite 32 * exterior "face". 33 * + So, the simple way to do this is to just go through the 34 * edges: every time we see an edge in a state other than 35 * LINE_UNKNOWN which separates two faces that aren't in the 36 * same dsf class, we can rectify that by merging the 37 * classes. Then, conversely, an edge in LINE_UNKNOWN state 38 * which separates two faces that _are_ in the same dsf 39 * class can immediately have its state determined. 40 * + But you can go one better, if you're prepared to loop 41 * over all _pairs_ of edges. Suppose we have edges A and B, 42 * which respectively separate faces A1,A2 and B1,B2. 43 * Suppose that A,B are in the same edge-dsf class and that 44 * A1,B1 (wlog) are in the same face-dsf class; then we can 45 * immediately place A2,B2 into the same face-dsf class (as 46 * each other, not as A1 and A2) one way round or the other. 47 * And conversely again, if A1,B1 are in the same face-dsf 48 * class and so are A2,B2, then we can put A,B into the same 49 * face-dsf class. 50 * * Of course, this deduction requires a quadratic-time 51 * loop over all pairs of edges in the grid, so it should 52 * be reserved until there's nothing easier left to be 53 * done. 54 * 55 * - The generalised grid support has made me (SGT) notice a 56 * possible extension to the loop-avoidance code. When you have 57 * a path of connected edges such that no other edges at all 58 * are incident on any vertex in the middle of the path - or, 59 * alternatively, such that any such edges are already known to 60 * be LINE_NO - then you know those edges are either all 61 * LINE_YES or all LINE_NO. Hence you can mentally merge the 62 * entire path into a single long curly edge for the purposes 63 * of loop avoidance, and look directly at whether or not the 64 * extreme endpoints of the path are connected by some other 65 * route. I find this coming up fairly often when I play on the 66 * octagonal grid setting, so it might be worth implementing in 67 * the solver. 68 * 69 * - (Just a speed optimisation.) Consider some todo list queue where every 70 * time we modify something we mark it for consideration by other bits of 71 * the solver, to save iteration over things that have already been done. 72 */ 73 74#include <stdio.h> 75#include <stdlib.h> 76#include <stddef.h> 77#include <string.h> 78#include <assert.h> 79#include <ctype.h> 80#ifdef NO_TGMATH_H 81# include <math.h> 82#else 83# include <tgmath.h> 84#endif 85 86#include "puzzles.h" 87#include "tree234.h" 88#include "grid.h" 89#include "loopgen.h" 90 91/* Debugging options */ 92 93/* 94#define DEBUG_CACHES 95#define SHOW_WORKING 96#define DEBUG_DLINES 97*/ 98 99/* ---------------------------------------------------------------------- 100 * Struct, enum and function declarations 101 */ 102 103enum { 104 COL_BACKGROUND, 105 COL_FOREGROUND, 106 COL_LINEUNKNOWN, 107 COL_HIGHLIGHT, 108 COL_MISTAKE, 109 COL_SATISFIED, 110 COL_FAINT, 111 NCOLOURS 112}; 113 114enum { 115 PREF_DRAW_FAINT_LINES, 116 PREF_AUTO_FOLLOW, 117 N_PREF_ITEMS 118}; 119 120struct game_state { 121 grid *game_grid; /* ref-counted (internally) */ 122 123 /* Put -1 in a face that doesn't get a clue */ 124 signed char *clues; 125 126 /* Array of line states, to store whether each line is 127 * YES, NO or UNKNOWN */ 128 char *lines; 129 130 bool *line_errors; 131 bool exactly_one_loop; 132 133 bool solved; 134 bool cheated; 135 136 /* Used in game_text_format(), so that it knows what type of 137 * grid it's trying to render as ASCII text. */ 138 int grid_type; 139}; 140 141enum solver_status { 142 SOLVER_SOLVED, /* This is the only solution the solver could find */ 143 SOLVER_MISTAKE, /* This is definitely not a solution */ 144 SOLVER_AMBIGUOUS, /* This _might_ be an ambiguous solution */ 145 SOLVER_INCOMPLETE /* This may be a partial solution */ 146}; 147 148/* ------ Solver state ------ */ 149typedef struct solver_state { 150 game_state *state; 151 enum solver_status solver_status; 152 /* NB looplen is the number of dots that are joined together at a point, ie a 153 * looplen of 1 means there are no lines to a particular dot */ 154 int *looplen; 155 156 /* Difficulty level of solver. Used by solver functions that want to 157 * vary their behaviour depending on the requested difficulty level. */ 158 int diff; 159 160 /* caches */ 161 char *dot_yes_count; 162 char *dot_no_count; 163 char *face_yes_count; 164 char *face_no_count; 165 bool *dot_solved, *face_solved; 166 DSF *dotdsf; 167 168 /* Information for Normal level deductions: 169 * For each dline, store a bitmask for whether we know: 170 * (bit 0) at least one is YES 171 * (bit 1) at most one is YES */ 172 char *dlines; 173 174 /* Hard level information */ 175 DSF *linedsf; 176} solver_state; 177 178/* 179 * Difficulty levels. I do some macro ickery here to ensure that my 180 * enum and the various forms of my name list always match up. 181 */ 182 183#define DIFFLIST(A) \ 184 A(EASY,Easy,e) \ 185 A(NORMAL,Normal,n) \ 186 A(TRICKY,Tricky,t) \ 187 A(HARD,Hard,h) 188#define ENUM(upper,title,lower) DIFF_ ## upper, 189#define TITLE(upper,title,lower) #title, 190#define ENCODE(upper,title,lower) #lower 191#define CONFIG(upper,title,lower) ":" #title 192enum { DIFFLIST(ENUM) DIFF_MAX }; 193static char const *const diffnames[] = { DIFFLIST(TITLE) }; 194static char const diffchars[] = DIFFLIST(ENCODE); 195#define DIFFCONFIG DIFFLIST(CONFIG) 196 197/* 198 * Solver routines, sorted roughly in order of computational cost. 199 * The solver will run the faster deductions first, and slower deductions are 200 * only invoked when the faster deductions are unable to make progress. 201 * Each function is associated with a difficulty level, so that the generated 202 * puzzles are solvable by applying only the functions with the chosen 203 * difficulty level or lower. 204 */ 205#define SOLVERLIST(A) \ 206 A(trivial_deductions, DIFF_EASY) \ 207 A(dline_deductions, DIFF_NORMAL) \ 208 A(linedsf_deductions, DIFF_HARD) \ 209 A(loop_deductions, DIFF_EASY) 210#define SOLVER_FN_DECL(fn,diff) static int fn(solver_state *); 211#define SOLVER_FN(fn,diff) &fn, 212#define SOLVER_DIFF(fn,diff) diff, 213SOLVERLIST(SOLVER_FN_DECL) 214static int (*(solver_fns[]))(solver_state *) = { SOLVERLIST(SOLVER_FN) }; 215static int const solver_diffs[] = { SOLVERLIST(SOLVER_DIFF) }; 216static const int NUM_SOLVERS = sizeof(solver_diffs)/sizeof(*solver_diffs); 217 218struct game_params { 219 int w, h; 220 int diff; 221 int type; 222}; 223 224/* line_drawstate is the same as line_state, but with the extra ERROR 225 * possibility. The drawing code copies line_state to line_drawstate, 226 * except in the case that the line is an error. */ 227enum line_state { LINE_YES, LINE_UNKNOWN, LINE_NO }; 228enum line_drawstate { DS_LINE_YES, DS_LINE_UNKNOWN, 229 DS_LINE_NO, DS_LINE_ERROR }; 230 231#define OPP(line_state) \ 232 (2 - line_state) 233 234 235struct game_drawstate { 236 bool started; 237 int tilesize; 238 bool flashing; 239 int *textx, *texty; 240 char *lines; 241 bool *clue_error; 242 bool *clue_satisfied; 243}; 244 245static const char *validate_desc(const game_params *params, const char *desc); 246static int dot_order(const game_state* state, int i, char line_type); 247static int face_order(const game_state* state, int i, char line_type); 248static solver_state *solve_game_rec(const solver_state *sstate); 249 250#ifdef DEBUG_CACHES 251static void check_caches(const solver_state* sstate); 252#else 253#define check_caches(s) 254#endif 255 256/* 257 * Grid type config options available in Loopy. 258 * 259 * Annoyingly, we have to use an enum here which doesn't match up 260 * exactly to the grid-type enum in grid.h. Values in params->types 261 * are given by names such as LOOPY_GRID_SQUARE, which shouldn't be 262 * confused with GRID_SQUARE which is the value you pass to grid_new() 263 * and friends. So beware! 264 * 265 * (This is partly for historical reasons - Loopy's version of the 266 * enum is encoded in game parameter strings, so we keep it for 267 * backwards compatibility. But also, we need to store additional data 268 * here alongside each enum value, such as names for the presets menu, 269 * which isn't stored in grid.h; so we have to have our own list macro 270 * here anyway, and C doesn't make it easy to enforce that that lines 271 * up exactly with grid.h.) 272 * 273 * Do not add values to this list _except_ at the end, or old game ids 274 * will stop working! 275 */ 276#define GRIDLIST(A) \ 277 A("Squares",SQUARE,3,3) \ 278 A("Triangular",TRIANGULAR,3,3) \ 279 A("Honeycomb",HONEYCOMB,3,3) \ 280 A("Snub-Square",SNUBSQUARE,3,3) \ 281 A("Cairo",CAIRO,3,4) \ 282 A("Great-Hexagonal",GREATHEXAGONAL,3,3) \ 283 A("Octagonal",OCTAGONAL,3,3) \ 284 A("Kites",KITE,3,3) \ 285 A("Floret",FLORET,1,2) \ 286 A("Dodecagonal",DODECAGONAL,2,2) \ 287 A("Great-Dodecagonal",GREATDODECAGONAL,2,2) \ 288 A("Penrose (kite/dart)",PENROSE_P2,3,3) \ 289 A("Penrose (rhombs)",PENROSE_P3,3,3) \ 290 A("Great-Great-Dodecagonal",GREATGREATDODECAGONAL,2,2) \ 291 A("Kagome",KAGOME,3,3) \ 292 A("Compass-Dodecagonal",COMPASSDODECAGONAL,2,2) \ 293 A("Hats",HATS,6,6) \ 294 A("Spectres",SPECTRES,6,6) \ 295 /* end of list */ 296 297#define GRID_NAME(title,type,amin,omin) title, 298#define GRID_CONFIG(title,type,amin,omin) ":" title 299#define GRID_LOOPYTYPE(title,type,amin,omin) LOOPY_GRID_ ## type, 300#define GRID_GRIDTYPE(title,type,amin,omin) GRID_ ## type, 301#define GRID_SIZES(title,type,amin,omin) \ 302 {amin, omin, \ 303 "Width and height for this grid type must both be at least " #amin, \ 304 "At least one of width and height for this grid type must be at least " #omin,}, 305enum { GRIDLIST(GRID_LOOPYTYPE) LOOPY_GRID_DUMMY_TERMINATOR }; 306static char const *const gridnames[] = { GRIDLIST(GRID_NAME) }; 307#define GRID_CONFIGS GRIDLIST(GRID_CONFIG) 308static grid_type grid_types[] = { GRIDLIST(GRID_GRIDTYPE) }; 309#define NUM_GRID_TYPES (sizeof(grid_types) / sizeof(grid_types[0])) 310static const struct { 311 int amin, omin; 312 const char *aerr, *oerr; 313} grid_size_limits[] = { GRIDLIST(GRID_SIZES) }; 314 315/* Generates a (dynamically allocated) new grid, according to the 316 * type and size requested in params. Does nothing if the grid is already 317 * generated. */ 318static grid *loopy_generate_grid(const game_params *params, 319 const char *grid_desc) 320{ 321 return grid_new(grid_types[params->type], params->w, params->h, grid_desc); 322} 323 324/* ---------------------------------------------------------------------- 325 * Preprocessor magic 326 */ 327 328/* General constants */ 329#define PREFERRED_TILE_SIZE 32 330#define BORDER(tilesize) ((tilesize) / 2) 331#define FLASH_TIME 0.5F 332 333#define BIT_SET(field, bit) ((field) & (1<<(bit))) 334 335#define SET_BIT(field, bit) (BIT_SET(field, bit) ? false : \ 336 ((field) |= (1<<(bit)), true)) 337 338#define CLEAR_BIT(field, bit) (BIT_SET(field, bit) ? \ 339 ((field) &= ~(1<<(bit)), true) : false) 340 341#define CLUE2CHAR(c) \ 342 ((c < 0) ? ' ' : c < 10 ? c + '0' : c - 10 + 'A') 343 344/* ---------------------------------------------------------------------- 345 * General struct manipulation and other straightforward code 346 */ 347 348static game_state *dup_game(const game_state *state) 349{ 350 game_state *ret = snew(game_state); 351 352 ret->game_grid = state->game_grid; 353 ret->game_grid->refcount++; 354 355 ret->solved = state->solved; 356 ret->cheated = state->cheated; 357 358 ret->clues = snewn(state->game_grid->num_faces, signed char); 359 memcpy(ret->clues, state->clues, state->game_grid->num_faces); 360 361 ret->lines = snewn(state->game_grid->num_edges, char); 362 memcpy(ret->lines, state->lines, state->game_grid->num_edges); 363 364 ret->line_errors = snewn(state->game_grid->num_edges, bool); 365 memcpy(ret->line_errors, state->line_errors, 366 state->game_grid->num_edges * sizeof(bool)); 367 ret->exactly_one_loop = state->exactly_one_loop; 368 369 ret->grid_type = state->grid_type; 370 return ret; 371} 372 373static void free_game(game_state *state) 374{ 375 if (state) { 376 grid_free(state->game_grid); 377 sfree(state->clues); 378 sfree(state->lines); 379 sfree(state->line_errors); 380 sfree(state); 381 } 382} 383 384static solver_state *new_solver_state(const game_state *state, int diff) { 385 int i; 386 int num_dots = state->game_grid->num_dots; 387 int num_faces = state->game_grid->num_faces; 388 int num_edges = state->game_grid->num_edges; 389 solver_state *ret = snew(solver_state); 390 391 ret->state = dup_game(state); 392 393 ret->solver_status = SOLVER_INCOMPLETE; 394 ret->diff = diff; 395 396 ret->dotdsf = dsf_new(num_dots); 397 ret->looplen = snewn(num_dots, int); 398 399 for (i = 0; i < num_dots; i++) { 400 ret->looplen[i] = 1; 401 } 402 403 ret->dot_solved = snewn(num_dots, bool); 404 ret->face_solved = snewn(num_faces, bool); 405 memset(ret->dot_solved, 0, num_dots * sizeof(bool)); 406 memset(ret->face_solved, 0, num_faces * sizeof(bool)); 407 408 ret->dot_yes_count = snewn(num_dots, char); 409 memset(ret->dot_yes_count, 0, num_dots); 410 ret->dot_no_count = snewn(num_dots, char); 411 memset(ret->dot_no_count, 0, num_dots); 412 ret->face_yes_count = snewn(num_faces, char); 413 memset(ret->face_yes_count, 0, num_faces); 414 ret->face_no_count = snewn(num_faces, char); 415 memset(ret->face_no_count, 0, num_faces); 416 417 if (diff < DIFF_NORMAL) { 418 ret->dlines = NULL; 419 } else { 420 ret->dlines = snewn(2*num_edges, char); 421 memset(ret->dlines, 0, 2*num_edges); 422 } 423 424 if (diff < DIFF_HARD) { 425 ret->linedsf = NULL; 426 } else { 427 ret->linedsf = dsf_new_flip(state->game_grid->num_edges); 428 } 429 430 return ret; 431} 432 433static void free_solver_state(solver_state *sstate) { 434 if (sstate) { 435 free_game(sstate->state); 436 dsf_free(sstate->dotdsf); 437 sfree(sstate->looplen); 438 sfree(sstate->dot_solved); 439 sfree(sstate->face_solved); 440 sfree(sstate->dot_yes_count); 441 sfree(sstate->dot_no_count); 442 sfree(sstate->face_yes_count); 443 sfree(sstate->face_no_count); 444 445 /* OK, because sfree(NULL) is a no-op */ 446 sfree(sstate->dlines); 447 dsf_free(sstate->linedsf); 448 449 sfree(sstate); 450 } 451} 452 453static solver_state *dup_solver_state(const solver_state *sstate) { 454 game_state *state = sstate->state; 455 int num_dots = state->game_grid->num_dots; 456 int num_faces = state->game_grid->num_faces; 457 int num_edges = state->game_grid->num_edges; 458 solver_state *ret = snew(solver_state); 459 460 ret->state = state = dup_game(sstate->state); 461 462 ret->solver_status = sstate->solver_status; 463 ret->diff = sstate->diff; 464 465 ret->dotdsf = dsf_new(num_dots); 466 ret->looplen = snewn(num_dots, int); 467 dsf_copy(ret->dotdsf, sstate->dotdsf); 468 memcpy(ret->looplen, sstate->looplen, 469 num_dots * sizeof(int)); 470 471 ret->dot_solved = snewn(num_dots, bool); 472 ret->face_solved = snewn(num_faces, bool); 473 memcpy(ret->dot_solved, sstate->dot_solved, num_dots * sizeof(bool)); 474 memcpy(ret->face_solved, sstate->face_solved, num_faces * sizeof(bool)); 475 476 ret->dot_yes_count = snewn(num_dots, char); 477 memcpy(ret->dot_yes_count, sstate->dot_yes_count, num_dots); 478 ret->dot_no_count = snewn(num_dots, char); 479 memcpy(ret->dot_no_count, sstate->dot_no_count, num_dots); 480 481 ret->face_yes_count = snewn(num_faces, char); 482 memcpy(ret->face_yes_count, sstate->face_yes_count, num_faces); 483 ret->face_no_count = snewn(num_faces, char); 484 memcpy(ret->face_no_count, sstate->face_no_count, num_faces); 485 486 if (sstate->dlines) { 487 ret->dlines = snewn(2*num_edges, char); 488 memcpy(ret->dlines, sstate->dlines, 489 2*num_edges); 490 } else { 491 ret->dlines = NULL; 492 } 493 494 if (sstate->linedsf) { 495 ret->linedsf = dsf_new_flip(num_edges); 496 dsf_copy(ret->linedsf, sstate->linedsf); 497 } else { 498 ret->linedsf = NULL; 499 } 500 501 return ret; 502} 503 504static game_params *default_params(void) 505{ 506 game_params *ret = snew(game_params); 507 508#ifdef SLOW_SYSTEM 509 ret->h = 7; 510 ret->w = 7; 511#else 512 ret->h = 10; 513 ret->w = 10; 514#endif 515 ret->diff = DIFF_EASY; 516 ret->type = 0; 517 518 return ret; 519} 520 521static game_params *dup_params(const game_params *params) 522{ 523 game_params *ret = snew(game_params); 524 525 *ret = *params; /* structure copy */ 526 return ret; 527} 528 529static const game_params loopy_presets_top[] = { 530#ifdef SMALL_SCREEN 531 { 7, 7, DIFF_EASY, LOOPY_GRID_SQUARE }, 532 { 7, 7, DIFF_NORMAL, LOOPY_GRID_SQUARE }, 533 { 7, 7, DIFF_HARD, LOOPY_GRID_SQUARE }, 534 { 7, 7, DIFF_HARD, LOOPY_GRID_TRIANGULAR }, 535 { 5, 5, DIFF_HARD, LOOPY_GRID_SNUBSQUARE }, 536 { 7, 7, DIFF_HARD, LOOPY_GRID_CAIRO }, 537 { 5, 5, DIFF_HARD, LOOPY_GRID_KITE }, 538 { 6, 6, DIFF_HARD, LOOPY_GRID_PENROSE_P2 }, 539 { 6, 6, DIFF_HARD, LOOPY_GRID_PENROSE_P3 }, 540#else 541 { 7, 7, DIFF_EASY, LOOPY_GRID_SQUARE }, 542 { 10, 10, DIFF_EASY, LOOPY_GRID_SQUARE }, 543 { 7, 7, DIFF_NORMAL, LOOPY_GRID_SQUARE }, 544 { 10, 10, DIFF_NORMAL, LOOPY_GRID_SQUARE }, 545 { 7, 7, DIFF_HARD, LOOPY_GRID_SQUARE }, 546 { 10, 10, DIFF_HARD, LOOPY_GRID_SQUARE }, 547 { 12, 10, DIFF_HARD, LOOPY_GRID_TRIANGULAR }, 548 { 7, 7, DIFF_HARD, LOOPY_GRID_SNUBSQUARE }, 549 { 9, 9, DIFF_HARD, LOOPY_GRID_CAIRO }, 550 { 5, 5, DIFF_HARD, LOOPY_GRID_KITE }, 551 { 10, 10, DIFF_HARD, LOOPY_GRID_PENROSE_P2 }, 552 { 10, 10, DIFF_HARD, LOOPY_GRID_PENROSE_P3 }, 553#endif 554}; 555 556static const game_params loopy_presets_more[] = { 557#ifdef SMALL_SCREEN 558 { 7, 7, DIFF_HARD, LOOPY_GRID_HONEYCOMB }, 559 { 5, 4, DIFF_HARD, LOOPY_GRID_GREATHEXAGONAL }, 560 { 5, 4, DIFF_HARD, LOOPY_GRID_KAGOME }, 561 { 5, 5, DIFF_HARD, LOOPY_GRID_OCTAGONAL }, 562 { 3, 3, DIFF_HARD, LOOPY_GRID_FLORET }, 563 { 3, 3, DIFF_HARD, LOOPY_GRID_DODECAGONAL }, 564 { 3, 3, DIFF_HARD, LOOPY_GRID_GREATDODECAGONAL }, 565 { 3, 2, DIFF_HARD, LOOPY_GRID_GREATGREATDODECAGONAL }, 566 { 3, 3, DIFF_HARD, LOOPY_GRID_COMPASSDODECAGONAL }, 567 { 6, 6, DIFF_HARD, LOOPY_GRID_HATS }, 568 { 6, 6, DIFF_HARD, LOOPY_GRID_SPECTRES }, 569#else 570 { 10, 10, DIFF_HARD, LOOPY_GRID_HONEYCOMB }, 571 { 5, 4, DIFF_HARD, LOOPY_GRID_GREATHEXAGONAL }, 572 { 5, 4, DIFF_HARD, LOOPY_GRID_KAGOME }, 573 { 7, 7, DIFF_HARD, LOOPY_GRID_OCTAGONAL }, 574 { 5, 5, DIFF_HARD, LOOPY_GRID_FLORET }, 575 { 5, 4, DIFF_HARD, LOOPY_GRID_DODECAGONAL }, 576 { 5, 4, DIFF_HARD, LOOPY_GRID_GREATDODECAGONAL }, 577 { 5, 3, DIFF_HARD, LOOPY_GRID_GREATGREATDODECAGONAL }, 578 { 5, 4, DIFF_HARD, LOOPY_GRID_COMPASSDODECAGONAL }, 579 { 10, 10, DIFF_HARD, LOOPY_GRID_HATS }, 580 { 10, 10, DIFF_HARD, LOOPY_GRID_SPECTRES }, 581#endif 582}; 583 584static void preset_menu_add_preset_with_title(struct preset_menu *menu, 585 const game_params *params) 586{ 587 char buf[80]; 588 game_params *dup_params; 589 590 sprintf(buf, "%dx%d %s - %s", params->h, params->w, 591 gridnames[params->type], diffnames[params->diff]); 592 593 dup_params = snew(game_params); 594 *dup_params = *params; 595 596 preset_menu_add_preset(menu, dupstr(buf), dup_params); 597} 598 599static struct preset_menu *game_preset_menu(void) 600{ 601 struct preset_menu *top, *more; 602 int i; 603 604 top = preset_menu_new(); 605 for (i = 0; i < lenof(loopy_presets_top); i++) 606 preset_menu_add_preset_with_title(top, &loopy_presets_top[i]); 607 608 more = preset_menu_add_submenu(top, dupstr("More...")); 609 for (i = 0; i < lenof(loopy_presets_more); i++) 610 preset_menu_add_preset_with_title(more, &loopy_presets_more[i]); 611 612 return top; 613} 614 615static void free_params(game_params *params) 616{ 617 sfree(params); 618} 619 620static void decode_params(game_params *params, char const *string) 621{ 622 params->h = params->w = atoi(string); 623 params->diff = DIFF_EASY; 624 while (*string && isdigit((unsigned char)*string)) string++; 625 if (*string == 'x') { 626 string++; 627 params->h = atoi(string); 628 while (*string && isdigit((unsigned char)*string)) string++; 629 } 630 if (*string == 't') { 631 string++; 632 params->type = atoi(string); 633 while (*string && isdigit((unsigned char)*string)) string++; 634 } 635 if (*string == 'd') { 636 int i; 637 string++; 638 for (i = 0; i < DIFF_MAX; i++) 639 if (*string == diffchars[i]) 640 params->diff = i; 641 if (*string) string++; 642 } 643} 644 645static char *encode_params(const game_params *params, bool full) 646{ 647 char str[80]; 648 sprintf(str, "%dx%dt%d", params->w, params->h, params->type); 649 if (full) 650 sprintf(str + strlen(str), "d%c", diffchars[params->diff]); 651 return dupstr(str); 652} 653 654static config_item *game_configure(const game_params *params) 655{ 656 config_item *ret; 657 char buf[80]; 658 659 ret = snewn(5, config_item); 660 661 ret[0].name = "Width"; 662 ret[0].type = C_STRING; 663 sprintf(buf, "%d", params->w); 664 ret[0].u.string.sval = dupstr(buf); 665 666 ret[1].name = "Height"; 667 ret[1].type = C_STRING; 668 sprintf(buf, "%d", params->h); 669 ret[1].u.string.sval = dupstr(buf); 670 671 ret[2].name = "Grid type"; 672 ret[2].type = C_CHOICES; 673 ret[2].u.choices.choicenames = GRID_CONFIGS; 674 ret[2].u.choices.selected = params->type; 675 676 ret[3].name = "Difficulty"; 677 ret[3].type = C_CHOICES; 678 ret[3].u.choices.choicenames = DIFFCONFIG; 679 ret[3].u.choices.selected = params->diff; 680 681 ret[4].name = NULL; 682 ret[4].type = C_END; 683 684 return ret; 685} 686 687static game_params *custom_params(const config_item *cfg) 688{ 689 game_params *ret = snew(game_params); 690 691 ret->w = atoi(cfg[0].u.string.sval); 692 ret->h = atoi(cfg[1].u.string.sval); 693 ret->type = cfg[2].u.choices.selected; 694 ret->diff = cfg[3].u.choices.selected; 695 696 return ret; 697} 698 699static const char *validate_params(const game_params *params, bool full) 700{ 701 const char *err; 702 if (params->type < 0 || params->type >= NUM_GRID_TYPES) 703 return "Illegal grid type"; 704 if (params->w < grid_size_limits[params->type].amin || 705 params->h < grid_size_limits[params->type].amin) 706 return grid_size_limits[params->type].aerr; 707 if (params->w < grid_size_limits[params->type].omin && 708 params->h < grid_size_limits[params->type].omin) 709 return grid_size_limits[params->type].oerr; 710 err = grid_validate_params(grid_types[params->type], params->w, params->h); 711 if (err != NULL) return err; 712 713 /* 714 * This shouldn't be able to happen at all, since decode_params 715 * and custom_params will never generate anything that isn't 716 * within range. 717 */ 718 assert(params->diff < DIFF_MAX); 719 720 return NULL; 721} 722 723/* Returns a newly allocated string describing the current puzzle */ 724static char *state_to_text(const game_state *state) 725{ 726 grid *g = state->game_grid; 727 char *retval; 728 int num_faces = g->num_faces; 729 char *description = snewn(num_faces + 1, char); 730 char *dp = description; 731 int empty_count = 0; 732 int i; 733 734 for (i = 0; i < num_faces; i++) { 735 if (state->clues[i] < 0) { 736 if (empty_count > 25) { 737 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); 738 empty_count = 0; 739 } 740 empty_count++; 741 } else { 742 if (empty_count) { 743 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); 744 empty_count = 0; 745 } 746 dp += sprintf(dp, "%c", (int)CLUE2CHAR(state->clues[i])); 747 } 748 } 749 750 if (empty_count) 751 dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1)); 752 753 retval = dupstr(description); 754 sfree(description); 755 756 return retval; 757} 758 759#define GRID_DESC_SEP '_' 760 761/* Splits up a (optional) grid_desc from the game desc. Returns the 762 * grid_desc (which needs freeing) and updates the desc pointer to 763 * start of real desc, or returns NULL if no desc. */ 764static char *extract_grid_desc(const char **desc) 765{ 766 char *sep = strchr(*desc, GRID_DESC_SEP), *gd; 767 int gd_len; 768 769 if (!sep) return NULL; 770 771 gd_len = sep - (*desc); 772 gd = snewn(gd_len+1, char); 773 memcpy(gd, *desc, gd_len); 774 gd[gd_len] = '\0'; 775 776 *desc = sep+1; 777 778 return gd; 779} 780 781/* We require that the params pass the test in validate_params and that the 782 * description fills the entire game area */ 783static const char *validate_desc(const game_params *params, const char *desc) 784{ 785 int count = 0; 786 grid *g; 787 char *grid_desc; 788 const char *ret; 789 790 /* It's pretty inefficient to do this just for validation. All we need to 791 * know is the precise number of faces. */ 792 grid_desc = extract_grid_desc(&desc); 793 ret = grid_validate_desc(grid_types[params->type], params->w, params->h, grid_desc); 794 if (ret) { 795 sfree(grid_desc); 796 return ret; 797 } 798 799 g = loopy_generate_grid(params, grid_desc); 800 sfree(grid_desc); 801 802 for (; *desc; ++desc) { 803 if ((*desc >= '0' && *desc <= '9') || (*desc >= 'A' && *desc <= 'Z')) { 804 count++; 805 continue; 806 } 807 if (*desc >= 'a') { 808 count += *desc - 'a' + 1; 809 continue; 810 } 811 grid_free(g); 812 return "Unknown character in description"; 813 } 814 815 if (count < g->num_faces) { 816 grid_free(g); 817 return "Description too short for board size"; 818 } 819 if (count > g->num_faces) { 820 grid_free(g); 821 return "Description too long for board size"; 822 } 823 824 grid_free(g); 825 826 return NULL; 827} 828 829/* Sums the lengths of the numbers in range [0,n) */ 830/* See equivalent function in solo.c for justification of this. */ 831static int len_0_to_n(int n) 832{ 833 int len = 1; /* Counting 0 as a bit of a special case */ 834 int i; 835 836 for (i = 1; i < n; i *= 10) { 837 len += max(n - i, 0); 838 } 839 840 return len; 841} 842 843static char *encode_solve_move(const game_state *state) 844{ 845 int len; 846 char *ret, *p; 847 int i; 848 int num_edges = state->game_grid->num_edges; 849 850 /* This is going to return a string representing the moves needed to set 851 * every line in a grid to be the same as the ones in 'state'. The exact 852 * length of this string is predictable. */ 853 854 len = 1; /* Count the 'S' prefix */ 855 /* Numbers in all lines */ 856 len += len_0_to_n(num_edges); 857 /* For each line we also have a letter */ 858 len += num_edges; 859 860 ret = snewn(len + 1, char); 861 p = ret; 862 863 p += sprintf(p, "S"); 864 865 for (i = 0; i < num_edges; i++) { 866 switch (state->lines[i]) { 867 case LINE_YES: 868 p += sprintf(p, "%dy", i); 869 break; 870 case LINE_NO: 871 p += sprintf(p, "%dn", i); 872 break; 873 } 874 } 875 876 /* No point in doing sums like that if they're going to be wrong */ 877 assert(strlen(ret) <= (size_t)len); 878 return ret; 879} 880 881struct game_ui { 882 /* 883 * User preference: should grid lines in LINE_NO state be drawn 884 * very faintly so users can still see where they are, or should 885 * they be completely invisible? 886 */ 887 bool draw_faint_lines; 888 889 /* 890 * User preference: when clicking an edge that has only one 891 * possible edge connecting to one (or both) of its ends, should 892 * that edge also change to the same state as the edge we just 893 * clicked? 894 */ 895 enum { 896 AF_OFF, /* no, all grid edges are independent in the UI */ 897 AF_FIXED, /* yes, but only based on the grid itself */ 898 AF_ADAPTIVE /* yes, and consider edges user has already set to NO */ 899 } autofollow; 900}; 901 902static void legacy_prefs_override(struct game_ui *ui_out) 903{ 904 static bool initialised = false; 905 static int draw_faint_lines = -1; 906 static int autofollow = -1; 907 908 if (!initialised) { 909 char *env; 910 911 initialised = true; 912 draw_faint_lines = getenv_bool("LOOPY_FAINT_LINES", -1); 913 914 if ((env = getenv("LOOPY_AUTOFOLLOW")) != NULL) { 915 if (!strcmp(env, "off")) 916 autofollow = AF_OFF; 917 else if (!strcmp(env, "fixed")) 918 autofollow = AF_FIXED; 919 else if (!strcmp(env, "adaptive")) 920 autofollow = AF_ADAPTIVE; 921 } 922 } 923 924 if (draw_faint_lines != -1) 925 ui_out->draw_faint_lines = draw_faint_lines; 926 if (autofollow != -1) 927 ui_out->autofollow = autofollow; 928} 929 930static game_ui *new_ui(const game_state *state) 931{ 932 game_ui *ui = snew(game_ui); 933 ui->draw_faint_lines = true; 934 ui->autofollow = AF_OFF; 935 legacy_prefs_override(ui); 936 return ui; 937} 938 939static void free_ui(game_ui *ui) 940{ 941 sfree(ui); 942} 943 944static config_item *get_prefs(game_ui *ui) 945{ 946 config_item *ret; 947 948 ret = snewn(N_PREF_ITEMS+1, config_item); 949 950 ret[PREF_DRAW_FAINT_LINES].name = "Draw excluded grid lines faintly"; 951 ret[PREF_DRAW_FAINT_LINES].kw = "draw-faint-lines"; 952 ret[PREF_DRAW_FAINT_LINES].type = C_BOOLEAN; 953 ret[PREF_DRAW_FAINT_LINES].u.boolean.bval = ui->draw_faint_lines; 954 955 ret[PREF_AUTO_FOLLOW].name = "Auto-follow unique paths of edges"; 956 ret[PREF_AUTO_FOLLOW].kw = "auto-follow"; 957 ret[PREF_AUTO_FOLLOW].type = C_CHOICES; 958 ret[PREF_AUTO_FOLLOW].u.choices.choicenames = 959 ":No:Based on grid only:Based on grid and game state"; 960 ret[PREF_AUTO_FOLLOW].u.choices.choicekws = ":off:fixed:adaptive"; 961 ret[PREF_AUTO_FOLLOW].u.choices.selected = ui->autofollow; 962 963 ret[N_PREF_ITEMS].name = NULL; 964 ret[N_PREF_ITEMS].type = C_END; 965 966 return ret; 967} 968 969static void set_prefs(game_ui *ui, const config_item *cfg) 970{ 971 ui->draw_faint_lines = cfg[PREF_DRAW_FAINT_LINES].u.boolean.bval; 972 ui->autofollow = cfg[PREF_AUTO_FOLLOW].u.choices.selected; 973} 974 975static void game_changed_state(game_ui *ui, const game_state *oldstate, 976 const game_state *newstate) 977{ 978} 979 980static void game_compute_size(const game_params *params, int tilesize, 981 const game_ui *ui, int *x, int *y) 982{ 983 int grid_width, grid_height, rendered_width, rendered_height; 984 int g_tilesize; 985 986 grid_compute_size(grid_types[params->type], params->w, params->h, 987 &g_tilesize, &grid_width, &grid_height); 988 989 /* multiply first to minimise rounding error on integer division */ 990 rendered_width = grid_width * tilesize / g_tilesize; 991 rendered_height = grid_height * tilesize / g_tilesize; 992 *x = rendered_width + 2 * BORDER(tilesize) + 1; 993 *y = rendered_height + 2 * BORDER(tilesize) + 1; 994} 995 996static void game_set_size(drawing *dr, game_drawstate *ds, 997 const game_params *params, int tilesize) 998{ 999 ds->tilesize = tilesize; 1000} 1001 1002static float *game_colours(frontend *fe, int *ncolours) 1003{ 1004 float *ret = snewn(3 * NCOLOURS, float); 1005 1006 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); 1007 1008 ret[COL_FOREGROUND * 3 + 0] = 0.0F; 1009 ret[COL_FOREGROUND * 3 + 1] = 0.0F; 1010 ret[COL_FOREGROUND * 3 + 2] = 0.0F; 1011 1012 /* 1013 * We want COL_LINEUNKNOWN to be a yellow which is a bit darker 1014 * than the background. (I previously set it to 0.8,0.8,0, but 1015 * found that this went badly with the 0.8,0.8,0.8 favoured as a 1016 * background by the Java frontend.) 1017 */ 1018 ret[COL_LINEUNKNOWN * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F; 1019 ret[COL_LINEUNKNOWN * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F; 1020 ret[COL_LINEUNKNOWN * 3 + 2] = 0.0F; 1021 1022 ret[COL_HIGHLIGHT * 3 + 0] = 1.0F; 1023 ret[COL_HIGHLIGHT * 3 + 1] = 1.0F; 1024 ret[COL_HIGHLIGHT * 3 + 2] = 1.0F; 1025 1026 ret[COL_MISTAKE * 3 + 0] = 1.0F; 1027 ret[COL_MISTAKE * 3 + 1] = 0.0F; 1028 ret[COL_MISTAKE * 3 + 2] = 0.0F; 1029 1030 ret[COL_SATISFIED * 3 + 0] = 0.0F; 1031 ret[COL_SATISFIED * 3 + 1] = 0.0F; 1032 ret[COL_SATISFIED * 3 + 2] = 0.0F; 1033 1034 /* We want the faint lines to be a bit darker than the background. 1035 * Except if the background is pretty dark already; then it ought to be a 1036 * bit lighter. Oy vey. 1037 */ 1038 ret[COL_FAINT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.9F; 1039 ret[COL_FAINT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.9F; 1040 ret[COL_FAINT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.9F; 1041 1042 *ncolours = NCOLOURS; 1043 return ret; 1044} 1045 1046static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state) 1047{ 1048 struct game_drawstate *ds = snew(struct game_drawstate); 1049 int num_faces = state->game_grid->num_faces; 1050 int num_edges = state->game_grid->num_edges; 1051 int i; 1052 1053 ds->tilesize = 0; 1054 ds->started = false; 1055 ds->lines = snewn(num_edges, char); 1056 ds->clue_error = snewn(num_faces, bool); 1057 ds->clue_satisfied = snewn(num_faces, bool); 1058 ds->textx = snewn(num_faces, int); 1059 ds->texty = snewn(num_faces, int); 1060 ds->flashing = false; 1061 1062 memset(ds->lines, LINE_UNKNOWN, num_edges); 1063 memset(ds->clue_error, 0, num_faces * sizeof(bool)); 1064 memset(ds->clue_satisfied, 0, num_faces * sizeof(bool)); 1065 for (i = 0; i < num_faces; i++) 1066 ds->textx[i] = ds->texty[i] = -1; 1067 1068 return ds; 1069} 1070 1071static void game_free_drawstate(drawing *dr, game_drawstate *ds) 1072{ 1073 sfree(ds->textx); 1074 sfree(ds->texty); 1075 sfree(ds->clue_error); 1076 sfree(ds->clue_satisfied); 1077 sfree(ds->lines); 1078 sfree(ds); 1079} 1080 1081static float game_anim_length(const game_state *oldstate, 1082 const game_state *newstate, int dir, game_ui *ui) 1083{ 1084 return 0.0F; 1085} 1086 1087static bool game_can_format_as_text_now(const game_params *params) 1088{ 1089 if (params->type != 0) 1090 return false; 1091 return true; 1092} 1093 1094static char *game_text_format(const game_state *state) 1095{ 1096 int w, h, W, H; 1097 int x, y, i; 1098 int cell_size; 1099 char *ret; 1100 grid *g = state->game_grid; 1101 grid_face *f; 1102 1103 assert(state->grid_type == 0); 1104 1105 /* Work out the basic size unit */ 1106 f = g->faces[0]; /* first face */ 1107 assert(f->order == 4); 1108 /* The dots are ordered clockwise, so the two opposite 1109 * corners are guaranteed to span the square */ 1110 cell_size = abs(f->dots[0]->x - f->dots[2]->x); 1111 1112 w = (g->highest_x - g->lowest_x) / cell_size; 1113 h = (g->highest_y - g->lowest_y) / cell_size; 1114 1115 /* Create a blank "canvas" to "draw" on */ 1116 W = 2 * w + 2; 1117 H = 2 * h + 1; 1118 ret = snewn(W * H + 1, char); 1119 for (y = 0; y < H; y++) { 1120 for (x = 0; x < W-1; x++) { 1121 ret[y*W + x] = ' '; 1122 } 1123 ret[y*W + W-1] = '\n'; 1124 } 1125 ret[H*W] = '\0'; 1126 1127 /* Fill in edge info */ 1128 for (i = 0; i < g->num_edges; i++) { 1129 grid_edge *e = g->edges[i]; 1130 /* Cell coordinates, from (0,0) to (w-1,h-1) */ 1131 int x1 = (e->dot1->x - g->lowest_x) / cell_size; 1132 int x2 = (e->dot2->x - g->lowest_x) / cell_size; 1133 int y1 = (e->dot1->y - g->lowest_y) / cell_size; 1134 int y2 = (e->dot2->y - g->lowest_y) / cell_size; 1135 /* Midpoint, in canvas coordinates (canvas coordinates are just twice 1136 * cell coordinates) */ 1137 x = x1 + x2; 1138 y = y1 + y2; 1139 switch (state->lines[i]) { 1140 case LINE_YES: 1141 ret[y*W + x] = (y1 == y2) ? '-' : '|'; 1142 break; 1143 case LINE_NO: 1144 ret[y*W + x] = 'x'; 1145 break; 1146 case LINE_UNKNOWN: 1147 break; /* already a space */ 1148 default: 1149 assert(!"Illegal line state"); 1150 } 1151 } 1152 1153 /* Fill in clues */ 1154 for (i = 0; i < g->num_faces; i++) { 1155 int x1, x2, y1, y2; 1156 1157 f = g->faces[i]; 1158 assert(f->order == 4); 1159 /* Cell coordinates, from (0,0) to (w-1,h-1) */ 1160 x1 = (f->dots[0]->x - g->lowest_x) / cell_size; 1161 x2 = (f->dots[2]->x - g->lowest_x) / cell_size; 1162 y1 = (f->dots[0]->y - g->lowest_y) / cell_size; 1163 y2 = (f->dots[2]->y - g->lowest_y) / cell_size; 1164 /* Midpoint, in canvas coordinates */ 1165 x = x1 + x2; 1166 y = y1 + y2; 1167 ret[y*W + x] = CLUE2CHAR(state->clues[i]); 1168 } 1169 return ret; 1170} 1171 1172/* ---------------------------------------------------------------------- 1173 * Debug code 1174 */ 1175 1176#ifdef DEBUG_CACHES 1177static void check_caches(const solver_state* sstate) 1178{ 1179 int i; 1180 const game_state *state = sstate->state; 1181 const grid *g = state->game_grid; 1182 1183 for (i = 0; i < g->num_dots; i++) { 1184 assert(dot_order(state, i, LINE_YES) == sstate->dot_yes_count[i]); 1185 assert(dot_order(state, i, LINE_NO) == sstate->dot_no_count[i]); 1186 } 1187 1188 for (i = 0; i < g->num_faces; i++) { 1189 assert(face_order(state, i, LINE_YES) == sstate->face_yes_count[i]); 1190 assert(face_order(state, i, LINE_NO) == sstate->face_no_count[i]); 1191 } 1192} 1193 1194#if 0 1195#define check_caches(s) \ 1196 do { \ 1197 fprintf(stderr, "check_caches at line %d\n", __LINE__); \ 1198 check_caches(s); \ 1199 } while (0) 1200#endif 1201#endif /* DEBUG_CACHES */ 1202 1203/* ---------------------------------------------------------------------- 1204 * Solver utility functions 1205 */ 1206 1207/* Sets the line (with index i) to the new state 'line_new', and updates 1208 * the cached counts of any affected faces and dots. 1209 * Returns true if this actually changed the line's state. */ 1210static bool solver_set_line(solver_state *sstate, int i, 1211 enum line_state line_new 1212#ifdef SHOW_WORKING 1213 , const char *reason 1214#endif 1215 ) 1216{ 1217 game_state *state = sstate->state; 1218 grid *g; 1219 grid_edge *e; 1220 1221 assert(line_new != LINE_UNKNOWN); 1222 1223 check_caches(sstate); 1224 1225 if (state->lines[i] == line_new) { 1226 return false; /* nothing changed */ 1227 } 1228 state->lines[i] = line_new; 1229 1230#ifdef SHOW_WORKING 1231 fprintf(stderr, "solver: set line [%d] to %s (%s)\n", 1232 i, line_new == LINE_YES ? "YES" : "NO", 1233 reason); 1234#endif 1235 1236 g = state->game_grid; 1237 e = g->edges[i]; 1238 1239 /* Update the cache for both dots and both faces affected by this. */ 1240 if (line_new == LINE_YES) { 1241 sstate->dot_yes_count[e->dot1->index]++; 1242 sstate->dot_yes_count[e->dot2->index]++; 1243 if (e->face1) { 1244 sstate->face_yes_count[e->face1->index]++; 1245 } 1246 if (e->face2) { 1247 sstate->face_yes_count[e->face2->index]++; 1248 } 1249 } else { 1250 sstate->dot_no_count[e->dot1->index]++; 1251 sstate->dot_no_count[e->dot2->index]++; 1252 if (e->face1) { 1253 sstate->face_no_count[e->face1->index]++; 1254 } 1255 if (e->face2) { 1256 sstate->face_no_count[e->face2->index]++; 1257 } 1258 } 1259 1260 check_caches(sstate); 1261 return true; 1262} 1263 1264#ifdef SHOW_WORKING 1265#define solver_set_line(a, b, c) \ 1266 solver_set_line(a, b, c, __FUNCTION__) 1267#endif 1268 1269/* 1270 * Merge two dots due to the existence of an edge between them. 1271 * Updates the dsf tracking equivalence classes, and keeps track of 1272 * the length of path each dot is currently a part of. 1273 * Returns true if the dots were already linked, ie if they are part of a 1274 * closed loop, and false otherwise. 1275 */ 1276static bool merge_dots(solver_state *sstate, int edge_index) 1277{ 1278 int i, j, len; 1279 grid *g = sstate->state->game_grid; 1280 grid_edge *e = g->edges[edge_index]; 1281 1282 i = e->dot1->index; 1283 j = e->dot2->index; 1284 1285 i = dsf_canonify(sstate->dotdsf, i); 1286 j = dsf_canonify(sstate->dotdsf, j); 1287 1288 if (i == j) { 1289 return true; 1290 } else { 1291 len = sstate->looplen[i] + sstate->looplen[j]; 1292 dsf_merge(sstate->dotdsf, i, j); 1293 i = dsf_canonify(sstate->dotdsf, i); 1294 sstate->looplen[i] = len; 1295 return false; 1296 } 1297} 1298 1299/* Merge two lines because the solver has deduced that they must be either 1300 * identical or opposite. Returns true if this is new information, otherwise 1301 * false. */ 1302static bool merge_lines(solver_state *sstate, int i, int j, bool inverse 1303#ifdef SHOW_WORKING 1304 , const char *reason 1305#endif 1306 ) 1307{ 1308 bool inv_tmp; 1309 1310 assert(i < sstate->state->game_grid->num_edges); 1311 assert(j < sstate->state->game_grid->num_edges); 1312 1313 i = dsf_canonify_flip(sstate->linedsf, i, &inv_tmp); 1314 inverse ^= inv_tmp; 1315 j = dsf_canonify_flip(sstate->linedsf, j, &inv_tmp); 1316 inverse ^= inv_tmp; 1317 1318 dsf_merge_flip(sstate->linedsf, i, j, inverse); 1319 1320#ifdef SHOW_WORKING 1321 if (i != j) { 1322 fprintf(stderr, "%s [%d] [%d] %s(%s)\n", 1323 __FUNCTION__, i, j, 1324 inverse ? "inverse " : "", reason); 1325 } 1326#endif 1327 return (i != j); 1328} 1329 1330#ifdef SHOW_WORKING 1331#define merge_lines(a, b, c, d) \ 1332 merge_lines(a, b, c, d, __FUNCTION__) 1333#endif 1334 1335/* Count the number of lines of a particular type currently going into the 1336 * given dot. */ 1337static int dot_order(const game_state* state, int dot, char line_type) 1338{ 1339 int n = 0; 1340 grid *g = state->game_grid; 1341 grid_dot *d = g->dots[dot]; 1342 int i; 1343 1344 for (i = 0; i < d->order; i++) { 1345 grid_edge *e = d->edges[i]; 1346 if (state->lines[e->index] == line_type) 1347 ++n; 1348 } 1349 return n; 1350} 1351 1352/* Count the number of lines of a particular type currently surrounding the 1353 * given face */ 1354static int face_order(const game_state* state, int face, char line_type) 1355{ 1356 int n = 0; 1357 grid *g = state->game_grid; 1358 grid_face *f = g->faces[face]; 1359 int i; 1360 1361 for (i = 0; i < f->order; i++) { 1362 grid_edge *e = f->edges[i]; 1363 if (state->lines[e->index] == line_type) 1364 ++n; 1365 } 1366 return n; 1367} 1368 1369/* Set all lines bordering a dot of type old_type to type new_type 1370 * Return value tells caller whether this function actually did anything */ 1371static bool dot_setall(solver_state *sstate, int dot, 1372 char old_type, char new_type) 1373{ 1374 bool retval = false, r; 1375 game_state *state = sstate->state; 1376 grid *g; 1377 grid_dot *d; 1378 int i; 1379 1380 if (old_type == new_type) 1381 return false; 1382 1383 g = state->game_grid; 1384 d = g->dots[dot]; 1385 1386 for (i = 0; i < d->order; i++) { 1387 int line_index = d->edges[i]->index; 1388 if (state->lines[line_index] == old_type) { 1389 r = solver_set_line(sstate, line_index, new_type); 1390 assert(r); 1391 retval = true; 1392 } 1393 } 1394 return retval; 1395} 1396 1397/* Set all lines bordering a face of type old_type to type new_type */ 1398static bool face_setall(solver_state *sstate, int face, 1399 char old_type, char new_type) 1400{ 1401 bool retval = false, r; 1402 game_state *state = sstate->state; 1403 grid *g; 1404 grid_face *f; 1405 int i; 1406 1407 if (old_type == new_type) 1408 return false; 1409 1410 g = state->game_grid; 1411 f = g->faces[face]; 1412 1413 for (i = 0; i < f->order; i++) { 1414 int line_index = f->edges[i]->index; 1415 if (state->lines[line_index] == old_type) { 1416 r = solver_set_line(sstate, line_index, new_type); 1417 assert(r); 1418 retval = true; 1419 } 1420 } 1421 return retval; 1422} 1423 1424/* ---------------------------------------------------------------------- 1425 * Loop generation and clue removal 1426 */ 1427 1428static void add_full_clues(game_state *state, random_state *rs) 1429{ 1430 signed char *clues = state->clues; 1431 grid *g = state->game_grid; 1432 char *board = snewn(g->num_faces, char); 1433 int i; 1434 1435 generate_loop(g, board, rs, NULL, NULL); 1436 1437 /* Fill out all the clues by initialising to 0, then iterating over 1438 * all edges and incrementing each clue as we find edges that border 1439 * between BLACK/WHITE faces. While we're at it, we verify that the 1440 * algorithm does work, and there aren't any GREY faces still there. */ 1441 memset(clues, 0, g->num_faces); 1442 for (i = 0; i < g->num_edges; i++) { 1443 grid_edge *e = g->edges[i]; 1444 grid_face *f1 = e->face1; 1445 grid_face *f2 = e->face2; 1446 enum face_colour c1 = FACE_COLOUR(f1); 1447 enum face_colour c2 = FACE_COLOUR(f2); 1448 assert(c1 != FACE_GREY); 1449 assert(c2 != FACE_GREY); 1450 if (c1 != c2) { 1451 if (f1) clues[f1->index]++; 1452 if (f2) clues[f2->index]++; 1453 } 1454 } 1455 sfree(board); 1456} 1457 1458 1459static bool game_has_unique_soln(const game_state *state, int diff) 1460{ 1461 bool ret; 1462 solver_state *sstate_new; 1463 solver_state *sstate = new_solver_state(state, diff); 1464 1465 sstate_new = solve_game_rec(sstate); 1466 1467 assert(sstate_new->solver_status != SOLVER_MISTAKE); 1468 ret = (sstate_new->solver_status == SOLVER_SOLVED); 1469 1470 free_solver_state(sstate_new); 1471 free_solver_state(sstate); 1472 1473 return ret; 1474} 1475 1476 1477/* Remove clues one at a time at random. */ 1478static game_state *remove_clues(game_state *state, random_state *rs, 1479 int diff) 1480{ 1481 int *face_list; 1482 int num_faces = state->game_grid->num_faces; 1483 game_state *ret = dup_game(state), *saved_ret; 1484 int n; 1485 1486 /* We need to remove some clues. We'll do this by forming a list of all 1487 * available clues, shuffling it, then going along one at a 1488 * time clearing each clue in turn for which doing so doesn't render the 1489 * board unsolvable. */ 1490 face_list = snewn(num_faces, int); 1491 for (n = 0; n < num_faces; ++n) { 1492 face_list[n] = n; 1493 } 1494 1495 shuffle(face_list, num_faces, sizeof(int), rs); 1496 1497 for (n = 0; n < num_faces; ++n) { 1498 saved_ret = dup_game(ret); 1499 ret->clues[face_list[n]] = -1; 1500 1501 if (game_has_unique_soln(ret, diff)) { 1502 free_game(saved_ret); 1503 } else { 1504 free_game(ret); 1505 ret = saved_ret; 1506 } 1507 } 1508 sfree(face_list); 1509 1510 return ret; 1511} 1512 1513 1514static char *new_game_desc(const game_params *params, random_state *rs, 1515 char **aux, bool interactive) 1516{ 1517 /* solution and description both use run-length encoding in obvious ways */ 1518 char *retval, *game_desc, *grid_desc; 1519 grid *g; 1520 game_state *state = snew(game_state); 1521 game_state *state_new; 1522 1523 grid_desc = grid_new_desc(grid_types[params->type], params->w, params->h, rs); 1524 state->game_grid = g = loopy_generate_grid(params, grid_desc); 1525 1526 state->clues = snewn(g->num_faces, signed char); 1527 state->lines = snewn(g->num_edges, char); 1528 state->line_errors = snewn(g->num_edges, bool); 1529 state->exactly_one_loop = false; 1530 1531 state->grid_type = params->type; 1532 1533 newboard_please: 1534 1535 memset(state->lines, LINE_UNKNOWN, g->num_edges); 1536 memset(state->line_errors, 0, g->num_edges * sizeof(bool)); 1537 1538 state->solved = false; 1539 state->cheated = false; 1540 1541 /* Get a new random solvable board with all its clues filled in. Yes, this 1542 * can loop for ever if the params are suitably unfavourable, but 1543 * preventing games smaller than 4x4 seems to stop this happening */ 1544 do { 1545 add_full_clues(state, rs); 1546 } while (!game_has_unique_soln(state, params->diff)); 1547 1548 state_new = remove_clues(state, rs, params->diff); 1549 free_game(state); 1550 state = state_new; 1551 1552 1553 if (params->diff > 0 && game_has_unique_soln(state, params->diff-1)) { 1554#ifdef SHOW_WORKING 1555 fprintf(stderr, "Rejecting board, it is too easy\n"); 1556#endif 1557 goto newboard_please; 1558 } 1559 1560 game_desc = state_to_text(state); 1561 1562 free_game(state); 1563 1564 if (grid_desc) { 1565 retval = snewn(strlen(grid_desc) + 1 + strlen(game_desc) + 1, char); 1566 sprintf(retval, "%s%c%s", grid_desc, (int)GRID_DESC_SEP, game_desc); 1567 sfree(grid_desc); 1568 sfree(game_desc); 1569 } else { 1570 retval = game_desc; 1571 } 1572 1573 assert(!validate_desc(params, retval)); 1574 1575 return retval; 1576} 1577 1578static game_state *new_game(midend *me, const game_params *params, 1579 const char *desc) 1580{ 1581 int i; 1582 game_state *state = snew(game_state); 1583 int empties_to_make = 0; 1584 int n,n2; 1585 const char *dp; 1586 char *grid_desc; 1587 grid *g; 1588 int num_faces, num_edges; 1589 1590 grid_desc = extract_grid_desc(&desc); 1591 state->game_grid = g = loopy_generate_grid(params, grid_desc); 1592 if (grid_desc) sfree(grid_desc); 1593 1594 dp = desc; 1595 1596 num_faces = g->num_faces; 1597 num_edges = g->num_edges; 1598 1599 state->clues = snewn(num_faces, signed char); 1600 state->lines = snewn(num_edges, char); 1601 state->line_errors = snewn(num_edges, bool); 1602 state->exactly_one_loop = false; 1603 1604 state->solved = state->cheated = false; 1605 1606 state->grid_type = params->type; 1607 1608 for (i = 0; i < num_faces; i++) { 1609 if (empties_to_make) { 1610 empties_to_make--; 1611 state->clues[i] = -1; 1612 continue; 1613 } 1614 1615 assert(*dp); 1616 n = *dp - '0'; 1617 n2 = *dp - 'A' + 10; 1618 if (n >= 0 && n < 10) { 1619 state->clues[i] = n; 1620 } else if (n2 >= 10 && n2 < 36) { 1621 state->clues[i] = n2; 1622 } else { 1623 n = *dp - 'a' + 1; 1624 assert(n > 0); 1625 state->clues[i] = -1; 1626 empties_to_make = n - 1; 1627 } 1628 ++dp; 1629 } 1630 1631 memset(state->lines, LINE_UNKNOWN, num_edges); 1632 memset(state->line_errors, 0, num_edges * sizeof(bool)); 1633 return state; 1634} 1635 1636/* Calculates the line_errors data, and checks if the current state is a 1637 * solution */ 1638static bool check_completion(game_state *state) 1639{ 1640 grid *g = state->game_grid; 1641 int i; 1642 bool ret; 1643 DSF *dsf; 1644 int *component_state; 1645 int nsilly, nloop, npath, largest_comp, largest_size, total_pathsize; 1646 enum { COMP_NONE, COMP_LOOP, COMP_PATH, COMP_SILLY, COMP_EMPTY }; 1647 1648 memset(state->line_errors, 0, g->num_edges * sizeof(bool)); 1649 1650 /* 1651 * Find loops in the grid, and determine whether the puzzle is 1652 * solved. 1653 * 1654 * Loopy is a bit more complicated than most puzzles that care 1655 * about loop detection. In most of them, loops are simply 1656 * _forbidden_; so the obviously right way to do 1657 * error-highlighting during play is to light up a graph edge red 1658 * iff it is part of a loop, which is exactly what the centralised 1659 * findloop.c makes easy. 1660 * 1661 * But Loopy is unusual in that you're _supposed_ to be making a 1662 * loop - and yet _some_ loops are not the right loop. So we need 1663 * to be more discriminating, by identifying loops one by one and 1664 * then thinking about which ones to highlight, and so findloop.c 1665 * isn't quite the right tool for the job in this case. 1666 * 1667 * Worse still, consider situations in which the grid contains a 1668 * loop and also some non-loop edges: there are some cases like 1669 * this in which the user's intuitive expectation would be to 1670 * highlight the loop (if you're only about half way through the 1671 * puzzle and have accidentally made a little loop in some corner 1672 * of the grid), and others in which they'd be more likely to 1673 * expect you to highlight the non-loop edges (if you've just 1674 * closed off a whole loop that you thought was the entire 1675 * solution, but forgot some disconnected edges in a corner 1676 * somewhere). So while it's easy enough to check whether the 1677 * solution is _right_, highlighting the wrong parts is a tricky 1678 * problem for this puzzle! 1679 * 1680 * I'd quite like, in some situations, to identify the largest 1681 * loop among the player's YES edges, and then light up everything 1682 * other than that. But finding the longest cycle in a graph is an 1683 * NP-complete problem (because, in particular, it must return a 1684 * Hamilton cycle if one exists). 1685 * 1686 * However, I think we can make the problem tractable by 1687 * exercising the Puzzles principle that it isn't absolutely 1688 * necessary to highlight _all_ errors: the key point is that by 1689 * the time the user has filled in the whole grid, they should 1690 * either have seen a completion flash, or have _some_ error 1691 * highlight showing them why the solution isn't right. So in 1692 * principle it would be *just about* good enough to highlight 1693 * just one error in the whole grid, if there was really no better 1694 * way. But we'd like to highlight as many errors as possible. 1695 * 1696 * In this case, I think the simple approach is to make use of the 1697 * fact that no vertex may have degree > 2, and that's really 1698 * simple to detect. So the plan goes like this: 1699 * 1700 * - Form the dsf of connected components of the graph vertices. 1701 * 1702 * - Highlight an error at any vertex with degree > 2. (It so 1703 * happens that we do this by lighting up all the edges 1704 * incident to that vertex, but that's an output detail.) 1705 * 1706 * - Any component that contains such a vertex is now excluded 1707 * from further consideration, because it already has a 1708 * highlight. 1709 * 1710 * - The remaining components have no vertex with degree > 2, and 1711 * hence they all consist of either a simple loop, or a simple 1712 * path with two endpoints. 1713 * 1714 * - For these purposes, group together all the paths and imagine 1715 * them to be a single component (because in most normal 1716 * situations the player will gradually build up the solution 1717 * _not_ all in one connected segment, but as lots of separate 1718 * little path pieces that gradually connect to each other). 1719 * 1720 * - After doing that, if there is exactly one (sensible) 1721 * component - be it a collection of paths or a loop - then 1722 * highlight no further edge errors. (The former case is normal 1723 * during play, and the latter is a potentially solved puzzle.) 1724 * 1725 * - Otherwise, find the largest of the sensible components, 1726 * leave that one unhighlighted, and light the rest up in red. 1727 */ 1728 1729 dsf = dsf_new(g->num_dots); 1730 1731 /* Build the dsf. */ 1732 for (i = 0; i < g->num_edges; i++) { 1733 if (state->lines[i] == LINE_YES) { 1734 grid_edge *e = g->edges[i]; 1735 int d1 = e->dot1->index, d2 = e->dot2->index; 1736 dsf_merge(dsf, d1, d2); 1737 } 1738 } 1739 1740 /* Initialise a state variable for each connected component. */ 1741 component_state = snewn(g->num_dots, int); 1742 for (i = 0; i < g->num_dots; i++) { 1743 if (dsf_canonify(dsf, i) == i) 1744 component_state[i] = COMP_LOOP; 1745 else 1746 component_state[i] = COMP_NONE; 1747 } 1748 1749 /* Check for dots with degree > 3. Here we also spot dots of 1750 * degree 1 in which the user has marked all the non-edges as 1751 * LINE_NO, because those are also clear vertex-level errors, so 1752 * we give them the same treatment of excluding their connected 1753 * component from the subsequent loop analysis. */ 1754 for (i = 0; i < g->num_dots; i++) { 1755 int comp = dsf_canonify(dsf, i); 1756 int yes = dot_order(state, i, LINE_YES); 1757 int unknown = dot_order(state, i, LINE_UNKNOWN); 1758 if ((yes == 1 && unknown == 0) || (yes >= 3)) { 1759 /* violation, so mark all YES edges as errors */ 1760 grid_dot *d = g->dots[i]; 1761 int j; 1762 for (j = 0; j < d->order; j++) { 1763 int e = d->edges[j]->index; 1764 if (state->lines[e] == LINE_YES) 1765 state->line_errors[e] = true; 1766 } 1767 /* And mark this component as not worthy of further 1768 * consideration. */ 1769 component_state[comp] = COMP_SILLY; 1770 1771 } else if (yes == 0) { 1772 /* A completely isolated dot must also be excluded it from 1773 * the subsequent loop highlighting pass, but we tag it 1774 * with a different enum value to avoid it counting 1775 * towards the components that inhibit returning a win 1776 * status. */ 1777 component_state[comp] = COMP_EMPTY; 1778 } else if (yes == 1) { 1779 /* A dot with degree 1 that didn't fall into the 'clearly 1780 * erroneous' case above indicates that this connected 1781 * component will be a path rather than a loop - unless 1782 * something worse elsewhere in the component has 1783 * classified it as silly. */ 1784 if (component_state[comp] != COMP_SILLY) 1785 component_state[comp] = COMP_PATH; 1786 } 1787 } 1788 1789 /* Count up the components. Also, find the largest sensible 1790 * component. (Tie-breaking condition is derived from the order of 1791 * vertices in the grid data structure, which is fairly arbitrary 1792 * but at least stays stable throughout the game.) */ 1793 nsilly = nloop = npath = 0; 1794 total_pathsize = 0; 1795 largest_comp = largest_size = -1; 1796 for (i = 0; i < g->num_dots; i++) { 1797 if (component_state[i] == COMP_SILLY) { 1798 nsilly++; 1799 } else if (component_state[i] == COMP_PATH) { 1800 total_pathsize += dsf_size(dsf, i); 1801 npath = 1; 1802 } else if (component_state[i] == COMP_LOOP) { 1803 int this_size; 1804 1805 nloop++; 1806 1807 if ((this_size = dsf_size(dsf, i)) > largest_size) { 1808 largest_comp = i; 1809 largest_size = this_size; 1810 } 1811 } 1812 } 1813 if (largest_size < total_pathsize) { 1814 largest_comp = -1; /* means the paths */ 1815 largest_size = total_pathsize; 1816 } 1817 1818 if (nloop > 0 && nloop + npath > 1) { 1819 /* 1820 * If there are at least two sensible components including at 1821 * least one loop, highlight all edges in every sensible 1822 * component that is not the largest one. 1823 */ 1824 for (i = 0; i < g->num_edges; i++) { 1825 if (state->lines[i] == LINE_YES) { 1826 grid_edge *e = g->edges[i]; 1827 int d1 = e->dot1->index; /* either endpoint is good enough */ 1828 int comp = dsf_canonify(dsf, d1); 1829 if ((component_state[comp] == COMP_PATH && 1830 -1 != largest_comp) || 1831 (component_state[comp] == COMP_LOOP && 1832 comp != largest_comp)) 1833 state->line_errors[i] = true; 1834 } 1835 } 1836 } 1837 1838 if (nloop == 1 && npath == 0 && nsilly == 0) { 1839 /* 1840 * If there is exactly one component and it is a loop, then 1841 * the puzzle is potentially complete, so check the clues. 1842 */ 1843 ret = true; 1844 1845 for (i = 0; i < g->num_faces; i++) { 1846 int c = state->clues[i]; 1847 if (c >= 0 && face_order(state, i, LINE_YES) != c) { 1848 ret = false; 1849 break; 1850 } 1851 } 1852 1853 /* 1854 * Also, whether or not the puzzle is actually complete, set 1855 * the flag that says this game_state has exactly one loop and 1856 * nothing else, which will be used to vary the semantics of 1857 * clue highlighting at display time. 1858 */ 1859 state->exactly_one_loop = true; 1860 } else { 1861 ret = false; 1862 state->exactly_one_loop = false; 1863 } 1864 1865 sfree(component_state); 1866 dsf_free(dsf); 1867 1868 return ret; 1869} 1870 1871/* ---------------------------------------------------------------------- 1872 * Solver logic 1873 * 1874 * Our solver modes operate as follows. Each mode also uses the modes above it. 1875 * 1876 * Easy Mode 1877 * Just implement the rules of the game. 1878 * 1879 * Normal and Tricky Modes 1880 * For each (adjacent) pair of lines through each dot we store a bit for 1881 * whether at least one of them is on and whether at most one is on. (If we 1882 * know both or neither is on that's already stored more directly.) 1883 * 1884 * Advanced Mode 1885 * Use flip dsf data structure to make equivalence classes of lines that are 1886 * known identical to or opposite to one another. 1887 */ 1888 1889 1890/* DLines: 1891 * For general grids, we consider "dlines" to be pairs of lines joined 1892 * at a dot. The lines must be adjacent around the dot, so we can think of 1893 * a dline as being a dot+face combination. Or, a dot+edge combination where 1894 * the second edge is taken to be the next clockwise edge from the dot. 1895 * Original loopy code didn't have this extra restriction of the lines being 1896 * adjacent. From my tests with square grids, this extra restriction seems to 1897 * take little, if anything, away from the quality of the puzzles. 1898 * A dline can be uniquely identified by an edge/dot combination, given that 1899 * a dline-pair always goes clockwise around its common dot. The edge/dot 1900 * combination can be represented by an edge/bool combination - if bool is 1901 * true, use edge->dot1 else use edge->dot2. So the total number of dlines is 1902 * exactly twice the number of edges in the grid - although the dlines 1903 * spanning the infinite face are not all that useful to the solver. 1904 * Note that, by convention, a dline goes clockwise around its common dot, 1905 * which means the dline goes anti-clockwise around its common face. 1906 */ 1907 1908/* Helper functions for obtaining an index into an array of dlines, given 1909 * various information. We assume the grid layout conventions about how 1910 * the various lists are interleaved - see grid_make_consistent() for 1911 * details. */ 1912 1913/* i points to the first edge of the dline pair, reading clockwise around 1914 * the dot. */ 1915static int dline_index_from_dot(grid *g, grid_dot *d, int i) 1916{ 1917 grid_edge *e = d->edges[i]; 1918 int ret; 1919#ifdef DEBUG_DLINES 1920 grid_edge *e2; 1921 int i2 = i+1; 1922 if (i2 == d->order) i2 = 0; 1923 e2 = d->edges[i2]; 1924#endif 1925 ret = 2 * (e->index) + ((e->dot1 == d) ? 1 : 0); 1926#ifdef DEBUG_DLINES 1927 printf("dline_index_from_dot: d=%d,i=%d, edges [%d,%d] - %d\n", 1928 (int)(d->index), i, (int)(e->index), (int)(e2 ->index), ret); 1929#endif 1930 return ret; 1931} 1932/* i points to the second edge of the dline pair, reading clockwise around 1933 * the face. That is, the edges of the dline, starting at edge{i}, read 1934 * anti-clockwise around the face. By layout conventions, the common dot 1935 * of the dline will be f->dots[i] */ 1936static int dline_index_from_face(grid *g, grid_face *f, int i) 1937{ 1938 grid_edge *e = f->edges[i]; 1939 grid_dot *d = f->dots[i]; 1940 int ret; 1941#ifdef DEBUG_DLINES 1942 grid_edge *e2; 1943 int i2 = i - 1; 1944 if (i2 < 0) i2 += f->order; 1945 e2 = f->edges[i2]; 1946#endif 1947 ret = 2 * (e->index) + ((e->dot1 == d) ? 1 : 0); 1948#ifdef DEBUG_DLINES 1949 printf("dline_index_from_face: f=%d,i=%d, edges [%d,%d] - %d\n", 1950 (int)(f->index), i, (int)(e->index), (int)(e2->index), ret); 1951#endif 1952 return ret; 1953} 1954static bool is_atleastone(const char *dline_array, int index) 1955{ 1956 return BIT_SET(dline_array[index], 0); 1957} 1958static bool set_atleastone(char *dline_array, int index) 1959{ 1960 return SET_BIT(dline_array[index], 0); 1961} 1962static bool is_atmostone(const char *dline_array, int index) 1963{ 1964 return BIT_SET(dline_array[index], 1); 1965} 1966static bool set_atmostone(char *dline_array, int index) 1967{ 1968 return SET_BIT(dline_array[index], 1); 1969} 1970 1971static void array_setall(char *array, char from, char to, int len) 1972{ 1973 char *p = array, *p_old = p; 1974 int len_remaining = len; 1975 1976 while ((p = memchr(p, from, len_remaining))) { 1977 *p = to; 1978 len_remaining -= p - p_old; 1979 p_old = p; 1980 } 1981} 1982 1983/* Helper, called when doing dline dot deductions, in the case where we 1984 * have 4 UNKNOWNs, and two of them (adjacent) have *exactly* one YES between 1985 * them (because of dline atmostone/atleastone). 1986 * On entry, edge points to the first of these two UNKNOWNs. This function 1987 * will find the opposite UNKNOWNS (if they are adjacent to one another) 1988 * and set their corresponding dline to atleastone. (Setting atmostone 1989 * already happens in earlier dline deductions) */ 1990static bool dline_set_opp_atleastone(solver_state *sstate, 1991 grid_dot *d, int edge) 1992{ 1993 game_state *state = sstate->state; 1994 grid *g = state->game_grid; 1995 int N = d->order; 1996 int opp, opp2; 1997 for (opp = 0; opp < N; opp++) { 1998 int opp_dline_index; 1999 if (opp == edge || opp == edge+1 || opp == edge-1) 2000 continue; 2001 if (opp == 0 && edge == N-1) 2002 continue; 2003 if (opp == N-1 && edge == 0) 2004 continue; 2005 opp2 = opp + 1; 2006 if (opp2 == N) opp2 = 0; 2007 /* Check if opp, opp2 point to LINE_UNKNOWNs */ 2008 if (state->lines[d->edges[opp]->index] != LINE_UNKNOWN) 2009 continue; 2010 if (state->lines[d->edges[opp2]->index] != LINE_UNKNOWN) 2011 continue; 2012 /* Found opposite UNKNOWNS and they're next to each other */ 2013 opp_dline_index = dline_index_from_dot(g, d, opp); 2014 return set_atleastone(sstate->dlines, opp_dline_index); 2015 } 2016 return false; 2017} 2018 2019 2020/* Set pairs of lines around this face which are known to be identical, to 2021 * the given line_state */ 2022static bool face_setall_identical(solver_state *sstate, int face_index, 2023 enum line_state line_new) 2024{ 2025 /* can[dir] contains the canonical line associated with the line in 2026 * direction dir from the square in question. Similarly inv[dir] is 2027 * whether or not the line in question is inverse to its canonical 2028 * element. */ 2029 bool retval = false; 2030 game_state *state = sstate->state; 2031 grid *g = state->game_grid; 2032 grid_face *f = g->faces[face_index]; 2033 int N = f->order; 2034 int i, j; 2035 int can1, can2; 2036 bool inv1, inv2; 2037 2038 for (i = 0; i < N; i++) { 2039 int line1_index = f->edges[i]->index; 2040 if (state->lines[line1_index] != LINE_UNKNOWN) 2041 continue; 2042 for (j = i + 1; j < N; j++) { 2043 int line2_index = f->edges[j]->index; 2044 if (state->lines[line2_index] != LINE_UNKNOWN) 2045 continue; 2046 2047 /* Found two UNKNOWNS */ 2048 can1 = dsf_canonify_flip(sstate->linedsf, line1_index, &inv1); 2049 can2 = dsf_canonify_flip(sstate->linedsf, line2_index, &inv2); 2050 if (can1 == can2 && inv1 == inv2) { 2051 solver_set_line(sstate, line1_index, line_new); 2052 solver_set_line(sstate, line2_index, line_new); 2053 } 2054 } 2055 } 2056 return retval; 2057} 2058 2059/* Given a dot or face, and a count of LINE_UNKNOWNs, find them and 2060 * return the edge indices into e. */ 2061static void find_unknowns(game_state *state, 2062 grid_edge **edge_list, /* Edge list to search (from a face or a dot) */ 2063 int expected_count, /* Number of UNKNOWNs (comes from solver's cache) */ 2064 int *e /* Returned edge indices */) 2065{ 2066 int c = 0; 2067 while (c < expected_count) { 2068 int line_index = (*edge_list)->index; 2069 if (state->lines[line_index] == LINE_UNKNOWN) { 2070 e[c] = line_index; 2071 c++; 2072 } 2073 ++edge_list; 2074 } 2075} 2076 2077/* If we have a list of edges, and we know whether the number of YESs should 2078 * be odd or even, and there are only a few UNKNOWNs, we can do some simple 2079 * linedsf deductions. This can be used for both face and dot deductions. 2080 * Returns the difficulty level of the next solver that should be used, 2081 * or DIFF_MAX if no progress was made. */ 2082static int parity_deductions(solver_state *sstate, 2083 grid_edge **edge_list, /* Edge list (from a face or a dot) */ 2084 int total_parity, /* Expected number of YESs modulo 2 (either 0 or 1) */ 2085 int unknown_count) 2086{ 2087 game_state *state = sstate->state; 2088 int diff = DIFF_MAX; 2089 DSF *linedsf = sstate->linedsf; 2090 2091 if (unknown_count == 2) { 2092 /* Lines are known alike/opposite, depending on inv. */ 2093 int e[2]; 2094 find_unknowns(state, edge_list, 2, e); 2095 if (merge_lines(sstate, e[0], e[1], total_parity)) 2096 diff = min(diff, DIFF_HARD); 2097 } else if (unknown_count == 3) { 2098 int e[3]; 2099 int can[3]; /* canonical edges */ 2100 bool inv[3]; /* whether can[x] is inverse to e[x] */ 2101 find_unknowns(state, edge_list, 3, e); 2102 can[0] = dsf_canonify_flip(linedsf, e[0], inv); 2103 can[1] = dsf_canonify_flip(linedsf, e[1], inv+1); 2104 can[2] = dsf_canonify_flip(linedsf, e[2], inv+2); 2105 if (can[0] == can[1]) { 2106 if (solver_set_line(sstate, e[2], (total_parity^inv[0]^inv[1]) ? 2107 LINE_YES : LINE_NO)) 2108 diff = min(diff, DIFF_EASY); 2109 } 2110 if (can[0] == can[2]) { 2111 if (solver_set_line(sstate, e[1], (total_parity^inv[0]^inv[2]) ? 2112 LINE_YES : LINE_NO)) 2113 diff = min(diff, DIFF_EASY); 2114 } 2115 if (can[1] == can[2]) { 2116 if (solver_set_line(sstate, e[0], (total_parity^inv[1]^inv[2]) ? 2117 LINE_YES : LINE_NO)) 2118 diff = min(diff, DIFF_EASY); 2119 } 2120 } else if (unknown_count == 4) { 2121 int e[4]; 2122 int can[4]; /* canonical edges */ 2123 bool inv[4]; /* whether can[x] is inverse to e[x] */ 2124 find_unknowns(state, edge_list, 4, e); 2125 can[0] = dsf_canonify_flip(linedsf, e[0], inv); 2126 can[1] = dsf_canonify_flip(linedsf, e[1], inv+1); 2127 can[2] = dsf_canonify_flip(linedsf, e[2], inv+2); 2128 can[3] = dsf_canonify_flip(linedsf, e[3], inv+3); 2129 if (can[0] == can[1]) { 2130 if (merge_lines(sstate, e[2], e[3], total_parity^inv[0]^inv[1])) 2131 diff = min(diff, DIFF_HARD); 2132 } else if (can[0] == can[2]) { 2133 if (merge_lines(sstate, e[1], e[3], total_parity^inv[0]^inv[2])) 2134 diff = min(diff, DIFF_HARD); 2135 } else if (can[0] == can[3]) { 2136 if (merge_lines(sstate, e[1], e[2], total_parity^inv[0]^inv[3])) 2137 diff = min(diff, DIFF_HARD); 2138 } else if (can[1] == can[2]) { 2139 if (merge_lines(sstate, e[0], e[3], total_parity^inv[1]^inv[2])) 2140 diff = min(diff, DIFF_HARD); 2141 } else if (can[1] == can[3]) { 2142 if (merge_lines(sstate, e[0], e[2], total_parity^inv[1]^inv[3])) 2143 diff = min(diff, DIFF_HARD); 2144 } else if (can[2] == can[3]) { 2145 if (merge_lines(sstate, e[0], e[1], total_parity^inv[2]^inv[3])) 2146 diff = min(diff, DIFF_HARD); 2147 } 2148 } 2149 return diff; 2150} 2151 2152 2153/* 2154 * These are the main solver functions. 2155 * 2156 * Their return values are diff values corresponding to the lowest mode solver 2157 * that would notice the work that they have done. For example if the normal 2158 * mode solver adds actual lines or crosses, it will return DIFF_EASY as the 2159 * easy mode solver might be able to make progress using that. It doesn't make 2160 * sense for one of them to return a diff value higher than that of the 2161 * function itself. 2162 * 2163 * Each function returns the lowest value it can, as early as possible, in 2164 * order to try and pass as much work as possible back to the lower level 2165 * solvers which progress more quickly. 2166 */ 2167 2168/* PROPOSED NEW DESIGN: 2169 * We have a work queue consisting of 'events' notifying us that something has 2170 * happened that a particular solver mode might be interested in. For example 2171 * the hard mode solver might do something that helps the normal mode solver at 2172 * dot [x,y] in which case it will enqueue an event recording this fact. Then 2173 * we pull events off the work queue, and hand each in turn to the solver that 2174 * is interested in them. If a solver reports that it failed we pass the same 2175 * event on to progressively more advanced solvers and the loop detector. Once 2176 * we've exhausted an event, or it has helped us progress, we drop it and 2177 * continue to the next one. The events are sorted first in order of solver 2178 * complexity (easy first) then order of insertion (oldest first). 2179 * Once we run out of events we loop over each permitted solver in turn 2180 * (easiest first) until either a deduction is made (and an event therefore 2181 * emerges) or no further deductions can be made (in which case we've failed). 2182 * 2183 * QUESTIONS: 2184 * * How do we 'loop over' a solver when both dots and squares are concerned. 2185 * Answer: first all squares then all dots. 2186 */ 2187 2188static int trivial_deductions(solver_state *sstate) 2189{ 2190 int i, current_yes, current_no; 2191 game_state *state = sstate->state; 2192 grid *g = state->game_grid; 2193 int diff = DIFF_MAX; 2194 2195 /* Per-face deductions */ 2196 for (i = 0; i < g->num_faces; i++) { 2197 grid_face *f = g->faces[i]; 2198 2199 if (sstate->face_solved[i]) 2200 continue; 2201 2202 current_yes = sstate->face_yes_count[i]; 2203 current_no = sstate->face_no_count[i]; 2204 2205 if (current_yes + current_no == f->order) { 2206 sstate->face_solved[i] = true; 2207 continue; 2208 } 2209 2210 if (state->clues[i] < 0) 2211 continue; 2212 2213 /* 2214 * This code checks whether the numeric clue on a face is so 2215 * large as to permit all its remaining LINE_UNKNOWNs to be 2216 * filled in as LINE_YES, or alternatively so small as to 2217 * permit them all to be filled in as LINE_NO. 2218 */ 2219 2220 if (state->clues[i] < current_yes) { 2221 sstate->solver_status = SOLVER_MISTAKE; 2222 return DIFF_EASY; 2223 } 2224 if (state->clues[i] == current_yes) { 2225 if (face_setall(sstate, i, LINE_UNKNOWN, LINE_NO)) 2226 diff = min(diff, DIFF_EASY); 2227 sstate->face_solved[i] = true; 2228 continue; 2229 } 2230 2231 if (f->order - state->clues[i] < current_no) { 2232 sstate->solver_status = SOLVER_MISTAKE; 2233 return DIFF_EASY; 2234 } 2235 if (f->order - state->clues[i] == current_no) { 2236 if (face_setall(sstate, i, LINE_UNKNOWN, LINE_YES)) 2237 diff = min(diff, DIFF_EASY); 2238 sstate->face_solved[i] = true; 2239 continue; 2240 } 2241 2242 if (f->order - state->clues[i] == current_no + 1 && 2243 f->order - current_yes - current_no > 2) { 2244 /* 2245 * One small refinement to the above: we also look for any 2246 * adjacent pair of LINE_UNKNOWNs around the face with 2247 * some LINE_YES incident on it from elsewhere. If we find 2248 * one, then we know that pair of LINE_UNKNOWNs can't 2249 * _both_ be LINE_YES, and hence that pushes us one line 2250 * closer to being able to determine all the rest. 2251 */ 2252 int j, k, e1, e2, e, d; 2253 2254 for (j = 0; j < f->order; j++) { 2255 e1 = f->edges[j]->index; 2256 e2 = f->edges[j+1 < f->order ? j+1 : 0]->index; 2257 2258 if (g->edges[e1]->dot1 == g->edges[e2]->dot1 || 2259 g->edges[e1]->dot1 == g->edges[e2]->dot2) { 2260 d = g->edges[e1]->dot1->index; 2261 } else { 2262 assert(g->edges[e1]->dot2 == g->edges[e2]->dot1 || 2263 g->edges[e1]->dot2 == g->edges[e2]->dot2); 2264 d = g->edges[e1]->dot2->index; 2265 } 2266 2267 if (state->lines[e1] == LINE_UNKNOWN && 2268 state->lines[e2] == LINE_UNKNOWN) { 2269 for (k = 0; k < g->dots[d]->order; k++) { 2270 int e = g->dots[d]->edges[k]->index; 2271 if (state->lines[e] == LINE_YES) 2272 goto found; /* multi-level break */ 2273 } 2274 } 2275 } 2276 continue; 2277 2278 found: 2279 /* 2280 * If we get here, we've found such a pair of edges, and 2281 * they're e1 and e2. 2282 */ 2283 for (j = 0; j < f->order; j++) { 2284 e = f->edges[j]->index; 2285 if (state->lines[e] == LINE_UNKNOWN && e != e1 && e != e2) { 2286 bool r = solver_set_line(sstate, e, LINE_YES); 2287 assert(r); 2288 diff = min(diff, DIFF_EASY); 2289 } 2290 } 2291 } 2292 } 2293 2294 check_caches(sstate); 2295 2296 /* Per-dot deductions */ 2297 for (i = 0; i < g->num_dots; i++) { 2298 grid_dot *d = g->dots[i]; 2299 int yes, no, unknown; 2300 2301 if (sstate->dot_solved[i]) 2302 continue; 2303 2304 yes = sstate->dot_yes_count[i]; 2305 no = sstate->dot_no_count[i]; 2306 unknown = d->order - yes - no; 2307 2308 if (yes == 0) { 2309 if (unknown == 0) { 2310 sstate->dot_solved[i] = true; 2311 } else if (unknown == 1) { 2312 dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO); 2313 diff = min(diff, DIFF_EASY); 2314 sstate->dot_solved[i] = true; 2315 } 2316 } else if (yes == 1) { 2317 if (unknown == 0) { 2318 sstate->solver_status = SOLVER_MISTAKE; 2319 return DIFF_EASY; 2320 } else if (unknown == 1) { 2321 dot_setall(sstate, i, LINE_UNKNOWN, LINE_YES); 2322 diff = min(diff, DIFF_EASY); 2323 } 2324 } else if (yes == 2) { 2325 if (unknown > 0) { 2326 dot_setall(sstate, i, LINE_UNKNOWN, LINE_NO); 2327 diff = min(diff, DIFF_EASY); 2328 } 2329 sstate->dot_solved[i] = true; 2330 } else { 2331 sstate->solver_status = SOLVER_MISTAKE; 2332 return DIFF_EASY; 2333 } 2334 } 2335 2336 check_caches(sstate); 2337 2338 return diff; 2339} 2340 2341static int dline_deductions(solver_state *sstate) 2342{ 2343 game_state *state = sstate->state; 2344 grid *g = state->game_grid; 2345 char *dlines = sstate->dlines; 2346 int i; 2347 int diff = DIFF_MAX; 2348 2349 /* ------ Face deductions ------ */ 2350 2351 /* Given a set of dline atmostone/atleastone constraints, need to figure 2352 * out if we can deduce any further info. For more general faces than 2353 * squares, this turns out to be a tricky problem. 2354 * The approach taken here is to define (per face) NxN matrices: 2355 * "maxs" and "mins". 2356 * The entries maxs(j,k) and mins(j,k) define the upper and lower limits 2357 * for the possible number of edges that are YES between positions j and k 2358 * going clockwise around the face. Can think of j and k as marking dots 2359 * around the face (recall the labelling scheme: edge0 joins dot0 to dot1, 2360 * edge1 joins dot1 to dot2 etc). 2361 * Trivially, mins(j,j) = maxs(j,j) = 0, and we don't even bother storing 2362 * these. mins(j,j+1) and maxs(j,j+1) are determined by whether edge{j} 2363 * is YES, NO or UNKNOWN. mins(j,j+2) and maxs(j,j+2) are related to 2364 * the dline atmostone/atleastone status for edges j and j+1. 2365 * 2366 * Then we calculate the remaining entries recursively. We definitely 2367 * know that 2368 * mins(j,k) >= { mins(j,u) + mins(u,k) } for any u between j and k. 2369 * This is because any valid placement of YESs between j and k must give 2370 * a valid placement between j and u, and also between u and k. 2371 * I believe it's sufficient to use just the two values of u: 2372 * j+1 and j+2. Seems to work well in practice - the bounds we compute 2373 * are rigorous, even if they might not be best-possible. 2374 * 2375 * Once we have maxs and mins calculated, we can make inferences about 2376 * each dline{j,j+1} by looking at the possible complementary edge-counts 2377 * mins(j+2,j) and maxs(j+2,j) and comparing these with the face clue. 2378 * As well as dlines, we can make similar inferences about single edges. 2379 * For example, consider a pentagon with clue 3, and we know at most one 2380 * of (edge0, edge1) is YES, and at most one of (edge2, edge3) is YES. 2381 * We could then deduce edge4 is YES, because maxs(0,4) would be 2, so 2382 * that final edge would have to be YES to make the count up to 3. 2383 */ 2384 2385 /* Much quicker to allocate arrays on the stack than the heap, so 2386 * define the largest possible face size, and base our array allocations 2387 * on that. We check this with an assertion, in case someone decides to 2388 * make a grid which has larger faces than this. Note, this algorithm 2389 * could get quite expensive if there are many large faces. */ 2390#define MAX_FACE_SIZE 14 2391 2392 for (i = 0; i < g->num_faces; i++) { 2393 int maxs[MAX_FACE_SIZE][MAX_FACE_SIZE]; 2394 int mins[MAX_FACE_SIZE][MAX_FACE_SIZE]; 2395 grid_face *f = g->faces[i]; 2396 int N = f->order; 2397 int j,m; 2398 int clue = state->clues[i]; 2399 assert(N <= MAX_FACE_SIZE); 2400 if (sstate->face_solved[i]) 2401 continue; 2402 if (clue < 0) continue; 2403 2404 /* Calculate the (j,j+1) entries */ 2405 for (j = 0; j < N; j++) { 2406 int edge_index = f->edges[j]->index; 2407 int dline_index; 2408 enum line_state line1 = state->lines[edge_index]; 2409 enum line_state line2; 2410 int tmp; 2411 int k = j + 1; 2412 if (k >= N) k = 0; 2413 maxs[j][k] = (line1 == LINE_NO) ? 0 : 1; 2414 mins[j][k] = (line1 == LINE_YES) ? 1 : 0; 2415 /* Calculate the (j,j+2) entries */ 2416 dline_index = dline_index_from_face(g, f, k); 2417 edge_index = f->edges[k]->index; 2418 line2 = state->lines[edge_index]; 2419 k++; 2420 if (k >= N) k = 0; 2421 2422 /* max */ 2423 tmp = 2; 2424 if (line1 == LINE_NO) tmp--; 2425 if (line2 == LINE_NO) tmp--; 2426 if (tmp == 2 && is_atmostone(dlines, dline_index)) 2427 tmp = 1; 2428 maxs[j][k] = tmp; 2429 2430 /* min */ 2431 tmp = 0; 2432 if (line1 == LINE_YES) tmp++; 2433 if (line2 == LINE_YES) tmp++; 2434 if (tmp == 0 && is_atleastone(dlines, dline_index)) 2435 tmp = 1; 2436 mins[j][k] = tmp; 2437 } 2438 2439 /* Calculate the (j,j+m) entries for m between 3 and N-1 */ 2440 for (m = 3; m < N; m++) { 2441 for (j = 0; j < N; j++) { 2442 int k = j + m; 2443 int u = j + 1; 2444 int v = j + 2; 2445 int tmp; 2446 if (k >= N) k -= N; 2447 if (u >= N) u -= N; 2448 if (v >= N) v -= N; 2449 maxs[j][k] = maxs[j][u] + maxs[u][k]; 2450 mins[j][k] = mins[j][u] + mins[u][k]; 2451 tmp = maxs[j][v] + maxs[v][k]; 2452 maxs[j][k] = min(maxs[j][k], tmp); 2453 tmp = mins[j][v] + mins[v][k]; 2454 mins[j][k] = max(mins[j][k], tmp); 2455 } 2456 } 2457 2458 /* See if we can make any deductions */ 2459 for (j = 0; j < N; j++) { 2460 int k; 2461 grid_edge *e = f->edges[j]; 2462 int line_index = e->index; 2463 int dline_index; 2464 2465 if (state->lines[line_index] != LINE_UNKNOWN) 2466 continue; 2467 k = j + 1; 2468 if (k >= N) k = 0; 2469 2470 /* minimum YESs in the complement of this edge */ 2471 if (mins[k][j] > clue) { 2472 sstate->solver_status = SOLVER_MISTAKE; 2473 return DIFF_EASY; 2474 } 2475 if (mins[k][j] == clue) { 2476 /* setting this edge to YES would make at least 2477 * (clue+1) edges - contradiction */ 2478 solver_set_line(sstate, line_index, LINE_NO); 2479 diff = min(diff, DIFF_EASY); 2480 } 2481 if (maxs[k][j] < clue - 1) { 2482 sstate->solver_status = SOLVER_MISTAKE; 2483 return DIFF_EASY; 2484 } 2485 if (maxs[k][j] == clue - 1) { 2486 /* Only way to satisfy the clue is to set edge{j} as YES */ 2487 solver_set_line(sstate, line_index, LINE_YES); 2488 diff = min(diff, DIFF_EASY); 2489 } 2490 2491 /* More advanced deduction that allows propagation along diagonal 2492 * chains of faces connected by dots, for example, 3-2-...-2-3 2493 * in square grids. */ 2494 if (sstate->diff >= DIFF_TRICKY) { 2495 /* Now see if we can make dline deduction for edges{j,j+1} */ 2496 e = f->edges[k]; 2497 if (state->lines[e->index] != LINE_UNKNOWN) 2498 /* Only worth doing this for an UNKNOWN,UNKNOWN pair. 2499 * Dlines where one of the edges is known, are handled in the 2500 * dot-deductions */ 2501 continue; 2502 2503 dline_index = dline_index_from_face(g, f, k); 2504 k++; 2505 if (k >= N) k = 0; 2506 2507 /* minimum YESs in the complement of this dline */ 2508 if (mins[k][j] > clue - 2) { 2509 /* Adding 2 YESs would break the clue */ 2510 if (set_atmostone(dlines, dline_index)) 2511 diff = min(diff, DIFF_NORMAL); 2512 } 2513 /* maximum YESs in the complement of this dline */ 2514 if (maxs[k][j] < clue) { 2515 /* Adding 2 NOs would mean not enough YESs */ 2516 if (set_atleastone(dlines, dline_index)) 2517 diff = min(diff, DIFF_NORMAL); 2518 } 2519 } 2520 } 2521 } 2522 2523 if (diff < DIFF_NORMAL) 2524 return diff; 2525 2526 /* ------ Dot deductions ------ */ 2527 2528 for (i = 0; i < g->num_dots; i++) { 2529 grid_dot *d = g->dots[i]; 2530 int N = d->order; 2531 int yes, no, unknown; 2532 int j; 2533 if (sstate->dot_solved[i]) 2534 continue; 2535 yes = sstate->dot_yes_count[i]; 2536 no = sstate->dot_no_count[i]; 2537 unknown = N - yes - no; 2538 2539 for (j = 0; j < N; j++) { 2540 int k; 2541 int dline_index; 2542 int line1_index, line2_index; 2543 enum line_state line1, line2; 2544 k = j + 1; 2545 if (k >= N) k = 0; 2546 dline_index = dline_index_from_dot(g, d, j); 2547 line1_index = d->edges[j]->index; 2548 line2_index = d->edges[k] ->index; 2549 line1 = state->lines[line1_index]; 2550 line2 = state->lines[line2_index]; 2551 2552 /* Infer dline state from line state */ 2553 if (line1 == LINE_NO || line2 == LINE_NO) { 2554 if (set_atmostone(dlines, dline_index)) 2555 diff = min(diff, DIFF_NORMAL); 2556 } 2557 if (line1 == LINE_YES || line2 == LINE_YES) { 2558 if (set_atleastone(dlines, dline_index)) 2559 diff = min(diff, DIFF_NORMAL); 2560 } 2561 /* Infer line state from dline state */ 2562 if (is_atmostone(dlines, dline_index)) { 2563 if (line1 == LINE_YES && line2 == LINE_UNKNOWN) { 2564 solver_set_line(sstate, line2_index, LINE_NO); 2565 diff = min(diff, DIFF_EASY); 2566 } 2567 if (line2 == LINE_YES && line1 == LINE_UNKNOWN) { 2568 solver_set_line(sstate, line1_index, LINE_NO); 2569 diff = min(diff, DIFF_EASY); 2570 } 2571 } 2572 if (is_atleastone(dlines, dline_index)) { 2573 if (line1 == LINE_NO && line2 == LINE_UNKNOWN) { 2574 solver_set_line(sstate, line2_index, LINE_YES); 2575 diff = min(diff, DIFF_EASY); 2576 } 2577 if (line2 == LINE_NO && line1 == LINE_UNKNOWN) { 2578 solver_set_line(sstate, line1_index, LINE_YES); 2579 diff = min(diff, DIFF_EASY); 2580 } 2581 } 2582 /* Deductions that depend on the numbers of lines. 2583 * Only bother if both lines are UNKNOWN, otherwise the 2584 * easy-mode solver (or deductions above) would have taken 2585 * care of it. */ 2586 if (line1 != LINE_UNKNOWN || line2 != LINE_UNKNOWN) 2587 continue; 2588 2589 if (yes == 0 && unknown == 2) { 2590 /* Both these unknowns must be identical. If we know 2591 * atmostone or atleastone, we can make progress. */ 2592 if (is_atmostone(dlines, dline_index)) { 2593 solver_set_line(sstate, line1_index, LINE_NO); 2594 solver_set_line(sstate, line2_index, LINE_NO); 2595 diff = min(diff, DIFF_EASY); 2596 } 2597 if (is_atleastone(dlines, dline_index)) { 2598 solver_set_line(sstate, line1_index, LINE_YES); 2599 solver_set_line(sstate, line2_index, LINE_YES); 2600 diff = min(diff, DIFF_EASY); 2601 } 2602 } 2603 if (yes == 1) { 2604 if (set_atmostone(dlines, dline_index)) 2605 diff = min(diff, DIFF_NORMAL); 2606 if (unknown == 2) { 2607 if (set_atleastone(dlines, dline_index)) 2608 diff = min(diff, DIFF_NORMAL); 2609 } 2610 } 2611 2612 /* More advanced deduction that allows propagation along diagonal 2613 * chains of faces connected by dots, for example: 3-2-...-2-3 2614 * in square grids. */ 2615 if (sstate->diff >= DIFF_TRICKY) { 2616 /* If we have atleastone set for this dline, infer 2617 * atmostone for each "opposite" dline (that is, each 2618 * dline without edges in common with this one). 2619 * Again, this test is only worth doing if both these 2620 * lines are UNKNOWN. For if one of these lines were YES, 2621 * the (yes == 1) test above would kick in instead. */ 2622 if (is_atleastone(dlines, dline_index)) { 2623 int opp; 2624 for (opp = 0; opp < N; opp++) { 2625 int opp_dline_index; 2626 if (opp == j || opp == j+1 || opp == j-1) 2627 continue; 2628 if (j == 0 && opp == N-1) 2629 continue; 2630 if (j == N-1 && opp == 0) 2631 continue; 2632 opp_dline_index = dline_index_from_dot(g, d, opp); 2633 if (set_atmostone(dlines, opp_dline_index)) 2634 diff = min(diff, DIFF_NORMAL); 2635 } 2636 if (yes == 0 && is_atmostone(dlines, dline_index)) { 2637 /* This dline has *exactly* one YES and there are no 2638 * other YESs. This allows more deductions. */ 2639 if (unknown == 3) { 2640 /* Third unknown must be YES */ 2641 for (opp = 0; opp < N; opp++) { 2642 int opp_index; 2643 if (opp == j || opp == k) 2644 continue; 2645 opp_index = d->edges[opp]->index; 2646 if (state->lines[opp_index] == LINE_UNKNOWN) { 2647 solver_set_line(sstate, opp_index, 2648 LINE_YES); 2649 diff = min(diff, DIFF_EASY); 2650 } 2651 } 2652 } else if (unknown == 4) { 2653 /* Exactly one of opposite UNKNOWNS is YES. We've 2654 * already set atmostone, so set atleastone as 2655 * well. 2656 */ 2657 if (dline_set_opp_atleastone(sstate, d, j)) 2658 diff = min(diff, DIFF_NORMAL); 2659 } 2660 } 2661 } 2662 } 2663 } 2664 } 2665 return diff; 2666} 2667 2668static int linedsf_deductions(solver_state *sstate) 2669{ 2670 game_state *state = sstate->state; 2671 grid *g = state->game_grid; 2672 char *dlines = sstate->dlines; 2673 int i; 2674 int diff = DIFF_MAX; 2675 int diff_tmp; 2676 2677 /* ------ Face deductions ------ */ 2678 2679 /* A fully-general linedsf deduction seems overly complicated 2680 * (I suspect the problem is NP-complete, though in practice it might just 2681 * be doable because faces are limited in size). 2682 * For simplicity, we only consider *pairs* of LINE_UNKNOWNS that are 2683 * known to be identical. If setting them both to YES (or NO) would break 2684 * the clue, set them to NO (or YES). */ 2685 2686 for (i = 0; i < g->num_faces; i++) { 2687 int N, yes, no, unknown; 2688 int clue; 2689 2690 if (sstate->face_solved[i]) 2691 continue; 2692 clue = state->clues[i]; 2693 if (clue < 0) 2694 continue; 2695 2696 N = g->faces[i]->order; 2697 yes = sstate->face_yes_count[i]; 2698 if (yes + 1 == clue) { 2699 if (face_setall_identical(sstate, i, LINE_NO)) 2700 diff = min(diff, DIFF_EASY); 2701 } 2702 no = sstate->face_no_count[i]; 2703 if (no + 1 == N - clue) { 2704 if (face_setall_identical(sstate, i, LINE_YES)) 2705 diff = min(diff, DIFF_EASY); 2706 } 2707 2708 /* Reload YES count, it might have changed */ 2709 yes = sstate->face_yes_count[i]; 2710 unknown = N - no - yes; 2711 2712 /* Deductions with small number of LINE_UNKNOWNs, based on overall 2713 * parity of lines. */ 2714 diff_tmp = parity_deductions(sstate, g->faces[i]->edges, 2715 (clue - yes) % 2, unknown); 2716 diff = min(diff, diff_tmp); 2717 } 2718 2719 /* ------ Dot deductions ------ */ 2720 for (i = 0; i < g->num_dots; i++) { 2721 grid_dot *d = g->dots[i]; 2722 int N = d->order; 2723 int j; 2724 int yes, no, unknown; 2725 /* Go through dlines, and do any dline<->linedsf deductions wherever 2726 * we find two UNKNOWNS. */ 2727 for (j = 0; j < N; j++) { 2728 int dline_index = dline_index_from_dot(g, d, j); 2729 int line1_index; 2730 int line2_index; 2731 int can1, can2; 2732 bool inv1, inv2; 2733 int j2; 2734 line1_index = d->edges[j]->index; 2735 if (state->lines[line1_index] != LINE_UNKNOWN) 2736 continue; 2737 j2 = j + 1; 2738 if (j2 == N) j2 = 0; 2739 line2_index = d->edges[j2]->index; 2740 if (state->lines[line2_index] != LINE_UNKNOWN) 2741 continue; 2742 /* Infer dline flags from linedsf */ 2743 can1 = dsf_canonify_flip(sstate->linedsf, line1_index, &inv1); 2744 can2 = dsf_canonify_flip(sstate->linedsf, line2_index, &inv2); 2745 if (can1 == can2 && inv1 != inv2) { 2746 /* These are opposites, so set dline atmostone/atleastone */ 2747 if (set_atmostone(dlines, dline_index)) 2748 diff = min(diff, DIFF_NORMAL); 2749 if (set_atleastone(dlines, dline_index)) 2750 diff = min(diff, DIFF_NORMAL); 2751 continue; 2752 } 2753 /* Infer linedsf from dline flags */ 2754 if (is_atmostone(dlines, dline_index) 2755 && is_atleastone(dlines, dline_index)) { 2756 if (merge_lines(sstate, line1_index, line2_index, true)) 2757 diff = min(diff, DIFF_HARD); 2758 } 2759 } 2760 2761 /* Deductions with small number of LINE_UNKNOWNs, based on overall 2762 * parity of lines. */ 2763 yes = sstate->dot_yes_count[i]; 2764 no = sstate->dot_no_count[i]; 2765 unknown = N - yes - no; 2766 diff_tmp = parity_deductions(sstate, d->edges, 2767 yes % 2, unknown); 2768 diff = min(diff, diff_tmp); 2769 } 2770 2771 /* ------ Edge dsf deductions ------ */ 2772 2773 /* If the state of a line is known, deduce the state of its canonical line 2774 * too, and vice versa. */ 2775 for (i = 0; i < g->num_edges; i++) { 2776 int can; 2777 bool inv; 2778 enum line_state s; 2779 can = dsf_canonify_flip(sstate->linedsf, i, &inv); 2780 if (can == i) 2781 continue; 2782 s = sstate->state->lines[can]; 2783 if (s != LINE_UNKNOWN) { 2784 if (solver_set_line(sstate, i, inv ? OPP(s) : s)) 2785 diff = min(diff, DIFF_EASY); 2786 } else { 2787 s = sstate->state->lines[i]; 2788 if (s != LINE_UNKNOWN) { 2789 if (solver_set_line(sstate, can, inv ? OPP(s) : s)) 2790 diff = min(diff, DIFF_EASY); 2791 } 2792 } 2793 } 2794 2795 return diff; 2796} 2797 2798static int loop_deductions(solver_state *sstate) 2799{ 2800 int edgecount = 0, clues = 0, satclues = 0, sm1clues = 0; 2801 game_state *state = sstate->state; 2802 grid *g = state->game_grid; 2803 int shortest_chainlen = g->num_dots; 2804 int dots_connected; 2805 bool progress = false; 2806 int i; 2807 2808 /* 2809 * Go through the grid and update for all the new edges. 2810 * Since merge_dots() is idempotent, the simplest way to 2811 * do this is just to update for _all_ the edges. 2812 * Also, while we're here, we count the edges. 2813 */ 2814 for (i = 0; i < g->num_edges; i++) { 2815 if (state->lines[i] == LINE_YES) { 2816 merge_dots(sstate, i); 2817 edgecount++; 2818 } 2819 } 2820 2821 /* 2822 * Count the clues, count the satisfied clues, and count the 2823 * satisfied-minus-one clues. 2824 */ 2825 for (i = 0; i < g->num_faces; i++) { 2826 int c = state->clues[i]; 2827 if (c >= 0) { 2828 int o = sstate->face_yes_count[i]; 2829 if (o == c) 2830 satclues++; 2831 else if (o == c-1) 2832 sm1clues++; 2833 clues++; 2834 } 2835 } 2836 2837 for (i = 0; i < g->num_dots; ++i) { 2838 dots_connected = 2839 sstate->looplen[dsf_canonify(sstate->dotdsf, i)]; 2840 if (dots_connected > 1) 2841 shortest_chainlen = min(shortest_chainlen, dots_connected); 2842 } 2843 2844 assert(sstate->solver_status == SOLVER_INCOMPLETE); 2845 2846 if (satclues == clues && shortest_chainlen == edgecount) { 2847 sstate->solver_status = SOLVER_SOLVED; 2848 /* This discovery clearly counts as progress, even if we haven't 2849 * just added any lines or anything */ 2850 progress = true; 2851 goto finished_loop_deductionsing; 2852 } 2853 2854 /* 2855 * Now go through looking for LINE_UNKNOWN edges which 2856 * connect two dots that are already in the same 2857 * equivalence class. If we find one, test to see if the 2858 * loop it would create is a solution. 2859 */ 2860 for (i = 0; i < g->num_edges; i++) { 2861 grid_edge *e = g->edges[i]; 2862 int d1 = e->dot1->index; 2863 int d2 = e->dot2->index; 2864 int eqclass, val; 2865 if (state->lines[i] != LINE_UNKNOWN) 2866 continue; 2867 2868 eqclass = dsf_canonify(sstate->dotdsf, d1); 2869 if (eqclass != dsf_canonify(sstate->dotdsf, d2)) 2870 continue; 2871 2872 val = LINE_NO; /* loop is bad until proven otherwise */ 2873 2874 /* 2875 * This edge would form a loop. Next 2876 * question: how long would the loop be? 2877 * Would it equal the total number of edges 2878 * (plus the one we'd be adding if we added 2879 * it)? 2880 */ 2881 if (sstate->looplen[eqclass] == edgecount + 1) { 2882 int sm1_nearby; 2883 2884 /* 2885 * This edge would form a loop which 2886 * took in all the edges in the entire 2887 * grid. So now we need to work out 2888 * whether it would be a valid solution 2889 * to the puzzle, which means we have to 2890 * check if it satisfies all the clues. 2891 * This means that every clue must be 2892 * either satisfied or satisfied-minus- 2893 * 1, and also that the number of 2894 * satisfied-minus-1 clues must be at 2895 * most two and they must lie on either 2896 * side of this edge. 2897 */ 2898 sm1_nearby = 0; 2899 if (e->face1) { 2900 int f = e->face1->index; 2901 int c = state->clues[f]; 2902 if (c >= 0 && sstate->face_yes_count[f] == c - 1) 2903 sm1_nearby++; 2904 } 2905 if (e->face2) { 2906 int f = e->face2->index; 2907 int c = state->clues[f]; 2908 if (c >= 0 && sstate->face_yes_count[f] == c - 1) 2909 sm1_nearby++; 2910 } 2911 if (sm1clues == sm1_nearby && 2912 sm1clues + satclues == clues) { 2913 val = LINE_YES; /* loop is good! */ 2914 } 2915 } 2916 2917 /* 2918 * Right. Now we know that adding this edge 2919 * would form a loop, and we know whether 2920 * that loop would be a viable solution or 2921 * not. 2922 * 2923 * If adding this edge produces a solution, 2924 * then we know we've found _a_ solution but 2925 * we don't know that it's _the_ solution - 2926 * if it were provably the solution then 2927 * we'd have deduced this edge some time ago 2928 * without the need to do loop detection. So 2929 * in this state we return SOLVER_AMBIGUOUS, 2930 * which has the effect that hitting Solve 2931 * on a user-provided puzzle will fill in a 2932 * solution but using the solver to 2933 * construct new puzzles won't consider this 2934 * a reasonable deduction for the user to 2935 * make. 2936 */ 2937 progress = solver_set_line(sstate, i, val); 2938 assert(progress); 2939 if (val == LINE_YES) { 2940 sstate->solver_status = SOLVER_AMBIGUOUS; 2941 goto finished_loop_deductionsing; 2942 } 2943 } 2944 2945 finished_loop_deductionsing: 2946 return progress ? DIFF_EASY : DIFF_MAX; 2947} 2948 2949/* This will return a dynamically allocated solver_state containing the (more) 2950 * solved grid */ 2951static solver_state *solve_game_rec(const solver_state *sstate_start) 2952{ 2953 solver_state *sstate; 2954 2955 /* Index of the solver we should call next. */ 2956 int i = 0; 2957 2958 /* As a speed-optimisation, we avoid re-running solvers that we know 2959 * won't make any progress. This happens when a high-difficulty 2960 * solver makes a deduction that can only help other high-difficulty 2961 * solvers. 2962 * For example: if a new 'dline' flag is set by dline_deductions, the 2963 * trivial_deductions solver cannot do anything with this information. 2964 * If we've already run the trivial_deductions solver (because it's 2965 * earlier in the list), there's no point running it again. 2966 * 2967 * Therefore: if a solver is earlier in the list than "threshold_index", 2968 * we don't bother running it if it's difficulty level is less than 2969 * "threshold_diff". 2970 */ 2971 int threshold_diff = 0; 2972 int threshold_index = 0; 2973 2974 sstate = dup_solver_state(sstate_start); 2975 2976 check_caches(sstate); 2977 2978 while (i < NUM_SOLVERS) { 2979 if (sstate->solver_status == SOLVER_MISTAKE) 2980 return sstate; 2981 if (sstate->solver_status == SOLVER_SOLVED || 2982 sstate->solver_status == SOLVER_AMBIGUOUS) { 2983 /* solver finished */ 2984 break; 2985 } 2986 2987 if ((solver_diffs[i] >= threshold_diff || i >= threshold_index) 2988 && solver_diffs[i] <= sstate->diff) { 2989 /* current_solver is eligible, so use it */ 2990 int next_diff = solver_fns[i](sstate); 2991 if (next_diff != DIFF_MAX) { 2992 /* solver made progress, so use new thresholds and 2993 * start again at top of list. */ 2994 threshold_diff = next_diff; 2995 threshold_index = i; 2996 i = 0; 2997 continue; 2998 } 2999 } 3000 /* current_solver is ineligible, or failed to make progress, so 3001 * go to the next solver in the list */ 3002 i++; 3003 } 3004 3005 if (sstate->solver_status == SOLVER_SOLVED || 3006 sstate->solver_status == SOLVER_AMBIGUOUS) { 3007 /* s/LINE_UNKNOWN/LINE_NO/g */ 3008 array_setall(sstate->state->lines, LINE_UNKNOWN, LINE_NO, 3009 sstate->state->game_grid->num_edges); 3010 return sstate; 3011 } 3012 3013 return sstate; 3014} 3015 3016static char *solve_game(const game_state *state, const game_state *currstate, 3017 const char *aux, const char **error) 3018{ 3019 char *soln = NULL; 3020 solver_state *sstate, *new_sstate; 3021 3022 sstate = new_solver_state(state, DIFF_MAX); 3023 new_sstate = solve_game_rec(sstate); 3024 3025 if (new_sstate->solver_status == SOLVER_SOLVED) { 3026 soln = encode_solve_move(new_sstate->state); 3027 } else if (new_sstate->solver_status == SOLVER_AMBIGUOUS) { 3028 soln = encode_solve_move(new_sstate->state); 3029 /**error = "Solver found ambiguous solutions"; */ 3030 } else { 3031 soln = encode_solve_move(new_sstate->state); 3032 /**error = "Solver failed"; */ 3033 } 3034 3035 free_solver_state(new_sstate); 3036 free_solver_state(sstate); 3037 3038 return soln; 3039} 3040 3041/* ---------------------------------------------------------------------- 3042 * Drawing and mouse-handling 3043 */ 3044 3045static char *interpret_move(const game_state *state, game_ui *ui, 3046 const game_drawstate *ds, 3047 int x, int y, int button) 3048{ 3049 grid *g = state->game_grid; 3050 grid_edge *e; 3051 int i; 3052 char *movebuf; 3053 int movelen, movesize; 3054 char button_char = ' '; 3055 enum line_state old_state; 3056 3057 button = STRIP_BUTTON_MODIFIERS(button); 3058 3059 /* Convert mouse-click (x,y) to grid coordinates */ 3060 x -= BORDER(ds->tilesize); 3061 y -= BORDER(ds->tilesize); 3062 x = x * g->tilesize / ds->tilesize; 3063 y = y * g->tilesize / ds->tilesize; 3064 x += g->lowest_x; 3065 y += g->lowest_y; 3066 3067 e = grid_nearest_edge(g, x, y); 3068 if (e == NULL) 3069 return NULL; 3070 3071 i = e->index; 3072 3073 /* I think it's only possible to play this game with mouse clicks, sorry */ 3074 /* Maybe will add mouse drag support some time */ 3075 old_state = state->lines[i]; 3076 3077 switch (button) { 3078 case LEFT_BUTTON: 3079 switch (old_state) { 3080 case LINE_UNKNOWN: 3081 button_char = 'y'; 3082 break; 3083 case LINE_YES: 3084#ifdef STYLUS_BASED 3085 button_char = 'n'; 3086 break; 3087#endif 3088 case LINE_NO: 3089 button_char = 'u'; 3090 break; 3091 } 3092 break; 3093 case MIDDLE_BUTTON: 3094 button_char = 'u'; 3095 break; 3096 case RIGHT_BUTTON: 3097 switch (old_state) { 3098 case LINE_UNKNOWN: 3099 button_char = 'n'; 3100 break; 3101 case LINE_NO: 3102#ifdef STYLUS_BASED 3103 button_char = 'y'; 3104 break; 3105#endif 3106 case LINE_YES: 3107 button_char = 'u'; 3108 break; 3109 } 3110 break; 3111 default: 3112 return NULL; 3113 } 3114 3115 movelen = 0; 3116 movesize = 80; 3117 movebuf = snewn(movesize, char); 3118 movelen = sprintf(movebuf, "%d%c", i, (int)button_char); 3119 3120 if (ui->autofollow != AF_OFF) { 3121 int dotid; 3122 for (dotid = 0; dotid < 2; dotid++) { 3123 grid_dot *dot = (dotid == 0 ? e->dot1 : e->dot2); 3124 grid_edge *e_this = e; 3125 3126 while (1) { 3127 int j, n_found; 3128 grid_edge *e_next = NULL; 3129 3130 for (j = n_found = 0; j < dot->order; j++) { 3131 grid_edge *e_candidate = dot->edges[j]; 3132 int i_candidate = e_candidate->index; 3133 if (e_candidate != e_this && 3134 (ui->autofollow == AF_FIXED || 3135 state->lines[i] == LINE_NO || 3136 state->lines[i_candidate] != LINE_NO)) { 3137 e_next = e_candidate; 3138 n_found++; 3139 } 3140 } 3141 3142 if (n_found != 1 || 3143 state->lines[e_next->index] != state->lines[i]) 3144 break; 3145 3146 if (e_next == e) { 3147 /* 3148 * Special case: we might have come all the way 3149 * round a loop and found our way back to the same 3150 * edge we started from. In that situation, we 3151 * must terminate not only this while loop, but 3152 * the 'for' outside it that was tracing in both 3153 * directions from the starting edge, because if 3154 * we let it trace in the second direction then 3155 * we'll only find ourself traversing the same 3156 * loop in the other order and generate an encoded 3157 * move string that mentions the same set of edges 3158 * twice. 3159 */ 3160 goto autofollow_done; 3161 } 3162 3163 dot = (e_next->dot1 != dot ? e_next->dot1 : e_next->dot2); 3164 if (movelen > movesize - 40) { 3165 movesize = movesize * 5 / 4 + 128; 3166 movebuf = sresize(movebuf, movesize, char); 3167 } 3168 e_this = e_next; 3169 movelen += sprintf(movebuf+movelen, "%d%c", 3170 (int)(e_this->index), button_char); 3171 } 3172 autofollow_done:; 3173 } 3174 } 3175 3176 return sresize(movebuf, movelen+1, char); 3177} 3178 3179static game_state *execute_move(const game_state *state, const char *move) 3180{ 3181 int i; 3182 game_state *newstate = dup_game(state); 3183 3184 if (move[0] == 'S') { 3185 move++; 3186 newstate->cheated = true; 3187 } 3188 3189 while (*move) { 3190 i = atoi(move); 3191 if (i < 0 || i >= newstate->game_grid->num_edges) 3192 goto fail; 3193 move += strspn(move, "1234567890"); 3194 switch (*(move++)) { 3195 case 'y': 3196 newstate->lines[i] = LINE_YES; 3197 break; 3198 case 'n': 3199 newstate->lines[i] = LINE_NO; 3200 break; 3201 case 'u': 3202 newstate->lines[i] = LINE_UNKNOWN; 3203 break; 3204 default: 3205 goto fail; 3206 } 3207 } 3208 3209 /* 3210 * Check for completion. 3211 */ 3212 if (check_completion(newstate)) 3213 newstate->solved = true; 3214 3215 return newstate; 3216 3217 fail: 3218 free_game(newstate); 3219 return NULL; 3220} 3221 3222/* ---------------------------------------------------------------------- 3223 * Drawing routines. 3224 */ 3225 3226/* Convert from grid coordinates to screen coordinates */ 3227static void grid_to_screen(const game_drawstate *ds, const grid *g, 3228 int grid_x, int grid_y, int *x, int *y) 3229{ 3230 *x = grid_x - g->lowest_x; 3231 *y = grid_y - g->lowest_y; 3232 *x = *x * ds->tilesize / g->tilesize; 3233 *y = *y * ds->tilesize / g->tilesize; 3234 *x += BORDER(ds->tilesize); 3235 *y += BORDER(ds->tilesize); 3236} 3237 3238/* Returns (into x,y) position of centre of face for rendering the text clue. 3239 */ 3240static void face_text_pos(const game_drawstate *ds, const grid *g, 3241 grid_face *f, int *xret, int *yret) 3242{ 3243 int faceindex = f->index; 3244 3245 /* 3246 * Return the cached position for this face, if we've already 3247 * worked it out. 3248 */ 3249 if (ds->textx[faceindex] >= 0) { 3250 *xret = ds->textx[faceindex]; 3251 *yret = ds->texty[faceindex]; 3252 return; 3253 } 3254 3255 /* 3256 * Otherwise, use the incentre computed by grid.c and convert it 3257 * to screen coordinates. 3258 */ 3259 grid_find_incentre(f); 3260 grid_to_screen(ds, g, f->ix, f->iy, 3261 &ds->textx[faceindex], &ds->texty[faceindex]); 3262 3263 *xret = ds->textx[faceindex]; 3264 *yret = ds->texty[faceindex]; 3265} 3266 3267static void face_text_bbox(game_drawstate *ds, grid *g, grid_face *f, 3268 int *x, int *y, int *w, int *h) 3269{ 3270 int xx, yy; 3271 face_text_pos(ds, g, f, &xx, &yy); 3272 3273 /* There seems to be a certain amount of trial-and-error involved 3274 * in working out the correct bounding-box for the text. */ 3275 3276 *x = xx - ds->tilesize * 5 / 4 - 1; 3277 *y = yy - ds->tilesize/4 - 3; 3278 *w = ds->tilesize * 5 / 2 + 2; 3279 *h = ds->tilesize/2 + 5; 3280} 3281 3282static void game_redraw_clue(drawing *dr, game_drawstate *ds, 3283 const game_state *state, int i) 3284{ 3285 grid *g = state->game_grid; 3286 grid_face *f = g->faces[i]; 3287 int x, y; 3288 char c[20]; 3289 3290 sprintf(c, "%d", state->clues[i]); 3291 3292 face_text_pos(ds, g, f, &x, &y); 3293 draw_text(dr, x, y, 3294 FONT_VARIABLE, ds->tilesize/2, 3295 ALIGN_VCENTRE | ALIGN_HCENTRE, 3296 ds->clue_error[i] ? COL_MISTAKE : 3297 ds->clue_satisfied[i] ? COL_SATISFIED : COL_FOREGROUND, c); 3298} 3299 3300static void edge_bbox(game_drawstate *ds, grid *g, grid_edge *e, 3301 int *x, int *y, int *w, int *h) 3302{ 3303 int x1 = e->dot1->x; 3304 int y1 = e->dot1->y; 3305 int x2 = e->dot2->x; 3306 int y2 = e->dot2->y; 3307 int xmin, xmax, ymin, ymax; 3308 3309 grid_to_screen(ds, g, x1, y1, &x1, &y1); 3310 grid_to_screen(ds, g, x2, y2, &x2, &y2); 3311 /* Allow extra margin for dots, and thickness of lines */ 3312 xmin = min(x1, x2) - (ds->tilesize + 15) / 16; 3313 xmax = max(x1, x2) + (ds->tilesize + 15) / 16; 3314 ymin = min(y1, y2) - (ds->tilesize + 15) / 16; 3315 ymax = max(y1, y2) + (ds->tilesize + 15) / 16; 3316 3317 *x = xmin; 3318 *y = ymin; 3319 *w = xmax - xmin + 1; 3320 *h = ymax - ymin + 1; 3321} 3322 3323static void dot_bbox(game_drawstate *ds, grid *g, grid_dot *d, 3324 int *x, int *y, int *w, int *h) 3325{ 3326 int x1, y1; 3327 int xmin, xmax, ymin, ymax; 3328 3329 grid_to_screen(ds, g, d->x, d->y, &x1, &y1); 3330 3331 xmin = x1 - (ds->tilesize * 5 + 63) / 64; 3332 xmax = x1 + (ds->tilesize * 5 + 63) / 64; 3333 ymin = y1 - (ds->tilesize * 5 + 63) / 64; 3334 ymax = y1 + (ds->tilesize * 5 + 63) / 64; 3335 3336 *x = xmin; 3337 *y = ymin; 3338 *w = xmax - xmin + 1; 3339 *h = ymax - ymin + 1; 3340} 3341 3342static const int loopy_line_redraw_phases[] = { 3343 COL_FAINT, COL_LINEUNKNOWN, COL_FOREGROUND, COL_HIGHLIGHT, COL_MISTAKE 3344}; 3345#define NPHASES lenof(loopy_line_redraw_phases) 3346 3347static void game_redraw_line(drawing *dr, game_drawstate *ds,const game_ui *ui, 3348 const game_state *state, int i, int phase) 3349{ 3350 grid *g = state->game_grid; 3351 grid_edge *e = g->edges[i]; 3352 int x1, x2, y1, y2; 3353 int line_colour; 3354 3355 if (state->line_errors[i]) 3356 line_colour = COL_MISTAKE; 3357 else if (state->lines[i] == LINE_UNKNOWN) 3358 line_colour = COL_LINEUNKNOWN; 3359 else if (state->lines[i] == LINE_NO) 3360 line_colour = COL_FAINT; 3361 else if (ds->flashing) 3362 line_colour = COL_HIGHLIGHT; 3363 else 3364 line_colour = COL_FOREGROUND; 3365 if (line_colour != loopy_line_redraw_phases[phase]) 3366 return; 3367 3368 /* Convert from grid to screen coordinates */ 3369 grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1); 3370 grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2); 3371 3372 if (line_colour == COL_FAINT) { 3373 if (ui->draw_faint_lines) 3374 draw_thick_line(dr, ds->tilesize/24.0, 3375 x1 + 0.5, y1 + 0.5, 3376 x2 + 0.5, y2 + 0.5, 3377 line_colour); 3378 } else { 3379 draw_thick_line(dr, ds->tilesize*3/32.0, 3380 x1 + 0.5, y1 + 0.5, 3381 x2 + 0.5, y2 + 0.5, 3382 line_colour); 3383 } 3384} 3385 3386static void game_redraw_dot(drawing *dr, game_drawstate *ds, 3387 const game_state *state, int i) 3388{ 3389 grid *g = state->game_grid; 3390 grid_dot *d = g->dots[i]; 3391 int x, y; 3392 3393 grid_to_screen(ds, g, d->x, d->y, &x, &y); 3394 draw_circle(dr, x, y, ds->tilesize*2.5/32.0, COL_FOREGROUND, COL_FOREGROUND); 3395} 3396 3397static bool boxes_intersect(int x0, int y0, int w0, int h0, 3398 int x1, int y1, int w1, int h1) 3399{ 3400 /* 3401 * Two intervals intersect iff neither is wholly on one side of 3402 * the other. Two boxes intersect iff their horizontal and 3403 * vertical intervals both intersect. 3404 */ 3405 return (x0 < x1+w1 && x1 < x0+w0 && y0 < y1+h1 && y1 < y0+h0); 3406} 3407 3408static void game_redraw_in_rect(drawing *dr, game_drawstate *ds, 3409 const game_ui *ui, const game_state *state, 3410 int x, int y, int w, int h) 3411{ 3412 grid *g = state->game_grid; 3413 int i, phase; 3414 int bx, by, bw, bh; 3415 3416 clip(dr, x, y, w, h); 3417 draw_rect(dr, x, y, w, h, COL_BACKGROUND); 3418 3419 for (i = 0; i < g->num_faces; i++) { 3420 if (state->clues[i] >= 0) { 3421 face_text_bbox(ds, g, g->faces[i], &bx, &by, &bw, &bh); 3422 if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) 3423 game_redraw_clue(dr, ds, state, i); 3424 } 3425 } 3426 for (phase = 0; phase < NPHASES; phase++) { 3427 for (i = 0; i < g->num_edges; i++) { 3428 edge_bbox(ds, g, g->edges[i], &bx, &by, &bw, &bh); 3429 if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) 3430 game_redraw_line(dr, ds, ui, state, i, phase); 3431 } 3432 } 3433 for (i = 0; i < g->num_dots; i++) { 3434 dot_bbox(ds, g, g->dots[i], &bx, &by, &bw, &bh); 3435 if (boxes_intersect(x, y, w, h, bx, by, bw, bh)) 3436 game_redraw_dot(dr, ds, state, i); 3437 } 3438 3439 unclip(dr); 3440 draw_update(dr, x, y, w, h); 3441} 3442 3443static void game_redraw(drawing *dr, game_drawstate *ds, 3444 const game_state *oldstate, const game_state *state, 3445 int dir, const game_ui *ui, 3446 float animtime, float flashtime) 3447{ 3448#define REDRAW_OBJECTS_LIMIT 16 /* Somewhat arbitrary tradeoff */ 3449 3450 grid *g = state->game_grid; 3451 int border = BORDER(ds->tilesize); 3452 int i; 3453 bool flash_changed; 3454 bool redraw_everything = false; 3455 3456 int edges[REDRAW_OBJECTS_LIMIT], nedges = 0; 3457 int faces[REDRAW_OBJECTS_LIMIT], nfaces = 0; 3458 3459 /* Redrawing is somewhat involved. 3460 * 3461 * An update can theoretically affect an arbitrary number of edges 3462 * (consider, for example, completing or breaking a cycle which doesn't 3463 * satisfy all the clues -- we'll switch many edges between error and 3464 * normal states). On the other hand, redrawing the whole grid takes a 3465 * while, making the game feel sluggish, and many updates are actually 3466 * quite well localized. 3467 * 3468 * This redraw algorithm attempts to cope with both situations gracefully 3469 * and correctly. For localized changes, we set a clip rectangle, fill 3470 * it with background, and then redraw (a plausible but conservative 3471 * guess at) the objects which intersect the rectangle; if several 3472 * objects need redrawing, we'll do them individually. However, if lots 3473 * of objects are affected, we'll just redraw everything. 3474 * 3475 * The reason for all of this is that it's just not safe to do the redraw 3476 * piecemeal. If you try to draw an antialiased diagonal line over 3477 * itself, you get a slightly thicker antialiased diagonal line, which 3478 * looks rather ugly after a while. 3479 * 3480 * So, we take two passes over the grid. The first attempts to work out 3481 * what needs doing, and the second actually does it. 3482 */ 3483 3484 if (!ds->started) { 3485 redraw_everything = true; 3486 /* 3487 * But we must still go through the upcoming loops, so that we 3488 * set up stuff in ds correctly for the initial redraw. 3489 */ 3490 } 3491 3492 /* First, trundle through the faces. */ 3493 for (i = 0; i < g->num_faces; i++) { 3494 grid_face *f = g->faces[i]; 3495 int sides = f->order; 3496 int yes_order, no_order; 3497 bool clue_mistake; 3498 bool clue_satisfied; 3499 int n = state->clues[i]; 3500 if (n < 0) 3501 continue; 3502 3503 yes_order = face_order(state, i, LINE_YES); 3504 if (state->exactly_one_loop) { 3505 /* 3506 * Special case: if the set of LINE_YES edges in the grid 3507 * consists of exactly one loop and nothing else, then we 3508 * switch to treating LINE_UNKNOWN the same as LINE_NO for 3509 * purposes of clue checking. 3510 * 3511 * This is because some people like to play Loopy without 3512 * using the right-click, i.e. never setting anything to 3513 * LINE_NO. Without this special case, if a person playing 3514 * in that style fills in what they think is a correct 3515 * solution loop but in fact it has an underfilled clue, 3516 * then we will display no victory flash and also no error 3517 * highlight explaining why not. With this special case, 3518 * we light up underfilled clues at the instant the loop 3519 * is closed. (Of course, *overfilled* clues are fine 3520 * either way.) 3521 * 3522 * (It might still be considered unfortunate that we can't 3523 * warn this style of player any earlier, if they make a 3524 * mistake very near the beginning which doesn't show up 3525 * until they close the last edge of the loop. One other 3526 * thing we _could_ do here is to treat any LINE_UNKNOWN 3527 * as LINE_NO if either of its endpoints has yes-degree 2, 3528 * reflecting the fact that setting that line to YES would 3529 * be an obvious error. But I don't think even that could 3530 * catch _all_ clue errors in a timely manner; I think 3531 * there are some that won't be displayed until the loop 3532 * is filled in, even so, and there's no way to avoid that 3533 * with complete reliability except to switch to being a 3534 * player who sets things to LINE_NO.) 3535 */ 3536 no_order = sides - yes_order; 3537 } else { 3538 no_order = face_order(state, i, LINE_NO); 3539 } 3540 3541 clue_mistake = (yes_order > n || no_order > (sides-n)); 3542 clue_satisfied = (yes_order == n && no_order == (sides-n)); 3543 3544 if (clue_mistake != ds->clue_error[i] || 3545 clue_satisfied != ds->clue_satisfied[i]) { 3546 ds->clue_error[i] = clue_mistake; 3547 ds->clue_satisfied[i] = clue_satisfied; 3548 if (nfaces == REDRAW_OBJECTS_LIMIT) 3549 redraw_everything = true; 3550 else 3551 faces[nfaces++] = i; 3552 } 3553 } 3554 3555 /* Work out what the flash state needs to be. */ 3556 if (flashtime > 0 && 3557 (flashtime <= FLASH_TIME/3 || 3558 flashtime >= FLASH_TIME*2/3)) { 3559 flash_changed = !ds->flashing; 3560 ds->flashing = true; 3561 } else { 3562 flash_changed = ds->flashing; 3563 ds->flashing = false; 3564 } 3565 3566 /* Now, trundle through the edges. */ 3567 for (i = 0; i < g->num_edges; i++) { 3568 char new_ds = 3569 state->line_errors[i] ? DS_LINE_ERROR : state->lines[i]; 3570 if (new_ds != ds->lines[i] || 3571 (flash_changed && state->lines[i] == LINE_YES)) { 3572 ds->lines[i] = new_ds; 3573 if (nedges == REDRAW_OBJECTS_LIMIT) 3574 redraw_everything = true; 3575 else 3576 edges[nedges++] = i; 3577 } 3578 } 3579 3580 /* Pass one is now done. Now we do the actual drawing. */ 3581 if (redraw_everything) { 3582 int grid_width = g->highest_x - g->lowest_x; 3583 int grid_height = g->highest_y - g->lowest_y; 3584 int w = grid_width * ds->tilesize / g->tilesize; 3585 int h = grid_height * ds->tilesize / g->tilesize; 3586 3587 game_redraw_in_rect(dr, ds, ui, state, 3588 0, 0, w + 2*border + 1, h + 2*border + 1); 3589 } else { 3590 3591 /* Right. Now we roll up our sleeves. */ 3592 3593 for (i = 0; i < nfaces; i++) { 3594 grid_face *f = g->faces[faces[i]]; 3595 int x, y, w, h; 3596 3597 face_text_bbox(ds, g, f, &x, &y, &w, &h); 3598 game_redraw_in_rect(dr, ds, ui, state, x, y, w, h); 3599 } 3600 3601 for (i = 0; i < nedges; i++) { 3602 grid_edge *e = g->edges[edges[i]]; 3603 int x, y, w, h; 3604 3605 edge_bbox(ds, g, e, &x, &y, &w, &h); 3606 game_redraw_in_rect(dr, ds, ui, state, x, y, w, h); 3607 } 3608 } 3609 3610 ds->started = true; 3611} 3612 3613static float game_flash_length(const game_state *oldstate, 3614 const game_state *newstate, int dir, game_ui *ui) 3615{ 3616 if (!oldstate->solved && newstate->solved && 3617 !oldstate->cheated && !newstate->cheated) { 3618 return FLASH_TIME; 3619 } 3620 3621 return 0.0F; 3622} 3623 3624static void game_get_cursor_location(const game_ui *ui, 3625 const game_drawstate *ds, 3626 const game_state *state, 3627 const game_params *params, 3628 int *x, int *y, int *w, int *h) 3629{ 3630} 3631 3632static int game_status(const game_state *state) 3633{ 3634 return state->solved ? +1 : 0; 3635} 3636 3637static void game_print_size(const game_params *params, const game_ui *ui, 3638 float *x, float *y) 3639{ 3640 int pw, ph; 3641 3642 /* 3643 * I'll use 7mm "squares" by default. 3644 */ 3645 game_compute_size(params, 700, ui, &pw, &ph); 3646 *x = pw / 100.0F; 3647 *y = ph / 100.0F; 3648} 3649 3650static void game_print(drawing *dr, const game_state *state, const game_ui *ui, 3651 int tilesize) 3652{ 3653 int ink = print_mono_colour(dr, 0); 3654 int i; 3655 game_drawstate ads, *ds = &ads; 3656 grid *g = state->game_grid; 3657 3658 ds->tilesize = tilesize; 3659 ds->textx = snewn(g->num_faces, int); 3660 ds->texty = snewn(g->num_faces, int); 3661 for (i = 0; i < g->num_faces; i++) 3662 ds->textx[i] = ds->texty[i] = -1; 3663 3664 for (i = 0; i < g->num_dots; i++) { 3665 int x, y; 3666 grid_to_screen(ds, g, g->dots[i]->x, g->dots[i]->y, &x, &y); 3667 draw_circle(dr, x, y, ds->tilesize / 15, ink, ink); 3668 } 3669 3670 /* 3671 * Clues. 3672 */ 3673 for (i = 0; i < g->num_faces; i++) { 3674 grid_face *f = g->faces[i]; 3675 int clue = state->clues[i]; 3676 if (clue >= 0) { 3677 char c[20]; 3678 int x, y; 3679 sprintf(c, "%d", state->clues[i]); 3680 face_text_pos(ds, g, f, &x, &y); 3681 draw_text(dr, x, y, 3682 FONT_VARIABLE, ds->tilesize / 2, 3683 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, c); 3684 } 3685 } 3686 3687 /* 3688 * Lines. 3689 */ 3690 for (i = 0; i < g->num_edges; i++) { 3691 int thickness = (state->lines[i] == LINE_YES) ? 30 : 150; 3692 grid_edge *e = g->edges[i]; 3693 int x1, y1, x2, y2; 3694 grid_to_screen(ds, g, e->dot1->x, e->dot1->y, &x1, &y1); 3695 grid_to_screen(ds, g, e->dot2->x, e->dot2->y, &x2, &y2); 3696 if (state->lines[i] == LINE_YES) 3697 { 3698 /* (dx, dy) points from (x1, y1) to (x2, y2). 3699 * The line is then "fattened" in a perpendicular 3700 * direction to create a thin rectangle. */ 3701 double d = sqrt(SQ((double)x1 - x2) + SQ((double)y1 - y2)); 3702 double dx = (x2 - x1) / d; 3703 double dy = (y2 - y1) / d; 3704 int points[8]; 3705 3706 dx = (dx * ds->tilesize) / thickness; 3707 dy = (dy * ds->tilesize) / thickness; 3708 points[0] = x1 + (int)dy; 3709 points[1] = y1 - (int)dx; 3710 points[2] = x1 - (int)dy; 3711 points[3] = y1 + (int)dx; 3712 points[4] = x2 - (int)dy; 3713 points[5] = y2 + (int)dx; 3714 points[6] = x2 + (int)dy; 3715 points[7] = y2 - (int)dx; 3716 draw_polygon(dr, points, 4, ink, ink); 3717 } 3718 else 3719 { 3720 /* Draw a dotted line */ 3721 int divisions = 6; 3722 int j; 3723 for (j = 1; j < divisions; j++) { 3724 /* Weighted average */ 3725 int x = (x1 * (divisions -j) + x2 * j) / divisions; 3726 int y = (y1 * (divisions -j) + y2 * j) / divisions; 3727 draw_circle(dr, x, y, ds->tilesize / thickness, ink, ink); 3728 } 3729 } 3730 } 3731 3732 sfree(ds->textx); 3733 sfree(ds->texty); 3734} 3735 3736#ifdef COMBINED 3737#define thegame loopy 3738#endif 3739 3740const struct game thegame = { 3741 "Loopy", "games.loopy", "loopy", 3742 default_params, 3743 NULL, game_preset_menu, 3744 decode_params, 3745 encode_params, 3746 free_params, 3747 dup_params, 3748 true, game_configure, custom_params, 3749 validate_params, 3750 new_game_desc, 3751 validate_desc, 3752 new_game, 3753 dup_game, 3754 free_game, 3755 true, solve_game, 3756 true, game_can_format_as_text_now, game_text_format, 3757 get_prefs, set_prefs, 3758 new_ui, 3759 free_ui, 3760 NULL, /* encode_ui */ 3761 NULL, /* decode_ui */ 3762 NULL, /* game_request_keys */ 3763 game_changed_state, 3764 NULL, /* current_key_label */ 3765 interpret_move, 3766 execute_move, 3767 PREFERRED_TILE_SIZE, game_compute_size, game_set_size, 3768 game_colours, 3769 game_new_drawstate, 3770 game_free_drawstate, 3771 game_redraw, 3772 game_anim_length, 3773 game_flash_length, 3774 game_get_cursor_location, 3775 game_status, 3776 true, false, game_print_size, game_print, 3777 false /* wants_statusbar */, 3778 false, NULL, /* timing_state */ 3779 0, /* mouse_priorities */ 3780}; 3781 3782#ifdef STANDALONE_SOLVER 3783 3784/* 3785 * Half-hearted standalone solver. It can't output the solution to 3786 * anything but a square puzzle, and it can't log the deductions 3787 * it makes either. But it can solve square puzzles, and more 3788 * importantly it can use its solver to grade the difficulty of 3789 * any puzzle you give it. 3790 */ 3791 3792#include <stdarg.h> 3793 3794int main(int argc, char **argv) 3795{ 3796 game_params *p; 3797 game_state *s; 3798 char *id = NULL, *desc; 3799 const char *err; 3800 bool grade = false; 3801 int ret, diff; 3802#if 0 /* verbose solver not supported here (yet) */ 3803 bool really_verbose = false; 3804#endif 3805 3806 while (--argc > 0) { 3807 char *p = *++argv; 3808#if 0 /* verbose solver not supported here (yet) */ 3809 if (!strcmp(p, "-v")) { 3810 really_verbose = true; 3811 } else 3812#endif 3813 if (!strcmp(p, "-g")) { 3814 grade = true; 3815 } else if (*p == '-') { 3816 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); 3817 return 1; 3818 } else { 3819 id = p; 3820 } 3821 } 3822 3823 if (!id) { 3824 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]); 3825 return 1; 3826 } 3827 3828 desc = strchr(id, ':'); 3829 if (!desc) { 3830 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); 3831 return 1; 3832 } 3833 *desc++ = '\0'; 3834 3835 p = default_params(); 3836 decode_params(p, id); 3837 err = validate_desc(p, desc); 3838 if (err) { 3839 fprintf(stderr, "%s: %s\n", argv[0], err); 3840 return 1; 3841 } 3842 s = new_game(NULL, p, desc); 3843 3844 /* 3845 * When solving an Easy puzzle, we don't want to bother the 3846 * user with Hard-level deductions. For this reason, we grade 3847 * the puzzle internally before doing anything else. 3848 */ 3849 ret = -1; /* placate optimiser */ 3850 for (diff = 0; diff < DIFF_MAX; diff++) { 3851 solver_state *sstate_new; 3852 solver_state *sstate = new_solver_state((game_state *)s, diff); 3853 3854 sstate_new = solve_game_rec(sstate); 3855 3856 if (sstate_new->solver_status == SOLVER_MISTAKE) 3857 ret = 0; 3858 else if (sstate_new->solver_status == SOLVER_SOLVED) 3859 ret = 1; 3860 else 3861 ret = 2; 3862 3863 free_solver_state(sstate_new); 3864 free_solver_state(sstate); 3865 3866 if (ret < 2) 3867 break; 3868 } 3869 3870 if (diff == DIFF_MAX) { 3871 if (grade) 3872 printf("Difficulty rating: harder than Hard, or ambiguous\n"); 3873 else 3874 printf("Unable to find a unique solution\n"); 3875 } else { 3876 if (grade) { 3877 if (ret == 0) 3878 printf("Difficulty rating: impossible (no solution exists)\n"); 3879 else if (ret == 1) 3880 printf("Difficulty rating: %s\n", diffnames[diff]); 3881 } else { 3882 solver_state *sstate_new; 3883 solver_state *sstate = new_solver_state((game_state *)s, diff); 3884 3885 /* If we supported a verbose solver, we'd set verbosity here */ 3886 3887 sstate_new = solve_game_rec(sstate); 3888 3889 if (sstate_new->solver_status == SOLVER_MISTAKE) 3890 printf("Puzzle is inconsistent\n"); 3891 else { 3892 assert(sstate_new->solver_status == SOLVER_SOLVED); 3893 if (s->grid_type == 0) { 3894 fputs(game_text_format(sstate_new->state), stdout); 3895 } else { 3896 printf("Unable to output non-square grids\n"); 3897 } 3898 } 3899 3900 free_solver_state(sstate_new); 3901 free_solver_state(sstate); 3902 } 3903 } 3904 3905 return 0; 3906} 3907 3908#endif 3909 3910/* vim: set shiftwidth=4 tabstop=8: */