2 * slant.c: Puzzle from nikoli.co.jp involving drawing a diagonal
3 * line through each square of a grid.
7 * In this puzzle you have a grid of squares, each of which must
8 * contain a diagonal line; you also have clue numbers placed at
9 * _points_ of that grid, which means there's a (w+1) x (h+1) array
10 * of possible clue positions.
12 * I'm therefore going to adopt a rigid convention throughout this
13 * source file of using w and h for the dimensions of the grid of
14 * squares, and W and H for the dimensions of the grid of points.
15 * Thus, W == w+1 and H == h+1 always.
17 * Clue arrays will be W*H `signed char's, and the clue at each
18 * point will be a number from 0 to 4, or -1 if there's no clue.
20 * Solution arrays will be W*H `signed char's, and the number at
21 * each point will be +1 for a forward slash (/), -1 for a
22 * backslash (\), and 0 for unknown.
45 typedef struct game_clues {
48 int *dsf; /* scratch space for completion check */
57 int used_solve; /* used to suppress completion flash */
60 static game_params *default_params(void)
62 game_params *ret = snew(game_params);
69 static const struct game_params slant_presets[] = {
75 static int game_fetch_preset(int i, char **name, game_params **params)
80 if (i < 0 || i >= lenof(slant_presets))
83 ret = snew(game_params);
84 *ret = slant_presets[i];
86 sprintf(str, "%dx%d", ret->w, ret->h);
93 static void free_params(game_params *params)
98 static game_params *dup_params(game_params *params)
100 game_params *ret = snew(game_params);
101 *ret = *params; /* structure copy */
105 static void decode_params(game_params *ret, char const *string)
107 ret->w = ret->h = atoi(string);
108 while (*string && isdigit((unsigned char)*string)) string++;
109 if (*string == 'x') {
111 ret->h = atoi(string);
115 static char *encode_params(game_params *params, int full)
119 sprintf(data, "%dx%d", params->w, params->h);
124 static config_item *game_configure(game_params *params)
129 ret = snewn(3, config_item);
131 ret[0].name = "Width";
132 ret[0].type = C_STRING;
133 sprintf(buf, "%d", params->w);
134 ret[0].sval = dupstr(buf);
137 ret[1].name = "Height";
138 ret[1].type = C_STRING;
139 sprintf(buf, "%d", params->h);
140 ret[1].sval = dupstr(buf);
151 static game_params *custom_params(config_item *cfg)
153 game_params *ret = snew(game_params);
155 ret->w = atoi(cfg[0].sval);
156 ret->h = atoi(cfg[1].sval);
161 static char *validate_params(game_params *params, int full)
164 * (At least at the time of writing this comment) The grid
165 * generator is actually capable of handling even zero grid
166 * dimensions without crashing. Puzzles with a zero-area grid
167 * are a bit boring, though, because they're already solved :-)
170 if (params->w < 1 || params->h < 1)
171 return "Width and height must both be at least one";
177 * Utility function used by both the solver and the filled-grid
181 static void fill_square(int w, int h, int y, int x, int v,
182 signed char *soln, int *dsf)
184 int W = w+1 /*, H = h+1 */;
189 dsf_merge(dsf, y*W+x, (y+1)*W+(x+1));
191 dsf_merge(dsf, y*W+(x+1), (y+1)*W+x);
195 * Scratch space for solver.
197 struct solver_scratch {
201 struct solver_scratch *new_scratch(int w, int h)
203 int W = w+1, H = h+1;
204 struct solver_scratch *ret = snew(struct solver_scratch);
205 ret->dsf = snewn(W*H, int);
209 void free_scratch(struct solver_scratch *sc)
216 * Solver. Returns 0 for impossibility, 1 for success, 2 for
217 * ambiguity or failure to converge.
219 static int slant_solve(int w, int h, const signed char *clues,
220 signed char *soln, struct solver_scratch *sc)
222 int W = w+1, H = h+1;
229 memset(soln, 0, w*h);
232 * Establish a disjoint set forest for tracking connectedness
233 * between grid points.
235 for (i = 0; i < W*H; i++)
236 sc->dsf[i] = i; /* initially all distinct */
239 * Repeatedly try to deduce something until we can't.
242 done_something = FALSE;
245 * Any clue point with the number of remaining lines equal
246 * to zero or to the number of remaining undecided
247 * neighbouring squares can be filled in completely.
249 for (y = 0; y < H; y++)
250 for (x = 0; x < W; x++) {
253 if ((c = clues[y*W+x]) < 0)
257 * We have a clue point. Count up the number of
258 * undecided neighbours, and also the number of
259 * lines already present.
263 if (x > 0 && y > 0 && (v = soln[(y-1)*w+(x-1)]) != +1)
264 v == 0 ? nu++ : nl--;
265 if (x > 0 && y < h && (v = soln[y*w+(x-1)]) != -1)
266 v == 0 ? nu++ : nl--;
267 if (x < w && y > 0 && (v = soln[(y-1)*w+x]) != -1)
268 v == 0 ? nu++ : nl--;
269 if (x < w && y < h && (v = soln[y*w+x]) != +1)
270 v == 0 ? nu++ : nl--;
275 if (nl < 0 || nl > nu) {
277 * No consistent value for this at all!
279 return 0; /* impossible */
282 if (nu > 0 && (nl == 0 || nl == nu)) {
283 #ifdef SOLVER_DIAGNOSTICS
284 printf("%s around clue point at %d,%d\n",
285 nl ? "filling" : "emptying", x, y);
287 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] == 0)
288 fill_square(w, h, y-1, x-1, (nl ? -1 : +1), soln,
290 if (x > 0 && y < h && soln[y*w+(x-1)] == 0)
291 fill_square(w, h, y, x-1, (nl ? +1 : -1), soln,
293 if (x < w && y > 0 && soln[(y-1)*w+x] == 0)
294 fill_square(w, h, y-1, x, (nl ? +1 : -1), soln,
296 if (x < w && y < h && soln[y*w+x] == 0)
297 fill_square(w, h, y, x, (nl ? -1 : +1), soln,
300 done_something = TRUE;
308 * Failing that, we now apply the second condition, which
309 * is that no square may be filled in such a way as to form
312 for (y = 0; y < h; y++)
313 for (x = 0; x < w; x++) {
317 continue; /* got this one already */
319 fs = (dsf_canonify(sc->dsf, y*W+x) ==
320 dsf_canonify(sc->dsf, (y+1)*W+(x+1)));
321 bs = (dsf_canonify(sc->dsf, (y+1)*W+x) ==
322 dsf_canonify(sc->dsf, y*W+(x+1)));
326 * Loop avoidance leaves no consistent value
329 return 0; /* impossible */
334 * Top left and bottom right corners of this
335 * square are already connected, which means we
336 * aren't allowed to put a backslash in here.
338 #ifdef SOLVER_DIAGNOSTICS
339 printf("placing / in %d,%d by loop avoidance\n", x, y);
341 fill_square(w, h, y, x, +1, soln, sc->dsf);
342 done_something = TRUE;
345 * Top right and bottom left corners of this
346 * square are already connected, which means we
347 * aren't allowed to put a forward slash in
350 #ifdef SOLVER_DIAGNOSTICS
351 printf("placing \\ in %d,%d by loop avoidance\n", x, y);
353 fill_square(w, h, y, x, -1, soln, sc->dsf);
354 done_something = TRUE;
358 } while (done_something);
361 * Solver can make no more progress. See if the grid is full.
363 for (i = 0; i < w*h; i++)
365 return 2; /* failed to converge */
366 return 1; /* success */
370 * Filled-grid generator.
372 static void slant_generate(int w, int h, signed char *soln, random_state *rs)
374 int W = w+1, H = h+1;
381 memset(soln, 0, w*h);
384 * Establish a disjoint set forest for tracking connectedness
385 * between grid points.
387 dsf = snewn(W*H, int);
388 for (i = 0; i < W*H; i++)
389 dsf[i] = i; /* initially all distinct */
392 * Prepare a list of the squares in the grid, and fill them in
395 indices = snewn(w*h, int);
396 for (i = 0; i < w*h; i++)
398 shuffle(indices, w*h, sizeof(*indices), rs);
401 * Fill in each one in turn.
403 for (i = 0; i < w*h; i++) {
409 fs = (dsf_canonify(dsf, y*W+x) ==
410 dsf_canonify(dsf, (y+1)*W+(x+1)));
411 bs = (dsf_canonify(dsf, (y+1)*W+x) ==
412 dsf_canonify(dsf, y*W+(x+1)));
415 * It isn't possible to get into a situation where we
416 * aren't allowed to place _either_ type of slash in a
419 * Proof (thanks to Gareth Taylor):
421 * If it were possible, it would have to be because there
422 * was an existing path (not using this square) between the
423 * top-left and bottom-right corners of this square, and
424 * another between the other two. These two paths would
425 * have to cross at some point.
427 * Obviously they can't cross in the middle of a square, so
428 * they must cross by sharing a point in common. But this
429 * isn't possible either: if you chessboard-colour all the
430 * points on the grid, you find that any continuous
431 * diagonal path is entirely composed of points of the same
432 * colour. And one of our two hypothetical paths is between
433 * two black points, and the other is between two white
434 * points - therefore they can have no point in common. []
438 v = fs ? +1 : bs ? -1 : 2 * random_upto(rs, 2) - 1;
439 fill_square(w, h, y, x, v, soln, dsf);
446 static char *new_game_desc(game_params *params, random_state *rs,
447 char **aux, int interactive)
449 int w = params->w, h = params->h, W = w+1, H = h+1;
450 signed char *soln, *tmpsoln, *clues;
452 struct solver_scratch *sc;
456 soln = snewn(w*h, signed char);
457 tmpsoln = snewn(w*h, signed char);
458 clues = snewn(W*H, signed char);
459 clueindices = snewn(W*H, int);
460 sc = new_scratch(w, h);
464 * Create the filled grid.
466 slant_generate(w, h, soln, rs);
469 * Fill in the complete set of clues.
471 for (y = 0; y < H; y++)
472 for (x = 0; x < W; x++) {
475 if (x > 0 && y > 0 && soln[(y-1)*w+(x-1)] == -1) v++;
476 if (x > 0 && y < h && soln[y*w+(x-1)] == +1) v++;
477 if (x < w && y > 0 && soln[(y-1)*w+x] == +1) v++;
478 if (x < w && y < h && soln[y*w+x] == -1) v++;
482 } while (slant_solve(w, h, clues, tmpsoln, sc) != 1);
485 * Remove as many clues as possible while retaining solubility.
487 for (i = 0; i < W*H; i++)
489 shuffle(clueindices, W*H, sizeof(*clueindices), rs);
490 for (i = 0; i < W*H; i++) {
491 y = clueindices[i] / W;
492 x = clueindices[i] % W;
495 if (slant_solve(w, h, clues, tmpsoln, sc) != 1)
496 clues[y*W+x] = v; /* put it back */
500 * Now we have the clue set as it will be presented to the
501 * user. Encode it in a game desc.
507 desc = snewn(W*H+1, char);
510 for (i = 0; i <= W*H; i++) {
511 int n = (i < W*H ? clues[i] : -2);
518 int c = 'a' - 1 + run;
522 run -= c - ('a' - 1);
530 assert(p - desc <= W*H);
532 desc = sresize(desc, p - desc, char);
536 * Encode the solution as an aux_info.
540 *aux = auxbuf = snewn(w*h+1, char);
541 for (i = 0; i < w*h; i++)
542 auxbuf[i] = soln[i] < 0 ? '\\' : '/';
555 static char *validate_desc(game_params *params, char *desc)
557 int w = params->w, h = params->h, W = w+1, H = h+1;
563 if (n >= 'a' && n <= 'z') {
564 squares += n - 'a' + 1;
565 } else if (n >= '0' && n <= '4') {
568 return "Invalid character in game description";
572 return "Not enough data to fill grid";
575 return "Too much data to fit in grid";
580 static game_state *new_game(midend_data *me, game_params *params, char *desc)
582 int w = params->w, h = params->h, W = w+1, H = h+1;
583 game_state *state = snew(game_state);
588 state->soln = snewn(w*h, signed char);
589 memset(state->soln, 0, w*h);
590 state->completed = state->used_solve = FALSE;
592 state->clues = snew(game_clues);
595 state->clues->clues = snewn(W*H, signed char);
596 state->clues->refcount = 1;
597 state->clues->dsf = snewn(W*H, int);
598 memset(state->clues->clues, -1, W*H);
601 if (n >= 'a' && n <= 'z') {
602 squares += n - 'a' + 1;
603 } else if (n >= '0' && n <= '4') {
604 state->clues->clues[squares++] = n - '0';
606 assert(!"can't get here");
608 assert(squares == area);
613 static game_state *dup_game(game_state *state)
615 int w = state->p.w, h = state->p.h;
616 game_state *ret = snew(game_state);
619 ret->clues = state->clues;
620 ret->clues->refcount++;
621 ret->completed = state->completed;
622 ret->used_solve = state->used_solve;
624 ret->soln = snewn(w*h, signed char);
625 memcpy(ret->soln, state->soln, w*h);
630 static void free_game(game_state *state)
635 static int check_completion(game_state *state)
637 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
641 * Establish a disjoint set forest for tracking connectedness
642 * between grid points. Use the dsf scratch space in the shared
643 * clues structure, to avoid mallocing too often.
645 for (i = 0; i < W*H; i++)
646 state->clues->dsf[i] = i; /* initially all distinct */
649 * Now go through the grid checking connectedness. While we're
650 * here, also check that everything is filled in.
652 for (y = 0; y < h; y++)
653 for (x = 0; x < w; x++) {
656 if (state->soln[y*w+x] == 0)
658 if (state->soln[y*w+x] < 0) {
667 * Our edge connects i1 with i2. If they're already
668 * connected, return failure. Otherwise, link them.
670 if (dsf_canonify(state->clues->dsf, i1) ==
671 dsf_canonify(state->clues->dsf, i2))
674 dsf_merge(state->clues->dsf, i1, i2);
678 * The grid is _a_ valid grid; let's see if it matches the
681 for (y = 0; y < H; y++)
682 for (x = 0; x < W; x++) {
685 if ((c = state->clues->clues[y*W+x]) < 0)
690 if (x > 0 && y > 0 && state->soln[(y-1)*w+(x-1)] == -1) v++;
691 if (x > 0 && y < h && state->soln[y*w+(x-1)] == +1) v++;
692 if (x < w && y > 0 && state->soln[(y-1)*w+x] == +1) v++;
693 if (x < w && y < h && state->soln[y*w+x] == -1) v++;
702 static char *solve_game(game_state *state, game_state *currstate,
703 char *aux, char **error)
705 int w = state->p.w, h = state->p.h;
708 int free_soln = FALSE;
710 int movelen, movesize;
715 * If we already have the solution, save ourselves some
718 soln = (signed char *)aux;
719 bs = (signed char)'\\';
722 struct solver_scratch *sc = new_scratch(w, h);
723 soln = snewn(w*h, signed char);
725 ret = slant_solve(w, h, state->clues->clues, soln, sc);
730 return "This puzzle is not self-consistent";
732 return "Unable to find a unique solution for this puzzle";
738 * Construct a move string which turns the current state into
742 move = snewn(movesize, char);
744 move[movelen++] = 'S';
745 move[movelen] = '\0';
746 for (y = 0; y < h; y++)
747 for (x = 0; x < w; x++) {
748 int v = (soln[y*w+x] == bs ? -1 : +1);
749 if (state->soln[y*w+x] != v) {
750 int len = sprintf(buf, ";%c%d,%d", v < 0 ? '\\' : '/', x, y);
751 if (movelen + len >= movesize) {
752 movesize = movelen + len + 256;
753 move = sresize(move, movesize, char);
755 strcpy(move + movelen, buf);
766 static char *game_text_format(game_state *state)
768 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
773 * There are h+H rows of w+W columns.
775 len = (h+H) * (w+W+1) + 1;
776 ret = snewn(len, char);
779 for (y = 0; y < H; y++) {
780 for (x = 0; x < W; x++) {
781 if (state->clues->clues[y*W+x] >= 0)
782 *p++ = state->clues->clues[y*W+x] + '0';
790 for (x = 0; x < W; x++) {
793 if (state->soln[y*w+x] != 0)
794 *p++ = (state->soln[y*w+x] < 0 ? '\\' : '/');
804 assert(p - ret == len);
808 static game_ui *new_ui(game_state *state)
813 static void free_ui(game_ui *ui)
817 static char *encode_ui(game_ui *ui)
822 static void decode_ui(game_ui *ui, char *encoding)
826 static void game_changed_state(game_ui *ui, game_state *oldstate,
827 game_state *newstate)
831 #define PREFERRED_TILESIZE 32
832 #define TILESIZE (ds->tilesize)
833 #define BORDER TILESIZE
834 #define CLUE_RADIUS (TILESIZE / 3)
835 #define CLUE_TEXTSIZE (TILESIZE / 2)
836 #define COORD(x) ( (x) * TILESIZE + BORDER )
837 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
839 #define FLASH_TIME 0.30F
842 * Bit fields in the `grid' and `todraw' elements of the drawstate.
844 #define BACKSLASH 0x0001
845 #define FORWSLASH 0x0002
860 struct game_drawstate {
867 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
868 int x, int y, int button)
870 int w = state->p.w, h = state->p.h;
872 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
878 if (x < 0 || y < 0 || x >= w || y >= h)
881 if (button == LEFT_BUTTON) {
883 * Left-clicking cycles blank -> \ -> / -> blank.
885 v = state->soln[y*w+x] - 1;
890 * Right-clicking cycles blank -> / -> \ -> blank.
892 v = state->soln[y*w+x] + 1;
897 sprintf(buf, "%c%d,%d", v==-1 ? '\\' : v==+1 ? '/' : 'C', x, y);
904 static game_state *execute_move(game_state *state, char *move)
906 int w = state->p.w, h = state->p.h;
909 game_state *ret = dup_game(state);
914 ret->used_solve = TRUE;
916 } else if (c == '\\' || c == '/' || c == 'C') {
918 if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
919 x < 0 || y < 0 || x >= w || y >= h) {
923 ret->soln[y*w+x] = (c == '\\' ? -1 : c == '/' ? +1 : 0);
938 ret->completed = check_completion(ret);
943 /* ----------------------------------------------------------------------
947 static void game_compute_size(game_params *params, int tilesize,
950 /* fool the macros */
951 struct dummy { int tilesize; } dummy = { tilesize }, *ds = &dummy;
953 *x = 2 * BORDER + params->w * TILESIZE + 1;
954 *y = 2 * BORDER + params->h * TILESIZE + 1;
957 static void game_set_size(game_drawstate *ds, game_params *params,
960 ds->tilesize = tilesize;
963 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
965 float *ret = snewn(3 * NCOLOURS, float);
967 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
969 ret[COL_GRID * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.7F;
970 ret[COL_GRID * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.7F;
971 ret[COL_GRID * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.7F;
973 ret[COL_INK * 3 + 0] = 0.0F;
974 ret[COL_INK * 3 + 1] = 0.0F;
975 ret[COL_INK * 3 + 2] = 0.0F;
977 *ncolours = NCOLOURS;
981 static game_drawstate *game_new_drawstate(game_state *state)
983 int w = state->p.w, h = state->p.h;
985 struct game_drawstate *ds = snew(struct game_drawstate);
989 ds->grid = snewn(w*h, int);
990 ds->todraw = snewn(w*h, int);
991 for (i = 0; i < w*h; i++)
992 ds->grid[i] = ds->todraw[i] = -1;
997 static void game_free_drawstate(game_drawstate *ds)
1003 static void draw_clue(frontend *fe, game_drawstate *ds,
1004 int x, int y, int v)
1013 draw_circle(fe, COORD(x), COORD(y), CLUE_RADIUS,
1014 COL_BACKGROUND, COL_INK);
1015 draw_text(fe, COORD(x), COORD(y), FONT_VARIABLE,
1016 CLUE_TEXTSIZE, ALIGN_VCENTRE|ALIGN_HCENTRE,
1020 static void draw_tile(frontend *fe, game_drawstate *ds, game_clues *clues,
1021 int x, int y, int v)
1023 int w = clues->w /*, h = clues->h*/, W = w+1 /*, H = h+1 */;
1026 clip(fe, COORD(x), COORD(y), TILESIZE+1, TILESIZE+1);
1028 draw_rect(fe, COORD(x), COORD(y), TILESIZE, TILESIZE,
1029 (v & FLASH) ? COL_GRID : COL_BACKGROUND);
1032 * Draw the grid lines.
1034 draw_line(fe, COORD(x), COORD(y), COORD(x+1), COORD(y), COL_GRID);
1035 draw_line(fe, COORD(x), COORD(y+1), COORD(x+1), COORD(y+1), COL_GRID);
1036 draw_line(fe, COORD(x), COORD(y), COORD(x), COORD(y+1), COL_GRID);
1037 draw_line(fe, COORD(x+1), COORD(y), COORD(x+1), COORD(y+1), COL_GRID);
1042 if (v & BACKSLASH) {
1043 draw_line(fe, COORD(x), COORD(y), COORD(x+1), COORD(y+1), COL_INK);
1044 draw_line(fe, COORD(x)+1, COORD(y), COORD(x+1), COORD(y+1)-1,
1046 draw_line(fe, COORD(x), COORD(y)+1, COORD(x+1)-1, COORD(y+1),
1048 } else if (v & FORWSLASH) {
1049 draw_line(fe, COORD(x+1), COORD(y), COORD(x), COORD(y+1), COL_INK);
1050 draw_line(fe, COORD(x+1)-1, COORD(y), COORD(x), COORD(y+1)-1,
1052 draw_line(fe, COORD(x+1), COORD(y)+1, COORD(x)+1, COORD(y+1),
1057 * Draw dots on the grid corners that appear if a slash is in a
1058 * neighbouring cell.
1061 draw_rect(fe, COORD(x), COORD(y)+1, 1, 1, COL_INK);
1063 draw_rect(fe, COORD(x), COORD(y+1)-1, 1, 1, COL_INK);
1065 draw_rect(fe, COORD(x+1), COORD(y)+1, 1, 1, COL_INK);
1067 draw_rect(fe, COORD(x+1), COORD(y+1)-1, 1, 1, COL_INK);
1069 draw_rect(fe, COORD(x)+1, COORD(y), 1, 1, COL_INK);
1071 draw_rect(fe, COORD(x+1)-1, COORD(y), 1, 1, COL_INK);
1073 draw_rect(fe, COORD(x)+1, COORD(y+1), 1, 1, COL_INK);
1075 draw_rect(fe, COORD(x+1)-1, COORD(y+1), 1, 1, COL_INK);
1077 draw_rect(fe, COORD(x), COORD(y), 1, 1, COL_INK);
1079 draw_rect(fe, COORD(x+1), COORD(y), 1, 1, COL_INK);
1081 draw_rect(fe, COORD(x), COORD(y+1), 1, 1, COL_INK);
1083 draw_rect(fe, COORD(x+1), COORD(y+1), 1, 1, COL_INK);
1086 * And finally the clues at the corners.
1088 for (xx = x; xx <= x+1; xx++)
1089 for (yy = y; yy <= y+1; yy++)
1090 draw_clue(fe, ds, xx, yy, clues->clues[yy*W+xx]);
1093 draw_update(fe, COORD(x), COORD(y), TILESIZE+1, TILESIZE+1);
1096 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1097 game_state *state, int dir, game_ui *ui,
1098 float animtime, float flashtime)
1100 int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
1105 flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
1111 game_compute_size(&state->p, TILESIZE, &ww, &wh);
1112 draw_rect(fe, 0, 0, ww, wh, COL_BACKGROUND);
1113 draw_update(fe, 0, 0, ww, wh);
1116 * Draw any clues on the very edges (since normal tile
1117 * redraw won't draw the bits outside the grid boundary).
1119 for (y = 0; y < H; y++) {
1120 draw_clue(fe, ds, 0, y, state->clues->clues[y*W+0]);
1121 draw_clue(fe, ds, w, y, state->clues->clues[y*W+w]);
1123 for (x = 0; x < W; x++) {
1124 draw_clue(fe, ds, x, 0, state->clues->clues[0*W+x]);
1125 draw_clue(fe, ds, x, h, state->clues->clues[h*W+x]);
1132 * Loop over the grid and work out where all the slashes are.
1133 * We need to do this because a slash in one square affects the
1134 * drawing of the next one along.
1136 for (y = 0; y < h; y++)
1137 for (x = 0; x < w; x++)
1138 ds->todraw[y*w+x] = flashing ? FLASH : 0;
1140 for (y = 0; y < h; y++) {
1141 for (x = 0; x < w; x++) {
1142 if (state->soln[y*w+x] < 0) {
1143 ds->todraw[y*w+x] |= BACKSLASH;
1145 ds->todraw[y*w+(x-1)] |= R_T | C_TR;
1147 ds->todraw[y*w+(x+1)] |= L_B | C_BL;
1149 ds->todraw[(y-1)*w+x] |= B_L | C_BL;
1151 ds->todraw[(y+1)*w+x] |= T_R | C_TR;
1153 ds->todraw[(y-1)*w+(x-1)] |= C_BR;
1154 if (x+1 < w && y+1 < h)
1155 ds->todraw[(y+1)*w+(x+1)] |= C_TL;
1156 } else if (state->soln[y*w+x] > 0) {
1157 ds->todraw[y*w+x] |= FORWSLASH;
1159 ds->todraw[y*w+(x-1)] |= R_B | C_BR;
1161 ds->todraw[y*w+(x+1)] |= L_T | C_TL;
1163 ds->todraw[(y-1)*w+x] |= B_R | C_BR;
1165 ds->todraw[(y+1)*w+x] |= T_L | C_TL;
1166 if (x > 0 && y+1 < h)
1167 ds->todraw[(y+1)*w+(x-1)] |= C_TR;
1168 if (x+1 < w && y > 0)
1169 ds->todraw[(y-1)*w+(x+1)] |= C_BL;
1175 * Now go through and draw the grid squares.
1177 for (y = 0; y < h; y++) {
1178 for (x = 0; x < w; x++) {
1179 if (ds->todraw[y*w+x] != ds->grid[y*w+x]) {
1180 draw_tile(fe, ds, state->clues, x, y, ds->todraw[y*w+x]);
1181 ds->grid[y*w+x] = ds->todraw[y*w+x];
1187 static float game_anim_length(game_state *oldstate, game_state *newstate,
1188 int dir, game_ui *ui)
1193 static float game_flash_length(game_state *oldstate, game_state *newstate,
1194 int dir, game_ui *ui)
1196 if (!oldstate->completed && newstate->completed &&
1197 !oldstate->used_solve && !newstate->used_solve)
1203 static int game_wants_statusbar(void)
1208 static int game_timing_state(game_state *state, game_ui *ui)
1214 #define thegame slant
1217 const struct game thegame = {
1218 "Slant", "games.slant",
1225 TRUE, game_configure, custom_params,
1233 TRUE, game_text_format,
1241 PREFERRED_TILESIZE, game_compute_size, game_set_size,
1244 game_free_drawstate,
1248 game_wants_statusbar,
1249 FALSE, game_timing_state,
1250 0, /* mouse_priorities */