#define TILE_SIZE 24
#define BORDER 18
+#define CORNER_TOLERANCE 0.15F
+#define CENTRE_TOLERANCE 0.15F
+
+#define FLASH_TIME 0.13F
+
#define COORD(x) ( (x) * TILE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
int *grid; /* contains the numbers */
unsigned char *vedge; /* (w+1) x h */
unsigned char *hedge; /* w x (h+1) */
+ int completed;
};
game_params *default_params(void)
{
if (params->w <= 0 && params->h <= 0)
return "Width and height must both be greater than zero";
- if (params->w * params->h < 4)
- return "Total area must be at least 4";
+ if (params->w < 2 && params->h < 2)
+ return "Grid area must be greater than one";
return NULL;
}
int nrects = 0, rectsize = 0;
/*
- * Maximum rectangle area is 1/6 of total grid size.
+ * Maximum rectangle area is 1/6 of total grid size, unless
+ * this means we can't place any rectangles at all in which
+ * case we set it to 2 at minimum.
*/
maxarea = params->w * params->h / 6;
+ if (maxarea < 2)
+ maxarea = 2;
for (rw = 1; rw <= params->w; rw++)
for (rh = 1; rh <= params->h; rh++) {
state->grid = snewn(area, int);
state->vedge = snewn(area, unsigned char);
state->hedge = snewn(area, unsigned char);
+ state->completed = FALSE;
i = 0;
while (*seed) {
ret->hedge = snewn(state->w * state->h, unsigned char);
ret->grid = snewn(state->w * state->h, int);
+ ret->completed = state->completed;
+
memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
memcpy(ret->hedge, state->hedge, state->w*state->h*sizeof(unsigned char));
sfree(ui);
}
-int coord_round(float coord)
+void coord_round(float x, float y, int *xr, int *yr)
{
- int i;
- float dist;
+ float xs, ys, xv, yv, dx, dy, dist;
/*
- * Find the nearest integer.
+ * Find the nearest square-centre.
*/
- i = (int)(coord + 0.5F);
+ xs = (float)floor(x) + 0.5F;
+ ys = (float)floor(y) + 0.5F;
/*
- * Find the distance from us to that integer.
+ * And find the nearest grid vertex.
*/
- dist = fabs(coord - (float)i);
+ xv = (float)floor(x + 0.5F);
+ yv = (float)floor(y + 0.5F);
/*
- * If we're within the tolerance limit, return the edge
- * coordinate. Otherwise, return the centre coordinate.
+ * We allocate clicks in parts of the grid square to either
+ * corners, edges or square centres, as follows:
+ *
+ * +--+--------+--+
+ * | | | |
+ * +--+ +--+
+ * | `. ,' |
+ * | +--+ |
+ * | | | |
+ * | +--+ |
+ * | ,' `. |
+ * +--+ +--+
+ * | | | |
+ * +--+--------+--+
+ *
+ * (Not to scale!)
+ *
+ * In other words: we measure the square distance (i.e.
+ * max(dx,dy)) from the click to the nearest corner, and if
+ * it's within CORNER_TOLERANCE then we return a corner click.
+ * We measure the square distance from the click to the nearest
+ * centre, and if that's within CENTRE_TOLERANCE we return a
+ * centre click. Failing that, we find which of the two edge
+ * centres is nearer to the click and return that edge.
*/
- if (dist < 0.3F)
- return i * 2;
- else
- return 1 + 2 * (int)coord;
+
+ /*
+ * Check for corner click.
+ */
+ dx = (float)fabs(x - xv);
+ dy = (float)fabs(y - yv);
+ dist = (dx > dy ? dx : dy);
+ if (dist < CORNER_TOLERANCE) {
+ *xr = 2 * (int)xv;
+ *yr = 2 * (int)yv;
+ } else {
+ /*
+ * Check for centre click.
+ */
+ dx = (float)fabs(x - xs);
+ dy = (float)fabs(y - ys);
+ dist = (dx > dy ? dx : dy);
+ if (dist < CENTRE_TOLERANCE) {
+ *xr = 1 + 2 * (int)xs;
+ *yr = 1 + 2 * (int)ys;
+ } else {
+ /*
+ * Failing both of those, see which edge we're closer to.
+ * Conveniently, this is simply done by testing the relative
+ * magnitude of dx and dy (which are currently distances from
+ * the square centre).
+ */
+ if (dx > dy) {
+ /* Vertical edge: x-coord of corner,
+ * y-coord of square centre. */
+ *xr = 2 * (int)xv;
+ *yr = 1 + 2 * (int)ys;
+ } else {
+ /* Horizontal edge: x-coord of square centre,
+ * y-coord of corner. */
+ *xr = 1 + 2 * (int)xs;
+ *yr = 2 * (int)yv;
+ }
+ }
+ }
}
static void ui_draw_rect(game_state *state, game_ui *ui,
return NULL;
}
- xc = coord_round(FROMCOORD((float)x));
- yc = coord_round(FROMCOORD((float)y));
+ coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc);
if (startdrag) {
ui->drag_start_x = xc;
active = TRUE;
}
- if (enddrag && (xc >= 0 && xc <= 2*from->w &&
- yc >= 0 && yc <= 2*from->h)) {
- ret = dup_game(from);
+ ret = NULL;
- if (ui->dragged) {
- ui_draw_rect(ret, ui, ret->hedge, ret->vedge, 1);
- } else {
- if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) {
- hedge(ret,xc/2,yc/2) = !hedge(ret,xc/2,yc/2);
- }
- if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) {
- vedge(ret,xc/2,yc/2) = !vedge(ret,xc/2,yc/2);
- }
- }
+ if (enddrag) {
+ if (xc >= 0 && xc <= 2*from->w &&
+ yc >= 0 && yc <= 2*from->h) {
+ ret = dup_game(from);
- ui->drag_start_x = -1;
- ui->drag_start_y = -1;
- ui->drag_end_x = -1;
- ui->drag_end_y = -1;
- ui->dragged = FALSE;
- active = TRUE;
+ if (ui->dragged) {
+ ui_draw_rect(ret, ui, ret->hedge, ret->vedge, 1);
+ } else {
+ if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) {
+ hedge(ret,xc/2,yc/2) = !hedge(ret,xc/2,yc/2);
+ }
+ if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) {
+ vedge(ret,xc/2,yc/2) = !vedge(ret,xc/2,yc/2);
+ }
+ }
- if (!memcmp(ret->hedge, from->hedge, from->w*from->h) &&
- !memcmp(ret->vedge, from->vedge, from->w*from->h)) {
- free_game(ret);
- } else
- return ret; /* a move has been made */
+ if (!memcmp(ret->hedge, from->hedge, from->w*from->h) &&
+ !memcmp(ret->vedge, from->vedge, from->w*from->h)) {
+ free_game(ret);
+ ret = NULL;
+ }
+
+ /*
+ * We've made a real change to the grid. Check to see
+ * if the game has been completed.
+ */
+ if (ret && !ret->completed) {
+ int x, y, ok;
+ unsigned char *correct = get_correct(ret);
+
+ ok = TRUE;
+ for (x = 0; x < ret->w; x++)
+ for (y = 0; y < ret->h; y++)
+ if (!index(ret, correct, x, y))
+ ok = FALSE;
+
+ sfree(correct);
+
+ if (ok)
+ ret->completed = TRUE;
+ }
+ }
+
+ ui->drag_start_x = -1;
+ ui->drag_start_y = -1;
+ ui->drag_end_x = -1;
+ ui->drag_end_y = -1;
+ ui->dragged = FALSE;
+ active = TRUE;
}
- if (active)
+ if (ret)
+ return ret; /* a move has been made */
+ else if (active)
return from; /* UI activity has occurred */
- return NULL;
+ else
+ return NULL;
}
/* ----------------------------------------------------------------------
if (grid(state,x,y)) {
sprintf(str, "%d", grid(state,x,y));
draw_text(fe, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE,
- TILE_SIZE/3, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
+ TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
}
/*
}
if (!ds->started) {
+ draw_rect(fe, 0, 0,
+ state->w * TILE_SIZE + 2*BORDER + 1,
+ state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND);
draw_rect(fe, COORD(0)-1, COORD(0)-1,
ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE);
ds->started = TRUE;
+ draw_update(fe, 0, 0,
+ state->w * TILE_SIZE + 2*BORDER + 1,
+ state->h * TILE_SIZE + 2*BORDER + 1);
}
for (x = 0; x < state->w; x++)
c |= index(state,vedge,x,y) << 4;
if (VRANGE(state,x,y+1))
c |= index(state,vedge,x,y+1) << 6;
- if (index(state, correct, x, y))
+ if (index(state, correct, x, y) && !flashtime)
c |= CORRECT;
if (index(ds,ds->visible,x,y) != c) {
float game_flash_length(game_state *oldstate, game_state *newstate)
{
+ if (!oldstate->completed && newstate->completed)
+ return FLASH_TIME;
return 0.0F;
}