2 * flip.c: Puzzle involving lighting up all the squares on a grid,
3 * where each click toggles an overlapping set of lights.
26 #define PREFERRED_TILE_SIZE 48
27 #define TILE_SIZE (ds->tilesize)
28 #define BORDER (TILE_SIZE / 2)
29 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
30 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
32 #define ANIM_TIME 0.25F
33 #define FLASH_FRAME 0.07F
36 * Possible ways to decide which lights are toggled by each click.
37 * Essentially, each of these describes a means of inventing a
50 * This structure is shared between all the game_states describing
51 * a particular game, so it's reference-counted.
55 unsigned char *matrix
; /* array of (w*h) by (w*h) */
60 int moves
, completed
, cheated
, hints_active
;
61 unsigned char *grid
; /* array of w*h */
62 struct matrix
*matrix
;
65 static game_params
*default_params(void)
67 game_params
*ret
= snew(game_params
);
70 ret
->matrix_type
= CROSSES
;
75 static const struct game_params flip_presets
[] = {
84 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
89 if (i
< 0 || i
>= lenof(flip_presets
))
92 ret
= snew(game_params
);
93 *ret
= flip_presets
[i
];
95 sprintf(str
, "%dx%d %s", ret
->w
, ret
->h
,
96 ret
->matrix_type
== CROSSES ?
"Crosses" : "Random");
103 static void free_params(game_params
*params
)
108 static game_params
*dup_params(game_params
*params
)
110 game_params
*ret
= snew(game_params
);
111 *ret
= *params
; /* structure copy */
115 static void decode_params(game_params
*ret
, char const *string
)
117 ret
->w
= ret
->h
= atoi(string
);
118 while (*string
&& isdigit(*string
)) string
++;
119 if (*string
== 'x') {
121 ret
->h
= atoi(string
);
122 while (*string
&& isdigit(*string
)) string
++;
124 if (*string
== 'r') {
126 ret
->matrix_type
= RANDOM
;
127 } else if (*string
== 'c') {
129 ret
->matrix_type
= CROSSES
;
133 static char *encode_params(game_params
*params
, int full
)
137 sprintf(data
, "%dx%d%s", params
->w
, params
->h
,
138 !full ?
"" : params
->matrix_type
== CROSSES ?
"c" : "r");
143 static config_item
*game_configure(game_params
*params
)
145 config_item
*ret
= snewn(4, config_item
);
148 ret
[0].name
= "Width";
149 ret
[0].type
= C_STRING
;
150 sprintf(buf
, "%d", params
->w
);
151 ret
[0].sval
= dupstr(buf
);
154 ret
[1].name
= "Height";
155 ret
[1].type
= C_STRING
;
156 sprintf(buf
, "%d", params
->h
);
157 ret
[1].sval
= dupstr(buf
);
160 ret
[2].name
= "Shape type";
161 ret
[2].type
= C_CHOICES
;
162 ret
[2].sval
= ":Crosses:Random";
163 ret
[2].ival
= params
->matrix_type
;
173 static game_params
*custom_params(config_item
*cfg
)
175 game_params
*ret
= snew(game_params
);
177 ret
->w
= atoi(cfg
[0].sval
);
178 ret
->h
= atoi(cfg
[1].sval
);
179 ret
->matrix_type
= cfg
[2].ival
;
184 static char *validate_params(game_params
*params
)
186 if (params
->w
<= 0 || params
->h
<= 0)
187 return "Width and height must both be greater than zero";
191 static char *encode_bitmap(unsigned char *bmp
, int len
)
193 int slen
= (len
+ 3) / 4;
197 ret
= snewn(slen
+ 1, char);
198 for (i
= 0; i
< slen
; i
++) {
201 for (j
= 0; j
< 4; j
++)
202 if (i
*4+j
< len
&& bmp
[i
*4+j
])
204 ret
[i
] = "0123456789abcdef"[v
];
210 static void decode_bitmap(unsigned char *bmp
, int len
, char *hex
)
212 int slen
= (len
+ 3) / 4;
215 for (i
= 0; i
< slen
; i
++) {
216 int j
, v
, c
= hex
[i
];
217 if (c
>= '0' && c
<= '9')
219 else if (c
>= 'A' && c
<= 'F')
221 else if (c
>= 'a' && c
<= 'f')
224 v
= 0; /* shouldn't happen */
225 for (j
= 0; j
< 4; j
++) {
237 * Structure used during random matrix generation, and a compare
238 * function to permit storage in a tree234.
241 int cx
, cy
; /* coords of click square */
242 int x
, y
; /* coords of output square */
244 * Number of click squares which currently affect this output
249 * Number of output squares currently affected by this click
254 #define SORT(field) do { \
255 if (a->field < b->field) \
257 else if (a->field > b->field) \
261 * Compare function for choosing the next square to add. We must
262 * sort by coverage, then by omino size, then everything else.
264 static int sqcmp_pick(void *av
, void *bv
)
266 struct sq
*a
= (struct sq
*)av
;
267 struct sq
*b
= (struct sq
*)bv
;
277 * Compare function for adjusting the coverage figures after a
278 * change. We sort first by coverage and output square, then by
281 static int sqcmp_cov(void *av
, void *bv
)
283 struct sq
*a
= (struct sq
*)av
;
284 struct sq
*b
= (struct sq
*)bv
;
294 * Compare function for adjusting the omino sizes after a change.
295 * We sort first by omino size and input square, then by everything
298 static int sqcmp_osize(void *av
, void *bv
)
300 struct sq
*a
= (struct sq
*)av
;
301 struct sq
*b
= (struct sq
*)bv
;
310 static void addsq(tree234
*t
, int w
, int h
, int cx
, int cy
,
311 int x
, int y
, unsigned char *matrix
)
317 if (x
< 0 || x
>= w
|| y
< 0 || y
>= h
)
319 if (abs(x
-cx
) > 1 || abs(y
-cy
) > 1)
321 if (matrix
[(cy
*w
+cx
) * wh
+ y
*w
+x
])
324 sq
= snew(struct sq
);
329 sq
->coverage
= sq
->ominosize
= 0;
330 for (i
= 0; i
< wh
; i
++) {
331 if (matrix
[i
* wh
+ y
*w
+x
])
333 if (matrix
[(cy
*w
+cx
) * wh
+ i
])
337 if (add234(t
, sq
) != sq
)
338 sfree(sq
); /* already there */
340 static void addneighbours(tree234
*t
, int w
, int h
, int cx
, int cy
,
341 int x
, int y
, unsigned char *matrix
)
343 addsq(t
, w
, h
, cx
, cy
, x
-1, y
, matrix
);
344 addsq(t
, w
, h
, cx
, cy
, x
+1, y
, matrix
);
345 addsq(t
, w
, h
, cx
, cy
, x
, y
-1, matrix
);
346 addsq(t
, w
, h
, cx
, cy
, x
, y
+1, matrix
);
349 static char *new_game_desc(game_params
*params
, random_state
*rs
,
350 game_aux_info
**aux
, int interactive
)
352 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
354 unsigned char *matrix
, *grid
;
355 char *mbmp
, *gbmp
, *ret
;
357 matrix
= snewn(wh
* wh
, unsigned char);
358 grid
= snewn(wh
, unsigned char);
361 * First set up the matrix.
363 switch (params
->matrix_type
) {
365 for (i
= 0; i
< wh
; i
++) {
366 int ix
= i
% w
, iy
= i
/ w
;
367 for (j
= 0; j
< wh
; j
++) {
368 int jx
= j
% w
, jy
= j
/ w
;
369 if (abs(jx
- ix
) + abs(jy
- iy
) <= 1)
378 tree234
*pick
, *cov
, *osize
;
381 pick
= newtree234(sqcmp_pick
);
382 cov
= newtree234(sqcmp_cov
);
383 osize
= newtree234(sqcmp_osize
);
385 memset(matrix
, 0, wh
* wh
);
386 for (i
= 0; i
< wh
; i
++) {
390 for (i
= 0; i
< wh
; i
++) {
391 int ix
= i
% w
, iy
= i
/ w
;
392 addneighbours(pick
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
393 addneighbours(cov
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
394 addneighbours(osize
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
398 * Repeatedly choose a square to add to the matrix,
399 * until we have enough. I'll arbitrarily choose our
400 * limit to be the same as the total number of set bits
401 * in the crosses matrix.
403 limit
= 4*wh
- 2*(w
+h
); /* centre squares already present */
405 while (limit
-- > 0) {
406 struct sq
*sq
, *sq2
, sqlocal
;
410 * Find the lowest element in the pick tree.
412 sq
= index234(pick
, 0);
415 * Find the highest element with the same coverage
416 * and omino size, by setting all other elements to
420 sqlocal
.cx
= sqlocal
.cy
= sqlocal
.x
= sqlocal
.y
= wh
;
421 sq
= findrelpos234(pick
, &sqlocal
, NULL
, REL234_LT
, &k
);
425 * Pick at random from all elements up to k of the
428 k
= random_upto(rs
, k
+1);
429 sq
= delpos234(pick
, k
);
434 * Add this square to the matrix.
436 matrix
[(sq
->cy
* w
+ sq
->cx
) * wh
+ (sq
->y
* w
+ sq
->x
)] = 1;
439 * Correct the matrix coverage field of any sq
440 * which points at this output square.
443 sqlocal
.cx
= sqlocal
.cy
= sqlocal
.ominosize
= -1;
444 while ((sq2
= findrel234(cov
, &sqlocal
, NULL
,
445 REL234_GT
)) != NULL
&&
446 sq2
->coverage
== sq
->coverage
&&
447 sq2
->x
== sq
->x
&& sq2
->y
== sq
->y
) {
458 * Correct the omino size field of any sq which
459 * points at this input square.
462 sqlocal
.x
= sqlocal
.y
= sqlocal
.coverage
= -1;
463 while ((sq2
= findrel234(osize
, &sqlocal
, NULL
,
464 REL234_GT
)) != NULL
&&
465 sq2
->ominosize
== sq
->ominosize
&&
466 sq2
->cx
== sq
->cx
&& sq2
->cy
== sq
->cy
) {
477 * The sq we actually picked out of the tree is
478 * finished with; but its neighbours now need to
481 addneighbours(pick
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
482 addneighbours(cov
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
483 addneighbours(osize
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
488 * Free all remaining sq structures.
492 while ((sq
= delpos234(pick
, 0)) != NULL
)
500 * Finally, check to see if any two matrix rows are
501 * exactly identical. If so, this is not an acceptable
502 * matrix, and we give up and go round again.
504 * I haven't been immediately able to think of a
505 * plausible means of algorithmically avoiding this
506 * situation (by, say, making a small perturbation to
507 * an offending matrix), so for the moment I'm just
508 * going to deal with it by throwing the whole thing
509 * away. I suspect this will lead to scalability
510 * problems (since most of the things happening in
511 * these matrices are local, the chance of _some_
512 * neighbourhood having two identical regions will
513 * increase with the grid area), but so far this puzzle
514 * seems to be really hard at large sizes so I'm not
515 * massively worried yet. Anyone needs this done
516 * better, they're welcome to submit a patch.
518 for (i
= 0; i
< wh
; i
++) {
519 for (j
= 0; j
< wh
; j
++)
521 !memcmp(matrix
+ i
* wh
, matrix
+ j
* wh
, wh
))
527 break; /* no matches found */
533 * Now invent a random initial set of lights.
535 * At first glance it looks as if it might be quite difficult
536 * to choose equiprobably from all soluble light sets. After
537 * all, soluble light sets are those in the image space of the
538 * transformation matrix; so first we'd have to identify that
539 * space and its dimension, then pick a random coordinate for
540 * each basis vector and recombine. Lot of fiddly matrix
543 * However, vector spaces are nicely orthogonal and relieve us
544 * of all that difficulty. For every point in the image space,
545 * there are precisely as many points in the input space that
546 * map to it as there are elements in the kernel of the
547 * transformation matrix (because adding any kernel element to
548 * the input does not change the output, and because any two
549 * inputs mapping to the same output must differ by an element
550 * of the kernel because that's what the kernel _is_); and
551 * these cosets are all disjoint (obviously, since no input
552 * point can map to more than one output point) and cover the
553 * whole space (equally obviously, because no input point can
554 * map to fewer than one output point!).
556 * So the input space contains the same number of points for
557 * each point in the output space; thus, we can simply choose
558 * equiprobably from elements of the _input_ space, and filter
559 * the result through the transformation matrix in the obvious
560 * way, and we thereby guarantee to choose equiprobably from
561 * all the output points. Phew!
565 for (i
= 0; i
< wh
; i
++) {
566 int v
= random_upto(rs
, 2);
568 for (j
= 0; j
< wh
; j
++)
569 grid
[j
] ^= matrix
[i
*wh
+j
];
573 * Ensure we don't have the starting state already!
575 for (i
= 0; i
< wh
; i
++)
583 * Now encode the matrix and the starting grid as a game
584 * description. We'll do this by concatenating two great big
587 mbmp
= encode_bitmap(matrix
, wh
*wh
);
588 gbmp
= encode_bitmap(grid
, wh
);
589 ret
= snewn(strlen(mbmp
) + strlen(gbmp
) + 2, char);
590 sprintf(ret
, "%s,%s", mbmp
, gbmp
);
596 static void game_free_aux_info(game_aux_info
*aux
)
598 assert(!"Shouldn't happen");
601 static char *validate_desc(game_params
*params
, char *desc
)
603 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
604 int mlen
= (wh
*wh
+3)/4, glen
= (wh
+3)/4;
606 if (strspn(desc
, "0123456789abcdefABCDEF") != mlen
)
607 return "Matrix description is wrong length";
608 if (desc
[mlen
] != ',')
609 return "Expected comma after matrix description";
610 if (strspn(desc
+mlen
+1, "0123456789abcdefABCDEF") != glen
)
611 return "Grid description is wrong length";
612 if (desc
[mlen
+1+glen
])
613 return "Unexpected data after grid description";
618 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
620 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
621 int mlen
= (wh
*wh
+3)/4;
623 game_state
*state
= snew(game_state
);
627 state
->completed
= FALSE
;
628 state
->cheated
= FALSE
;
629 state
->hints_active
= FALSE
;
631 state
->matrix
= snew(struct matrix
);
632 state
->matrix
->refcount
= 1;
633 state
->matrix
->matrix
= snewn(wh
*wh
, unsigned char);
634 decode_bitmap(state
->matrix
->matrix
, wh
*wh
, desc
);
635 state
->grid
= snewn(wh
, unsigned char);
636 decode_bitmap(state
->grid
, wh
, desc
+ mlen
+ 1);
641 static game_state
*dup_game(game_state
*state
)
643 game_state
*ret
= snew(game_state
);
647 ret
->completed
= state
->completed
;
648 ret
->cheated
= state
->cheated
;
649 ret
->hints_active
= state
->hints_active
;
650 ret
->moves
= state
->moves
;
651 ret
->matrix
= state
->matrix
;
652 state
->matrix
->refcount
++;
653 ret
->grid
= snewn(ret
->w
* ret
->h
, unsigned char);
654 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
659 static void free_game(game_state
*state
)
662 if (--state
->matrix
->refcount
<= 0) {
663 sfree(state
->matrix
->matrix
);
664 sfree(state
->matrix
);
669 static void rowxor(unsigned char *row1
, unsigned char *row2
, int len
)
672 for (i
= 0; i
< len
; i
++)
676 static game_state
*solve_game(game_state
*state
, game_state
*currstate
,
677 game_aux_info
*aux
, char **error
)
679 int w
= state
->w
, h
= state
->h
, wh
= w
* h
;
680 unsigned char *equations
, *solution
, *shortest
;
682 int rowsdone
, colsdone
;
683 int i
, j
, k
, len
, bestlen
;
687 * Set up a list of simultaneous equations. Each one is of
688 * length (wh+1) and has wh coefficients followed by a value.
690 equations
= snewn((wh
+ 1) * wh
, unsigned char);
691 for (i
= 0; i
< wh
; i
++) {
692 for (j
= 0; j
< wh
; j
++)
693 equations
[i
* (wh
+1) + j
] = currstate
->matrix
->matrix
[j
*wh
+i
];
694 equations
[i
* (wh
+1) + wh
] = currstate
->grid
[i
] & 1;
698 * Perform Gaussian elimination over GF(2).
700 rowsdone
= colsdone
= 0;
702 und
= snewn(wh
, int);
705 * Find the leftmost column which has a 1 in it somewhere
706 * outside the first `rowsdone' rows.
709 for (i
= colsdone
; i
< wh
; i
++) {
710 for (j
= rowsdone
; j
< wh
; j
++)
711 if (equations
[j
* (wh
+1) + i
])
714 break; /* found one */
716 * This is a column which will not have an equation
717 * controlling it. Mark it as undetermined.
723 * If there wasn't one, then we've finished: all remaining
724 * equations are of the form 0 = constant. Check to see if
725 * any of them wants 0 to be equal to 1; this is the
726 * condition which indicates an insoluble problem
727 * (therefore _hopefully_ one typed in by a user!).
730 for (j
= rowsdone
; j
< wh
; j
++)
731 if (equations
[j
* (wh
+1) + wh
]) {
732 *error
= "No solution exists for this position";
740 * We've found a 1. It's in column i, and the topmost 1 in
741 * that column is in row j. Do a row-XOR to move it up to
742 * the topmost row if it isn't already there.
746 rowxor(equations
+ rowsdone
*(wh
+1), equations
+ j
*(wh
+1), wh
+1);
749 * Do row-XORs to eliminate that 1 from all rows below the
752 for (j
= rowsdone
+ 1; j
< wh
; j
++)
753 if (equations
[j
*(wh
+1) + i
])
754 rowxor(equations
+ j
*(wh
+1),
755 equations
+ rowsdone
*(wh
+1), wh
+1);
758 * Mark this row and column as done.
764 * If we've done all the rows, terminate.
766 } while (rowsdone
< wh
);
769 * If we reach here, we have the ability to produce a solution.
770 * So we go through _all_ possible solutions (each
771 * corresponding to a set of arbitrary choices of those
772 * components not directly determined by an equation), and pick
773 * one requiring the smallest number of flips.
775 solution
= snewn(wh
, unsigned char);
776 shortest
= snewn(wh
, unsigned char);
777 memset(solution
, 0, wh
);
781 * Find a solution based on the current values of the
782 * undetermined variables.
784 for (j
= rowsdone
; j
-- ;) {
788 * Find the leftmost set bit in this equation.
790 for (i
= 0; i
< wh
; i
++)
791 if (equations
[j
* (wh
+1) + i
])
793 assert(i
< wh
); /* there must have been one! */
796 * Compute this variable using the rest.
798 v
= equations
[j
* (wh
+1) + wh
];
799 for (k
= i
+1; k
< wh
; k
++)
800 if (equations
[j
* (wh
+1) + k
])
807 * Compare this solution to the current best one, and
808 * replace the best one if this one is shorter.
811 for (i
= 0; i
< wh
; i
++)
816 memcpy(shortest
, solution
, wh
);
820 * Now increment the binary number given by the
821 * undetermined variables: turn all 1s into 0s until we see
822 * a 0, at which point we turn it into a 1.
824 for (i
= 0; i
< nund
; i
++) {
825 solution
[und
[i
]] = !solution
[und
[i
]];
826 if (solution
[und
[i
]])
831 * If we didn't find a 0 at any point, we have wrapped
832 * round and are back at the start, i.e. we have enumerated
840 * We have a solution. Produce a game state with the solution
841 * marked in annotations.
843 ret
= dup_game(currstate
);
844 ret
->hints_active
= TRUE
;
846 for (i
= 0; i
< wh
; i
++) {
859 static char *game_text_format(game_state
*state
)
864 static game_ui
*new_ui(game_state
*state
)
869 static void free_ui(game_ui
*ui
)
873 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
874 game_state
*newstate
)
878 struct game_drawstate
{
880 unsigned char *tiles
;
884 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
885 int x
, int y
, int button
)
887 int w
= from
->w
, h
= from
->h
, wh
= w
* h
;
890 if (button
== LEFT_BUTTON
) {
891 int tx
= FROMCOORD(x
), ty
= FROMCOORD(y
);
892 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< h
) {
895 ret
= dup_game(from
);
903 for (j
= 0; j
< wh
; j
++) {
904 ret
->grid
[j
] ^= ret
->matrix
->matrix
[i
*wh
+j
];
905 if (ret
->grid
[j
] & 1)
908 ret
->grid
[i
] ^= 2; /* toggle hint */
910 ret
->completed
= TRUE
;
911 ret
->hints_active
= FALSE
;
921 /* ----------------------------------------------------------------------
925 static void game_size(game_params
*params
, game_drawstate
*ds
,
926 int *x
, int *y
, int expand
)
930 * Each window dimension equals the tile size times one more
931 * than the grid dimension (the border is half the width of the
934 tsx
= *x
/ (params
->w
+ 1);
935 tsy
= *y
/ (params
->h
+ 1);
940 ds
->tilesize
= min(ts
, PREFERRED_TILE_SIZE
);
942 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
943 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
946 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
948 float *ret
= snewn(3 * NCOLOURS
, float);
950 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
952 ret
[COL_WRONG
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] / 3;
953 ret
[COL_WRONG
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] / 3;
954 ret
[COL_WRONG
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] / 3;
956 ret
[COL_RIGHT
* 3 + 0] = 1.0F
;
957 ret
[COL_RIGHT
* 3 + 1] = 1.0F
;
958 ret
[COL_RIGHT
* 3 + 2] = 1.0F
;
960 ret
[COL_GRID
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] / 1.5F
;
961 ret
[COL_GRID
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] / 1.5F
;
962 ret
[COL_GRID
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] / 1.5F
;
964 ret
[COL_DIAG
* 3 + 0] = ret
[COL_GRID
* 3 + 0];
965 ret
[COL_DIAG
* 3 + 1] = ret
[COL_GRID
* 3 + 1];
966 ret
[COL_DIAG
* 3 + 2] = ret
[COL_GRID
* 3 + 2];
968 ret
[COL_HINT
* 3 + 0] = 1.0F
;
969 ret
[COL_HINT
* 3 + 1] = 0.0F
;
970 ret
[COL_HINT
* 3 + 2] = 0.0F
;
972 *ncolours
= NCOLOURS
;
976 static game_drawstate
*game_new_drawstate(game_state
*state
)
978 struct game_drawstate
*ds
= snew(struct game_drawstate
);
984 ds
->tiles
= snewn(ds
->w
*ds
->h
, unsigned char);
985 ds
->tilesize
= 0; /* haven't decided yet */
986 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
992 static void game_free_drawstate(game_drawstate
*ds
)
998 static void draw_tile(frontend
*fe
, game_drawstate
*ds
,
999 game_state
*state
, int x
, int y
, int tile
, int anim
,
1002 int w
= ds
->w
, h
= ds
->h
, wh
= w
* h
;
1003 int bx
= x
* TILE_SIZE
+ BORDER
, by
= y
* TILE_SIZE
+ BORDER
;
1006 clip(fe
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1);
1008 draw_rect(fe
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1,
1009 anim ? COL_BACKGROUND
: tile
& 1 ? COL_WRONG
: COL_RIGHT
);
1012 * Draw a polygon indicating that the square is diagonally
1015 int coords
[8], colour
;
1017 coords
[0] = bx
+ TILE_SIZE
;
1019 coords
[2] = bx
+ TILE_SIZE
* animtime
;
1020 coords
[3] = by
+ TILE_SIZE
* animtime
;
1022 coords
[5] = by
+ TILE_SIZE
;
1023 coords
[6] = bx
+ TILE_SIZE
- TILE_SIZE
* animtime
;
1024 coords
[7] = by
+ TILE_SIZE
- TILE_SIZE
* animtime
;
1026 colour
= (tile
& 1 ? COL_WRONG
: COL_RIGHT
);
1028 colour
= COL_WRONG
+ COL_RIGHT
- colour
;
1030 draw_polygon(fe
, coords
, 4, TRUE
, colour
);
1031 draw_polygon(fe
, coords
, 4, FALSE
, COL_GRID
);
1035 * Draw a little diagram in the tile which indicates which
1036 * surrounding tiles flip when this one is clicked.
1038 for (i
= 0; i
< h
; i
++)
1039 for (j
= 0; j
< w
; j
++)
1040 if (state
->matrix
->matrix
[(y
*w
+x
)*wh
+ i
*w
+j
]) {
1041 int ox
= j
- x
, oy
= i
- y
;
1042 int td
= TILE_SIZE
/ 16;
1043 int cx
= (bx
+ TILE_SIZE
/2) + (2 * ox
- 1) * td
;
1044 int cy
= (by
+ TILE_SIZE
/2) + (2 * oy
- 1) * td
;
1045 if (ox
== 0 && oy
== 0)
1046 draw_rect(fe
, cx
, cy
, 2*td
+1, 2*td
+1, COL_DIAG
);
1048 draw_line(fe
, cx
, cy
, cx
+2*td
, cy
, COL_DIAG
);
1049 draw_line(fe
, cx
, cy
+2*td
, cx
+2*td
, cy
+2*td
, COL_DIAG
);
1050 draw_line(fe
, cx
, cy
, cx
, cy
+2*td
, COL_DIAG
);
1051 draw_line(fe
, cx
+2*td
, cy
, cx
+2*td
, cy
+2*td
, COL_DIAG
);
1056 * Draw a hint blob if required.
1059 draw_rect(fe
, bx
+ TILE_SIZE
/20, by
+ TILE_SIZE
/ 20,
1060 TILE_SIZE
/ 6, TILE_SIZE
/ 6, COL_HINT
);
1065 draw_update(fe
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1);
1068 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1069 game_state
*state
, int dir
, game_ui
*ui
,
1070 float animtime
, float flashtime
)
1072 int w
= ds
->w
, h
= ds
->h
, wh
= w
* h
;
1076 draw_rect(fe
, 0, 0, TILE_SIZE
* w
+ 2 * BORDER
,
1077 TILE_SIZE
* h
+ 2 * BORDER
, COL_BACKGROUND
);
1080 * Draw the grid lines.
1082 for (i
= 0; i
<= w
; i
++)
1083 draw_line(fe
, i
* TILE_SIZE
+ BORDER
, BORDER
,
1084 i
* TILE_SIZE
+ BORDER
, h
* TILE_SIZE
+ BORDER
,
1086 for (i
= 0; i
<= h
; i
++)
1087 draw_line(fe
, BORDER
, i
* TILE_SIZE
+ BORDER
,
1088 w
* TILE_SIZE
+ BORDER
, i
* TILE_SIZE
+ BORDER
,
1091 draw_update(fe
, 0, 0, TILE_SIZE
* w
+ 2 * BORDER
,
1092 TILE_SIZE
* h
+ 2 * BORDER
);
1098 flashframe
= flashtime
/ FLASH_FRAME
;
1102 animtime
/= ANIM_TIME
; /* scale it so it goes from 0 to 1 */
1104 for (i
= 0; i
< wh
; i
++) {
1105 int x
= i
% w
, y
= i
/ w
;
1107 int v
= state
->grid
[i
];
1110 if (flashframe
>= 0) {
1111 fx
= (w
+1)/2 - min(x
+1, w
-x
);
1112 fy
= (h
+1)/2 - min(y
+1, h
-y
);
1114 if (fd
== flashframe
)
1116 else if (fd
== flashframe
- 1)
1120 if (!state
->hints_active
)
1123 if (oldstate
&& state
->grid
[i
] != oldstate
->grid
[i
])
1124 vv
= 255; /* means `animated' */
1128 if (ds
->tiles
[i
] == 255 || vv
== 255 || ds
->tiles
[i
] != vv
) {
1129 draw_tile(fe
, ds
, state
, x
, y
, v
, vv
== 255, animtime
);
1137 sprintf(buf
, "%sMoves: %d",
1139 (state
->cheated ?
"Auto-solved. " : "COMPLETED! ") :
1140 (state
->cheated ?
"Auto-solver used. " : "")),
1143 status_bar(fe
, buf
);
1147 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1148 int dir
, game_ui
*ui
)
1153 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1154 int dir
, game_ui
*ui
)
1156 if (!oldstate
->completed
&& newstate
->completed
)
1157 return FLASH_FRAME
* (max((newstate
->w
+1)/2, (newstate
->h
+1)/2)+1);
1162 static int game_wants_statusbar(void)
1167 static int game_timing_state(game_state
*state
)
1173 #define thegame flip
1176 const struct game thegame
= {
1184 TRUE
, game_configure
, custom_params
,
1193 FALSE
, game_text_format
,
1201 game_free_drawstate
,
1205 game_wants_statusbar
,
1206 FALSE
, game_timing_state
,
1207 0, /* mouse_priorities */