sfree(params);
}
-static game_params *dup_params(game_params *params)
+static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
}
}
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
{
char data[256];
return dupstr(data);
}
-static config_item *game_configure(game_params *params)
+static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
return ret;
}
-static game_params *custom_params(config_item *cfg)
+static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
return ret;
}
-static char *validate_params(game_params *params, int full)
+static char *validate_params(const game_params *params, int full)
{
/*
* (At least at the time of writing this comment) The grid
return desc;
}
-static char *validate_desc(const game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w, h = params->h, W = w+1, H = h+1;
int area = W*H;
return NULL;
}
-static game_state *new_game(midend *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, const game_params *params,
+ const char *desc)
{
int w = params->w, h = params->h, W = w+1, H = h+1;
game_state *state = snew(game_state);
return state;
}
-static game_state *dup_game(game_state *state)
+static game_state *dup_game(const game_state *state)
{
int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
game_state *ret = snew(game_state);
return anti ? 4 - ret : ret;
}
+struct slant_neighbour_ctx {
+ const game_state *state;
+ int i, n, neighbours[4];
+};
+static int slant_neighbour(int vertex, void *vctx)
+{
+ struct slant_neighbour_ctx *ctx = (struct slant_neighbour_ctx *)vctx;
+
+ if (vertex >= 0) {
+ int w = ctx->state->p.w, h = ctx->state->p.h, W = w+1;
+ int x = vertex % W, y = vertex / W;
+ ctx->n = ctx->i = 0;
+ if (x < w && y < h && ctx->state->soln[y*w+x] < 0)
+ ctx->neighbours[ctx->n++] = (y+1)*W+(x+1);
+ if (x > 0 && y > 0 && ctx->state->soln[(y-1)*w+(x-1)] < 0)
+ ctx->neighbours[ctx->n++] = (y-1)*W+(x-1);
+ if (x > 0 && y < h && ctx->state->soln[y*w+(x-1)] > 0)
+ ctx->neighbours[ctx->n++] = (y+1)*W+(x-1);
+ if (x < w && y > 0 && ctx->state->soln[(y-1)*w+x] > 0)
+ ctx->neighbours[ctx->n++] = (y-1)*W+(x+1);
+ }
+
+ if (ctx->i < ctx->n)
+ return ctx->neighbours[ctx->i++];
+ else
+ return -1;
+}
+
static int check_completion(game_state *state)
{
int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
int x, y, err = FALSE;
- int *dsf;
memset(state->errors, 0, W*H);
/*
- * To detect loops in the grid, we iterate through each edge
- * building up a dsf of connected components of the space
- * around the edges; if there's more than one such component,
- * we have a loop, and in particular we can then easily
- * identify and highlight every edge forming part of a loop
- * because it separates two nonequivalent regions.
- *
- * We use the `tmpdsf' scratch space in the shared clues
- * structure, to avoid mallocing too often.
- *
- * For these purposes, the grid is considered to be divided
- * into diamond-shaped regions surrounding an orthogonal edge.
- * This means we have W*h vertical edges and w*H horizontal
- * ones; so our vertical edges are indexed in the dsf as
- * (y*W+x) (0<=y<h, 0<=x<W), and the horizontal ones as (W*h +
- * y*w+x) (0<=y<H, 0<=x<w), where (x,y) is the topmost or
- * leftmost point on the edge.
+ * Detect and error-highlight loops in the grid.
*/
- dsf = state->clues->tmpdsf;
- dsf_init(dsf, W*h + w*H);
- /* Start by identifying all the outer edges with each other. */
- for (y = 0; y < h; y++) {
- dsf_merge(dsf, 0, y*W+0);
- dsf_merge(dsf, 0, y*W+w);
- }
- for (x = 0; x < w; x++) {
- dsf_merge(dsf, 0, W*h + 0*w+x);
- dsf_merge(dsf, 0, W*h + h*w+x);
- }
- /* Now go through the actual grid. */
- for (y = 0; y < h; y++)
- for (x = 0; x < w; x++) {
- if (state->soln[y*w+x] >= 0) {
- /*
- * There isn't a \ in this square, so we can unify
- * the top edge with the left, and the bottom with
- * the right.
- */
- dsf_merge(dsf, y*W+x, W*h + y*w+x);
- dsf_merge(dsf, y*W+(x+1), W*h + (y+1)*w+x);
- }
- if (state->soln[y*w+x] <= 0) {
- /*
- * There isn't a / in this square, so we can unify
- * the top edge with the right, and the bottom
- * with the left.
- */
- dsf_merge(dsf, y*W+x, W*h + (y+1)*w+x);
- dsf_merge(dsf, y*W+(x+1), W*h + y*w+x);
- }
- }
- /* Now go through again and mark the appropriate edges as erroneous. */
- for (y = 0; y < h; y++)
- for (x = 0; x < w; x++) {
- int erroneous = 0;
- if (state->soln[y*w+x] > 0) {
- /*
- * A / separates the top and left edges (which
- * must already have been identified with each
- * other) from the bottom and right (likewise).
- * Hence it is erroneous if and only if the top
- * and right edges are nonequivalent.
- */
- erroneous = (dsf_canonify(dsf, y*W+(x+1)) !=
- dsf_canonify(dsf, W*h + y*w+x));
- } else if (state->soln[y*w+x] < 0) {
- /*
- * A \ separates the top and right edges (which
- * must already have been identified with each
- * other) from the bottom and left (likewise).
- * Hence it is erroneous if and only if the top
- * and left edges are nonequivalent.
- */
- erroneous = (dsf_canonify(dsf, y*W+x) !=
- dsf_canonify(dsf, W*h + y*w+x));
- }
- if (erroneous) {
- state->errors[y*W+x] |= ERR_SQUARE;
- err = TRUE;
+ {
+ struct findloopstate *fls = findloop_new_state(W*H);
+ struct slant_neighbour_ctx ctx;
+ ctx.state = state;
+
+ if (findloop_run(fls, W*H, slant_neighbour, &ctx))
+ err = TRUE;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ int u, v;
+ if (state->soln[y*w+x] == 0) {
+ continue;
+ } else if (state->soln[y*w+x] > 0) {
+ u = y*W+(x+1);
+ v = (y+1)*W+x;
+ } else {
+ u = (y+1)*W+(x+1);
+ v = y*W+x;
+ }
+ if (findloop_is_loop_edge(fls, u, v))
+ state->errors[y*W+x] |= ERR_SQUARE;
}
}
+ findloop_free_state(fls);
+ }
+
/*
* Now go through and check the degree of each clue vertex, and
* mark it with ERR_VERTEX if it cannot be fulfilled.
return TRUE;
}
-static char *solve_game(game_state *state, game_state *currstate,
- char *aux, char **error)
+static char *solve_game(const game_state *state, const game_state *currstate,
+ const char *aux, char **error)
{
int w = state->p.w, h = state->p.h;
signed char *soln;
return move;
}
-static int game_can_format_as_text_now(game_params *params)
+static int game_can_format_as_text_now(const game_params *params)
{
return TRUE;
}
-static char *game_text_format(game_state *state)
+static char *game_text_format(const game_state *state)
{
int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
int x, y, len;
int cur_x, cur_y, cur_visible;
};
-static game_ui *new_ui(game_state *state)
+static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->cur_x = ui->cur_y = ui->cur_visible = 0;
sfree(ui);
}
-static char *encode_ui(game_ui *ui)
+static char *encode_ui(const game_ui *ui)
{
return NULL;
}
-static void decode_ui(game_ui *ui, char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding)
{
}
-static void game_changed_state(game_ui *ui, game_state *oldstate,
- game_state *newstate)
+static void game_changed_state(game_ui *ui, const game_state *oldstate,
+ const game_state *newstate)
{
}
long *todraw;
};
-static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds,
- int x, int y, int button)
+static char *interpret_move(const game_state *state, game_ui *ui,
+ const game_drawstate *ds,
+ int x, int y, int button)
{
int w = state->p.w, h = state->p.h;
int v;
y = FROMCOORD(y);
if (x < 0 || y < 0 || x >= w || y >= h)
return NULL;
+ ui->cur_visible = 0;
} else if (IS_CURSOR_SELECT(button)) {
if (!ui->cur_visible) {
ui->cur_visible = 1;
move_cursor(button, &ui->cur_x, &ui->cur_y, w, h, 0);
ui->cur_visible = 1;
return "";
+ } else if (button == '\\' || button == '\b' || button == '/') {
+ int x = ui->cur_x, y = ui->cur_y;
+ if (button == ("\\" "\b" "/")[state->soln[y*w + x] + 1]) return NULL;
+ sprintf(buf, "%c%d,%d", button == '\b' ? 'C' : button, x, y);
+ return dupstr(buf);
}
if (action != NONE) {
return NULL;
}
-static game_state *execute_move(game_state *state, char *move)
+static game_state *execute_move(const game_state *state, const char *move)
{
int w = state->p.w, h = state->p.h;
char c;
* Drawing routines.
*/
-static void game_compute_size(game_params *params, int tilesize,
- int *x, int *y)
+static void game_compute_size(const game_params *params, int tilesize,
+ int *x, int *y)
{
/* fool the macros */
struct dummy { int tilesize; } dummy, *ds = &dummy;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
- game_params *params, int tilesize)
+ const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
}
return ret;
}
-static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
{
int w = state->p.w, h = state->p.h;
int i;
draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
}
-static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
- game_state *state, int dir, game_ui *ui,
- float animtime, float flashtime)
+static void game_redraw(drawing *dr, game_drawstate *ds,
+ const game_state *oldstate, const game_state *state,
+ int dir, const game_ui *ui,
+ float animtime, float flashtime)
{
int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
int x, y;
}
}
-static float game_anim_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_anim_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
return 0.0F;
}
-static float game_flash_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_flash_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
if (!oldstate->completed && newstate->completed &&
!oldstate->used_solve && !newstate->used_solve)
return 0.0F;
}
-static int game_status(game_state *state)
+static int game_status(const game_state *state)
{
return state->completed ? +1 : 0;
}
-static int game_timing_state(game_state *state, game_ui *ui)
+static int game_timing_state(const game_state *state, game_ui *ui)
{
return TRUE;
}
-static void game_print_size(game_params *params, float *x, float *y)
+static void game_print_size(const game_params *params, float *x, float *y)
{
int pw, ph;
*y = ph / 100.0F;
}
-static void game_print(drawing *dr, game_state *state, int tilesize)
+static void game_print(drawing *dr, const game_state *state, int tilesize)
{
int w = state->p.w, h = state->p.h, W = w+1;
int ink = print_mono_colour(dr, 0);
const struct game thegame = {
"Slant", "games.slant", "slant",
default_params,
- game_fetch_preset,
+ game_fetch_preset, NULL,
decode_params,
encode_params,
free_params,