2 * flip.c: Puzzle involving lighting up all the squares on a grid,
3 * where each click toggles an overlapping set of lights.
27 #define PREFERRED_TILE_SIZE 48
28 #define TILE_SIZE (ds->tilesize)
29 #define BORDER (TILE_SIZE / 2)
30 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
31 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
33 #define ANIM_TIME 0.25F
34 #define FLASH_FRAME 0.07F
37 * Possible ways to decide which lights are toggled by each click.
38 * Essentially, each of these describes a means of inventing a
51 * This structure is shared between all the game_states describing
52 * a particular game, so it's reference-counted.
56 unsigned char *matrix
; /* array of (w*h) by (w*h) */
61 int moves
, completed
, cheated
, hints_active
;
62 unsigned char *grid
; /* array of w*h */
63 struct matrix
*matrix
;
66 static game_params
*default_params(void)
68 game_params
*ret
= snew(game_params
);
71 ret
->matrix_type
= CROSSES
;
76 static const struct game_params flip_presets
[] = {
85 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
90 if (i
< 0 || i
>= lenof(flip_presets
))
93 ret
= snew(game_params
);
94 *ret
= flip_presets
[i
];
96 sprintf(str
, "%dx%d %s", ret
->w
, ret
->h
,
97 ret
->matrix_type
== CROSSES
? "Crosses" : "Random");
104 static void free_params(game_params
*params
)
109 static game_params
*dup_params(game_params
*params
)
111 game_params
*ret
= snew(game_params
);
112 *ret
= *params
; /* structure copy */
116 static void decode_params(game_params
*ret
, char const *string
)
118 ret
->w
= ret
->h
= atoi(string
);
119 while (*string
&& isdigit((unsigned char)*string
)) string
++;
120 if (*string
== 'x') {
122 ret
->h
= atoi(string
);
123 while (*string
&& isdigit((unsigned char)*string
)) string
++;
125 if (*string
== 'r') {
127 ret
->matrix_type
= RANDOM
;
128 } else if (*string
== 'c') {
130 ret
->matrix_type
= CROSSES
;
134 static char *encode_params(game_params
*params
, int full
)
138 sprintf(data
, "%dx%d%s", params
->w
, params
->h
,
139 !full
? "" : params
->matrix_type
== CROSSES
? "c" : "r");
144 static config_item
*game_configure(game_params
*params
)
146 config_item
*ret
= snewn(4, config_item
);
149 ret
[0].name
= "Width";
150 ret
[0].type
= C_STRING
;
151 sprintf(buf
, "%d", params
->w
);
152 ret
[0].sval
= dupstr(buf
);
155 ret
[1].name
= "Height";
156 ret
[1].type
= C_STRING
;
157 sprintf(buf
, "%d", params
->h
);
158 ret
[1].sval
= dupstr(buf
);
161 ret
[2].name
= "Shape type";
162 ret
[2].type
= C_CHOICES
;
163 ret
[2].sval
= ":Crosses:Random";
164 ret
[2].ival
= params
->matrix_type
;
174 static game_params
*custom_params(config_item
*cfg
)
176 game_params
*ret
= snew(game_params
);
178 ret
->w
= atoi(cfg
[0].sval
);
179 ret
->h
= atoi(cfg
[1].sval
);
180 ret
->matrix_type
= cfg
[2].ival
;
185 static char *validate_params(game_params
*params
, int full
)
187 if (params
->w
<= 0 || params
->h
<= 0)
188 return "Width and height must both be greater than zero";
192 static char *encode_bitmap(unsigned char *bmp
, int len
)
194 int slen
= (len
+ 3) / 4;
198 ret
= snewn(slen
+ 1, char);
199 for (i
= 0; i
< slen
; i
++) {
202 for (j
= 0; j
< 4; j
++)
203 if (i
*4+j
< len
&& bmp
[i
*4+j
])
205 ret
[i
] = "0123456789abcdef"[v
];
211 static void decode_bitmap(unsigned char *bmp
, int len
, char *hex
)
213 int slen
= (len
+ 3) / 4;
216 for (i
= 0; i
< slen
; i
++) {
217 int j
, v
, c
= hex
[i
];
218 if (c
>= '0' && c
<= '9')
220 else if (c
>= 'A' && c
<= 'F')
222 else if (c
>= 'a' && c
<= 'f')
225 v
= 0; /* shouldn't happen */
226 for (j
= 0; j
< 4; j
++) {
238 * Structure used during random matrix generation, and a compare
239 * function to permit storage in a tree234.
242 int cx
, cy
; /* coords of click square */
243 int x
, y
; /* coords of output square */
245 * Number of click squares which currently affect this output
250 * Number of output squares currently affected by this click
255 #define SORT(field) do { \
256 if (a->field < b->field) \
258 else if (a->field > b->field) \
262 * Compare function for choosing the next square to add. We must
263 * sort by coverage, then by omino size, then everything else.
265 static int sqcmp_pick(void *av
, void *bv
)
267 struct sq
*a
= (struct sq
*)av
;
268 struct sq
*b
= (struct sq
*)bv
;
278 * Compare function for adjusting the coverage figures after a
279 * change. We sort first by coverage and output square, then by
282 static int sqcmp_cov(void *av
, void *bv
)
284 struct sq
*a
= (struct sq
*)av
;
285 struct sq
*b
= (struct sq
*)bv
;
295 * Compare function for adjusting the omino sizes after a change.
296 * We sort first by omino size and input square, then by everything
299 static int sqcmp_osize(void *av
, void *bv
)
301 struct sq
*a
= (struct sq
*)av
;
302 struct sq
*b
= (struct sq
*)bv
;
311 static void addsq(tree234
*t
, int w
, int h
, int cx
, int cy
,
312 int x
, int y
, unsigned char *matrix
)
318 if (x
< 0 || x
>= w
|| y
< 0 || y
>= h
)
320 if (abs(x
-cx
) > 1 || abs(y
-cy
) > 1)
322 if (matrix
[(cy
*w
+cx
) * wh
+ y
*w
+x
])
325 sq
= snew(struct sq
);
330 sq
->coverage
= sq
->ominosize
= 0;
331 for (i
= 0; i
< wh
; i
++) {
332 if (matrix
[i
* wh
+ y
*w
+x
])
334 if (matrix
[(cy
*w
+cx
) * wh
+ i
])
338 if (add234(t
, sq
) != sq
)
339 sfree(sq
); /* already there */
341 static void addneighbours(tree234
*t
, int w
, int h
, int cx
, int cy
,
342 int x
, int y
, unsigned char *matrix
)
344 addsq(t
, w
, h
, cx
, cy
, x
-1, y
, matrix
);
345 addsq(t
, w
, h
, cx
, cy
, x
+1, y
, matrix
);
346 addsq(t
, w
, h
, cx
, cy
, x
, y
-1, matrix
);
347 addsq(t
, w
, h
, cx
, cy
, x
, y
+1, matrix
);
350 static char *new_game_desc(game_params
*params
, random_state
*rs
,
351 char **aux
, int interactive
)
353 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
355 unsigned char *matrix
, *grid
;
356 char *mbmp
, *gbmp
, *ret
;
358 matrix
= snewn(wh
* wh
, unsigned char);
359 grid
= snewn(wh
, unsigned char);
362 * First set up the matrix.
364 switch (params
->matrix_type
) {
366 for (i
= 0; i
< wh
; i
++) {
367 int ix
= i
% w
, iy
= i
/ w
;
368 for (j
= 0; j
< wh
; j
++) {
369 int jx
= j
% w
, jy
= j
/ w
;
370 if (abs(jx
- ix
) + abs(jy
- iy
) <= 1)
379 tree234
*pick
, *cov
, *osize
;
382 pick
= newtree234(sqcmp_pick
);
383 cov
= newtree234(sqcmp_cov
);
384 osize
= newtree234(sqcmp_osize
);
386 memset(matrix
, 0, wh
* wh
);
387 for (i
= 0; i
< wh
; i
++) {
391 for (i
= 0; i
< wh
; i
++) {
392 int ix
= i
% w
, iy
= i
/ w
;
393 addneighbours(pick
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
394 addneighbours(cov
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
395 addneighbours(osize
, w
, h
, ix
, iy
, ix
, iy
, matrix
);
399 * Repeatedly choose a square to add to the matrix,
400 * until we have enough. I'll arbitrarily choose our
401 * limit to be the same as the total number of set bits
402 * in the crosses matrix.
404 limit
= 4*wh
- 2*(w
+h
); /* centre squares already present */
406 while (limit
-- > 0) {
407 struct sq
*sq
, *sq2
, sqlocal
;
411 * Find the lowest element in the pick tree.
413 sq
= index234(pick
, 0);
416 * Find the highest element with the same coverage
417 * and omino size, by setting all other elements to
421 sqlocal
.cx
= sqlocal
.cy
= sqlocal
.x
= sqlocal
.y
= wh
;
422 sq
= findrelpos234(pick
, &sqlocal
, NULL
, REL234_LT
, &k
);
426 * Pick at random from all elements up to k of the
429 k
= random_upto(rs
, k
+1);
430 sq
= delpos234(pick
, k
);
435 * Add this square to the matrix.
437 matrix
[(sq
->cy
* w
+ sq
->cx
) * wh
+ (sq
->y
* w
+ sq
->x
)] = 1;
440 * Correct the matrix coverage field of any sq
441 * which points at this output square.
444 sqlocal
.cx
= sqlocal
.cy
= sqlocal
.ominosize
= -1;
445 while ((sq2
= findrel234(cov
, &sqlocal
, NULL
,
446 REL234_GT
)) != NULL
&&
447 sq2
->coverage
== sq
->coverage
&&
448 sq2
->x
== sq
->x
&& sq2
->y
== sq
->y
) {
459 * Correct the omino size field of any sq which
460 * points at this input square.
463 sqlocal
.x
= sqlocal
.y
= sqlocal
.coverage
= -1;
464 while ((sq2
= findrel234(osize
, &sqlocal
, NULL
,
465 REL234_GT
)) != NULL
&&
466 sq2
->ominosize
== sq
->ominosize
&&
467 sq2
->cx
== sq
->cx
&& sq2
->cy
== sq
->cy
) {
478 * The sq we actually picked out of the tree is
479 * finished with; but its neighbours now need to
482 addneighbours(pick
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
483 addneighbours(cov
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
484 addneighbours(osize
, w
,h
, sq
->cx
,sq
->cy
, sq
->x
,sq
->y
, matrix
);
489 * Free all remaining sq structures.
493 while ((sq
= delpos234(pick
, 0)) != NULL
)
501 * Finally, check to see if any two matrix rows are
502 * exactly identical. If so, this is not an acceptable
503 * matrix, and we give up and go round again.
505 * I haven't been immediately able to think of a
506 * plausible means of algorithmically avoiding this
507 * situation (by, say, making a small perturbation to
508 * an offending matrix), so for the moment I'm just
509 * going to deal with it by throwing the whole thing
510 * away. I suspect this will lead to scalability
511 * problems (since most of the things happening in
512 * these matrices are local, the chance of _some_
513 * neighbourhood having two identical regions will
514 * increase with the grid area), but so far this puzzle
515 * seems to be really hard at large sizes so I'm not
516 * massively worried yet. Anyone needs this done
517 * better, they're welcome to submit a patch.
519 for (i
= 0; i
< wh
; i
++) {
520 for (j
= 0; j
< wh
; j
++)
522 !memcmp(matrix
+ i
* wh
, matrix
+ j
* wh
, wh
))
528 break; /* no matches found */
534 * Now invent a random initial set of lights.
536 * At first glance it looks as if it might be quite difficult
537 * to choose equiprobably from all soluble light sets. After
538 * all, soluble light sets are those in the image space of the
539 * transformation matrix; so first we'd have to identify that
540 * space and its dimension, then pick a random coordinate for
541 * each basis vector and recombine. Lot of fiddly matrix
544 * However, vector spaces are nicely orthogonal and relieve us
545 * of all that difficulty. For every point in the image space,
546 * there are precisely as many points in the input space that
547 * map to it as there are elements in the kernel of the
548 * transformation matrix (because adding any kernel element to
549 * the input does not change the output, and because any two
550 * inputs mapping to the same output must differ by an element
551 * of the kernel because that's what the kernel _is_); and
552 * these cosets are all disjoint (obviously, since no input
553 * point can map to more than one output point) and cover the
554 * whole space (equally obviously, because no input point can
555 * map to fewer than one output point!).
557 * So the input space contains the same number of points for
558 * each point in the output space; thus, we can simply choose
559 * equiprobably from elements of the _input_ space, and filter
560 * the result through the transformation matrix in the obvious
561 * way, and we thereby guarantee to choose equiprobably from
562 * all the output points. Phew!
566 for (i
= 0; i
< wh
; i
++) {
567 int v
= random_upto(rs
, 2);
569 for (j
= 0; j
< wh
; j
++)
570 grid
[j
] ^= matrix
[i
*wh
+j
];
574 * Ensure we don't have the starting state already!
576 for (i
= 0; i
< wh
; i
++)
584 * Now encode the matrix and the starting grid as a game
585 * description. We'll do this by concatenating two great big
588 mbmp
= encode_bitmap(matrix
, wh
*wh
);
589 gbmp
= encode_bitmap(grid
, wh
);
590 ret
= snewn(strlen(mbmp
) + strlen(gbmp
) + 2, char);
591 sprintf(ret
, "%s,%s", mbmp
, gbmp
);
599 static char *validate_desc(game_params
*params
, char *desc
)
601 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
602 int mlen
= (wh
*wh
+3)/4, glen
= (wh
+3)/4;
604 if (strspn(desc
, "0123456789abcdefABCDEF") != mlen
)
605 return "Matrix description is wrong length";
606 if (desc
[mlen
] != ',')
607 return "Expected comma after matrix description";
608 if (strspn(desc
+mlen
+1, "0123456789abcdefABCDEF") != glen
)
609 return "Grid description is wrong length";
610 if (desc
[mlen
+1+glen
])
611 return "Unexpected data after grid description";
616 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
618 int w
= params
->w
, h
= params
->h
, wh
= w
* h
;
619 int mlen
= (wh
*wh
+3)/4;
621 game_state
*state
= snew(game_state
);
625 state
->completed
= FALSE
;
626 state
->cheated
= FALSE
;
627 state
->hints_active
= FALSE
;
629 state
->matrix
= snew(struct matrix
);
630 state
->matrix
->refcount
= 1;
631 state
->matrix
->matrix
= snewn(wh
*wh
, unsigned char);
632 decode_bitmap(state
->matrix
->matrix
, wh
*wh
, desc
);
633 state
->grid
= snewn(wh
, unsigned char);
634 decode_bitmap(state
->grid
, wh
, desc
+ mlen
+ 1);
639 static game_state
*dup_game(game_state
*state
)
641 game_state
*ret
= snew(game_state
);
645 ret
->completed
= state
->completed
;
646 ret
->cheated
= state
->cheated
;
647 ret
->hints_active
= state
->hints_active
;
648 ret
->moves
= state
->moves
;
649 ret
->matrix
= state
->matrix
;
650 state
->matrix
->refcount
++;
651 ret
->grid
= snewn(ret
->w
* ret
->h
, unsigned char);
652 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
657 static void free_game(game_state
*state
)
660 if (--state
->matrix
->refcount
<= 0) {
661 sfree(state
->matrix
->matrix
);
662 sfree(state
->matrix
);
667 static void rowxor(unsigned char *row1
, unsigned char *row2
, int len
)
670 for (i
= 0; i
< len
; i
++)
674 static char *solve_game(game_state
*state
, game_state
*currstate
,
675 char *aux
, char **error
)
677 int w
= state
->w
, h
= state
->h
, wh
= w
* h
;
678 unsigned char *equations
, *solution
, *shortest
;
680 int rowsdone
, colsdone
;
681 int i
, j
, k
, len
, bestlen
;
685 * Set up a list of simultaneous equations. Each one is of
686 * length (wh+1) and has wh coefficients followed by a value.
688 equations
= snewn((wh
+ 1) * wh
, unsigned char);
689 for (i
= 0; i
< wh
; i
++) {
690 for (j
= 0; j
< wh
; j
++)
691 equations
[i
* (wh
+1) + j
] = currstate
->matrix
->matrix
[j
*wh
+i
];
692 equations
[i
* (wh
+1) + wh
] = currstate
->grid
[i
] & 1;
696 * Perform Gaussian elimination over GF(2).
698 rowsdone
= colsdone
= 0;
700 und
= snewn(wh
, int);
703 * Find the leftmost column which has a 1 in it somewhere
704 * outside the first `rowsdone' rows.
707 for (i
= colsdone
; i
< wh
; i
++) {
708 for (j
= rowsdone
; j
< wh
; j
++)
709 if (equations
[j
* (wh
+1) + i
])
712 break; /* found one */
714 * This is a column which will not have an equation
715 * controlling it. Mark it as undetermined.
721 * If there wasn't one, then we've finished: all remaining
722 * equations are of the form 0 = constant. Check to see if
723 * any of them wants 0 to be equal to 1; this is the
724 * condition which indicates an insoluble problem
725 * (therefore _hopefully_ one typed in by a user!).
728 for (j
= rowsdone
; j
< wh
; j
++)
729 if (equations
[j
* (wh
+1) + wh
]) {
730 *error
= "No solution exists for this position";
739 * We've found a 1. It's in column i, and the topmost 1 in
740 * that column is in row j. Do a row-XOR to move it up to
741 * the topmost row if it isn't already there.
745 rowxor(equations
+ rowsdone
*(wh
+1), equations
+ j
*(wh
+1), wh
+1);
748 * Do row-XORs to eliminate that 1 from all rows below the
751 for (j
= rowsdone
+ 1; j
< wh
; j
++)
752 if (equations
[j
*(wh
+1) + i
])
753 rowxor(equations
+ j
*(wh
+1),
754 equations
+ rowsdone
*(wh
+1), wh
+1);
757 * Mark this row and column as done.
763 * If we've done all the rows, terminate.
765 } while (rowsdone
< wh
);
768 * If we reach here, we have the ability to produce a solution.
769 * So we go through _all_ possible solutions (each
770 * corresponding to a set of arbitrary choices of those
771 * components not directly determined by an equation), and pick
772 * one requiring the smallest number of flips.
774 solution
= snewn(wh
, unsigned char);
775 shortest
= snewn(wh
, unsigned char);
776 memset(solution
, 0, wh
);
780 * Find a solution based on the current values of the
781 * undetermined variables.
783 for (j
= rowsdone
; j
-- ;) {
787 * Find the leftmost set bit in this equation.
789 for (i
= 0; i
< wh
; i
++)
790 if (equations
[j
* (wh
+1) + i
])
792 assert(i
< wh
); /* there must have been one! */
795 * Compute this variable using the rest.
797 v
= equations
[j
* (wh
+1) + wh
];
798 for (k
= i
+1; k
< wh
; k
++)
799 if (equations
[j
* (wh
+1) + k
])
806 * Compare this solution to the current best one, and
807 * replace the best one if this one is shorter.
810 for (i
= 0; i
< wh
; i
++)
815 memcpy(shortest
, solution
, wh
);
819 * Now increment the binary number given by the
820 * undetermined variables: turn all 1s into 0s until we see
821 * a 0, at which point we turn it into a 1.
823 for (i
= 0; i
< nund
; i
++) {
824 solution
[und
[i
]] = !solution
[und
[i
]];
825 if (solution
[und
[i
]])
830 * If we didn't find a 0 at any point, we have wrapped
831 * round and are back at the start, i.e. we have enumerated
839 * We have a solution. Produce a move string encoding the
842 ret
= snewn(wh
+ 2, char);
844 for (i
= 0; i
< wh
; i
++)
845 ret
[i
+1] = shortest
[i
] ? '1' : '0';
856 static int game_can_format_as_text_now(game_params
*params
)
861 static char *game_text_format(game_state
*state
)
870 static game_ui
*new_ui(game_state
*state
)
872 game_ui
*ui
= snew(game_ui
);
873 ui
->cx
= ui
->cy
= ui
->cdraw
= 0;
877 static void free_ui(game_ui
*ui
)
882 static char *encode_ui(game_ui
*ui
)
887 static void decode_ui(game_ui
*ui
, char *encoding
)
891 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
892 game_state
*newstate
)
896 struct game_drawstate
{
898 unsigned char *tiles
;
902 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
903 int x
, int y
, int button
)
905 int w
= state
->w
, h
= state
->h
, wh
= w
* h
;
906 char buf
[80], *nullret
= NULL
;
908 if (button
== LEFT_BUTTON
|| IS_CURSOR_SELECT(button
)) {
910 if (button
== LEFT_BUTTON
) {
911 tx
= FROMCOORD(x
), ty
= FROMCOORD(y
);
914 tx
= ui
->cx
; ty
= ui
->cy
;
919 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< h
) {
921 * It's just possible that a manually entered game ID
922 * will have at least one square do nothing whatsoever.
923 * If so, we avoid encoding a move at all.
925 int i
= ty
*w
+tx
, j
, makemove
= FALSE
;
926 for (j
= 0; j
< wh
; j
++) {
927 if (state
->matrix
->matrix
[i
*wh
+j
])
931 sprintf(buf
, "M%d,%d", tx
, ty
);
938 else if (IS_CURSOR_MOVE(button
)) {
941 case CURSOR_UP
: dy
= -1; break;
942 case CURSOR_DOWN
: dy
= 1; break;
943 case CURSOR_RIGHT
: dx
= 1; break;
944 case CURSOR_LEFT
: dx
= -1; break;
945 default: assert(!"shouldn't get here");
947 ui
->cx
+= dx
; ui
->cy
+= dy
;
948 ui
->cx
= min(max(ui
->cx
, 0), state
->w
- 1);
949 ui
->cy
= min(max(ui
->cy
, 0), state
->h
- 1);
957 static game_state
*execute_move(game_state
*from
, char *move
)
959 int w
= from
->w
, h
= from
->h
, wh
= w
* h
;
963 if (move
[0] == 'S' && strlen(move
) == wh
+1) {
966 ret
= dup_game(from
);
967 ret
->hints_active
= TRUE
;
969 for (i
= 0; i
< wh
; i
++) {
971 if (move
[i
+1] != '0')
975 } else if (move
[0] == 'M' &&
976 sscanf(move
+1, "%d,%d", &x
, &y
) == 2 &&
977 x
>= 0 && x
< w
&& y
>= 0 && y
< h
) {
980 ret
= dup_game(from
);
988 for (j
= 0; j
< wh
; j
++) {
989 ret
->grid
[j
] ^= ret
->matrix
->matrix
[i
*wh
+j
];
990 if (ret
->grid
[j
] & 1)
993 ret
->grid
[i
] ^= 2; /* toggle hint */
995 ret
->completed
= TRUE
;
996 ret
->hints_active
= FALSE
;
1001 return NULL
; /* can't parse move string */
1004 /* ----------------------------------------------------------------------
1008 static void game_compute_size(game_params
*params
, int tilesize
,
1011 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1012 struct { int tilesize
; } ads
, *ds
= &ads
;
1013 ads
.tilesize
= tilesize
;
1015 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
1016 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
1019 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1020 game_params
*params
, int tilesize
)
1022 ds
->tilesize
= tilesize
;
1025 static float *game_colours(frontend
*fe
, int *ncolours
)
1027 float *ret
= snewn(3 * NCOLOURS
, float);
1029 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1031 ret
[COL_WRONG
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] / 3;
1032 ret
[COL_WRONG
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] / 3;
1033 ret
[COL_WRONG
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] / 3;
1035 ret
[COL_RIGHT
* 3 + 0] = 1.0F
;
1036 ret
[COL_RIGHT
* 3 + 1] = 1.0F
;
1037 ret
[COL_RIGHT
* 3 + 2] = 1.0F
;
1039 ret
[COL_GRID
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] / 1.5F
;
1040 ret
[COL_GRID
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] / 1.5F
;
1041 ret
[COL_GRID
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] / 1.5F
;
1043 ret
[COL_DIAG
* 3 + 0] = ret
[COL_GRID
* 3 + 0];
1044 ret
[COL_DIAG
* 3 + 1] = ret
[COL_GRID
* 3 + 1];
1045 ret
[COL_DIAG
* 3 + 2] = ret
[COL_GRID
* 3 + 2];
1047 ret
[COL_HINT
* 3 + 0] = 1.0F
;
1048 ret
[COL_HINT
* 3 + 1] = 0.0F
;
1049 ret
[COL_HINT
* 3 + 2] = 0.0F
;
1051 ret
[COL_CURSOR
* 3 + 0] = 0.8F
;
1052 ret
[COL_CURSOR
* 3 + 1] = 0.0F
;
1053 ret
[COL_CURSOR
* 3 + 2] = 0.0F
;
1055 *ncolours
= NCOLOURS
;
1059 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1061 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1064 ds
->started
= FALSE
;
1067 ds
->tiles
= snewn(ds
->w
*ds
->h
, unsigned char);
1068 ds
->tilesize
= 0; /* haven't decided yet */
1069 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
1075 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1081 static void draw_tile(drawing
*dr
, game_drawstate
*ds
,
1082 game_state
*state
, int x
, int y
, int tile
, int anim
,
1085 int w
= ds
->w
, h
= ds
->h
, wh
= w
* h
;
1086 int bx
= x
* TILE_SIZE
+ BORDER
, by
= y
* TILE_SIZE
+ BORDER
;
1087 int i
, j
, dcol
= (tile
& 4) ? COL_CURSOR
: COL_DIAG
;
1089 clip(dr
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1);
1091 draw_rect(dr
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1,
1092 anim
? COL_BACKGROUND
: tile
& 1 ? COL_WRONG
: COL_RIGHT
);
1095 * Draw a polygon indicating that the square is diagonally
1098 int coords
[8], colour
;
1100 coords
[0] = bx
+ TILE_SIZE
;
1102 coords
[2] = bx
+ (int)((float)TILE_SIZE
* animtime
);
1103 coords
[3] = by
+ (int)((float)TILE_SIZE
* animtime
);
1105 coords
[5] = by
+ TILE_SIZE
;
1106 coords
[6] = bx
+ TILE_SIZE
- (int)((float)TILE_SIZE
* animtime
);
1107 coords
[7] = by
+ TILE_SIZE
- (int)((float)TILE_SIZE
* animtime
);
1109 colour
= (tile
& 1 ? COL_WRONG
: COL_RIGHT
);
1111 colour
= COL_WRONG
+ COL_RIGHT
- colour
;
1113 draw_polygon(dr
, coords
, 4, colour
, COL_GRID
);
1117 * Draw a little diagram in the tile which indicates which
1118 * surrounding tiles flip when this one is clicked.
1120 for (i
= 0; i
< h
; i
++)
1121 for (j
= 0; j
< w
; j
++)
1122 if (state
->matrix
->matrix
[(y
*w
+x
)*wh
+ i
*w
+j
]) {
1123 int ox
= j
- x
, oy
= i
- y
;
1124 int td
= TILE_SIZE
/ 16;
1125 int cx
= (bx
+ TILE_SIZE
/2) + (2 * ox
- 1) * td
;
1126 int cy
= (by
+ TILE_SIZE
/2) + (2 * oy
- 1) * td
;
1127 if (ox
== 0 && oy
== 0)
1128 draw_rect(dr
, cx
, cy
, 2*td
+1, 2*td
+1, dcol
);
1130 draw_line(dr
, cx
, cy
, cx
+2*td
, cy
, dcol
);
1131 draw_line(dr
, cx
, cy
+2*td
, cx
+2*td
, cy
+2*td
, dcol
);
1132 draw_line(dr
, cx
, cy
, cx
, cy
+2*td
, dcol
);
1133 draw_line(dr
, cx
+2*td
, cy
, cx
+2*td
, cy
+2*td
, dcol
);
1138 * Draw a hint rectangle if required.
1141 int x1
= bx
+ TILE_SIZE
/ 20, x2
= bx
+ TILE_SIZE
- TILE_SIZE
/ 20;
1142 int y1
= by
+ TILE_SIZE
/ 20, y2
= by
+ TILE_SIZE
- TILE_SIZE
/ 20;
1145 draw_line(dr
, x1
, y1
, x2
, y1
, COL_HINT
);
1146 draw_line(dr
, x1
, y2
, x2
, y2
, COL_HINT
);
1147 draw_line(dr
, x1
, y1
, x1
, y2
, COL_HINT
);
1148 draw_line(dr
, x2
, y1
, x2
, y2
, COL_HINT
);
1149 x1
++, y1
++, x2
--, y2
--;
1155 draw_update(dr
, bx
+1, by
+1, TILE_SIZE
-1, TILE_SIZE
-1);
1158 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1159 game_state
*state
, int dir
, game_ui
*ui
,
1160 float animtime
, float flashtime
)
1162 int w
= ds
->w
, h
= ds
->h
, wh
= w
* h
;
1166 draw_rect(dr
, 0, 0, TILE_SIZE
* w
+ 2 * BORDER
,
1167 TILE_SIZE
* h
+ 2 * BORDER
, COL_BACKGROUND
);
1170 * Draw the grid lines.
1172 for (i
= 0; i
<= w
; i
++)
1173 draw_line(dr
, i
* TILE_SIZE
+ BORDER
, BORDER
,
1174 i
* TILE_SIZE
+ BORDER
, h
* TILE_SIZE
+ BORDER
,
1176 for (i
= 0; i
<= h
; i
++)
1177 draw_line(dr
, BORDER
, i
* TILE_SIZE
+ BORDER
,
1178 w
* TILE_SIZE
+ BORDER
, i
* TILE_SIZE
+ BORDER
,
1181 draw_update(dr
, 0, 0, TILE_SIZE
* w
+ 2 * BORDER
,
1182 TILE_SIZE
* h
+ 2 * BORDER
);
1188 flashframe
= (int)(flashtime
/ FLASH_FRAME
);
1192 animtime
/= ANIM_TIME
; /* scale it so it goes from 0 to 1 */
1194 for (i
= 0; i
< wh
; i
++) {
1195 int x
= i
% w
, y
= i
/ w
;
1197 int v
= state
->grid
[i
];
1200 if (flashframe
>= 0) {
1201 fx
= (w
+1)/2 - min(x
+1, w
-x
);
1202 fy
= (h
+1)/2 - min(y
+1, h
-y
);
1204 if (fd
== flashframe
)
1206 else if (fd
== flashframe
- 1)
1210 if (!state
->hints_active
)
1212 if (ui
->cdraw
&& ui
->cx
== x
&& ui
->cy
== y
)
1215 if (oldstate
&& ((state
->grid
[i
] ^ oldstate
->grid
[i
]) &~ 2))
1216 vv
= 255; /* means `animated' */
1220 if (ds
->tiles
[i
] == 255 || vv
== 255 || ds
->tiles
[i
] != vv
) {
1221 draw_tile(dr
, ds
, state
, x
, y
, v
, vv
== 255, animtime
);
1229 sprintf(buf
, "%sMoves: %d",
1231 (state
->cheated
? "Auto-solved. " : "COMPLETED! ") :
1232 (state
->cheated
? "Auto-solver used. " : "")),
1235 status_bar(dr
, buf
);
1239 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1240 int dir
, game_ui
*ui
)
1245 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1246 int dir
, game_ui
*ui
)
1248 if (!oldstate
->completed
&& newstate
->completed
)
1249 return FLASH_FRAME
* (max((newstate
->w
+1)/2, (newstate
->h
+1)/2)+1);
1254 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1259 static void game_print_size(game_params
*params
, float *x
, float *y
)
1263 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1268 #define thegame flip
1271 const struct game thegame
= {
1272 "Flip", "games.flip", "flip",
1279 TRUE
, game_configure
, custom_params
,
1287 FALSE
, game_can_format_as_text_now
, game_text_format
,
1295 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1298 game_free_drawstate
,
1302 FALSE
, FALSE
, game_print_size
, game_print
,
1303 TRUE
, /* wants_statusbar */
1304 FALSE
, game_timing_state
,