2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
6 * - make some sort of stab at number-of-numbers judgment
18 #define max(x,y) ( (x)>(y) ? (x):(y) )
19 #define min(x,y) ( (x)<(y) ? (x):(y) )
31 #define TLBORDER(d) ( (d) / 5 + 2 )
35 #define FROMCOORD(d, x) \
36 ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
38 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
40 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
46 #define GRID_UNKNOWN 2
54 int *rowdata
, *rowlen
;
58 #define FLASH_TIME 0.13F
60 static game_params
*default_params(void)
62 game_params
*ret
= snew(game_params
);
69 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
73 static const struct { int x
, y
; } values
[] = {
81 if (i
< 0 || i
>= lenof(values
))
84 ret
= snew(game_params
);
88 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
95 static void free_params(game_params
*params
)
100 static game_params
*dup_params(game_params
*params
)
102 game_params
*ret
= snew(game_params
);
103 *ret
= *params
; /* structure copy */
107 static game_params
*decode_params(char const *string
)
109 game_params
*ret
= default_params();
110 char const *p
= string
;
113 while (*p
&& isdigit(*p
)) p
++;
117 while (*p
&& isdigit(*p
)) p
++;
125 static char *encode_params(game_params
*params
)
130 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
131 assert(len
< lenof(ret
));
137 static config_item
*game_configure(game_params
*params
)
142 ret
= snewn(3, config_item
);
144 ret
[0].name
= "Width";
145 ret
[0].type
= C_STRING
;
146 sprintf(buf
, "%d", params
->w
);
147 ret
[0].sval
= dupstr(buf
);
150 ret
[1].name
= "Height";
151 ret
[1].type
= C_STRING
;
152 sprintf(buf
, "%d", params
->h
);
153 ret
[1].sval
= dupstr(buf
);
164 static game_params
*custom_params(config_item
*cfg
)
166 game_params
*ret
= snew(game_params
);
168 ret
->w
= atoi(cfg
[0].sval
);
169 ret
->h
= atoi(cfg
[1].sval
);
174 static char *validate_params(game_params
*params
)
176 if (params
->w
<= 0 && params
->h
<= 0)
177 return "Width and height must both be greater than zero";
179 return "Width must be greater than zero";
181 return "Height must be greater than zero";
185 /* ----------------------------------------------------------------------
186 * Puzzle generation code.
188 * For this particular puzzle, it seemed important to me to ensure
189 * a unique solution. I do this the brute-force way, by having a
190 * solver algorithm alongside the generator, and repeatedly
191 * generating a random grid until I find one whose solution is
192 * unique. It turns out that this isn't too onerous on a modern PC
193 * provided you keep grid size below around 30. Any offers of
194 * better algorithms, however, will be very gratefully received.
196 * Another annoyance of this approach is that it limits the
197 * available puzzles to those solvable by the algorithm I've used.
198 * My algorithm only ever considers a single row or column at any
199 * one time, which means it's incapable of solving the following
200 * difficult example (found by Bella Image around 1995/6, when she
201 * and I were both doing maths degrees):
215 * Obviously this cannot be solved by a one-row-or-column-at-a-time
216 * algorithm (it would require at least one row or column reading
217 * `2 1', `1 2', `3' or `4' to get started). However, it can be
218 * proved to have a unique solution: if the top left square were
219 * empty, then the only option for the top row would be to fill the
220 * two squares in the 1 columns, which would imply the squares
221 * below those were empty, leaving no place for the 2 in the second
222 * row. Contradiction. Hence the top left square is full, and the
223 * unique solution follows easily from that starting point.
225 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case
226 * it's useful to anyone.)
229 static int float_compare(const void *av
, const void *bv
)
231 const float *a
= (const float *)av
;
232 const float *b
= (const float *)bv
;
241 static void generate(random_state
*rs
, int w
, int h
, unsigned char *retgrid
)
248 fgrid
= snewn(w
*h
, float);
250 for (i
= 0; i
< h
; i
++) {
251 for (j
= 0; j
< w
; j
++) {
252 fgrid
[i
*w
+j
] = random_upto(rs
, 100000000UL) / 100000000.F
;
257 * The above gives a completely random splattering of black and
258 * white cells. We want to gently bias this in favour of _some_
259 * reasonably thick areas of white and black, while retaining
260 * some randomness and fine detail.
262 * So we evolve the starting grid using a cellular automaton.
263 * Currently, I'm doing something very simple indeed, which is
264 * to set each square to the average of the surrounding nine
265 * cells (or the average of fewer, if we're on a corner).
267 for (step
= 0; step
< 1; step
++) {
268 fgrid2
= snewn(w
*h
, float);
270 for (i
= 0; i
< h
; i
++) {
271 for (j
= 0; j
< w
; j
++) {
276 * Compute the average of the surrounding cells.
280 for (p
= -1; p
<= +1; p
++) {
281 for (q
= -1; q
<= +1; q
++) {
282 if (i
+p
< 0 || i
+p
>= h
|| j
+q
< 0 || j
+q
>= w
)
285 * An additional special case not mentioned
286 * above: if a grid dimension is 2xn then
287 * we do not average across that dimension
288 * at all. Otherwise a 2x2 grid would
289 * contain four identical squares.
291 if ((h
==2 && p
!=0) || (w
==2 && q
!=0))
294 sx
+= fgrid
[(i
+p
)*w
+(j
+q
)];
299 fgrid2
[i
*w
+j
] = xbar
;
307 fgrid2
= snewn(w
*h
, float);
308 memcpy(fgrid2
, fgrid
, w
*h
*sizeof(float));
309 qsort(fgrid2
, w
*h
, sizeof(float), float_compare
);
310 threshold
= fgrid2
[w
*h
/2];
313 for (i
= 0; i
< h
; i
++) {
314 for (j
= 0; j
< w
; j
++) {
315 retgrid
[i
*w
+j
] = (fgrid
[i
*w
+j
] >= threshold ? GRID_FULL
:
323 static int compute_rowdata(int *ret
, unsigned char *start
, int len
, int step
)
329 for (i
= 0; i
< len
; i
++) {
330 if (start
[i
*step
] == GRID_FULL
) {
332 while (i
+runlen
< len
&& start
[(i
+runlen
)*step
] == GRID_FULL
)
338 if (i
< len
&& start
[i
*step
] == GRID_UNKNOWN
)
348 #define STILL_UNKNOWN 3
350 static void do_recurse(unsigned char *known
, unsigned char *deduced
,
351 unsigned char *row
, int *data
, int len
,
352 int freespace
, int ndone
, int lowest
)
357 for (i
=0; i
<=freespace
; i
++) {
359 for (k
=0; k
<i
; k
++) row
[j
++] = DOT
;
360 for (k
=0; k
<data
[ndone
]; k
++) row
[j
++] = BLOCK
;
361 if (j
< len
) row
[j
++] = DOT
;
362 do_recurse(known
, deduced
, row
, data
, len
,
363 freespace
-i
, ndone
+1, j
);
366 for (i
=lowest
; i
<len
; i
++)
368 for (i
=0; i
<len
; i
++)
369 if (known
[i
] && known
[i
] != row
[i
])
371 for (i
=0; i
<len
; i
++)
372 deduced
[i
] |= row
[i
];
376 static int do_row(unsigned char *known
, unsigned char *deduced
,
378 unsigned char *start
, int len
, int step
, int *data
)
380 int rowlen
, i
, freespace
, done_any
;
383 for (rowlen
= 0; data
[rowlen
]; rowlen
++)
384 freespace
-= data
[rowlen
]+1;
386 for (i
= 0; i
< len
; i
++) {
387 known
[i
] = start
[i
*step
];
391 do_recurse(known
, deduced
, row
, data
, len
, freespace
, 0, 0);
393 for (i
=0; i
<len
; i
++)
394 if (deduced
[i
] && deduced
[i
] != STILL_UNKNOWN
&& !known
[i
]) {
395 start
[i
*step
] = deduced
[i
];
401 static unsigned char *generate_soluble(random_state
*rs
, int w
, int h
)
403 int i
, j
, done_any
, ok
, ntries
, max
;
404 unsigned char *grid
, *matrix
, *workspace
;
407 grid
= snewn(w
*h
, unsigned char);
408 matrix
= snewn(w
*h
, unsigned char);
410 workspace
= snewn(max
*3, unsigned char);
411 rowdata
= snewn(max
+1, int);
418 generate(rs
, w
, h
, grid
);
421 * The game is a bit too easy if any row or column is
422 * completely black or completely white. An exception is
423 * made for rows/columns that are under 3 squares,
424 * otherwise nothing will ever be successfully generated.
428 for (i
= 0; i
< h
; i
++) {
430 for (j
= 0; j
< w
; j
++)
431 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
437 for (j
= 0; j
< w
; j
++) {
439 for (i
= 0; i
< h
; i
++)
440 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
448 memset(matrix
, 0, w
*h
);
452 for (i
=0; i
<h
; i
++) {
453 rowdata
[compute_rowdata(rowdata
, grid
+i
*w
, w
, 1)] = 0;
454 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
455 matrix
+i
*w
, w
, 1, rowdata
);
457 for (i
=0; i
<w
; i
++) {
458 rowdata
[compute_rowdata(rowdata
, grid
+i
, h
, w
)] = 0;
459 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
460 matrix
+i
, h
, w
, rowdata
);
465 for (i
=0; i
<h
; i
++) {
466 for (j
=0; j
<w
; j
++) {
467 if (matrix
[i
*w
+j
] == UNKNOWN
)
479 static char *new_game_seed(game_params
*params
, random_state
*rs
)
482 int i
, j
, max
, rowlen
, *rowdata
;
483 char intbuf
[80], *seed
;
484 int seedlen
, seedpos
;
486 grid
= generate_soluble(rs
, params
->w
, params
->h
);
487 max
= max(params
->w
, params
->h
);
488 rowdata
= snewn(max
, int);
491 * Seed is a slash-separated list of row contents; each row
492 * contents section is a dot-separated list of integers. Row
493 * contents are listed in the order (columns left to right,
494 * then rows top to bottom).
496 * Simplest way to handle memory allocation is to make two
497 * passes, first computing the seed size and then writing it
501 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
503 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
505 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
508 for (j
= 0; j
< rowlen
; j
++) {
509 seedlen
+= 1 + sprintf(intbuf
, "%d", rowdata
[j
]);
515 seed
= snewn(seedlen
, char);
517 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
519 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
521 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
524 for (j
= 0; j
< rowlen
; j
++) {
525 int len
= sprintf(seed
+seedpos
, "%d", rowdata
[j
]);
527 seed
[seedpos
+ len
] = '.';
529 seed
[seedpos
+ len
] = '/';
533 seed
[seedpos
++] = '/';
536 assert(seedpos
== seedlen
);
537 assert(seed
[seedlen
-1] == '/');
538 seed
[seedlen
-1] = '\0';
543 static char *validate_seed(game_params
*params
, char *seed
)
548 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
550 rowspace
= params
->h
+ 1;
552 rowspace
= params
->w
+ 1;
554 if (*seed
&& isdigit((unsigned char)*seed
)) {
557 while (seed
&& isdigit((unsigned char)*seed
)) seed
++;
563 return "at least one column contains more numbers than will fit";
565 return "at least one row contains more numbers than will fit";
567 } while (*seed
++ == '.');
569 seed
++; /* expect a slash immediately */
572 if (seed
[-1] == '/') {
573 if (i
+1 == params
->w
+ params
->h
)
574 return "too many row/column specifications";
575 } else if (seed
[-1] == '\0') {
576 if (i
+1 < params
->w
+ params
->h
)
577 return "too few row/column specifications";
579 return "unrecognised character in game specification";
585 static game_state
*new_game(game_params
*params
, char *seed
)
589 game_state
*state
= snew(game_state
);
591 state
->w
= params
->w
;
592 state
->h
= params
->h
;
594 state
->grid
= snewn(state
->w
* state
->h
, unsigned char);
595 memset(state
->grid
, GRID_UNKNOWN
, state
->w
* state
->h
);
597 state
->rowsize
= max(state
->w
, state
->h
);
598 state
->rowdata
= snewn(state
->rowsize
* (state
->w
+ state
->h
), int);
599 state
->rowlen
= snewn(state
->w
+ state
->h
, int);
601 state
->completed
= FALSE
;
603 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
604 state
->rowlen
[i
] = 0;
605 if (*seed
&& isdigit((unsigned char)*seed
)) {
608 while (seed
&& isdigit((unsigned char)*seed
)) seed
++;
609 state
->rowdata
[state
->rowsize
* i
+ state
->rowlen
[i
]++] =
611 } while (*seed
++ == '.');
613 seed
++; /* expect a slash immediately */
620 static game_state
*dup_game(game_state
*state
)
622 game_state
*ret
= snew(game_state
);
627 ret
->grid
= snewn(ret
->w
* ret
->h
, unsigned char);
628 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
630 ret
->rowsize
= state
->rowsize
;
631 ret
->rowdata
= snewn(ret
->rowsize
* (ret
->w
+ ret
->h
), int);
632 ret
->rowlen
= snewn(ret
->w
+ ret
->h
, int);
633 memcpy(ret
->rowdata
, state
->rowdata
,
634 ret
->rowsize
* (ret
->w
+ ret
->h
) * sizeof(int));
635 memcpy(ret
->rowlen
, state
->rowlen
,
636 (ret
->w
+ ret
->h
) * sizeof(int));
638 ret
->completed
= state
->completed
;
643 static void free_game(game_state
*state
)
645 sfree(state
->rowdata
);
646 sfree(state
->rowlen
);
657 int drag
, release
, state
;
660 static game_ui
*new_ui(game_state
*state
)
665 ret
->dragging
= FALSE
;
670 static void free_ui(game_ui
*ui
)
675 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
676 int x
, int y
, int button
)
680 x
= FROMCOORD(from
->w
, x
);
681 y
= FROMCOORD(from
->h
, y
);
683 if (x
>= 0 && x
< from
->w
&& y
>= 0 && y
< from
->h
&&
684 (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
||
685 button
== MIDDLE_BUTTON
)) {
689 if (button
== LEFT_BUTTON
) {
690 ui
->drag
= LEFT_DRAG
;
691 ui
->release
= LEFT_RELEASE
;
692 ui
->state
= GRID_FULL
;
693 } else if (button
== RIGHT_BUTTON
) {
694 ui
->drag
= RIGHT_DRAG
;
695 ui
->release
= RIGHT_RELEASE
;
696 ui
->state
= GRID_EMPTY
;
697 } else /* if (button == MIDDLE_BUTTON) */ {
698 ui
->drag
= MIDDLE_DRAG
;
699 ui
->release
= MIDDLE_RELEASE
;
700 ui
->state
= GRID_UNKNOWN
;
703 ui
->drag_start_x
= ui
->drag_end_x
= x
;
704 ui
->drag_start_y
= ui
->drag_end_y
= y
;
706 return from
; /* UI activity occurred */
709 if (ui
->dragging
&& button
== ui
->drag
) {
711 * There doesn't seem much point in allowing a rectangle
712 * drag; people will generally only want to drag a single
713 * horizontal or vertical line, so we make that easy by
716 * Exception: if we're _middle_-button dragging to tag
717 * things as UNKNOWN, we may well want to trash an entire
718 * area and start over!
720 if (ui
->state
!= GRID_UNKNOWN
) {
721 if (abs(x
- ui
->drag_start_x
) > abs(y
- ui
->drag_start_y
))
722 y
= ui
->drag_start_y
;
724 x
= ui
->drag_start_x
;
729 if (x
>= from
->w
) x
= from
->w
- 1;
730 if (y
>= from
->h
) y
= from
->h
- 1;
735 return from
; /* UI activity occurred */
738 if (ui
->dragging
&& button
== ui
->release
) {
739 int x1
, x2
, y1
, y2
, xx
, yy
;
740 int move_needed
= FALSE
;
742 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
743 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
744 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
745 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
747 for (yy
= y1
; yy
<= y2
; yy
++)
748 for (xx
= x1
; xx
<= x2
; xx
++)
749 if (from
->grid
[yy
* from
->w
+ xx
] != ui
->state
)
752 ui
->dragging
= FALSE
;
755 ret
= dup_game(from
);
756 for (yy
= y1
; yy
<= y2
; yy
++)
757 for (xx
= x1
; xx
<= x2
; xx
++)
758 ret
->grid
[yy
* ret
->w
+ xx
] = ui
->state
;
761 * An actual change, so check to see if we've completed
764 if (!ret
->completed
) {
765 int *rowdata
= snewn(ret
->rowsize
, int);
768 ret
->completed
= TRUE
;
770 for (i
=0; i
<ret
->w
; i
++) {
771 len
= compute_rowdata(rowdata
,
772 ret
->grid
+i
, ret
->h
, ret
->w
);
773 if (len
!= ret
->rowlen
[i
] ||
774 memcmp(ret
->rowdata
+i
*ret
->rowsize
, rowdata
,
775 len
* sizeof(int))) {
776 ret
->completed
= FALSE
;
780 for (i
=0; i
<ret
->h
; i
++) {
781 len
= compute_rowdata(rowdata
,
782 ret
->grid
+i
*ret
->w
, ret
->w
, 1);
783 if (len
!= ret
->rowlen
[i
+ret
->w
] ||
784 memcmp(ret
->rowdata
+(i
+ret
->w
)*ret
->rowsize
, rowdata
,
785 len
* sizeof(int))) {
786 ret
->completed
= FALSE
;
796 return from
; /* UI activity occurred */
802 /* ----------------------------------------------------------------------
806 struct game_drawstate
{
809 unsigned char *visible
;
812 static void game_size(game_params
*params
, int *x
, int *y
)
814 *x
= SIZE(params
->w
);
815 *y
= SIZE(params
->h
);
818 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
820 float *ret
= snewn(3 * NCOLOURS
, float);
822 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
824 ret
[COL_GRID
* 3 + 0] = 0.3F
;
825 ret
[COL_GRID
* 3 + 1] = 0.3F
;
826 ret
[COL_GRID
* 3 + 2] = 0.3F
;
828 ret
[COL_UNKNOWN
* 3 + 0] = 0.5F
;
829 ret
[COL_UNKNOWN
* 3 + 1] = 0.5F
;
830 ret
[COL_UNKNOWN
* 3 + 2] = 0.5F
;
832 ret
[COL_FULL
* 3 + 0] = 0.0F
;
833 ret
[COL_FULL
* 3 + 1] = 0.0F
;
834 ret
[COL_FULL
* 3 + 2] = 0.0F
;
836 ret
[COL_EMPTY
* 3 + 0] = 1.0F
;
837 ret
[COL_EMPTY
* 3 + 1] = 1.0F
;
838 ret
[COL_EMPTY
* 3 + 2] = 1.0F
;
840 *ncolours
= NCOLOURS
;
844 static game_drawstate
*game_new_drawstate(game_state
*state
)
846 struct game_drawstate
*ds
= snew(struct game_drawstate
);
851 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned char);
852 memset(ds
->visible
, 255, ds
->w
* ds
->h
);
857 static void game_free_drawstate(game_drawstate
*ds
)
863 static void grid_square(frontend
*fe
, game_drawstate
*ds
,
864 int y
, int x
, int state
)
868 draw_rect(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
869 TILE_SIZE
, TILE_SIZE
, COL_GRID
);
871 xl
= (x
% 5 == 0 ?
1 : 0);
872 yt
= (y
% 5 == 0 ?
1 : 0);
873 xr
= (x
% 5 == 4 || x
== ds
->w
-1 ?
1 : 0);
874 yb
= (y
% 5 == 4 || y
== ds
->h
-1 ?
1 : 0);
876 draw_rect(fe
, TOCOORD(ds
->w
, x
) + 1 + xl
, TOCOORD(ds
->h
, y
) + 1 + yt
,
877 TILE_SIZE
- xl
- xr
- 1, TILE_SIZE
- yt
- yb
- 1,
878 (state
== GRID_FULL ? COL_FULL
:
879 state
== GRID_EMPTY ? COL_EMPTY
: COL_UNKNOWN
));
881 draw_update(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
882 TILE_SIZE
, TILE_SIZE
);
885 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
886 game_state
*state
, int dir
, game_ui
*ui
,
887 float animtime
, float flashtime
)
894 * The initial contents of the window are not guaranteed
895 * and can vary with front ends. To be on the safe side,
896 * all games should start by drawing a big background-
897 * colour rectangle covering the whole window.
899 draw_rect(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
), COL_BACKGROUND
);
904 for (i
= 0; i
< ds
->w
+ ds
->h
; i
++) {
905 int rowlen
= state
->rowlen
[i
];
906 int *rowdata
= state
->rowdata
+ state
->rowsize
* i
;
910 * Normally I space the numbers out by the same
911 * distance as the tile size. However, if there are
912 * more numbers than available spaces, I have to squash
915 nfit
= max(rowlen
, TLBORDER(ds
->h
))-1;
918 for (j
= 0; j
< rowlen
; j
++) {
923 x
= TOCOORD(ds
->w
, i
);
924 y
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->h
)-1);
925 y
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
927 y
= TOCOORD(ds
->h
, i
- ds
->w
);
928 x
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->w
)-1);
929 x
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
932 sprintf(str
, "%d", rowdata
[j
]);
933 draw_text(fe
, x
+TILE_SIZE
/2, y
+TILE_SIZE
/2, FONT_VARIABLE
,
934 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
,
935 COL_FULL
, str
); /* FIXME: COL_TEXT */
940 * Draw the grid outline.
942 draw_rect(fe
, TOCOORD(ds
->w
, 0) - 1, TOCOORD(ds
->h
, 0) - 1,
943 ds
->w
* TILE_SIZE
+ 3, ds
->h
* TILE_SIZE
+ 3,
948 draw_update(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
));
952 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
953 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
954 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
955 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
957 x1
= x2
= y1
= y2
= -1; /* placate gcc warnings */
961 * Now draw any grid squares which have changed since last
964 for (i
= 0; i
< ds
->h
; i
++) {
965 for (j
= 0; j
< ds
->w
; j
++) {
969 * Work out what state this square should be drawn in,
970 * taking any current drag operation into account.
972 if (ui
->dragging
&& x1
<= j
&& j
<= x2
&& y1
<= i
&& i
<= y2
)
975 val
= state
->grid
[i
* state
->w
+ j
];
978 * Briefly invert everything twice during a completion
982 (flashtime
<= FLASH_TIME
/3 || flashtime
>= FLASH_TIME
*2/3) &&
984 val
= (GRID_FULL
^ GRID_EMPTY
) ^ val
;
986 if (ds
->visible
[i
* ds
->w
+ j
] != val
) {
987 grid_square(fe
, ds
, i
, j
, val
);
988 ds
->visible
[i
* ds
->w
+ j
] = val
;
994 static float game_anim_length(game_state
*oldstate
,
995 game_state
*newstate
, int dir
)
1000 static float game_flash_length(game_state
*oldstate
,
1001 game_state
*newstate
, int dir
)
1003 if (!oldstate
->completed
&& newstate
->completed
)
1008 static int game_wants_statusbar(void)
1014 #define thegame pattern
1017 const struct game thegame
= {
1018 "Pattern", "games.pattern", TRUE
,
1039 game_free_drawstate
,
1043 game_wants_statusbar
,
1046 #ifdef STANDALONE_SOLVER
1049 * gcc -DSTANDALONE_SOLVER -o patternsolver pattern.c malloc.c
1054 void frontend_default_colour(frontend
*fe
, float *output
) {}
1055 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
1056 int align
, int colour
, char *text
) {}
1057 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
) {}
1058 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
) {}
1059 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
1060 int fill
, int colour
) {}
1061 void clip(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1062 void unclip(frontend
*fe
) {}
1063 void start_draw(frontend
*fe
) {}
1064 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1065 void end_draw(frontend
*fe
) {}
1066 unsigned long random_upto(random_state
*state
, unsigned long limit
)
1067 { assert(!"Shouldn't get randomness"); return 0; }
1069 void fatal(char *fmt
, ...)
1073 fprintf(stderr
, "fatal error: ");
1076 vfprintf(stderr
, fmt
, ap
);
1079 fprintf(stderr
, "\n");
1083 int main(int argc
, char **argv
)
1088 char *id
= NULL
, *seed
, *err
;
1092 while (--argc
> 0) {
1095 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0]);
1103 fprintf(stderr
, "usage: %s <game_id>\n", argv
[0]);
1107 seed
= strchr(id
, ':');
1109 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1114 p
= decode_params(id
);
1115 err
= validate_seed(p
, seed
);
1117 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1120 s
= new_game(p
, seed
);
1123 int w
= p
->w
, h
= p
->h
, i
, j
, done_any
, max
;
1124 unsigned char *matrix
, *workspace
;
1127 matrix
= snewn(w
*h
, unsigned char);
1129 workspace
= snewn(max
*3, unsigned char);
1130 rowdata
= snewn(max
+1, int);
1132 memset(matrix
, 0, w
*h
);
1136 for (i
=0; i
<h
; i
++) {
1137 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*(w
+i
),
1139 rowdata
[s
->rowlen
[w
+i
]] = 0;
1140 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1141 matrix
+i
*w
, w
, 1, rowdata
);
1143 for (i
=0; i
<w
; i
++) {
1144 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*i
, max
*sizeof(int));
1145 rowdata
[s
->rowlen
[i
]] = 0;
1146 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1147 matrix
+i
, h
, w
, rowdata
);
1151 for (i
= 0; i
< h
; i
++) {
1152 for (j
= 0; j
< w
; j
++) {
1153 int c
= (matrix
[i
*w
+j
] == UNKNOWN ?
'?' :
1154 matrix
[i
*w
+j
] == BLOCK ?
'#' :
1155 matrix
[i
*w
+j
] == DOT ?
'.' :