/*
- * keen.c: an implementation of the Times's 'KenKen' puzzle.
+ * keen.c: an implementation of the Times's 'KenKen' puzzle, and
+ * also of Nikoli's very similar 'Inshi No Heya' puzzle.
*/
#include <stdio.h>
};
struct game_params {
- int w, diff;
+ int w, diff, multiplication_only;
};
struct clues {
ret->w = 6;
ret->diff = DIFF_NORMAL;
+ ret->multiplication_only = FALSE;
return ret;
}
const static struct game_params keen_presets[] = {
- { 4, DIFF_EASY },
- { 5, DIFF_EASY },
- { 6, DIFF_EASY },
- { 6, DIFF_NORMAL },
- { 6, DIFF_HARD },
- { 6, DIFF_EXTREME },
- { 6, DIFF_UNREASONABLE },
- { 9, DIFF_NORMAL },
+ { 4, DIFF_EASY, FALSE },
+ { 5, DIFF_EASY, FALSE },
+ { 5, DIFF_EASY, TRUE },
+ { 6, DIFF_EASY, FALSE },
+ { 6, DIFF_NORMAL, FALSE },
+ { 6, DIFF_NORMAL, TRUE },
+ { 6, DIFF_HARD, FALSE },
+ { 6, DIFF_EXTREME, FALSE },
+ { 6, DIFF_UNREASONABLE, FALSE },
+ { 9, DIFF_NORMAL, FALSE },
};
static int game_fetch_preset(int i, char **name, game_params **params)
ret = snew(game_params);
*ret = keen_presets[i]; /* structure copy */
- sprintf(buf, "%dx%d %s", ret->w, ret->w, keen_diffnames[ret->diff]);
+ sprintf(buf, "%dx%d %s%s", ret->w, ret->w, keen_diffnames[ret->diff],
+ ret->multiplication_only ? ", multiplication only" : "");
*name = dupstr(buf);
*params = ret;
p++;
}
}
+
+ if (*p == 'm') {
+ p++;
+ params->multiplication_only = TRUE;
+ }
}
static char *encode_params(const game_params *params, int full)
sprintf(ret, "%d", params->w);
if (full)
- sprintf(ret + strlen(ret), "d%c", keen_diffchars[params->diff]);
+ sprintf(ret + strlen(ret), "d%c%s", keen_diffchars[params->diff],
+ params->multiplication_only ? "m" : "");
return dupstr(ret);
}
config_item *ret;
char buf[80];
- ret = snewn(3, config_item);
+ ret = snewn(4, config_item);
ret[0].name = "Grid size";
ret[0].type = C_STRING;
ret[1].sval = DIFFCONFIG;
ret[1].ival = params->diff;
- ret[2].name = NULL;
- ret[2].type = C_END;
+ ret[2].name = "Multiplication only";
+ ret[2].type = C_BOOLEAN;
ret[2].sval = NULL;
- ret[2].ival = 0;
+ ret[2].ival = params->multiplication_only;
+
+ ret[3].name = NULL;
+ ret[3].type = C_END;
+ ret[3].sval = NULL;
+ ret[3].ival = 0;
return ret;
}
ret->w = atoi(cfg[0].sval);
ret->diff = cfg[1].ival;
+ ret->multiplication_only = cfg[2].ival;
return ret;
}
* digit constraints in that box. We expect to find the digits
* of the candidate layout in ctx->dscratch, and we update
* ctx->iscratch as appropriate.
+ *
+ * The contents of ctx->iscratch are completely different
+ * depending on whether diff == DIFF_HARD or not. This function
+ * uses iscratch completely differently between the two cases, and
+ * the code in solver_common() which consumes the result must
+ * likewise have an if statement with completely different
+ * branches for the two cases.
+ *
+ * In DIFF_EASY and DIFF_NORMAL modes, the valid entries in
+ * ctx->iscratch are 0,...,n-1, and each of those entries
+ * ctx->iscratch[i] gives a bitmap of the possible digits in the
+ * ith square of the clue box currently under consideration. So
+ * each entry of iscratch starts off as an empty bitmap, and we
+ * set bits in it as possible layouts for the clue box are
+ * considered (and the difference between DIFF_EASY and
+ * DIFF_NORMAL is just that in DIFF_EASY mode we deliberately set
+ * more bits than absolutely necessary, hence restricting our own
+ * knowledge).
+ *
+ * But in DIFF_HARD mode, the valid entries are 0,...,2*w-1 (at
+ * least outside *this* function - inside this function, we also
+ * use 2*w,...,4*w-1 as scratch space in the loop below); the
+ * first w of those give the possible digits in the intersection
+ * of the current clue box with each column of the puzzle, and the
+ * next w do the same for each row. In this mode, each iscratch
+ * entry starts off as a _full_ bitmap, and in this function we
+ * _clear_ bits for digits that are absent from a given row or
+ * column in each candidate layout, so that the only bits which
+ * remain set are those for digits which have to appear in a given
+ * row/column no matter how the clue box is laid out.
*/
if (diff == DIFF_EASY) {
unsigned mask = 0;
long value = ctx->clues[box] & ~CMASK;
long op = ctx->clues[box] & CMASK;
+ /*
+ * Initialise ctx->iscratch for this clue box. At different
+ * difficulty levels we must initialise a different amount of
+ * it to different things; see the comments in
+ * solver_clue_candidate explaining what each version does.
+ */
if (diff == DIFF_HARD) {
- for (i = 0; i < n; i++)
+ for (i = 0; i < 2*w; i++)
ctx->iscratch[i] = (1 << (w+1)) - (1 << 1);
} else {
for (i = 0; i < n; i++)
break;
}
+ /*
+ * Do deductions based on the information we've now
+ * accumulated in ctx->iscratch. See the comments above in
+ * solver_clue_candidate explaining what data is left in here,
+ * and how it differs between DIFF_HARD and lower difficulty
+ * levels (hence the big if statement here).
+ */
if (diff < DIFF_HARD) {
#ifdef STANDALONE_SOLVER
char prefix[256];
singletons[i] = 0;
j = dsf_canonify(dsf, i);
k = dsf_size(dsf, j);
- if (j == i && k > 2) {
+ if (params->multiplication_only)
+ singletons[j] = F_MUL;
+ else if (j == i && k > 2) {
singletons[j] |= F_ADD | F_MUL;
} else if (j != i && k == 2) {
/* Fetch the two numbers and sort them into order. */
}
static void draw_tile(drawing *dr, game_drawstate *ds, struct clues *clues,
- int x, int y, long tile)
+ int x, int y, long tile, int only_one_op)
{
int w = clues->w /* , a = w*w */;
int tx, ty, tw, th;
draw_rect(dr, cx, cy, cw, ch,
(tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND);
+ /* pencil-mode highlight */
+ if (tile & DF_HIGHLIGHT_PENCIL) {
+ int coords[6];
+ coords[0] = cx;
+ coords[1] = cy;
+ coords[2] = cx+cw/2;
+ coords[3] = cy;
+ coords[4] = cx;
+ coords[5] = cy+ch/2;
+ draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
+ }
+
/*
* Draw the corners of thick lines in corner-adjacent squares,
* which jut into this square by one pixel.
if (x+1 < w && y+1 < w && dsf_canonify(clues->dsf, y*w+x) != dsf_canonify(clues->dsf, (y+1)*w+x+1))
draw_rect(dr, tx+TILESIZE-1-2*GRIDEXTRA, ty+TILESIZE-1-2*GRIDEXTRA, GRIDEXTRA, GRIDEXTRA, COL_GRID);
- /* pencil-mode highlight */
- if (tile & DF_HIGHLIGHT_PENCIL) {
- int coords[6];
- coords[0] = cx;
- coords[1] = cy;
- coords[2] = cx+cw/2;
- coords[3] = cy;
- coords[4] = cx;
- coords[5] = cy+ch/2;
- draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
- }
-
/* Draw the box clue. */
if (dsf_canonify(clues->dsf, y*w+x) == y*w+x) {
long clue = clues->clues[y*w+x];
* want to display them right if so.
*/
sprintf (str, "%ld%s", clueval,
- (size == 1 ? "" :
+ (size == 1 || only_one_op ? "" :
cluetype == C_ADD ? "+" :
cluetype == C_SUB ? ds->minus_sign :
cluetype == C_MUL ? ds->times_sign :
if (ds->tiles[y*w+x] != tile) {
ds->tiles[y*w+x] = tile;
- draw_tile(dr, ds, state->clues, x, y, tile);
+ draw_tile(dr, ds, state->clues, x, y, tile,
+ state->par.multiplication_only);
}
}
}
const struct game thegame = {
"Keen", "games.keen", "keen",
default_params,
- game_fetch_preset,
+ game_fetch_preset, NULL,
decode_params,
encode_params,
free_params,