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 static 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 static 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
)
633 assert(state
->clues
);
634 if (--state
->clues
->refcount
<= 0) {
635 sfree(state
->clues
->clues
);
636 sfree(state
->clues
->dsf
);
642 static int check_completion(game_state
*state
)
644 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
648 * Establish a disjoint set forest for tracking connectedness
649 * between grid points. Use the dsf scratch space in the shared
650 * clues structure, to avoid mallocing too often.
652 for (i
= 0; i
< W
*H
; i
++)
653 state
->clues
->dsf
[i
] = i
; /* initially all distinct */
656 * Now go through the grid checking connectedness. While we're
657 * here, also check that everything is filled in.
659 for (y
= 0; y
< h
; y
++)
660 for (x
= 0; x
< w
; x
++) {
663 if (state
->soln
[y
*w
+x
] == 0)
665 if (state
->soln
[y
*w
+x
] < 0) {
674 * Our edge connects i1 with i2. If they're already
675 * connected, return failure. Otherwise, link them.
677 if (dsf_canonify(state
->clues
->dsf
, i1
) ==
678 dsf_canonify(state
->clues
->dsf
, i2
))
681 dsf_merge(state
->clues
->dsf
, i1
, i2
);
685 * The grid is _a_ valid grid; let's see if it matches the
688 for (y
= 0; y
< H
; y
++)
689 for (x
= 0; x
< W
; x
++) {
692 if ((c
= state
->clues
->clues
[y
*W
+x
]) < 0)
697 if (x
> 0 && y
> 0 && state
->soln
[(y
-1)*w
+(x
-1)] == -1) v
++;
698 if (x
> 0 && y
< h
&& state
->soln
[y
*w
+(x
-1)] == +1) v
++;
699 if (x
< w
&& y
> 0 && state
->soln
[(y
-1)*w
+x
] == +1) v
++;
700 if (x
< w
&& y
< h
&& state
->soln
[y
*w
+x
] == -1) v
++;
709 static char *solve_game(game_state
*state
, game_state
*currstate
,
710 char *aux
, char **error
)
712 int w
= state
->p
.w
, h
= state
->p
.h
;
715 int free_soln
= FALSE
;
717 int movelen
, movesize
;
722 * If we already have the solution, save ourselves some
725 soln
= (signed char *)aux
;
726 bs
= (signed char)'\\';
729 struct solver_scratch
*sc
= new_scratch(w
, h
);
730 soln
= snewn(w
*h
, signed char);
732 ret
= slant_solve(w
, h
, state
->clues
->clues
, soln
, sc
);
737 *error
= "This puzzle is not self-consistent";
739 *error
= "Unable to find a unique solution for this puzzle";
746 * Construct a move string which turns the current state into
750 move
= snewn(movesize
, char);
752 move
[movelen
++] = 'S';
753 move
[movelen
] = '\0';
754 for (y
= 0; y
< h
; y
++)
755 for (x
= 0; x
< w
; x
++) {
756 int v
= (soln
[y
*w
+x
] == bs ?
-1 : +1);
757 if (state
->soln
[y
*w
+x
] != v
) {
758 int len
= sprintf(buf
, ";%c%d,%d", (int)(v
< 0 ?
'\\' : '/'), x
, y
);
759 if (movelen
+ len
>= movesize
) {
760 movesize
= movelen
+ len
+ 256;
761 move
= sresize(move
, movesize
, char);
763 strcpy(move
+ movelen
, buf
);
774 static char *game_text_format(game_state
*state
)
776 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
781 * There are h+H rows of w+W columns.
783 len
= (h
+H
) * (w
+W
+1) + 1;
784 ret
= snewn(len
, char);
787 for (y
= 0; y
< H
; y
++) {
788 for (x
= 0; x
< W
; x
++) {
789 if (state
->clues
->clues
[y
*W
+x
] >= 0)
790 *p
++ = state
->clues
->clues
[y
*W
+x
] + '0';
798 for (x
= 0; x
< W
; x
++) {
801 if (state
->soln
[y
*w
+x
] != 0)
802 *p
++ = (state
->soln
[y
*w
+x
] < 0 ?
'\\' : '/');
812 assert(p
- ret
== len
);
816 static game_ui
*new_ui(game_state
*state
)
821 static void free_ui(game_ui
*ui
)
825 static char *encode_ui(game_ui
*ui
)
830 static void decode_ui(game_ui
*ui
, char *encoding
)
834 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
835 game_state
*newstate
)
839 #define PREFERRED_TILESIZE 32
840 #define TILESIZE (ds->tilesize)
841 #define BORDER TILESIZE
842 #define CLUE_RADIUS (TILESIZE / 3)
843 #define CLUE_TEXTSIZE (TILESIZE / 2)
844 #define COORD(x) ( (x) * TILESIZE + BORDER )
845 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
847 #define FLASH_TIME 0.30F
850 * Bit fields in the `grid' and `todraw' elements of the drawstate.
852 #define BACKSLASH 0x0001
853 #define FORWSLASH 0x0002
868 struct game_drawstate
{
875 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
876 int x
, int y
, int button
)
878 int w
= state
->p
.w
, h
= state
->p
.h
;
880 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
886 if (x
< 0 || y
< 0 || x
>= w
|| y
>= h
)
889 if (button
== LEFT_BUTTON
) {
891 * Left-clicking cycles blank -> \ -> / -> blank.
893 v
= state
->soln
[y
*w
+x
] - 1;
898 * Right-clicking cycles blank -> / -> \ -> blank.
900 v
= state
->soln
[y
*w
+x
] + 1;
905 sprintf(buf
, "%c%d,%d", (int)(v
==-1 ?
'\\' : v
==+1 ?
'/' : 'C'), x
, y
);
912 static game_state
*execute_move(game_state
*state
, char *move
)
914 int w
= state
->p
.w
, h
= state
->p
.h
;
917 game_state
*ret
= dup_game(state
);
922 ret
->used_solve
= TRUE
;
924 } else if (c
== '\\' || c
== '/' || c
== 'C') {
926 if (sscanf(move
, "%d,%d%n", &x
, &y
, &n
) != 2 ||
927 x
< 0 || y
< 0 || x
>= w
|| y
>= h
) {
931 ret
->soln
[y
*w
+x
] = (c
== '\\' ?
-1 : c
== '/' ?
+1 : 0);
946 ret
->completed
= check_completion(ret
);
951 /* ----------------------------------------------------------------------
955 static void game_compute_size(game_params
*params
, int tilesize
,
958 /* fool the macros */
959 struct dummy
{ int tilesize
; } dummy
= { tilesize
}, *ds
= &dummy
;
961 *x
= 2 * BORDER
+ params
->w
* TILESIZE
+ 1;
962 *y
= 2 * BORDER
+ params
->h
* TILESIZE
+ 1;
965 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
968 ds
->tilesize
= tilesize
;
971 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
973 float *ret
= snewn(3 * NCOLOURS
, float);
975 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
977 ret
[COL_GRID
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.7F
;
978 ret
[COL_GRID
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 0.7F
;
979 ret
[COL_GRID
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 0.7F
;
981 ret
[COL_INK
* 3 + 0] = 0.0F
;
982 ret
[COL_INK
* 3 + 1] = 0.0F
;
983 ret
[COL_INK
* 3 + 2] = 0.0F
;
985 *ncolours
= NCOLOURS
;
989 static game_drawstate
*game_new_drawstate(game_state
*state
)
991 int w
= state
->p
.w
, h
= state
->p
.h
;
993 struct game_drawstate
*ds
= snew(struct game_drawstate
);
997 ds
->grid
= snewn(w
*h
, int);
998 ds
->todraw
= snewn(w
*h
, int);
999 for (i
= 0; i
< w
*h
; i
++)
1000 ds
->grid
[i
] = ds
->todraw
[i
] = -1;
1005 static void game_free_drawstate(game_drawstate
*ds
)
1012 static void draw_clue(frontend
*fe
, game_drawstate
*ds
,
1013 int x
, int y
, int v
)
1022 draw_circle(fe
, COORD(x
), COORD(y
), CLUE_RADIUS
,
1023 COL_BACKGROUND
, COL_INK
);
1024 draw_text(fe
, COORD(x
), COORD(y
), FONT_VARIABLE
,
1025 CLUE_TEXTSIZE
, ALIGN_VCENTRE
|ALIGN_HCENTRE
,
1029 static void draw_tile(frontend
*fe
, game_drawstate
*ds
, game_clues
*clues
,
1030 int x
, int y
, int v
)
1032 int w
= clues
->w
/*, h = clues->h*/, W
= w
+1 /*, H = h+1 */;
1035 clip(fe
, COORD(x
), COORD(y
), TILESIZE
+1, TILESIZE
+1);
1037 draw_rect(fe
, COORD(x
), COORD(y
), TILESIZE
, TILESIZE
,
1038 (v
& FLASH
) ? COL_GRID
: COL_BACKGROUND
);
1041 * Draw the grid lines.
1043 draw_line(fe
, COORD(x
), COORD(y
), COORD(x
+1), COORD(y
), COL_GRID
);
1044 draw_line(fe
, COORD(x
), COORD(y
+1), COORD(x
+1), COORD(y
+1), COL_GRID
);
1045 draw_line(fe
, COORD(x
), COORD(y
), COORD(x
), COORD(y
+1), COL_GRID
);
1046 draw_line(fe
, COORD(x
+1), COORD(y
), COORD(x
+1), COORD(y
+1), COL_GRID
);
1051 if (v
& BACKSLASH
) {
1052 draw_line(fe
, COORD(x
), COORD(y
), COORD(x
+1), COORD(y
+1), COL_INK
);
1053 draw_line(fe
, COORD(x
)+1, COORD(y
), COORD(x
+1), COORD(y
+1)-1,
1055 draw_line(fe
, COORD(x
), COORD(y
)+1, COORD(x
+1)-1, COORD(y
+1),
1057 } else if (v
& FORWSLASH
) {
1058 draw_line(fe
, COORD(x
+1), COORD(y
), COORD(x
), COORD(y
+1), COL_INK
);
1059 draw_line(fe
, COORD(x
+1)-1, COORD(y
), COORD(x
), COORD(y
+1)-1,
1061 draw_line(fe
, COORD(x
+1), COORD(y
)+1, COORD(x
)+1, COORD(y
+1),
1066 * Draw dots on the grid corners that appear if a slash is in a
1067 * neighbouring cell.
1070 draw_rect(fe
, COORD(x
), COORD(y
)+1, 1, 1, COL_INK
);
1072 draw_rect(fe
, COORD(x
), COORD(y
+1)-1, 1, 1, COL_INK
);
1074 draw_rect(fe
, COORD(x
+1), COORD(y
)+1, 1, 1, COL_INK
);
1076 draw_rect(fe
, COORD(x
+1), COORD(y
+1)-1, 1, 1, COL_INK
);
1078 draw_rect(fe
, COORD(x
)+1, COORD(y
), 1, 1, COL_INK
);
1080 draw_rect(fe
, COORD(x
+1)-1, COORD(y
), 1, 1, COL_INK
);
1082 draw_rect(fe
, COORD(x
)+1, COORD(y
+1), 1, 1, COL_INK
);
1084 draw_rect(fe
, COORD(x
+1)-1, COORD(y
+1), 1, 1, COL_INK
);
1086 draw_rect(fe
, COORD(x
), COORD(y
), 1, 1, COL_INK
);
1088 draw_rect(fe
, COORD(x
+1), COORD(y
), 1, 1, COL_INK
);
1090 draw_rect(fe
, COORD(x
), COORD(y
+1), 1, 1, COL_INK
);
1092 draw_rect(fe
, COORD(x
+1), COORD(y
+1), 1, 1, COL_INK
);
1095 * And finally the clues at the corners.
1097 for (xx
= x
; xx
<= x
+1; xx
++)
1098 for (yy
= y
; yy
<= y
+1; yy
++)
1099 draw_clue(fe
, ds
, xx
, yy
, clues
->clues
[yy
*W
+xx
]);
1102 draw_update(fe
, COORD(x
), COORD(y
), TILESIZE
+1, TILESIZE
+1);
1105 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1106 game_state
*state
, int dir
, game_ui
*ui
,
1107 float animtime
, float flashtime
)
1109 int w
= state
->p
.w
, h
= state
->p
.h
, W
= w
+1, H
= h
+1;
1114 flashing
= (int)(flashtime
* 3 / FLASH_TIME
) != 1;
1120 game_compute_size(&state
->p
, TILESIZE
, &ww
, &wh
);
1121 draw_rect(fe
, 0, 0, ww
, wh
, COL_BACKGROUND
);
1122 draw_update(fe
, 0, 0, ww
, wh
);
1125 * Draw any clues on the very edges (since normal tile
1126 * redraw won't draw the bits outside the grid boundary).
1128 for (y
= 0; y
< H
; y
++) {
1129 draw_clue(fe
, ds
, 0, y
, state
->clues
->clues
[y
*W
+0]);
1130 draw_clue(fe
, ds
, w
, y
, state
->clues
->clues
[y
*W
+w
]);
1132 for (x
= 0; x
< W
; x
++) {
1133 draw_clue(fe
, ds
, x
, 0, state
->clues
->clues
[0*W
+x
]);
1134 draw_clue(fe
, ds
, x
, h
, state
->clues
->clues
[h
*W
+x
]);
1141 * Loop over the grid and work out where all the slashes are.
1142 * We need to do this because a slash in one square affects the
1143 * drawing of the next one along.
1145 for (y
= 0; y
< h
; y
++)
1146 for (x
= 0; x
< w
; x
++)
1147 ds
->todraw
[y
*w
+x
] = flashing ? FLASH
: 0;
1149 for (y
= 0; y
< h
; y
++) {
1150 for (x
= 0; x
< w
; x
++) {
1151 if (state
->soln
[y
*w
+x
] < 0) {
1152 ds
->todraw
[y
*w
+x
] |= BACKSLASH
;
1154 ds
->todraw
[y
*w
+(x
-1)] |= R_T
| C_TR
;
1156 ds
->todraw
[y
*w
+(x
+1)] |= L_B
| C_BL
;
1158 ds
->todraw
[(y
-1)*w
+x
] |= B_L
| C_BL
;
1160 ds
->todraw
[(y
+1)*w
+x
] |= T_R
| C_TR
;
1162 ds
->todraw
[(y
-1)*w
+(x
-1)] |= C_BR
;
1163 if (x
+1 < w
&& y
+1 < h
)
1164 ds
->todraw
[(y
+1)*w
+(x
+1)] |= C_TL
;
1165 } else if (state
->soln
[y
*w
+x
] > 0) {
1166 ds
->todraw
[y
*w
+x
] |= FORWSLASH
;
1168 ds
->todraw
[y
*w
+(x
-1)] |= R_B
| C_BR
;
1170 ds
->todraw
[y
*w
+(x
+1)] |= L_T
| C_TL
;
1172 ds
->todraw
[(y
-1)*w
+x
] |= B_R
| C_BR
;
1174 ds
->todraw
[(y
+1)*w
+x
] |= T_L
| C_TL
;
1175 if (x
> 0 && y
+1 < h
)
1176 ds
->todraw
[(y
+1)*w
+(x
-1)] |= C_TR
;
1177 if (x
+1 < w
&& y
> 0)
1178 ds
->todraw
[(y
-1)*w
+(x
+1)] |= C_BL
;
1184 * Now go through and draw the grid squares.
1186 for (y
= 0; y
< h
; y
++) {
1187 for (x
= 0; x
< w
; x
++) {
1188 if (ds
->todraw
[y
*w
+x
] != ds
->grid
[y
*w
+x
]) {
1189 draw_tile(fe
, ds
, state
->clues
, x
, y
, ds
->todraw
[y
*w
+x
]);
1190 ds
->grid
[y
*w
+x
] = ds
->todraw
[y
*w
+x
];
1196 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1197 int dir
, game_ui
*ui
)
1202 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1203 int dir
, game_ui
*ui
)
1205 if (!oldstate
->completed
&& newstate
->completed
&&
1206 !oldstate
->used_solve
&& !newstate
->used_solve
)
1212 static int game_wants_statusbar(void)
1217 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1223 #define thegame slant
1226 const struct game thegame
= {
1227 "Slant", "games.slant",
1234 TRUE
, game_configure
, custom_params
,
1242 TRUE
, game_text_format
,
1250 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1253 game_free_drawstate
,
1257 game_wants_statusbar
,
1258 FALSE
, game_timing_state
,
1259 0, /* mouse_priorities */