X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/ab3a1e43565de2cb5d3404ee326c0f7f8ed11610..a7be78fca974c134729825ac95a946fe49368ec9:/galaxies.c diff --git a/galaxies.c b/galaxies.c index 3fe33dc..5e75981 100644 --- a/galaxies.c +++ b/galaxies.c @@ -53,6 +53,25 @@ int solver_show_working; #define solvep(x) do { if (solver_show_working) { printf x; } } while(0) #endif +#ifdef STANDALONE_PICTURE_GENERATOR +/* + * Dirty hack to enable the generator to construct a game ID which + * solves to a specified black-and-white bitmap. We define a global + * variable here which gives the desired colour of each square, and + * we arrange that the grid generator never merges squares of + * different colours. + * + * The bitmap as stored here is a simple int array (at these sizes + * it isn't worth doing fiddly bit-packing). picture[y*w+x] is 1 + * iff the pixel at (x,y) is intended to be black. + * + * (It might be nice to be able to specify some pixels as + * don't-care, to give the generator more leeway. But that might be + * fiddly.) + */ +static int *picture; +#endif + enum { COL_BACKGROUND, COL_WHITEBG, @@ -66,9 +85,8 @@ enum { }; #define DIFFLIST(A) \ - A(EASY,Easy,e) \ - A(HARD,Hard,h) \ - A(RECURSIVE,Recursive,r) + A(NORMAL,Normal,n) \ + A(UNREASONABLE,Unreasonable,u) #define ENUM(upper,title,lower) DIFF_ ## upper, #define TITLE(upper,title,lower) #title, @@ -135,16 +153,13 @@ struct game_state { /* make up some sensible default sizes */ -#define DEFAULT_PRESET 1 +#define DEFAULT_PRESET 0 static const game_params galaxies_presets[] = { - { 7, 7, DIFF_EASY }, - { 7, 7, DIFF_HARD }, - { 7, 7, DIFF_RECURSIVE }, - { 10, 10, DIFF_EASY }, - { 10, 10, DIFF_HARD }, - { 15, 15, DIFF_EASY }, - { 15, 15, DIFF_HARD }, + { 7, 7, DIFF_NORMAL }, + { 7, 7, DIFF_UNREASONABLE }, + { 10, 10, DIFF_NORMAL }, + { 15, 15, DIFF_NORMAL }, }; static int game_fetch_preset(int i, char **name, game_params **params) @@ -188,7 +203,7 @@ static game_params *dup_params(game_params *params) static void decode_params(game_params *params, char const *string) { params->h = params->w = atoi(string); - params->diff = DIFF_EASY; + params->diff = DIFF_NORMAL; while (*string && isdigit((unsigned char)*string)) string++; if (*string == 'x') { string++; @@ -198,7 +213,7 @@ static void decode_params(game_params *params, char const *string) if (*string == 'd') { int i; string++; - for (i = 0; i <= DIFF_RECURSIVE; i++) + for (i = 0; i <= DIFF_UNREASONABLE; i++) if (*string == galaxies_diffchars[i]) params->diff = i; if (*string) string++; @@ -266,7 +281,7 @@ static char *validate_params(game_params *params, int full) * and custom_params will never generate anything that isn't * within range. */ - assert(params->diff <= DIFF_RECURSIVE); + assert(params->diff <= DIFF_UNREASONABLE); return NULL; } @@ -298,12 +313,17 @@ static void remove_assoc(game_state *state, space *tile) { static void add_assoc(game_state *state, space *tile, space *dot) { remove_assoc(state, tile); +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) + assert(!picture[(tile->y/2) * state->w + (tile->x/2)] == + !(dot->flags & F_DOT_BLACK)); +#endif tile->flags |= F_TILE_ASSOC; tile->dotx = dot->x; tile->doty = dot->y; dot->nassoc++; - debug(("add_assoc sp %d %d --> dot %d,%d, new nassoc %d.\n", - tile->x, tile->y, dot->x, dot->y, dot->nassoc)); + /*debug(("add_assoc sp %d %d --> dot %d,%d, new nassoc %d.\n", + tile->x, tile->y, dot->x, dot->y, dot->nassoc));*/ } static struct space *sp2dot(game_state *state, int x, int y) @@ -329,9 +349,11 @@ static char *game_text_format(game_state *state) sp = &SPACE(state, x, y); if (sp->flags & F_DOT) *p++ = 'o'; +#if 0 else if (sp->flags & (F_REACHABLE|F_MULTIPLE|F_MARK)) *p++ = (sp->flags & F_MULTIPLE) ? 'M' : (sp->flags & F_REACHABLE) ? 'R' : 'X'; +#endif else { switch (sp->type) { case s_tile: @@ -587,85 +609,8 @@ static void tiles_from_edge(struct game_state *state, ts[1] = INGRID(state, xs[1], ys[1]) ? &SPACE(state, xs[1], ys[1]) : NULL; } - /* Check all tiles are associated with something, and all shapes - * are the correct symmetry (i.e. all tiles have a matching tile - * the opposite direction from the dot) */ -static int cccb_assoc(game_state *state, space *tile, void *unused) -{ - assert(tile->type == s_tile); - - if (!(tile->flags & F_TILE_ASSOC)) return -1; - return 0; -} - -struct dgs_ctx { - space *dot; - int ndots; -}; - -static int dgs_cb_check(game_state *state, space *tile, void *vctx) -{ - struct dgs_ctx *ctx = (struct dgs_ctx *)vctx; - space *opp; - - if (!(tile->flags & F_TILE_ASSOC)) return 0; - if (tile->dotx != ctx->dot->x || - tile->doty != ctx->dot->y) return 0; - - ctx->ndots += 1; - - /* Check this tile has an opposite associated with same dot. */ - opp = tile_opposite(state, tile); - if (!opp || !(opp->flags & F_TILE_ASSOC)) return -1; - if (opp->dotx != tile->dotx || opp->doty != tile->doty) return -1; - - /* Check its edges are correct */ - if (outline_tile_fordot(state, tile, 0) == 1) - return -1; /* there was some fixing required, we're wrong. */ - - return 0; -} - -static int dot_good_shape(game_state *state, space *dot, int mark) -{ - struct dgs_ctx ctx; - - ctx.dot = dot; - ctx.ndots = 0; - - if (mark) dot->flags &= ~F_GOOD; - - if (foreach_tile(state, dgs_cb_check, 0, &ctx) == -1) - return 0; - if (ctx.ndots == 0) return 0; /* no dots assoc. with tile. */ - - if (mark) { - debug(("marking dot %d,%d good tile.\n", dot->x, dot->y)); - dot->flags |= F_GOOD; - } - return 1; -} - -static int check_complete(game_state *state, int mark_errors) -{ - int i, complete = 1; - - /* Are all tiles associated? */ - if (foreach_tile(state, cccb_assoc, 0, NULL) == -1) - complete = 0; - - /* Check all dots are associated, and their tiles are well-formed. */ - for (i = 0; i < state->ndots; i++) { - if (!dot_good_shape(state, state->dots[i], mark_errors)) - complete = 0; - } - - /*if (complete == 1) printf("Complete!\n");*/ - return complete; -} - -/* Returns a move string for use by 'solve'; if you don't want the - * initial 'S;' use ret[2]. */ +/* Returns a move string for use by 'solve', including the initial + * 'S' if issolve is true. */ static char *diff_game(game_state *src, game_state *dest, int issolve) { int movelen = 0, movesize = 256, x, y, len; @@ -732,6 +677,9 @@ static int dot_is_possible(game_state *state, space *sp, int allow_assoc) { int bx = 0, by = 0, dx, dy; space *adj; +#ifdef STANDALONE_PICTURE_GENERATOR + int col = -1; +#endif switch (sp->type) { case s_tile: @@ -753,8 +701,24 @@ static int dot_is_possible(game_state *state, space *sp, int allow_assoc) adj = &SPACE(state, sp->x+dx, sp->y+dy); - if (!allow_assoc && (adj->flags & F_TILE_ASSOC)) - return 0; +#ifdef STANDALONE_PICTURE_GENERATOR + /* + * Check that all the squares we're looking at have the + * same colour. + */ + if (picture) { + if (adj->type == s_tile) { + int c = picture[(adj->y / 2) * state->w + (adj->x / 2)]; + if (col < 0) + col = c; + if (c != col) + return 0; /* colour mismatch */ + } + } +#endif + + if (!allow_assoc && (adj->flags & F_TILE_ASSOC)) + return 0; if (dx != 0 || dy != 0) { /* Other than our own square, no dots nearby. */ @@ -952,6 +916,19 @@ static int movedot_cb(game_state *state, space *tile, void *vctx) newopp->doty != md->olddot->y) return -1; /* associated, but wrong dot. */ } +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * Reject if either tile and the dot don't match in colour. + */ + if (!(picture[(tile->y/2) * state->w + (tile->x/2)]) ^ + !(md->newdot->flags & F_DOT_BLACK)) + return -1; + if (!(picture[(newopp->y/2) * state->w + (newopp->x/2)]) ^ + !(md->newdot->flags & F_DOT_BLACK)) + return -1; + } +#endif break; case MD_MOVE: @@ -987,12 +964,36 @@ static int dot_expand_or_move(game_state *state, space *dot, toadd[i]->x, toadd[i]->y)); assert(dot->flags & F_DOT); +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * Reject the expansion totally if any of the new tiles are + * the wrong colour. + */ + for (i = 0; i < nadd; i++) { + if (!(picture[(toadd[i]->y/2) * state->w + (toadd[i]->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + return 0; + } + } +#endif + /* First off, could we just expand the current dot's tile to cover * the space(s) passed in and their opposites? */ for (i = 0; i < nadd; i++) { tileopp = space_opposite_dot(state, toadd[i], dot); if (!tileopp) goto noexpand; if (tileopp->flags & F_TILE_ASSOC) goto noexpand; +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * The opposite tiles have to be the right colour as well. + */ + if (!(picture[(tileopp->y/2) * state->w + (tileopp->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + goto noexpand; + } +#endif } /* OK, all spaces have valid empty opposites: associate spaces and * opposites with our dot. */ @@ -1010,7 +1011,7 @@ static int dot_expand_or_move(game_state *state, space *dot, noexpand: /* Otherwise, try to move dot so as to encompass given spaces: */ - /* first, alculate the 'centre of gravity' of the new dot. */ + /* first, calculate the 'centre of gravity' of the new dot. */ nnew = dot->nassoc + nadd; /* number of tiles assoc. with new dot. */ cx = dot->x * dot->nassoc; cy = dot->y * dot->nassoc; @@ -1050,6 +1051,13 @@ noexpand: dot->x, dot->y)); return 0; } +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + if (!(picture[(tileopp->y/2) * state->w + (tileopp->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + return 0; + } +#endif } /* If we've got here, we're ok. First, associate all of 'toadd' @@ -1064,8 +1072,16 @@ noexpand: /* Finally, move the dot and fix up all the old associations. */ debug(("Moving dot at %d,%d to %d,%d\n", dot->x, dot->y, md.newdot->x, md.newdot->y)); - remove_dot(dot); - add_dot(md.newdot); + { +#ifdef STANDALONE_PICTURE_GENERATOR + int f = dot->flags & F_DOT_BLACK; +#endif + remove_dot(dot); + add_dot(md.newdot); +#ifdef STANDALONE_PICTURE_GENERATOR + md.newdot->flags |= f; +#endif + } md.op = MD_MOVE; ret = foreach_tile(state, movedot_cb, 0, &md); @@ -1192,6 +1208,12 @@ static void generate_pass(game_state *state, random_state *rs, int *scratch, * if we can, and add one if so. */ if (dot_is_possible(state, sp, 0)) { add_dot(sp); +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + if (picture[(sp->y/2) * state->w + (sp->x/2)]) + sp->flags |= F_DOT_BLACK; + } +#endif ret = solver_obvious_dot(state, sp); assert(ret != -1); debug(("Added dot (and obvious associations) at %d,%d\n", @@ -1202,6 +1224,7 @@ static void generate_pass(game_state *state, random_state *rs, int *scratch, dbg_state(state); } +static int check_complete(game_state *state, int *dsf, int *colours); static int solver_state(game_state *state, int maxdiff); static char *new_game_desc(game_params *params, random_state *rs, @@ -1210,7 +1233,7 @@ static char *new_game_desc(game_params *params, random_state *rs, game_state *state = blank_game(params->w, params->h), *copy; char *desc; int *scratch, sz = state->sx*state->sy, i; - int diff, ntries = 0; + int diff, ntries = 0, cc; /* Random list of squares to try and process, one-by-one. */ scratch = snewn(sz, int); @@ -1220,8 +1243,8 @@ generate: clear_game(state, 1); ntries++; - //generate_pass(state, rs, scratch, 10, GP_DOTS); - //generate_pass(state, rs, scratch, 100, 0); + /* generate_pass(state, rs, scratch, 10, GP_DOTS); */ + /* generate_pass(state, rs, scratch, 100, 0); */ generate_pass(state, rs, scratch, 100, GP_DOTS); game_update_dots(state); @@ -1234,6 +1257,12 @@ generate: } #endif + for (i = 0; i < state->sx*state->sy; i++) + if (state->grid[i].type == s_tile) + outline_tile_fordot(state, &state->grid[i], TRUE); + cc = check_complete(state, NULL, NULL); + assert(cc); + copy = dup_game(state); clear_game(copy, 0); dbg_state(copy); @@ -1242,8 +1271,181 @@ generate: assert(diff != DIFF_IMPOSSIBLE); if (diff != params->diff) { - if (ntries < MAXTRIES) goto generate; + /* + * We'll grudgingly accept a too-easy puzzle, but we must + * _not_ permit a too-hard one (one which the solver + * couldn't handle at all). + */ + if (diff > params->diff || + ntries < MAXTRIES) goto generate; + } + +#ifdef STANDALONE_PICTURE_GENERATOR + /* + * Postprocessing pass to prevent excessive numbers of adjacent + * singletons. Iterate over all edges in random shuffled order; + * for each edge that separates two regions, investigate + * whether removing that edge and merging the regions would + * still yield a valid and soluble puzzle. (The two regions + * must also be the same colour, of course.) If so, do it. + * + * This postprocessing pass is slow (due to repeated solver + * invocations), and seems to be unnecessary during normal + * unconstrained game generation. However, when generating a + * game under colour constraints, excessive singletons seem to + * turn up more often, so it's worth doing this. + */ + { + int *posns, nposns; + int i, j, newdiff; + game_state *copy2; + + nposns = params->w * (params->h+1) + params->h * (params->w+1); + posns = snewn(nposns, int); + for (i = j = 0; i < state->sx*state->sy; i++) + if (state->grid[i].type == s_edge) + posns[j++] = i; + assert(j == nposns); + + shuffle(posns, nposns, sizeof(*posns), rs); + + for (i = 0; i < nposns; i++) { + int x, y, x0, y0, x1, y1, cx, cy, cn, cx0, cy0, cx1, cy1, tx, ty; + space *s0, *s1, *ts, *d0, *d1, *dn; + int ok; + + /* Coordinates of edge space */ + x = posns[i] % state->sx; + y = posns[i] / state->sx; + + /* Coordinates of square spaces on either side of edge */ + x0 = ((x+1) & ~1) - 1; /* round down to next odd number */ + y0 = ((y+1) & ~1) - 1; + x1 = 2*x-x0; /* and reflect about x to get x1 */ + y1 = 2*y-y0; + + if (!INGRID(state, x0, y0) || !INGRID(state, x1, y1)) + continue; /* outermost edge of grid */ + s0 = &SPACE(state, x0, y0); + s1 = &SPACE(state, x1, y1); + assert(s0->type == s_tile && s1->type == s_tile); + + if (s0->dotx == s1->dotx && s0->doty == s1->doty) + continue; /* tiles _already_ owned by same dot */ + + d0 = &SPACE(state, s0->dotx, s0->doty); + d1 = &SPACE(state, s1->dotx, s1->doty); + + if ((d0->flags ^ d1->flags) & F_DOT_BLACK) + continue; /* different colours: cannot merge */ + + /* + * Work out where the centre of gravity of the new + * region would be. + */ + cx = d0->nassoc * d0->x + d1->nassoc * d1->x; + cy = d0->nassoc * d0->y + d1->nassoc * d1->y; + cn = d0->nassoc + d1->nassoc; + if (cx % cn || cy % cn) + continue; /* CoG not at integer coordinates */ + cx /= cn; + cy /= cn; + assert(INUI(state, cx, cy)); + + /* + * Ensure that the CoG would actually be _in_ the new + * region, by verifying that all its surrounding tiles + * belong to one or other of our two dots. + */ + cx0 = ((cx+1) & ~1) - 1; /* round down to next odd number */ + cy0 = ((cy+1) & ~1) - 1; + cx1 = 2*cx-cx0; /* and reflect about cx to get cx1 */ + cy1 = 2*cy-cy0; + ok = TRUE; + for (ty = cy0; ty <= cy1; ty += 2) + for (tx = cx0; tx <= cx1; tx += 2) { + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + ok = FALSE; + } + if (!ok) + continue; + + /* + * Verify that for every tile in either source region, + * that tile's image in the new CoG is also in one of + * the two source regions. + */ + for (ty = 1; ty < state->sy; ty += 2) { + for (tx = 1; tx < state->sx; tx += 2) { + int tx1, ty1; + + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + continue; /* not part of these tiles anyway */ + tx1 = 2*cx-tx; + ty1 = 2*cy-ty; + if (!INGRID(state, tx1, ty1)) { + ok = FALSE; + break; + } + ts = &SPACE(state, cx+cx-tx, cy+cy-ty); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) { + ok = FALSE; + break; + } + } + if (!ok) + break; + } + if (!ok) + continue; + + /* + * Now we're clear to attempt the merge. We take a copy + * of the game state first, so we can revert it easily + * if the resulting puzzle turns out to have become + * insoluble. + */ + copy2 = dup_game(state); + + remove_dot(d0); + remove_dot(d1); + dn = &SPACE(state, cx, cy); + add_dot(dn); + dn->flags |= (d0->flags & F_DOT_BLACK); + for (ty = 1; ty < state->sy; ty += 2) { + for (tx = 1; tx < state->sx; tx += 2) { + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + continue; /* not part of these tiles anyway */ + add_assoc(state, ts, dn); + } + } + + copy = dup_game(state); + clear_game(copy, 0); + dbg_state(copy); + newdiff = solver_state(copy, params->diff); + free_game(copy); + if (diff == newdiff) { + /* Still just as soluble. Let the merge stand. */ + free_game(copy2); + } else { + /* Became insoluble. Revert. */ + free_game(state); + state = copy2; + } + } } +#endif desc = encode_game(state); #ifndef STANDALONE_SOLVER @@ -1915,7 +2117,7 @@ static int solver_recurse(game_state *state, int maxdiff) else { /* precisely one solution */ if (diff == DIFF_IMPOSSIBLE) - diff = DIFF_RECURSIVE; + diff = DIFF_UNREASONABLE; else diff = DIFF_AMBIGUOUS; } @@ -1941,7 +2143,14 @@ static int solver_recurse(game_state *state, int maxdiff) static int solver_state(game_state *state, int maxdiff) { solver_ctx *sctx = new_solver(state); - int ret, diff = DIFF_EASY; + int ret, diff = DIFF_NORMAL; + +#ifdef STANDALONE_PICTURE_GENERATOR + /* hack, hack: set picture to NULL during solving so that add_assoc + * won't complain when we attempt recursive guessing and guess wrong */ + int *savepic = picture; + picture = NULL; +#endif ret = solver_obvious(state); if (ret < 0) { @@ -1958,21 +2167,16 @@ static int solver_state(game_state *state, int maxdiff) cont: ret = foreach_edge(state, solver_lines_opposite_cb, IMPOSSIBLE_QUITS, sctx); - CHECKRET(DIFF_EASY); + CHECKRET(DIFF_NORMAL); ret = foreach_tile(state, solver_spaces_oneposs_cb, IMPOSSIBLE_QUITS, sctx); - CHECKRET(DIFF_EASY); - - /* more easy stuff? */ - - if (maxdiff <= DIFF_EASY) - break; + CHECKRET(DIFF_NORMAL); ret = solver_expand_dots(state, sctx); - CHECKRET(DIFF_HARD); + CHECKRET(DIFF_NORMAL); - if (maxdiff <= DIFF_HARD) + if (maxdiff <= DIFF_NORMAL) break; /* harder still? */ @@ -1981,18 +2185,22 @@ cont: break; } - if (check_complete(state, 0)) goto got_result; + if (check_complete(state, NULL, NULL)) goto got_result; - diff = (maxdiff >= DIFF_RECURSIVE) ? + diff = (maxdiff >= DIFF_UNREASONABLE) ? solver_recurse(state, maxdiff) : DIFF_UNFINISHED; got_result: free_solver(sctx); #ifndef STANDALONE_SOLVER - debug(("solver_state ends:\n")); + debug(("solver_state ends, diff %s:\n", galaxies_diffnames[diff])); dbg_state(state); #endif +#ifdef STANDALONE_PICTURE_GENERATOR + picture = savepic; +#endif + return diff; } @@ -2006,7 +2214,7 @@ static char *solve_game(game_state *state, game_state *currstate, int diff; tosolve = dup_game(currstate); - diff = solver_state(tosolve, DIFF_RECURSIVE); + diff = solver_state(tosolve, DIFF_UNREASONABLE); if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) { debug(("solve_game solved with current state.\n")); goto solved; @@ -2014,7 +2222,7 @@ static char *solve_game(game_state *state, game_state *currstate, free_game(tosolve); tosolve = dup_game(state); - diff = solver_state(tosolve, DIFF_RECURSIVE); + diff = solver_state(tosolve, DIFF_UNREASONABLE); if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) { debug(("solve_game solved with original state.\n")); goto solved; @@ -2078,7 +2286,7 @@ static void game_changed_state(game_ui *ui, game_state *oldstate, #define PREFERRED_TILE_SIZE 32 #define TILE_SIZE (ds->tilesize) #define DOT_SIZE (TILE_SIZE / 4) -#define EDGE_THICKNESS (TILE_SIZE / 16) +#define EDGE_THICKNESS (max(TILE_SIZE / 16, 2)) #define BORDER TILE_SIZE #define COORD(x) ( (x) * TILE_SIZE + BORDER ) @@ -2163,7 +2371,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if (button == 'S' || button == 's') { char *ret; game_state *tmp = dup_game(state); - state->cdiff = solver_state(tmp, DIFF_RECURSIVE-1); + state->cdiff = solver_state(tmp, DIFF_UNREASONABLE-1); ret = diff_game(state, tmp, 0); free_game(tmp); return ret; @@ -2200,14 +2408,10 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, int px, py; struct space *sp, *dot; - if (button == 'H' || button == 'h' || - button == 'S' || button == 's') { + if (button == 'H' || button == 'h') { char *ret; game_state *tmp = dup_game(state); - if (button == 'H' || button == 'h') - solver_obvious(tmp); - else - solver_state(tmp, DIFF_RECURSIVE-1); + solver_obvious(tmp); ret = diff_game(state, tmp, 0); free_game(tmp); return ret; @@ -2228,8 +2432,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, } else if (button == RIGHT_BUTTON) { int px1, py1; - px = 2*FROMCOORD((float)x) + 0.5; - py = 2*FROMCOORD((float)y) + 0.5; + px = (int)(2*FROMCOORD((float)x) + 0.5); + py = (int)(2*FROMCOORD((float)y) + 0.5); dot = NULL; @@ -2240,7 +2444,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, for (py1 = py-1; py1 <= py+1; py1++) for (px1 = px-1; px1 <= px+1; px1++) { if (px1 >= 0 && px1 < state->sx && - py1 >= 0 && py1 < state->sx && + py1 >= 0 && py1 < state->sy && x >= SCOORD(px1-1) && x < SCOORD(px1+1) && y >= SCOORD(py1-1) && y < SCOORD(py1+1) && SPACE(state, px1, py1).flags & F_DOT) { @@ -2248,8 +2452,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, * Found a dot. Begin a drag from it. */ dot = &SPACE(state, px1, py1); - ui->srcx = px; - ui->srcy = py; + ui->srcx = px1; + ui->srcy = py1; goto done; /* multi-level break */ } } @@ -2261,7 +2465,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if (!dot) { px = 2*FROMCOORD(x+TILE_SIZE) - 1; py = 2*FROMCOORD(y+TILE_SIZE) - 1; - if (px >= 0 && px < state->sx && py >= 0 && py < state->sx) { + if (px >= 0 && px < state->sx && py >= 0 && py < state->sy) { sp = &SPACE(state, px, py); if (sp->flags & F_TILE_ASSOC) { dot = &SPACE(state, sp->dotx, sp->doty); @@ -2339,7 +2543,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, } #endif -static int check_complete_in_play(game_state *state, int *dsf, int *colours) +static int check_complete(game_state *state, int *dsf, int *colours) { int w = state->w, h = state->h; int x, y, i, ret; @@ -2418,10 +2622,16 @@ static int check_complete_in_play(game_state *state, int *dsf, int *colours) */ for (i = 0; i < w*h; i++) if (sqdata[i].valid) { - sqdata[i].cx = sqdata[i].minx + sqdata[i].maxx + 1; - sqdata[i].cy = sqdata[i].miny + sqdata[i].maxy + 1; + int cx, cy; + cx = sqdata[i].cx = sqdata[i].minx + sqdata[i].maxx + 1; + cy = sqdata[i].cy = sqdata[i].miny + sqdata[i].maxy + 1; if (!(SPACE(state, sqdata[i].cx, sqdata[i].cy).flags & F_DOT)) sqdata[i].valid = FALSE; /* no dot at centre of symmetry */ + if (dsf_canonify(dsf, (cy-1)/2*w+(cx-1)/2) != i || + dsf_canonify(dsf, (cy)/2*w+(cx-1)/2) != i || + dsf_canonify(dsf, (cy-1)/2*w+(cx)/2) != i || + dsf_canonify(dsf, (cy)/2*w+(cx)/2) != i) + sqdata[i].valid = FALSE; /* dot at cx,cy isn't ours */ if (SPACE(state, sqdata[i].cx, sqdata[i].cy).flags & F_DOT_BLACK) sqdata[i].colour = 2; else @@ -2597,6 +2807,7 @@ static game_state *execute_move(game_state *state, char *move) #endif } else if (c == 'S') { move++; + ret->used_solve = 1; } else goto badmove; @@ -2605,7 +2816,7 @@ static game_state *execute_move(game_state *state, char *move) else if (*move) goto badmove; } - if (check_complete_in_play(ret, NULL, NULL)) + if (check_complete(ret, NULL, NULL)) ret->completed = 1; return ret; @@ -2761,13 +2972,13 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds) static void draw_arrow(drawing *dr, game_drawstate *ds, int cx, int cy, int ddx, int ddy) { - float vlen = sqrt(ddx*ddx+ddy*ddy); + float vlen = (float)sqrt(ddx*ddx+ddy*ddy); float xdx = ddx/vlen, xdy = ddy/vlen; float ydx = -xdy, ydy = xdx; - int e1x = cx + xdx*TILE_SIZE/3, e1y = cy + xdy*TILE_SIZE/3; - int e2x = cx - xdx*TILE_SIZE/3, e2y = cy - xdy*TILE_SIZE/3; - int adx = (ydx-xdx)*TILE_SIZE/8, ady = (ydy-xdy)*TILE_SIZE/8; - int adx2 = (-ydx-xdx)*TILE_SIZE/8, ady2 = (-ydy-xdy)*TILE_SIZE/8; + int e1x = cx + (int)(xdx*TILE_SIZE/3), e1y = cy + (int)(xdy*TILE_SIZE/3); + int e2x = cx - (int)(xdx*TILE_SIZE/3), e2y = cy - (int)(xdy*TILE_SIZE/3); + int adx = (int)((ydx-xdx)*TILE_SIZE/8), ady = (int)((ydy-xdy)*TILE_SIZE/8); + int adx2 = (int)((-ydx-xdx)*TILE_SIZE/8), ady2 = (int)((-ydy-xdy)*TILE_SIZE/8); draw_line(dr, e1x, e1y, e2x, e2y, COL_ARROW); draw_line(dr, e1x, e1y, e1x+adx, e1y+ady, COL_ARROW); @@ -2876,7 +3087,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, ds->started = TRUE; } - check_complete_in_play(state, NULL, ds->colour_scratch); + check_complete(state, NULL, ds->colour_scratch); for (y = 0; y < h; y++) for (x = 0; x < w; x++) { @@ -3050,7 +3261,7 @@ static void game_print(drawing *dr, game_state *state, int sz) */ dsf = snewn(w * h, int); colours = snewn(w * h, int); - check_complete_in_play(state, dsf, colours); + check_complete(state, dsf, colours); /* * Draw the grid. @@ -3179,7 +3390,7 @@ static void game_print(drawing *dr, game_state *state, int sz) for (y = 0; y <= 2*h; y++) for (x = 0; x <= 2*w; x++) if (SPACE(state, x, y).flags & F_DOT) { - draw_circle(dr, COORD(x/2.0), COORD(y/2.0), DOT_SIZE, + draw_circle(dr, (int)COORD(x/2.0), (int)COORD(y/2.0), DOT_SIZE, (SPACE(state, x, y).flags & F_DOT_BLACK ? black : white), black); } @@ -3233,11 +3444,11 @@ const struct game thegame = { FALSE, FALSE, NULL, NULL, TRUE, /* wants_statusbar */ #else - TRUE, TRUE, game_print_size, game_print, + TRUE, FALSE, game_print_size, game_print, FALSE, /* wants_statusbar */ #endif FALSE, game_timing_state, - 0, /* flags */ + REQUIRE_RBUTTON, /* flags */ }; #ifdef STANDALONE_SOLVER @@ -3277,7 +3488,7 @@ static int gen(game_params *p, random_state *rs, int debug) state = new_game(NULL, p, desc); dump_state(state); - diff = solver_state(state, DIFF_RECURSIVE); + diff = solver_state(state, DIFF_UNREASONABLE); printf("Generated %s game %dx%d:%s\n", galaxies_diffnames[diff], p->w, p->h, desc); dump_state(state); @@ -3378,7 +3589,7 @@ int main(int argc, char **argv) while (1) { p->w = random_upto(rs, 15) + 3; p->h = random_upto(rs, 15) + 3; - p->diff = random_upto(rs, DIFF_RECURSIVE); + p->diff = random_upto(rs, DIFF_UNREASONABLE); diff = gen(p, rs, 0); } return 0; @@ -3400,7 +3611,7 @@ int main(int argc, char **argv) exit(1); } s = new_game(NULL, p, desc); - diff = solver_state(s, DIFF_RECURSIVE); + diff = solver_state(s, DIFF_UNREASONABLE); dump_state(s); printf("Puzzle is %s.\n", galaxies_diffnames[diff]); free_game(s); @@ -3413,4 +3624,146 @@ int main(int argc, char **argv) #endif +#ifdef STANDALONE_PICTURE_GENERATOR + +/* + * Main program for the standalone picture generator. To use it, + * simply provide it with an XBM-format bitmap file (note XBM, not + * XPM) on standard input, and it will output a game ID in return. + * For example: + * + * $ ./galaxiespicture < badly-drawn-cat.xbm + * 11x11:eloMBLzFeEzLNMWifhaWYdDbixCymBbBMLoDdewGg + * + * If you want a puzzle with a non-standard difficulty level, pass + * a partial parameters string as a command-line argument (e.g. + * `./galaxiespicture du < foo.xbm', where `du' is the same suffix + * which if it appeared in a random-seed game ID would set the + * difficulty level to Unreasonable). However, be aware that if the + * generator fails to produce an adequately difficult puzzle too + * many times then it will give up and return an easier one (just + * as it does during normal GUI play). To be sure you really have + * the difficulty you asked for, use galaxiessolver to + * double-check. + * + * (Perhaps I ought to include an option to make this standalone + * generator carry on looping until it really does get the right + * difficulty. Hmmm.) + */ + +#include + +int main(int argc, char **argv) +{ + game_params *par; + char *params, *desc; + random_state *rs; + time_t seed = time(NULL); + char buf[4096]; + int i; + int x, y; + + par = default_params(); + if (argc > 1) + decode_params(par, argv[1]); /* get difficulty */ + par->w = par->h = -1; + + /* + * Now read an XBM file from standard input. This is simple and + * hacky and will do very little error detection, so don't feed + * it bogus data. + */ + picture = NULL; + x = y = 0; + while (fgets(buf, sizeof(buf), stdin)) { + buf[strcspn(buf, "\r\n")] = '\0'; + if (!strncmp(buf, "#define", 7)) { + /* + * Lines starting `#define' give the width and height. + */ + char *num = buf + strlen(buf); + char *symend; + + while (num > buf && isdigit((unsigned char)num[-1])) + num--; + symend = num; + while (symend > buf && isspace((unsigned char)symend[-1])) + symend--; + + if (symend-5 >= buf && !strncmp(symend-5, "width", 5)) + par->w = atoi(num); + else if (symend-6 >= buf && !strncmp(symend-6, "height", 6)) + par->h = atoi(num); + } else { + /* + * Otherwise, break the string up into words and take + * any word of the form `0x' plus hex digits to be a + * byte. + */ + char *p, *wordstart; + + if (!picture) { + if (par->w < 0 || par->h < 0) { + printf("failed to read width and height\n"); + return 1; + } + picture = snewn(par->w * par->h, int); + for (i = 0; i < par->w * par->h; i++) + picture[i] = -1; + } + + p = buf; + while (*p) { + while (*p && (*p == ',' || isspace((unsigned char)*p))) + p++; + wordstart = p; + while (*p && !(*p == ',' || *p == '}' || + isspace((unsigned char)*p))) + p++; + if (*p) + *p++ = '\0'; + + if (wordstart[0] == '0' && + (wordstart[1] == 'x' || wordstart[1] == 'X') && + !wordstart[2 + strspn(wordstart+2, + "0123456789abcdefABCDEF")]) { + unsigned long byte = strtoul(wordstart+2, NULL, 16); + for (i = 0; i < 8; i++) { + int bit = (byte >> i) & 1; + if (y < par->h && x < par->w) + picture[y * par->w + x] = bit; + x++; + } + + if (x >= par->w) { + x = 0; + y++; + } + } + } + } + } + + for (i = 0; i < par->w * par->h; i++) + if (picture[i] < 0) { + fprintf(stderr, "failed to read enough bitmap data\n"); + return 1; + } + + rs = random_new((void*)&seed, sizeof(time_t)); + + desc = new_game_desc(par, rs, NULL, FALSE); + params = encode_params(par, FALSE); + printf("%s:%s\n", params, desc); + + sfree(desc); + sfree(params); + free_params(par); + random_free(rs); + + return 0; +} + +#endif + /* vim: set shiftwidth=4 tabstop=8: */