2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
24 #define TLBORDER(d) ( (d) / 5 + 2 )
28 #define FROMCOORD(d, x) \
29 ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
31 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
33 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
39 #define GRID_UNKNOWN 2
47 int *rowdata
, *rowlen
;
48 int completed
, cheated
;
51 #define FLASH_TIME 0.13F
53 static game_params
*default_params(void)
55 game_params
*ret
= snew(game_params
);
62 static const struct game_params pattern_presets
[] = {
72 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
77 if (i
< 0 || i
>= lenof(pattern_presets
))
80 ret
= snew(game_params
);
81 *ret
= pattern_presets
[i
];
83 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
90 static void free_params(game_params
*params
)
95 static game_params
*dup_params(game_params
*params
)
97 game_params
*ret
= snew(game_params
);
98 *ret
= *params
; /* structure copy */
102 static void decode_params(game_params
*ret
, char const *string
)
104 char const *p
= string
;
107 while (*p
&& isdigit(*p
)) p
++;
111 while (*p
&& isdigit(*p
)) p
++;
117 static char *encode_params(game_params
*params
, int full
)
122 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
123 assert(len
< lenof(ret
));
129 static config_item
*game_configure(game_params
*params
)
134 ret
= snewn(3, config_item
);
136 ret
[0].name
= "Width";
137 ret
[0].type
= C_STRING
;
138 sprintf(buf
, "%d", params
->w
);
139 ret
[0].sval
= dupstr(buf
);
142 ret
[1].name
= "Height";
143 ret
[1].type
= C_STRING
;
144 sprintf(buf
, "%d", params
->h
);
145 ret
[1].sval
= dupstr(buf
);
156 static game_params
*custom_params(config_item
*cfg
)
158 game_params
*ret
= snew(game_params
);
160 ret
->w
= atoi(cfg
[0].sval
);
161 ret
->h
= atoi(cfg
[1].sval
);
166 static char *validate_params(game_params
*params
)
168 if (params
->w
<= 0 || params
->h
<= 0)
169 return "Width and height must both be greater than zero";
173 /* ----------------------------------------------------------------------
174 * Puzzle generation code.
176 * For this particular puzzle, it seemed important to me to ensure
177 * a unique solution. I do this the brute-force way, by having a
178 * solver algorithm alongside the generator, and repeatedly
179 * generating a random grid until I find one whose solution is
180 * unique. It turns out that this isn't too onerous on a modern PC
181 * provided you keep grid size below around 30. Any offers of
182 * better algorithms, however, will be very gratefully received.
184 * Another annoyance of this approach is that it limits the
185 * available puzzles to those solvable by the algorithm I've used.
186 * My algorithm only ever considers a single row or column at any
187 * one time, which means it's incapable of solving the following
188 * difficult example (found by Bella Image around 1995/6, when she
189 * and I were both doing maths degrees):
203 * Obviously this cannot be solved by a one-row-or-column-at-a-time
204 * algorithm (it would require at least one row or column reading
205 * `2 1', `1 2', `3' or `4' to get started). However, it can be
206 * proved to have a unique solution: if the top left square were
207 * empty, then the only option for the top row would be to fill the
208 * two squares in the 1 columns, which would imply the squares
209 * below those were empty, leaving no place for the 2 in the second
210 * row. Contradiction. Hence the top left square is full, and the
211 * unique solution follows easily from that starting point.
213 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case
214 * it's useful to anyone.)
217 static int float_compare(const void *av
, const void *bv
)
219 const float *a
= (const float *)av
;
220 const float *b
= (const float *)bv
;
229 static void generate(random_state
*rs
, int w
, int h
, unsigned char *retgrid
)
236 fgrid
= snewn(w
*h
, float);
238 for (i
= 0; i
< h
; i
++) {
239 for (j
= 0; j
< w
; j
++) {
240 fgrid
[i
*w
+j
] = random_upto(rs
, 100000000UL) / 100000000.F
;
245 * The above gives a completely random splattering of black and
246 * white cells. We want to gently bias this in favour of _some_
247 * reasonably thick areas of white and black, while retaining
248 * some randomness and fine detail.
250 * So we evolve the starting grid using a cellular automaton.
251 * Currently, I'm doing something very simple indeed, which is
252 * to set each square to the average of the surrounding nine
253 * cells (or the average of fewer, if we're on a corner).
255 for (step
= 0; step
< 1; step
++) {
256 fgrid2
= snewn(w
*h
, float);
258 for (i
= 0; i
< h
; i
++) {
259 for (j
= 0; j
< w
; j
++) {
264 * Compute the average of the surrounding cells.
268 for (p
= -1; p
<= +1; p
++) {
269 for (q
= -1; q
<= +1; q
++) {
270 if (i
+p
< 0 || i
+p
>= h
|| j
+q
< 0 || j
+q
>= w
)
273 * An additional special case not mentioned
274 * above: if a grid dimension is 2xn then
275 * we do not average across that dimension
276 * at all. Otherwise a 2x2 grid would
277 * contain four identical squares.
279 if ((h
==2 && p
!=0) || (w
==2 && q
!=0))
282 sx
+= fgrid
[(i
+p
)*w
+(j
+q
)];
287 fgrid2
[i
*w
+j
] = xbar
;
295 fgrid2
= snewn(w
*h
, float);
296 memcpy(fgrid2
, fgrid
, w
*h
*sizeof(float));
297 qsort(fgrid2
, w
*h
, sizeof(float), float_compare
);
298 threshold
= fgrid2
[w
*h
/2];
301 for (i
= 0; i
< h
; i
++) {
302 for (j
= 0; j
< w
; j
++) {
303 retgrid
[i
*w
+j
] = (fgrid
[i
*w
+j
] >= threshold ? GRID_FULL
:
311 static int compute_rowdata(int *ret
, unsigned char *start
, int len
, int step
)
317 for (i
= 0; i
< len
; i
++) {
318 if (start
[i
*step
] == GRID_FULL
) {
320 while (i
+runlen
< len
&& start
[(i
+runlen
)*step
] == GRID_FULL
)
326 if (i
< len
&& start
[i
*step
] == GRID_UNKNOWN
)
336 #define STILL_UNKNOWN 3
338 static void do_recurse(unsigned char *known
, unsigned char *deduced
,
339 unsigned char *row
, int *data
, int len
,
340 int freespace
, int ndone
, int lowest
)
345 for (i
=0; i
<=freespace
; i
++) {
347 for (k
=0; k
<i
; k
++) row
[j
++] = DOT
;
348 for (k
=0; k
<data
[ndone
]; k
++) row
[j
++] = BLOCK
;
349 if (j
< len
) row
[j
++] = DOT
;
350 do_recurse(known
, deduced
, row
, data
, len
,
351 freespace
-i
, ndone
+1, j
);
354 for (i
=lowest
; i
<len
; i
++)
356 for (i
=0; i
<len
; i
++)
357 if (known
[i
] && known
[i
] != row
[i
])
359 for (i
=0; i
<len
; i
++)
360 deduced
[i
] |= row
[i
];
364 static int do_row(unsigned char *known
, unsigned char *deduced
,
366 unsigned char *start
, int len
, int step
, int *data
)
368 int rowlen
, i
, freespace
, done_any
;
371 for (rowlen
= 0; data
[rowlen
]; rowlen
++)
372 freespace
-= data
[rowlen
]+1;
374 for (i
= 0; i
< len
; i
++) {
375 known
[i
] = start
[i
*step
];
379 do_recurse(known
, deduced
, row
, data
, len
, freespace
, 0, 0);
381 for (i
=0; i
<len
; i
++)
382 if (deduced
[i
] && deduced
[i
] != STILL_UNKNOWN
&& !known
[i
]) {
383 start
[i
*step
] = deduced
[i
];
389 static unsigned char *generate_soluble(random_state
*rs
, int w
, int h
)
391 int i
, j
, done_any
, ok
, ntries
, max
;
392 unsigned char *grid
, *matrix
, *workspace
;
395 grid
= snewn(w
*h
, unsigned char);
396 matrix
= snewn(w
*h
, unsigned char);
398 workspace
= snewn(max
*3, unsigned char);
399 rowdata
= snewn(max
+1, int);
406 generate(rs
, w
, h
, grid
);
409 * The game is a bit too easy if any row or column is
410 * completely black or completely white. An exception is
411 * made for rows/columns that are under 3 squares,
412 * otherwise nothing will ever be successfully generated.
416 for (i
= 0; i
< h
; i
++) {
418 for (j
= 0; j
< w
; j
++)
419 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
425 for (j
= 0; j
< w
; j
++) {
427 for (i
= 0; i
< h
; i
++)
428 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
436 memset(matrix
, 0, w
*h
);
440 for (i
=0; i
<h
; i
++) {
441 rowdata
[compute_rowdata(rowdata
, grid
+i
*w
, w
, 1)] = 0;
442 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
443 matrix
+i
*w
, w
, 1, rowdata
);
445 for (i
=0; i
<w
; i
++) {
446 rowdata
[compute_rowdata(rowdata
, grid
+i
, h
, w
)] = 0;
447 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
448 matrix
+i
, h
, w
, rowdata
);
453 for (i
=0; i
<h
; i
++) {
454 for (j
=0; j
<w
; j
++) {
455 if (matrix
[i
*w
+j
] == UNKNOWN
)
467 struct game_aux_info
{
472 static char *new_game_desc(game_params
*params
, random_state
*rs
,
473 game_aux_info
**aux
, int interactive
)
476 int i
, j
, max
, rowlen
, *rowdata
;
477 char intbuf
[80], *desc
;
478 int desclen
, descpos
;
480 grid
= generate_soluble(rs
, params
->w
, params
->h
);
481 max
= max(params
->w
, params
->h
);
482 rowdata
= snewn(max
, int);
485 * Save the solved game in an aux_info.
488 game_aux_info
*ai
= snew(game_aux_info
);
498 * Seed is a slash-separated list of row contents; each row
499 * contents section is a dot-separated list of integers. Row
500 * contents are listed in the order (columns left to right,
501 * then rows top to bottom).
503 * Simplest way to handle memory allocation is to make two
504 * passes, first computing the seed size and then writing it
508 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
510 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
512 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
515 for (j
= 0; j
< rowlen
; j
++) {
516 desclen
+= 1 + sprintf(intbuf
, "%d", rowdata
[j
]);
522 desc
= snewn(desclen
, char);
524 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
526 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
528 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
531 for (j
= 0; j
< rowlen
; j
++) {
532 int len
= sprintf(desc
+descpos
, "%d", rowdata
[j
]);
534 desc
[descpos
+ len
] = '.';
536 desc
[descpos
+ len
] = '/';
540 desc
[descpos
++] = '/';
543 assert(descpos
== desclen
);
544 assert(desc
[desclen
-1] == '/');
545 desc
[desclen
-1] = '\0';
550 static void game_free_aux_info(game_aux_info
*aux
)
556 static char *validate_desc(game_params
*params
, char *desc
)
561 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
563 rowspace
= params
->h
+ 1;
565 rowspace
= params
->w
+ 1;
567 if (*desc
&& isdigit((unsigned char)*desc
)) {
570 while (desc
&& isdigit((unsigned char)*desc
)) desc
++;
576 return "at least one column contains more numbers than will fit";
578 return "at least one row contains more numbers than will fit";
580 } while (*desc
++ == '.');
582 desc
++; /* expect a slash immediately */
585 if (desc
[-1] == '/') {
586 if (i
+1 == params
->w
+ params
->h
)
587 return "too many row/column specifications";
588 } else if (desc
[-1] == '\0') {
589 if (i
+1 < params
->w
+ params
->h
)
590 return "too few row/column specifications";
592 return "unrecognised character in game specification";
598 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
602 game_state
*state
= snew(game_state
);
604 state
->w
= params
->w
;
605 state
->h
= params
->h
;
607 state
->grid
= snewn(state
->w
* state
->h
, unsigned char);
608 memset(state
->grid
, GRID_UNKNOWN
, state
->w
* state
->h
);
610 state
->rowsize
= max(state
->w
, state
->h
);
611 state
->rowdata
= snewn(state
->rowsize
* (state
->w
+ state
->h
), int);
612 state
->rowlen
= snewn(state
->w
+ state
->h
, int);
614 state
->completed
= state
->cheated
= FALSE
;
616 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
617 state
->rowlen
[i
] = 0;
618 if (*desc
&& isdigit((unsigned char)*desc
)) {
621 while (desc
&& isdigit((unsigned char)*desc
)) desc
++;
622 state
->rowdata
[state
->rowsize
* i
+ state
->rowlen
[i
]++] =
624 } while (*desc
++ == '.');
626 desc
++; /* expect a slash immediately */
633 static game_state
*dup_game(game_state
*state
)
635 game_state
*ret
= snew(game_state
);
640 ret
->grid
= snewn(ret
->w
* ret
->h
, unsigned char);
641 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
);
643 ret
->rowsize
= state
->rowsize
;
644 ret
->rowdata
= snewn(ret
->rowsize
* (ret
->w
+ ret
->h
), int);
645 ret
->rowlen
= snewn(ret
->w
+ ret
->h
, int);
646 memcpy(ret
->rowdata
, state
->rowdata
,
647 ret
->rowsize
* (ret
->w
+ ret
->h
) * sizeof(int));
648 memcpy(ret
->rowlen
, state
->rowlen
,
649 (ret
->w
+ ret
->h
) * sizeof(int));
651 ret
->completed
= state
->completed
;
652 ret
->cheated
= state
->cheated
;
657 static void free_game(game_state
*state
)
659 sfree(state
->rowdata
);
660 sfree(state
->rowlen
);
665 static game_state
*solve_game(game_state
*state
, game_aux_info
*ai
,
670 ret
= dup_game(state
);
671 ret
->completed
= ret
->cheated
= TRUE
;
674 * If we already have the solved state in an aux_info, copy it
679 assert(ret
->w
== ai
->w
);
680 assert(ret
->h
== ai
->h
);
681 memcpy(ret
->grid
, ai
->grid
, ai
->w
* ai
->h
);
684 int w
= state
->w
, h
= state
->h
, i
, j
, done_any
, max
;
685 unsigned char *matrix
, *workspace
;
688 matrix
= snewn(w
*h
, unsigned char);
690 workspace
= snewn(max
*3, unsigned char);
691 rowdata
= snewn(max
+1, int);
693 memset(matrix
, 0, w
*h
);
697 for (i
=0; i
<h
; i
++) {
698 memcpy(rowdata
, state
->rowdata
+ state
->rowsize
*(w
+i
),
700 rowdata
[state
->rowlen
[w
+i
]] = 0;
701 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
702 matrix
+i
*w
, w
, 1, rowdata
);
704 for (i
=0; i
<w
; i
++) {
705 memcpy(rowdata
, state
->rowdata
+ state
->rowsize
*i
, max
*sizeof(int));
706 rowdata
[state
->rowlen
[i
]] = 0;
707 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
708 matrix
+i
, h
, w
, rowdata
);
712 for (i
= 0; i
< h
; i
++) {
713 for (j
= 0; j
< w
; j
++) {
714 int c
= (matrix
[i
*w
+j
] == BLOCK ? GRID_FULL
:
715 matrix
[i
*w
+j
] == DOT ? GRID_EMPTY
: GRID_UNKNOWN
);
716 ret
->grid
[i
*w
+j
] = c
;
717 if (c
== GRID_UNKNOWN
)
718 ret
->completed
= FALSE
;
722 if (!ret
->completed
) {
724 *error
= "Solving algorithm cannot complete this puzzle";
732 static char *game_text_format(game_state
*state
)
743 int drag
, release
, state
;
746 static game_ui
*new_ui(game_state
*state
)
751 ret
->dragging
= FALSE
;
756 static void free_ui(game_ui
*ui
)
761 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
762 int x
, int y
, int button
) {
767 x
= FROMCOORD(from
->w
, x
);
768 y
= FROMCOORD(from
->h
, y
);
770 if (x
>= 0 && x
< from
->w
&& y
>= 0 && y
< from
->h
&&
771 (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
||
772 button
== MIDDLE_BUTTON
)) {
776 if (button
== LEFT_BUTTON
) {
777 ui
->drag
= LEFT_DRAG
;
778 ui
->release
= LEFT_RELEASE
;
779 ui
->state
= GRID_FULL
;
780 } else if (button
== RIGHT_BUTTON
) {
781 ui
->drag
= RIGHT_DRAG
;
782 ui
->release
= RIGHT_RELEASE
;
783 ui
->state
= GRID_EMPTY
;
784 } else /* if (button == MIDDLE_BUTTON) */ {
785 ui
->drag
= MIDDLE_DRAG
;
786 ui
->release
= MIDDLE_RELEASE
;
787 ui
->state
= GRID_UNKNOWN
;
790 ui
->drag_start_x
= ui
->drag_end_x
= x
;
791 ui
->drag_start_y
= ui
->drag_end_y
= y
;
793 return from
; /* UI activity occurred */
796 if (ui
->dragging
&& button
== ui
->drag
) {
798 * There doesn't seem much point in allowing a rectangle
799 * drag; people will generally only want to drag a single
800 * horizontal or vertical line, so we make that easy by
803 * Exception: if we're _middle_-button dragging to tag
804 * things as UNKNOWN, we may well want to trash an entire
805 * area and start over!
807 if (ui
->state
!= GRID_UNKNOWN
) {
808 if (abs(x
- ui
->drag_start_x
) > abs(y
- ui
->drag_start_y
))
809 y
= ui
->drag_start_y
;
811 x
= ui
->drag_start_x
;
816 if (x
>= from
->w
) x
= from
->w
- 1;
817 if (y
>= from
->h
) y
= from
->h
- 1;
822 return from
; /* UI activity occurred */
825 if (ui
->dragging
&& button
== ui
->release
) {
826 int x1
, x2
, y1
, y2
, xx
, yy
;
827 int move_needed
= FALSE
;
829 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
830 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
831 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
832 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
834 for (yy
= y1
; yy
<= y2
; yy
++)
835 for (xx
= x1
; xx
<= x2
; xx
++)
836 if (from
->grid
[yy
* from
->w
+ xx
] != ui
->state
)
839 ui
->dragging
= FALSE
;
842 ret
= dup_game(from
);
843 for (yy
= y1
; yy
<= y2
; yy
++)
844 for (xx
= x1
; xx
<= x2
; xx
++)
845 ret
->grid
[yy
* ret
->w
+ xx
] = ui
->state
;
848 * An actual change, so check to see if we've completed
851 if (!ret
->completed
) {
852 int *rowdata
= snewn(ret
->rowsize
, int);
855 ret
->completed
= TRUE
;
857 for (i
=0; i
<ret
->w
; i
++) {
858 len
= compute_rowdata(rowdata
,
859 ret
->grid
+i
, ret
->h
, ret
->w
);
860 if (len
!= ret
->rowlen
[i
] ||
861 memcmp(ret
->rowdata
+i
*ret
->rowsize
, rowdata
,
862 len
* sizeof(int))) {
863 ret
->completed
= FALSE
;
867 for (i
=0; i
<ret
->h
; i
++) {
868 len
= compute_rowdata(rowdata
,
869 ret
->grid
+i
*ret
->w
, ret
->w
, 1);
870 if (len
!= ret
->rowlen
[i
+ret
->w
] ||
871 memcmp(ret
->rowdata
+(i
+ret
->w
)*ret
->rowsize
, rowdata
,
872 len
* sizeof(int))) {
873 ret
->completed
= FALSE
;
883 return from
; /* UI activity occurred */
889 /* ----------------------------------------------------------------------
893 struct game_drawstate
{
896 unsigned char *visible
;
899 static void game_size(game_params
*params
, int *x
, int *y
)
901 *x
= SIZE(params
->w
);
902 *y
= SIZE(params
->h
);
905 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
907 float *ret
= snewn(3 * NCOLOURS
, float);
909 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
911 ret
[COL_GRID
* 3 + 0] = 0.3F
;
912 ret
[COL_GRID
* 3 + 1] = 0.3F
;
913 ret
[COL_GRID
* 3 + 2] = 0.3F
;
915 ret
[COL_UNKNOWN
* 3 + 0] = 0.5F
;
916 ret
[COL_UNKNOWN
* 3 + 1] = 0.5F
;
917 ret
[COL_UNKNOWN
* 3 + 2] = 0.5F
;
919 ret
[COL_FULL
* 3 + 0] = 0.0F
;
920 ret
[COL_FULL
* 3 + 1] = 0.0F
;
921 ret
[COL_FULL
* 3 + 2] = 0.0F
;
923 ret
[COL_EMPTY
* 3 + 0] = 1.0F
;
924 ret
[COL_EMPTY
* 3 + 1] = 1.0F
;
925 ret
[COL_EMPTY
* 3 + 2] = 1.0F
;
927 *ncolours
= NCOLOURS
;
931 static game_drawstate
*game_new_drawstate(game_state
*state
)
933 struct game_drawstate
*ds
= snew(struct game_drawstate
);
938 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned char);
939 memset(ds
->visible
, 255, ds
->w
* ds
->h
);
944 static void game_free_drawstate(game_drawstate
*ds
)
950 static void grid_square(frontend
*fe
, game_drawstate
*ds
,
951 int y
, int x
, int state
)
955 draw_rect(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
956 TILE_SIZE
, TILE_SIZE
, COL_GRID
);
958 xl
= (x
% 5 == 0 ?
1 : 0);
959 yt
= (y
% 5 == 0 ?
1 : 0);
960 xr
= (x
% 5 == 4 || x
== ds
->w
-1 ?
1 : 0);
961 yb
= (y
% 5 == 4 || y
== ds
->h
-1 ?
1 : 0);
963 draw_rect(fe
, TOCOORD(ds
->w
, x
) + 1 + xl
, TOCOORD(ds
->h
, y
) + 1 + yt
,
964 TILE_SIZE
- xl
- xr
- 1, TILE_SIZE
- yt
- yb
- 1,
965 (state
== GRID_FULL ? COL_FULL
:
966 state
== GRID_EMPTY ? COL_EMPTY
: COL_UNKNOWN
));
968 draw_update(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
969 TILE_SIZE
, TILE_SIZE
);
972 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
973 game_state
*state
, int dir
, game_ui
*ui
,
974 float animtime
, float flashtime
)
981 * The initial contents of the window are not guaranteed
982 * and can vary with front ends. To be on the safe side,
983 * all games should start by drawing a big background-
984 * colour rectangle covering the whole window.
986 draw_rect(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
), COL_BACKGROUND
);
991 for (i
= 0; i
< ds
->w
+ ds
->h
; i
++) {
992 int rowlen
= state
->rowlen
[i
];
993 int *rowdata
= state
->rowdata
+ state
->rowsize
* i
;
997 * Normally I space the numbers out by the same
998 * distance as the tile size. However, if there are
999 * more numbers than available spaces, I have to squash
1002 nfit
= max(rowlen
, TLBORDER(ds
->h
))-1;
1005 for (j
= 0; j
< rowlen
; j
++) {
1010 x
= TOCOORD(ds
->w
, i
);
1011 y
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->h
)-1);
1012 y
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
1014 y
= TOCOORD(ds
->h
, i
- ds
->w
);
1015 x
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->w
)-1);
1016 x
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
1019 sprintf(str
, "%d", rowdata
[j
]);
1020 draw_text(fe
, x
+TILE_SIZE
/2, y
+TILE_SIZE
/2, FONT_VARIABLE
,
1021 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
,
1022 COL_FULL
, str
); /* FIXME: COL_TEXT */
1027 * Draw the grid outline.
1029 draw_rect(fe
, TOCOORD(ds
->w
, 0) - 1, TOCOORD(ds
->h
, 0) - 1,
1030 ds
->w
* TILE_SIZE
+ 3, ds
->h
* TILE_SIZE
+ 3,
1035 draw_update(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
));
1039 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
1040 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
1041 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
1042 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
1044 x1
= x2
= y1
= y2
= -1; /* placate gcc warnings */
1048 * Now draw any grid squares which have changed since last
1051 for (i
= 0; i
< ds
->h
; i
++) {
1052 for (j
= 0; j
< ds
->w
; j
++) {
1056 * Work out what state this square should be drawn in,
1057 * taking any current drag operation into account.
1059 if (ui
->dragging
&& x1
<= j
&& j
<= x2
&& y1
<= i
&& i
<= y2
)
1062 val
= state
->grid
[i
* state
->w
+ j
];
1065 * Briefly invert everything twice during a completion
1068 if (flashtime
> 0 &&
1069 (flashtime
<= FLASH_TIME
/3 || flashtime
>= FLASH_TIME
*2/3) &&
1070 val
!= GRID_UNKNOWN
)
1071 val
= (GRID_FULL
^ GRID_EMPTY
) ^ val
;
1073 if (ds
->visible
[i
* ds
->w
+ j
] != val
) {
1074 grid_square(fe
, ds
, i
, j
, val
);
1075 ds
->visible
[i
* ds
->w
+ j
] = val
;
1081 static float game_anim_length(game_state
*oldstate
,
1082 game_state
*newstate
, int dir
, game_ui
*ui
)
1087 static float game_flash_length(game_state
*oldstate
,
1088 game_state
*newstate
, int dir
, game_ui
*ui
)
1090 if (!oldstate
->completed
&& newstate
->completed
&&
1091 !oldstate
->cheated
&& !newstate
->cheated
)
1096 static int game_wants_statusbar(void)
1101 static int game_timing_state(game_state
*state
)
1107 #define thegame pattern
1110 const struct game thegame
= {
1111 "Pattern", "games.pattern",
1118 TRUE
, game_configure
, custom_params
,
1127 FALSE
, game_text_format
,
1134 game_free_drawstate
,
1138 game_wants_statusbar
,
1139 FALSE
, game_timing_state
,
1140 0, /* mouse_priorities */
1143 #ifdef STANDALONE_SOLVER
1146 * gcc -DSTANDALONE_SOLVER -o patternsolver pattern.c malloc.c
1151 void frontend_default_colour(frontend
*fe
, float *output
) {}
1152 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
1153 int align
, int colour
, char *text
) {}
1154 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
) {}
1155 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
) {}
1156 void draw_polygon(frontend
*fe
, int *coords
, int npoints
,
1157 int fill
, int colour
) {}
1158 void clip(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1159 void unclip(frontend
*fe
) {}
1160 void start_draw(frontend
*fe
) {}
1161 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1162 void end_draw(frontend
*fe
) {}
1163 unsigned long random_upto(random_state
*state
, unsigned long limit
)
1164 { assert(!"Shouldn't get randomness"); return 0; }
1166 void fatal(char *fmt
, ...)
1170 fprintf(stderr
, "fatal error: ");
1173 vfprintf(stderr
, fmt
, ap
);
1176 fprintf(stderr
, "\n");
1180 int main(int argc
, char **argv
)
1185 char *id
= NULL
, *desc
, *err
;
1189 while (--argc
> 0) {
1192 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0]);
1200 fprintf(stderr
, "usage: %s <game_id>\n", argv
[0]);
1204 desc
= strchr(id
, ':');
1206 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1211 p
= default_params();
1212 decode_params(p
, id
);
1213 err
= validate_desc(p
, desc
);
1215 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1218 s
= new_game(NULL
, p
, desc
);
1221 int w
= p
->w
, h
= p
->h
, i
, j
, done_any
, max
;
1222 unsigned char *matrix
, *workspace
;
1225 matrix
= snewn(w
*h
, unsigned char);
1227 workspace
= snewn(max
*3, unsigned char);
1228 rowdata
= snewn(max
+1, int);
1230 memset(matrix
, 0, w
*h
);
1234 for (i
=0; i
<h
; i
++) {
1235 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*(w
+i
),
1237 rowdata
[s
->rowlen
[w
+i
]] = 0;
1238 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1239 matrix
+i
*w
, w
, 1, rowdata
);
1241 for (i
=0; i
<w
; i
++) {
1242 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*i
, max
*sizeof(int));
1243 rowdata
[s
->rowlen
[i
]] = 0;
1244 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1245 matrix
+i
, h
, w
, rowdata
);
1249 for (i
= 0; i
< h
; i
++) {
1250 for (j
= 0; j
< w
; j
++) {
1251 int c
= (matrix
[i
*w
+j
] == UNKNOWN ?
'?' :
1252 matrix
[i
*w
+j
] == BLOCK ?
'#' :
1253 matrix
[i
*w
+j
] == DOT ?
'.' :