2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
23 #define PREFERRED_TILE_SIZE 24
24 #define TILE_SIZE (ds->tilesize)
25 #define BORDER (3 * TILE_SIZE / 4)
26 #define TLBORDER(d) ( (d) / 5 + 2 )
27 #define GUTTER (TILE_SIZE / 2)
29 #define FROMCOORD(d, x) \
30 ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
32 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
33 #define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d)))
35 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
41 #define GRID_UNKNOWN 2
49 int *rowdata
, *rowlen
;
50 int completed
, cheated
;
53 #define FLASH_TIME 0.13F
55 static game_params
*default_params(void)
57 game_params
*ret
= snew(game_params
);
64 static const struct game_params pattern_presets
[] = {
74 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
79 if (i
< 0 || i
>= lenof(pattern_presets
))
82 ret
= snew(game_params
);
83 *ret
= pattern_presets
[i
];
85 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
92 static void free_params(game_params
*params
)
97 static game_params
*dup_params(game_params
*params
)
99 game_params
*ret
= snew(game_params
);
100 *ret
= *params
; /* structure copy */
104 static void decode_params(game_params
*ret
, char const *string
)
106 char const *p
= string
;
109 while (*p
&& isdigit((unsigned char)*p
)) p
++;
113 while (*p
&& isdigit((unsigned char)*p
)) p
++;
119 static char *encode_params(game_params
*params
, int full
)
124 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
125 assert(len
< lenof(ret
));
131 static config_item
*game_configure(game_params
*params
)
136 ret
= snewn(3, config_item
);
138 ret
[0].name
= "Width";
139 ret
[0].type
= C_STRING
;
140 sprintf(buf
, "%d", params
->w
);
141 ret
[0].sval
= dupstr(buf
);
144 ret
[1].name
= "Height";
145 ret
[1].type
= C_STRING
;
146 sprintf(buf
, "%d", params
->h
);
147 ret
[1].sval
= dupstr(buf
);
158 static game_params
*custom_params(config_item
*cfg
)
160 game_params
*ret
= snew(game_params
);
162 ret
->w
= atoi(cfg
[0].sval
);
163 ret
->h
= atoi(cfg
[1].sval
);
168 static char *validate_params(game_params
*params
, int full
)
170 if (params
->w
<= 0 || params
->h
<= 0)
171 return "Width and height must both be greater than zero";
175 /* ----------------------------------------------------------------------
176 * Puzzle generation code.
178 * For this particular puzzle, it seemed important to me to ensure
179 * a unique solution. I do this the brute-force way, by having a
180 * solver algorithm alongside the generator, and repeatedly
181 * generating a random grid until I find one whose solution is
182 * unique. It turns out that this isn't too onerous on a modern PC
183 * provided you keep grid size below around 30. Any offers of
184 * better algorithms, however, will be very gratefully received.
186 * Another annoyance of this approach is that it limits the
187 * available puzzles to those solvable by the algorithm I've used.
188 * My algorithm only ever considers a single row or column at any
189 * one time, which means it's incapable of solving the following
190 * difficult example (found by Bella Image around 1995/6, when she
191 * and I were both doing maths degrees):
205 * Obviously this cannot be solved by a one-row-or-column-at-a-time
206 * algorithm (it would require at least one row or column reading
207 * `2 1', `1 2', `3' or `4' to get started). However, it can be
208 * proved to have a unique solution: if the top left square were
209 * empty, then the only option for the top row would be to fill the
210 * two squares in the 1 columns, which would imply the squares
211 * below those were empty, leaving no place for the 2 in the second
212 * row. Contradiction. Hence the top left square is full, and the
213 * unique solution follows easily from that starting point.
215 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case
216 * it's useful to anyone.)
219 static int float_compare(const void *av
, const void *bv
)
221 const float *a
= (const float *)av
;
222 const float *b
= (const float *)bv
;
231 static void generate(random_state
*rs
, int w
, int h
, unsigned char *retgrid
)
238 fgrid
= snewn(w
*h
, float);
240 for (i
= 0; i
< h
; i
++) {
241 for (j
= 0; j
< w
; j
++) {
242 fgrid
[i
*w
+j
] = random_upto(rs
, 100000000UL) / 100000000.F
;
247 * The above gives a completely random splattering of black and
248 * white cells. We want to gently bias this in favour of _some_
249 * reasonably thick areas of white and black, while retaining
250 * some randomness and fine detail.
252 * So we evolve the starting grid using a cellular automaton.
253 * Currently, I'm doing something very simple indeed, which is
254 * to set each square to the average of the surrounding nine
255 * cells (or the average of fewer, if we're on a corner).
257 for (step
= 0; step
< 1; step
++) {
258 fgrid2
= snewn(w
*h
, float);
260 for (i
= 0; i
< h
; i
++) {
261 for (j
= 0; j
< w
; j
++) {
266 * Compute the average of the surrounding cells.
270 for (p
= -1; p
<= +1; p
++) {
271 for (q
= -1; q
<= +1; q
++) {
272 if (i
+p
< 0 || i
+p
>= h
|| j
+q
< 0 || j
+q
>= w
)
275 * An additional special case not mentioned
276 * above: if a grid dimension is 2xn then
277 * we do not average across that dimension
278 * at all. Otherwise a 2x2 grid would
279 * contain four identical squares.
281 if ((h
==2 && p
!=0) || (w
==2 && q
!=0))
284 sx
+= fgrid
[(i
+p
)*w
+(j
+q
)];
289 fgrid2
[i
*w
+j
] = xbar
;
297 fgrid2
= snewn(w
*h
, float);
298 memcpy(fgrid2
, fgrid
, w
*h
*sizeof(float));
299 qsort(fgrid2
, w
*h
, sizeof(float), float_compare
);
300 threshold
= fgrid2
[w
*h
/2];
303 for (i
= 0; i
< h
; i
++) {
304 for (j
= 0; j
< w
; j
++) {
305 retgrid
[i
*w
+j
] = (fgrid
[i
*w
+j
] >= threshold ? GRID_FULL
:
313 static int compute_rowdata(int *ret
, unsigned char *start
, int len
, int step
)
319 for (i
= 0; i
< len
; i
++) {
320 if (start
[i
*step
] == GRID_FULL
) {
322 while (i
+runlen
< len
&& start
[(i
+runlen
)*step
] == GRID_FULL
)
328 if (i
< len
&& start
[i
*step
] == GRID_UNKNOWN
)
338 #define STILL_UNKNOWN 3
340 static void do_recurse(unsigned char *known
, unsigned char *deduced
,
341 unsigned char *row
, int *data
, int len
,
342 int freespace
, int ndone
, int lowest
)
347 for (i
=0; i
<=freespace
; i
++) {
349 for (k
=0; k
<i
; k
++) row
[j
++] = DOT
;
350 for (k
=0; k
<data
[ndone
]; k
++) row
[j
++] = BLOCK
;
351 if (j
< len
) row
[j
++] = DOT
;
352 do_recurse(known
, deduced
, row
, data
, len
,
353 freespace
-i
, ndone
+1, j
);
356 for (i
=lowest
; i
<len
; i
++)
358 for (i
=0; i
<len
; i
++)
359 if (known
[i
] && known
[i
] != row
[i
])
361 for (i
=0; i
<len
; i
++)
362 deduced
[i
] |= row
[i
];
366 static int do_row(unsigned char *known
, unsigned char *deduced
,
368 unsigned char *start
, int len
, int step
, int *data
)
370 int rowlen
, i
, freespace
, done_any
;
373 for (rowlen
= 0; data
[rowlen
]; rowlen
++)
374 freespace
-= data
[rowlen
]+1;
376 for (i
= 0; i
< len
; i
++) {
377 known
[i
] = start
[i
*step
];
381 do_recurse(known
, deduced
, row
, data
, len
, freespace
, 0, 0);
383 for (i
=0; i
<len
; i
++)
384 if (deduced
[i
] && deduced
[i
] != STILL_UNKNOWN
&& !known
[i
]) {
385 start
[i
*step
] = deduced
[i
];
391 static unsigned char *generate_soluble(random_state
*rs
, int w
, int h
)
393 int i
, j
, done_any
, ok
, ntries
, max
;
394 unsigned char *grid
, *matrix
, *workspace
;
397 grid
= snewn(w
*h
, unsigned char);
398 matrix
= snewn(w
*h
, unsigned char);
400 workspace
= snewn(max
*3, unsigned char);
401 rowdata
= snewn(max
+1, int);
408 generate(rs
, w
, h
, grid
);
411 * The game is a bit too easy if any row or column is
412 * completely black or completely white. An exception is
413 * made for rows/columns that are under 3 squares,
414 * otherwise nothing will ever be successfully generated.
418 for (i
= 0; i
< h
; i
++) {
420 for (j
= 0; j
< w
; j
++)
421 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
427 for (j
= 0; j
< w
; j
++) {
429 for (i
= 0; i
< h
; i
++)
430 colours
|= (grid
[i
*w
+j
] == GRID_FULL ?
2 : 1);
438 memset(matrix
, 0, w
*h
);
442 for (i
=0; i
<h
; i
++) {
443 rowdata
[compute_rowdata(rowdata
, grid
+i
*w
, w
, 1)] = 0;
444 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
445 matrix
+i
*w
, w
, 1, rowdata
);
447 for (i
=0; i
<w
; i
++) {
448 rowdata
[compute_rowdata(rowdata
, grid
+i
, h
, w
)] = 0;
449 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
450 matrix
+i
, h
, w
, rowdata
);
455 for (i
=0; i
<h
; i
++) {
456 for (j
=0; j
<w
; j
++) {
457 if (matrix
[i
*w
+j
] == UNKNOWN
)
469 static char *new_game_desc(game_params
*params
, random_state
*rs
,
470 char **aux
, int interactive
)
473 int i
, j
, max
, rowlen
, *rowdata
;
474 char intbuf
[80], *desc
;
475 int desclen
, descpos
;
477 grid
= generate_soluble(rs
, params
->w
, params
->h
);
478 max
= max(params
->w
, params
->h
);
479 rowdata
= snewn(max
, int);
482 * Save the solved game in aux.
485 char *ai
= snewn(params
->w
* params
->h
+ 2, char);
488 * String format is exactly the same as a solve move, so we
489 * can just dupstr this in solve_game().
494 for (i
= 0; i
< params
->w
* params
->h
; i
++)
495 ai
[i
+1] = grid
[i
] ?
'1' : '0';
497 ai
[params
->w
* params
->h
+ 1] = '\0';
503 * Seed is a slash-separated list of row contents; each row
504 * contents section is a dot-separated list of integers. Row
505 * contents are listed in the order (columns left to right,
506 * then rows top to bottom).
508 * Simplest way to handle memory allocation is to make two
509 * passes, first computing the seed size and then writing it
513 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
515 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
517 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
520 for (j
= 0; j
< rowlen
; j
++) {
521 desclen
+= 1 + sprintf(intbuf
, "%d", rowdata
[j
]);
527 desc
= snewn(desclen
, char);
529 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
531 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
533 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
536 for (j
= 0; j
< rowlen
; j
++) {
537 int len
= sprintf(desc
+descpos
, "%d", rowdata
[j
]);
539 desc
[descpos
+ len
] = '.';
541 desc
[descpos
+ len
] = '/';
545 desc
[descpos
++] = '/';
548 assert(descpos
== desclen
);
549 assert(desc
[desclen
-1] == '/');
550 desc
[desclen
-1] = '\0';
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 char *solve_game(game_state
*state
, game_state
*currstate
,
666 char *ai
, char **error
)
668 unsigned char *matrix
;
669 int w
= state
->w
, h
= state
->h
;
673 unsigned char *workspace
;
677 * If we already have the solved state in ai, copy it out.
682 matrix
= snewn(w
*h
, unsigned char);
684 workspace
= snewn(max
*3, unsigned char);
685 rowdata
= snewn(max
+1, int);
687 memset(matrix
, 0, w
*h
);
691 for (i
=0; i
<h
; i
++) {
692 memcpy(rowdata
, state
->rowdata
+ state
->rowsize
*(w
+i
),
694 rowdata
[state
->rowlen
[w
+i
]] = 0;
695 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
696 matrix
+i
*w
, w
, 1, rowdata
);
698 for (i
=0; i
<w
; i
++) {
699 memcpy(rowdata
, state
->rowdata
+ state
->rowsize
*i
, max
*sizeof(int));
700 rowdata
[state
->rowlen
[i
]] = 0;
701 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
702 matrix
+i
, h
, w
, rowdata
);
709 for (i
= 0; i
< w
*h
; i
++) {
710 if (matrix
[i
] != BLOCK
&& matrix
[i
] != DOT
) {
712 *error
= "Solving algorithm cannot complete this puzzle";
717 ret
= snewn(w
*h
+2, char);
719 for (i
= 0; i
< w
*h
; i
++) {
720 assert(matrix
[i
] == BLOCK
|| matrix
[i
] == DOT
);
721 ret
[i
+1] = (matrix
[i
] == BLOCK ?
'1' : '0');
730 static char *game_text_format(game_state
*state
)
741 int drag
, release
, state
;
744 static game_ui
*new_ui(game_state
*state
)
749 ret
->dragging
= FALSE
;
754 static void free_ui(game_ui
*ui
)
759 static char *encode_ui(game_ui
*ui
)
764 static void decode_ui(game_ui
*ui
, char *encoding
)
768 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
769 game_state
*newstate
)
773 struct game_drawstate
{
777 unsigned char *visible
;
780 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
781 int x
, int y
, int button
)
785 x
= FROMCOORD(state
->w
, x
);
786 y
= FROMCOORD(state
->h
, y
);
788 if (x
>= 0 && x
< state
->w
&& y
>= 0 && y
< state
->h
&&
789 (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
||
790 button
== MIDDLE_BUTTON
)) {
794 if (button
== LEFT_BUTTON
) {
795 ui
->drag
= LEFT_DRAG
;
796 ui
->release
= LEFT_RELEASE
;
797 ui
->state
= GRID_FULL
;
798 } else if (button
== RIGHT_BUTTON
) {
799 ui
->drag
= RIGHT_DRAG
;
800 ui
->release
= RIGHT_RELEASE
;
801 ui
->state
= GRID_EMPTY
;
802 } else /* if (button == MIDDLE_BUTTON) */ {
803 ui
->drag
= MIDDLE_DRAG
;
804 ui
->release
= MIDDLE_RELEASE
;
805 ui
->state
= GRID_UNKNOWN
;
808 ui
->drag_start_x
= ui
->drag_end_x
= x
;
809 ui
->drag_start_y
= ui
->drag_end_y
= y
;
811 return ""; /* UI activity occurred */
814 if (ui
->dragging
&& button
== ui
->drag
) {
816 * There doesn't seem much point in allowing a rectangle
817 * drag; people will generally only want to drag a single
818 * horizontal or vertical line, so we make that easy by
821 * Exception: if we're _middle_-button dragging to tag
822 * things as UNKNOWN, we may well want to trash an entire
823 * area and start over!
825 if (ui
->state
!= GRID_UNKNOWN
) {
826 if (abs(x
- ui
->drag_start_x
) > abs(y
- ui
->drag_start_y
))
827 y
= ui
->drag_start_y
;
829 x
= ui
->drag_start_x
;
834 if (x
>= state
->w
) x
= state
->w
- 1;
835 if (y
>= state
->h
) y
= state
->h
- 1;
840 return ""; /* UI activity occurred */
843 if (ui
->dragging
&& button
== ui
->release
) {
844 int x1
, x2
, y1
, y2
, xx
, yy
;
845 int move_needed
= FALSE
;
847 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
848 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
849 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
850 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
852 for (yy
= y1
; yy
<= y2
; yy
++)
853 for (xx
= x1
; xx
<= x2
; xx
++)
854 if (state
->grid
[yy
* state
->w
+ xx
] != ui
->state
)
857 ui
->dragging
= FALSE
;
861 sprintf(buf
, "%c%d,%d,%d,%d",
862 (char)(ui
->state
== GRID_FULL ?
'F' :
863 ui
->state
== GRID_EMPTY ?
'E' : 'U'),
864 x1
, y1
, x2
-x1
+1, y2
-y1
+1);
867 return ""; /* UI activity occurred */
873 static game_state
*execute_move(game_state
*from
, char *move
)
876 int x1
, x2
, y1
, y2
, xx
, yy
;
879 if (move
[0] == 'S' && strlen(move
) == from
->w
* from
->h
+ 1) {
882 ret
= dup_game(from
);
884 for (i
= 0; i
< ret
->w
* ret
->h
; i
++)
885 ret
->grid
[i
] = (move
[i
+1] == '1' ? GRID_FULL
: GRID_EMPTY
);
887 ret
->completed
= ret
->cheated
= TRUE
;
890 } else if ((move
[0] == 'F' || move
[0] == 'E' || move
[0] == 'U') &&
891 sscanf(move
+1, "%d,%d,%d,%d", &x1
, &y1
, &x2
, &y2
) == 4 &&
892 x1
>= 0 && x2
>= 0 && x1
+x2
<= from
->w
&&
893 y1
>= 0 && y2
>= 0 && y1
+y2
<= from
->h
) {
897 val
= (move
[0] == 'F' ? GRID_FULL
:
898 move
[0] == 'E' ? GRID_EMPTY
: GRID_UNKNOWN
);
900 ret
= dup_game(from
);
901 for (yy
= y1
; yy
< y2
; yy
++)
902 for (xx
= x1
; xx
< x2
; xx
++)
903 ret
->grid
[yy
* ret
->w
+ xx
] = val
;
906 * An actual change, so check to see if we've completed the
909 if (!ret
->completed
) {
910 int *rowdata
= snewn(ret
->rowsize
, int);
913 ret
->completed
= TRUE
;
915 for (i
=0; i
<ret
->w
; i
++) {
916 len
= compute_rowdata(rowdata
,
917 ret
->grid
+i
, ret
->h
, ret
->w
);
918 if (len
!= ret
->rowlen
[i
] ||
919 memcmp(ret
->rowdata
+i
*ret
->rowsize
, rowdata
,
920 len
* sizeof(int))) {
921 ret
->completed
= FALSE
;
925 for (i
=0; i
<ret
->h
; i
++) {
926 len
= compute_rowdata(rowdata
,
927 ret
->grid
+i
*ret
->w
, ret
->w
, 1);
928 if (len
!= ret
->rowlen
[i
+ret
->w
] ||
929 memcmp(ret
->rowdata
+(i
+ret
->w
)*ret
->rowsize
, rowdata
,
930 len
* sizeof(int))) {
931 ret
->completed
= FALSE
;
944 /* ----------------------------------------------------------------------
948 static void game_compute_size(game_params
*params
, int tilesize
,
951 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
952 struct { int tilesize
; } ads
, *ds
= &ads
;
953 ads
.tilesize
= tilesize
;
955 *x
= SIZE(params
->w
);
956 *y
= SIZE(params
->h
);
959 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
962 ds
->tilesize
= tilesize
;
965 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
967 float *ret
= snewn(3 * NCOLOURS
, float);
969 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
971 ret
[COL_GRID
* 3 + 0] = 0.3F
;
972 ret
[COL_GRID
* 3 + 1] = 0.3F
;
973 ret
[COL_GRID
* 3 + 2] = 0.3F
;
975 ret
[COL_UNKNOWN
* 3 + 0] = 0.5F
;
976 ret
[COL_UNKNOWN
* 3 + 1] = 0.5F
;
977 ret
[COL_UNKNOWN
* 3 + 2] = 0.5F
;
979 ret
[COL_FULL
* 3 + 0] = 0.0F
;
980 ret
[COL_FULL
* 3 + 1] = 0.0F
;
981 ret
[COL_FULL
* 3 + 2] = 0.0F
;
983 ret
[COL_EMPTY
* 3 + 0] = 1.0F
;
984 ret
[COL_EMPTY
* 3 + 1] = 1.0F
;
985 ret
[COL_EMPTY
* 3 + 2] = 1.0F
;
987 *ncolours
= NCOLOURS
;
991 static game_drawstate
*game_new_drawstate(game_state
*state
)
993 struct game_drawstate
*ds
= snew(struct game_drawstate
);
998 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned char);
999 ds
->tilesize
= 0; /* not decided yet */
1000 memset(ds
->visible
, 255, ds
->w
* ds
->h
);
1005 static void game_free_drawstate(game_drawstate
*ds
)
1011 static void grid_square(frontend
*fe
, game_drawstate
*ds
,
1012 int y
, int x
, int state
)
1016 draw_rect(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
1017 TILE_SIZE
, TILE_SIZE
, COL_GRID
);
1019 xl
= (x
% 5 == 0 ?
1 : 0);
1020 yt
= (y
% 5 == 0 ?
1 : 0);
1021 xr
= (x
% 5 == 4 || x
== ds
->w
-1 ?
1 : 0);
1022 yb
= (y
% 5 == 4 || y
== ds
->h
-1 ?
1 : 0);
1024 draw_rect(fe
, TOCOORD(ds
->w
, x
) + 1 + xl
, TOCOORD(ds
->h
, y
) + 1 + yt
,
1025 TILE_SIZE
- xl
- xr
- 1, TILE_SIZE
- yt
- yb
- 1,
1026 (state
== GRID_FULL ? COL_FULL
:
1027 state
== GRID_EMPTY ? COL_EMPTY
: COL_UNKNOWN
));
1029 draw_update(fe
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
1030 TILE_SIZE
, TILE_SIZE
);
1033 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1034 game_state
*state
, int dir
, game_ui
*ui
,
1035 float animtime
, float flashtime
)
1042 * The initial contents of the window are not guaranteed
1043 * and can vary with front ends. To be on the safe side,
1044 * all games should start by drawing a big background-
1045 * colour rectangle covering the whole window.
1047 draw_rect(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
), COL_BACKGROUND
);
1052 for (i
= 0; i
< ds
->w
+ ds
->h
; i
++) {
1053 int rowlen
= state
->rowlen
[i
];
1054 int *rowdata
= state
->rowdata
+ state
->rowsize
* i
;
1058 * Normally I space the numbers out by the same
1059 * distance as the tile size. However, if there are
1060 * more numbers than available spaces, I have to squash
1063 nfit
= max(rowlen
, TLBORDER(ds
->h
))-1;
1066 for (j
= 0; j
< rowlen
; j
++) {
1071 x
= TOCOORD(ds
->w
, i
);
1072 y
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->h
)-1);
1073 y
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
1075 y
= TOCOORD(ds
->h
, i
- ds
->w
);
1076 x
= BORDER
+ TILE_SIZE
* (TLBORDER(ds
->w
)-1);
1077 x
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(ds
->h
)-1) / nfit
;
1080 sprintf(str
, "%d", rowdata
[j
]);
1081 draw_text(fe
, x
+TILE_SIZE
/2, y
+TILE_SIZE
/2, FONT_VARIABLE
,
1082 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
,
1083 COL_FULL
, str
); /* FIXME: COL_TEXT */
1088 * Draw the grid outline.
1090 draw_rect(fe
, TOCOORD(ds
->w
, 0) - 1, TOCOORD(ds
->h
, 0) - 1,
1091 ds
->w
* TILE_SIZE
+ 3, ds
->h
* TILE_SIZE
+ 3,
1096 draw_update(fe
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
));
1100 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
1101 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
1102 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
1103 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
1105 x1
= x2
= y1
= y2
= -1; /* placate gcc warnings */
1109 * Now draw any grid squares which have changed since last
1112 for (i
= 0; i
< ds
->h
; i
++) {
1113 for (j
= 0; j
< ds
->w
; j
++) {
1117 * Work out what state this square should be drawn in,
1118 * taking any current drag operation into account.
1120 if (ui
->dragging
&& x1
<= j
&& j
<= x2
&& y1
<= i
&& i
<= y2
)
1123 val
= state
->grid
[i
* state
->w
+ j
];
1126 * Briefly invert everything twice during a completion
1129 if (flashtime
> 0 &&
1130 (flashtime
<= FLASH_TIME
/3 || flashtime
>= FLASH_TIME
*2/3) &&
1131 val
!= GRID_UNKNOWN
)
1132 val
= (GRID_FULL
^ GRID_EMPTY
) ^ val
;
1134 if (ds
->visible
[i
* ds
->w
+ j
] != val
) {
1135 grid_square(fe
, ds
, i
, j
, val
);
1136 ds
->visible
[i
* ds
->w
+ j
] = val
;
1142 static float game_anim_length(game_state
*oldstate
,
1143 game_state
*newstate
, int dir
, game_ui
*ui
)
1148 static float game_flash_length(game_state
*oldstate
,
1149 game_state
*newstate
, int dir
, game_ui
*ui
)
1151 if (!oldstate
->completed
&& newstate
->completed
&&
1152 !oldstate
->cheated
&& !newstate
->cheated
)
1157 static int game_wants_statusbar(void)
1162 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1168 #define thegame pattern
1171 const struct game thegame
= {
1172 "Pattern", "games.pattern",
1179 TRUE
, game_configure
, custom_params
,
1187 FALSE
, game_text_format
,
1195 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1198 game_free_drawstate
,
1202 game_wants_statusbar
,
1203 FALSE
, game_timing_state
,
1204 0, /* mouse_priorities */
1207 #ifdef STANDALONE_SOLVER
1210 * gcc -DSTANDALONE_SOLVER -o patternsolver pattern.c malloc.c
1215 void frontend_default_colour(frontend
*fe
, float *output
) {}
1216 void draw_text(frontend
*fe
, int x
, int y
, int fonttype
, int fontsize
,
1217 int align
, int colour
, char *text
) {}
1218 void draw_rect(frontend
*fe
, int x
, int y
, int w
, int h
, int colour
) {}
1219 void draw_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
, int colour
) {}
1220 void clip(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1221 void unclip(frontend
*fe
) {}
1222 void start_draw(frontend
*fe
) {}
1223 void draw_update(frontend
*fe
, int x
, int y
, int w
, int h
) {}
1224 void end_draw(frontend
*fe
) {}
1225 unsigned long random_upto(random_state
*state
, unsigned long limit
)
1226 { assert(!"Shouldn't get randomness"); return 0; }
1228 void fatal(char *fmt
, ...)
1232 fprintf(stderr
, "fatal error: ");
1235 vfprintf(stderr
, fmt
, ap
);
1238 fprintf(stderr
, "\n");
1242 int main(int argc
, char **argv
)
1246 char *id
= NULL
, *desc
, *err
;
1248 while (--argc
> 0) {
1251 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
1259 fprintf(stderr
, "usage: %s <game_id>\n", argv
[0]);
1263 desc
= strchr(id
, ':');
1265 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1270 p
= default_params();
1271 decode_params(p
, id
);
1272 err
= validate_desc(p
, desc
);
1274 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1277 s
= new_game(NULL
, p
, desc
);
1280 int w
= p
->w
, h
= p
->h
, i
, j
, done_any
, max
;
1281 unsigned char *matrix
, *workspace
;
1284 matrix
= snewn(w
*h
, unsigned char);
1286 workspace
= snewn(max
*3, unsigned char);
1287 rowdata
= snewn(max
+1, int);
1289 memset(matrix
, 0, w
*h
);
1293 for (i
=0; i
<h
; i
++) {
1294 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*(w
+i
),
1296 rowdata
[s
->rowlen
[w
+i
]] = 0;
1297 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1298 matrix
+i
*w
, w
, 1, rowdata
);
1300 for (i
=0; i
<w
; i
++) {
1301 memcpy(rowdata
, s
->rowdata
+ s
->rowsize
*i
, max
*sizeof(int));
1302 rowdata
[s
->rowlen
[i
]] = 0;
1303 done_any
|= do_row(workspace
, workspace
+max
, workspace
+2*max
,
1304 matrix
+i
, h
, w
, rowdata
);
1308 for (i
= 0; i
< h
; i
++) {
1309 for (j
= 0; j
< w
; j
++) {
1310 int c
= (matrix
[i
*w
+j
] == UNKNOWN ?
'?' :
1311 matrix
[i
*w
+j
] == BLOCK ?
'#' :
1312 matrix
[i
*w
+j
] == DOT ?
'.' :