2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
26 #define PREFERRED_TILE_SIZE 24
27 #define TILE_SIZE (ds->tilesize)
28 #define BORDER (3 * TILE_SIZE / 4)
29 #define TLBORDER(d) ( (d) / 5 + 2 )
30 #define GUTTER (TILE_SIZE / 2)
32 #define FROMCOORD(d, x) \
33 ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
35 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
36 #define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d)))
38 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
44 #define GRID_UNKNOWN 2
48 typedef struct game_state_common
{
49 /* Parts of the game state that don't change during play. */
52 int *rowdata
, *rowlen
;
53 unsigned char *immutable
;
58 game_state_common
*common
;
60 int completed
, cheated
;
63 #define FLASH_TIME 0.13F
65 static game_params
*default_params(void)
67 game_params
*ret
= snew(game_params
);
74 static const struct game_params pattern_presets
[] = {
84 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
89 if (i
< 0 || i
>= lenof(pattern_presets
))
92 ret
= snew(game_params
);
93 *ret
= pattern_presets
[i
];
95 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
102 static void free_params(game_params
*params
)
107 static game_params
*dup_params(const game_params
*params
)
109 game_params
*ret
= snew(game_params
);
110 *ret
= *params
; /* structure copy */
114 static void decode_params(game_params
*ret
, char const *string
)
116 char const *p
= string
;
119 while (*p
&& isdigit((unsigned char)*p
)) p
++;
123 while (*p
&& isdigit((unsigned char)*p
)) p
++;
129 static char *encode_params(const game_params
*params
, int full
)
134 len
= sprintf(ret
, "%dx%d", params
->w
, params
->h
);
135 assert(len
< lenof(ret
));
141 static config_item
*game_configure(const game_params
*params
)
146 ret
= snewn(3, config_item
);
148 ret
[0].name
= "Width";
149 ret
[0].type
= C_STRING
;
150 sprintf(buf
, "%d", params
->w
);
151 ret
[0].sval
= dupstr(buf
);
154 ret
[1].name
= "Height";
155 ret
[1].type
= C_STRING
;
156 sprintf(buf
, "%d", params
->h
);
157 ret
[1].sval
= dupstr(buf
);
168 static game_params
*custom_params(const config_item
*cfg
)
170 game_params
*ret
= snew(game_params
);
172 ret
->w
= atoi(cfg
[0].sval
);
173 ret
->h
= atoi(cfg
[1].sval
);
178 static char *validate_params(const game_params
*params
, int full
)
180 if (params
->w
<= 0 || params
->h
<= 0)
181 return "Width and height must both 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 #ifndef STANDALONE_PICTURE_GENERATOR
230 static int float_compare(const void *av
, const void *bv
)
232 const float *a
= (const float *)av
;
233 const float *b
= (const float *)bv
;
242 static void generate(random_state
*rs
, int w
, int h
, unsigned char *retgrid
)
249 fgrid
= snewn(w
*h
, float);
251 for (i
= 0; i
< h
; i
++) {
252 for (j
= 0; j
< w
; j
++) {
253 fgrid
[i
*w
+j
] = random_upto(rs
, 100000000UL) / 100000000.F
;
258 * The above gives a completely random splattering of black and
259 * white cells. We want to gently bias this in favour of _some_
260 * reasonably thick areas of white and black, while retaining
261 * some randomness and fine detail.
263 * So we evolve the starting grid using a cellular automaton.
264 * Currently, I'm doing something very simple indeed, which is
265 * to set each square to the average of the surrounding nine
266 * cells (or the average of fewer, if we're on a corner).
268 for (step
= 0; step
< 1; step
++) {
269 fgrid2
= snewn(w
*h
, float);
271 for (i
= 0; i
< h
; i
++) {
272 for (j
= 0; j
< w
; j
++) {
277 * Compute the average of the surrounding cells.
281 for (p
= -1; p
<= +1; p
++) {
282 for (q
= -1; q
<= +1; q
++) {
283 if (i
+p
< 0 || i
+p
>= h
|| j
+q
< 0 || j
+q
>= w
)
286 * An additional special case not mentioned
287 * above: if a grid dimension is 2xn then
288 * we do not average across that dimension
289 * at all. Otherwise a 2x2 grid would
290 * contain four identical squares.
292 if ((h
==2 && p
!=0) || (w
==2 && q
!=0))
295 sx
+= fgrid
[(i
+p
)*w
+(j
+q
)];
300 fgrid2
[i
*w
+j
] = xbar
;
308 fgrid2
= snewn(w
*h
, float);
309 memcpy(fgrid2
, fgrid
, w
*h
*sizeof(float));
310 qsort(fgrid2
, w
*h
, sizeof(float), float_compare
);
311 threshold
= fgrid2
[w
*h
/2];
314 for (i
= 0; i
< h
; i
++) {
315 for (j
= 0; j
< w
; j
++) {
316 retgrid
[i
*w
+j
] = (fgrid
[i
*w
+j
] >= threshold
? GRID_FULL
:
325 static int compute_rowdata(int *ret
, unsigned char *start
, int len
, int step
)
331 for (i
= 0; i
< len
; i
++) {
332 if (start
[i
*step
] == GRID_FULL
) {
334 while (i
+runlen
< len
&& start
[(i
+runlen
)*step
] == GRID_FULL
)
340 if (i
< len
&& start
[i
*step
] == GRID_UNKNOWN
)
350 #define STILL_UNKNOWN 3
352 #ifdef STANDALONE_SOLVER
356 static int do_recurse(unsigned char *known
, unsigned char *deduced
,
358 unsigned char *minpos_done
, unsigned char *maxpos_done
,
359 unsigned char *minpos_ok
, unsigned char *maxpos_ok
,
361 int freespace
, int ndone
, int lowest
)
366 /* This algorithm basically tries all possible ways the given rows of
367 * black blocks can be laid out in the row/column being examined.
368 * Special care is taken to avoid checking the tail of a row/column
369 * if the same conditions have already been checked during this recursion
370 * The algorithm also takes care to cut its losses as soon as an
371 * invalid (partial) solution is detected.
374 if (lowest
>= minpos_done
[ndone
] && lowest
<= maxpos_done
[ndone
]) {
375 if (lowest
>= minpos_ok
[ndone
] && lowest
<= maxpos_ok
[ndone
]) {
376 for (i
=0; i
<lowest
; i
++)
377 deduced
[i
] |= row
[i
];
379 return lowest
>= minpos_ok
[ndone
] && lowest
<= maxpos_ok
[ndone
];
381 if (lowest
< minpos_done
[ndone
]) minpos_done
[ndone
] = lowest
;
382 if (lowest
> maxpos_done
[ndone
]) maxpos_done
[ndone
] = lowest
;
384 for (i
=0; i
<=freespace
; i
++) {
386 for (k
=0; k
<i
; k
++) {
387 if (known
[j
] == BLOCK
) goto next_iter
;
390 for (k
=0; k
<data
[ndone
]; k
++) {
391 if (known
[j
] == DOT
) goto next_iter
;
395 if (known
[j
] == BLOCK
) goto next_iter
;
398 if (do_recurse(known
, deduced
, row
, minpos_done
, maxpos_done
,
399 minpos_ok
, maxpos_ok
, data
, len
, freespace
-i
, ndone
+1, j
)) {
400 if (lowest
< minpos_ok
[ndone
]) minpos_ok
[ndone
] = lowest
;
401 if (lowest
+ i
> maxpos_ok
[ndone
]) maxpos_ok
[ndone
] = lowest
+ i
;
402 if (lowest
+ i
> maxpos_done
[ndone
]) maxpos_done
[ndone
] = lowest
+ i
;
407 return lowest
>= minpos_ok
[ndone
] && lowest
<= maxpos_ok
[ndone
];
409 for (i
=lowest
; i
<len
; i
++) {
410 if (known
[i
] == BLOCK
) return FALSE
;
413 for (i
=0; i
<len
; i
++)
414 deduced
[i
] |= row
[i
];
420 static int do_row(unsigned char *known
, unsigned char *deduced
,
422 unsigned char *minpos_done
, unsigned char *maxpos_done
,
423 unsigned char *minpos_ok
, unsigned char *maxpos_ok
,
424 unsigned char *start
, int len
, int step
, int *data
,
425 unsigned int *changed
426 #ifdef STANDALONE_SOLVER
427 , const char *rowcol
, int index
, int cluewid
431 int rowlen
, i
, freespace
, done_any
;
434 for (rowlen
= 0; data
[rowlen
]; rowlen
++) {
435 minpos_done
[rowlen
] = minpos_ok
[rowlen
] = len
- 1;
436 maxpos_done
[rowlen
] = maxpos_ok
[rowlen
] = 0;
437 freespace
-= data
[rowlen
]+1;
440 for (i
= 0; i
< len
; i
++) {
441 known
[i
] = start
[i
*step
];
444 for (i
= len
- 1; i
>= 0 && known
[i
] == DOT
; i
--)
448 memset(deduced
, DOT
, len
);
450 do_recurse(known
, deduced
, row
, minpos_done
, maxpos_done
, minpos_ok
,
451 maxpos_ok
, data
, len
, freespace
, 0, 0);
455 for (i
=0; i
<len
; i
++)
456 if (deduced
[i
] && deduced
[i
] != STILL_UNKNOWN
&& !known
[i
]) {
457 start
[i
*step
] = deduced
[i
];
458 if (changed
) changed
[i
]++;
461 #ifdef STANDALONE_SOLVER
462 if (verbose
&& done_any
) {
465 printf("%s %2d: [", rowcol
, index
);
466 for (thiscluewid
= -1, i
= 0; data
[i
]; i
++)
467 thiscluewid
+= sprintf(buf
, " %d", data
[i
]);
468 printf("%*s", cluewid
- thiscluewid
, "");
469 for (i
= 0; data
[i
]; i
++)
470 printf(" %d", data
[i
]);
472 for (i
= 0; i
< len
; i
++)
473 putchar(known
[i
] == BLOCK
? '#' :
474 known
[i
] == DOT
? '.' : '?');
476 for (i
= 0; i
< len
; i
++)
477 putchar(start
[i
*step
] == BLOCK
? '#' :
478 start
[i
*step
] == DOT
? '.' : '?');
485 static int solve_puzzle(const game_state
*state
, unsigned char *grid
,
487 unsigned char *matrix
, unsigned char *workspace
,
488 unsigned int *changed_h
, unsigned int *changed_w
,
490 #ifdef STANDALONE_SOLVER
500 assert((state
!=NULL
&& state
->common
->rowdata
!=NULL
) ^ (grid
!=NULL
));
504 memset(matrix
, 0, w
*h
);
506 for (i
=0; i
<w
*h
; i
++) {
507 if (state
->common
->immutable
[i
])
508 matrix
[i
] = state
->grid
[i
];
512 /* For each column, compute how many squares can be deduced
513 * from just the row-data and initial clues.
514 * Later, changed_* will hold how many squares were changed
515 * in every row/column in the previous iteration
516 * Changed_* is used to choose the next rows / cols to re-examine
518 for (i
=0; i
<h
; i
++) {
519 int freespace
, rowlen
;
520 if (state
&& state
->common
->rowdata
) {
521 memcpy(rowdata
, state
->common
->rowdata
+ state
->common
->rowsize
*(w
+i
), max
*sizeof(int));
522 rowlen
= state
->common
->rowlen
[w
+i
];
524 rowlen
= compute_rowdata(rowdata
, grid
+i
*w
, w
, 1);
530 for (j
=0, freespace
=w
+1; rowdata
[j
]; j
++)
531 freespace
-= rowdata
[j
] + 1;
532 for (j
=0, changed_h
[i
]=0; rowdata
[j
]; j
++)
533 if (rowdata
[j
] > freespace
)
534 changed_h
[i
] += rowdata
[j
] - freespace
;
536 for (j
= 0; j
< w
; j
++)
540 for (i
=0,max_h
=0; i
<h
; i
++)
541 if (changed_h
[i
] > max_h
)
542 max_h
= changed_h
[i
];
543 for (i
=0; i
<w
; i
++) {
544 int freespace
, rowlen
;
545 if (state
&& state
->common
->rowdata
) {
546 memcpy(rowdata
, state
->common
->rowdata
+ state
->common
->rowsize
*i
, max
*sizeof(int));
547 rowlen
= state
->common
->rowlen
[i
];
549 rowlen
= compute_rowdata(rowdata
, grid
+i
, h
, w
);
555 for (j
=0, freespace
=h
+1; rowdata
[j
]; j
++)
556 freespace
-= rowdata
[j
] + 1;
557 for (j
=0, changed_w
[i
]=0; rowdata
[j
]; j
++)
558 if (rowdata
[j
] > freespace
)
559 changed_w
[i
] += rowdata
[j
] - freespace
;
561 for (j
= 0; j
< h
; j
++)
565 for (i
=0,max_w
=0; i
<w
; i
++)
566 if (changed_w
[i
] > max_w
)
567 max_w
= changed_w
[i
];
570 * Process rows/columns individually. Deductions involving more than one
571 * row and/or column at a time are not supported.
572 * Take care to only process rows/columns which have been changed since they
573 * were previously processed.
574 * Also, prioritize rows/columns which have had the most changes since their
575 * previous processing, as they promise the greatest benefit.
576 * Extremely rectangular grids (e.g. 10x20, 15x40, etc.) are not treated specially.
579 for (; max_h
&& max_h
>= max_w
; max_h
--) {
580 for (i
=0; i
<h
; i
++) {
581 if (changed_h
[i
] >= max_h
) {
582 if (state
&& state
->common
->rowdata
) {
583 memcpy(rowdata
, state
->common
->rowdata
+ state
->common
->rowsize
*(w
+i
), max
*sizeof(int));
584 rowdata
[state
->common
->rowlen
[w
+i
]] = 0;
586 rowdata
[compute_rowdata(rowdata
, grid
+i
*w
, w
, 1)] = 0;
588 do_row(workspace
, workspace
+max
, workspace
+2*max
,
589 workspace
+3*max
, workspace
+4*max
,
590 workspace
+5*max
, workspace
+6*max
,
591 matrix
+i
*w
, w
, 1, rowdata
, changed_w
592 #ifdef STANDALONE_SOLVER
593 , "row", i
+1, cluewid
599 for (i
=0,max_w
=0; i
<w
; i
++)
600 if (changed_w
[i
] > max_w
)
601 max_w
= changed_w
[i
];
603 for (; max_w
&& max_w
>= max_h
; max_w
--) {
604 for (i
=0; i
<w
; i
++) {
605 if (changed_w
[i
] >= max_w
) {
606 if (state
&& state
->common
->rowdata
) {
607 memcpy(rowdata
, state
->common
->rowdata
+ state
->common
->rowsize
*i
, max
*sizeof(int));
608 rowdata
[state
->common
->rowlen
[i
]] = 0;
610 rowdata
[compute_rowdata(rowdata
, grid
+i
, h
, w
)] = 0;
612 do_row(workspace
, workspace
+max
, workspace
+2*max
,
613 workspace
+3*max
, workspace
+4*max
,
614 workspace
+5*max
, workspace
+6*max
,
615 matrix
+i
, h
, w
, rowdata
, changed_h
616 #ifdef STANDALONE_SOLVER
617 , "col", i
+1, cluewid
623 for (i
=0,max_h
=0; i
<h
; i
++)
624 if (changed_h
[i
] > max_h
)
625 max_h
= changed_h
[i
];
627 } while (max_h
>0 || max_w
>0);
630 for (i
=0; i
<h
; i
++) {
631 for (j
=0; j
<w
; j
++) {
632 if (matrix
[i
*w
+j
] == UNKNOWN
)
640 #ifndef STANDALONE_PICTURE_GENERATOR
641 static unsigned char *generate_soluble(random_state
*rs
, int w
, int h
)
643 int i
, j
, ok
, ntries
, max
;
644 unsigned char *grid
, *matrix
, *workspace
;
645 unsigned int *changed_h
, *changed_w
;
650 grid
= snewn(w
*h
, unsigned char);
651 /* Allocate this here, to avoid having to reallocate it again for every geneerated grid */
652 matrix
= snewn(w
*h
, unsigned char);
653 workspace
= snewn(max
*7, unsigned char);
654 changed_h
= snewn(max
+1, unsigned int);
655 changed_w
= snewn(max
+1, unsigned int);
656 rowdata
= snewn(max
+1, int);
663 generate(rs
, w
, h
, grid
);
666 * The game is a bit too easy if any row or column is
667 * completely black or completely white. An exception is
668 * made for rows/columns that are under 3 squares,
669 * otherwise nothing will ever be successfully generated.
673 for (i
= 0; i
< h
; i
++) {
675 for (j
= 0; j
< w
; j
++)
676 colours
|= (grid
[i
*w
+j
] == GRID_FULL
? 2 : 1);
682 for (j
= 0; j
< w
; j
++) {
684 for (i
= 0; i
< h
; i
++)
685 colours
|= (grid
[i
*w
+j
] == GRID_FULL
? 2 : 1);
693 ok
= solve_puzzle(NULL
, grid
, w
, h
, matrix
, workspace
,
694 changed_h
, changed_w
, rowdata
, 0);
706 #ifdef STANDALONE_PICTURE_GENERATOR
707 unsigned char *picture
;
710 static char *new_game_desc(const game_params
*params
, random_state
*rs
,
711 char **aux
, int interactive
)
714 int i
, j
, max
, rowlen
, *rowdata
;
715 char intbuf
[80], *desc
;
716 int desclen
, descpos
;
717 #ifdef STANDALONE_PICTURE_GENERATOR
722 max
= max(params
->w
, params
->h
);
724 #ifdef STANDALONE_PICTURE_GENERATOR
726 * Fixed input picture.
728 grid
= snewn(params
->w
* params
->h
, unsigned char);
729 memcpy(grid
, picture
, params
->w
* params
->h
);
732 * Now winnow the immutable square set as far as possible.
734 state
= snew(game_state
);
736 state
->common
= snew(game_state_common
);
737 state
->common
->rowdata
= NULL
;
738 state
->common
->immutable
= snewn(params
->w
* params
->h
, unsigned char);
739 memset(state
->common
->immutable
, 1, params
->w
* params
->h
);
741 index
= snewn(params
->w
* params
->h
, int);
742 for (i
= 0; i
< params
->w
* params
->h
; i
++)
744 shuffle(index
, params
->w
* params
->h
, sizeof(*index
), rs
);
747 unsigned char *matrix
= snewn(params
->w
*params
->h
, unsigned char);
748 unsigned char *workspace
= snewn(max
*7, unsigned char);
749 unsigned int *changed_h
= snewn(max
+1, unsigned int);
750 unsigned int *changed_w
= snewn(max
+1, unsigned int);
751 int *rowdata
= snewn(max
+1, int);
752 for (i
= 0; i
< params
->w
* params
->h
; i
++) {
753 state
->common
->immutable
[index
[i
]] = 0;
754 if (!solve_puzzle(state
, grid
, params
->w
, params
->h
,
755 matrix
, workspace
, changed_h
, changed_w
,
757 state
->common
->immutable
[index
[i
]] = 1;
766 grid
= generate_soluble(rs
, params
->w
, params
->h
);
768 rowdata
= snewn(max
, int);
771 * Save the solved game in aux.
774 char *ai
= snewn(params
->w
* params
->h
+ 2, char);
777 * String format is exactly the same as a solve move, so we
778 * can just dupstr this in solve_game().
783 for (i
= 0; i
< params
->w
* params
->h
; i
++)
784 ai
[i
+1] = grid
[i
] ? '1' : '0';
786 ai
[params
->w
* params
->h
+ 1] = '\0';
792 * Seed is a slash-separated list of row contents; each row
793 * contents section is a dot-separated list of integers. Row
794 * contents are listed in the order (columns left to right,
795 * then rows top to bottom).
797 * Simplest way to handle memory allocation is to make two
798 * passes, first computing the seed size and then writing it
802 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
804 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
806 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
809 for (j
= 0; j
< rowlen
; j
++) {
810 desclen
+= 1 + sprintf(intbuf
, "%d", rowdata
[j
]);
816 desc
= snewn(desclen
, char);
818 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
820 rowlen
= compute_rowdata(rowdata
, grid
+i
, params
->h
, params
->w
);
822 rowlen
= compute_rowdata(rowdata
, grid
+(i
-params
->w
)*params
->w
,
825 for (j
= 0; j
< rowlen
; j
++) {
826 int len
= sprintf(desc
+descpos
, "%d", rowdata
[j
]);
828 desc
[descpos
+ len
] = '.';
830 desc
[descpos
+ len
] = '/';
834 desc
[descpos
++] = '/';
837 assert(descpos
== desclen
);
838 assert(desc
[desclen
-1] == '/');
839 desc
[desclen
-1] = '\0';
840 #ifdef STANDALONE_PICTURE_GENERATOR
841 for (i
= 0; i
< params
->w
* params
->h
; i
++)
842 if (state
->common
->immutable
[i
])
844 if (i
< params
->w
* params
->h
) {
846 * At least one immutable square, so we need a suffix.
850 desc
= sresize(desc
, desclen
+ params
->w
* params
->h
+ 3, char);
851 desc
[descpos
-1] = ',';
854 for (i
= 0; i
< params
->w
* params
->h
; i
++) {
855 if (!state
->common
->immutable
[i
]) {
858 desc
[descpos
++] = 'z';
862 desc
[descpos
++] = run
+ (grid
[i
] == GRID_FULL
? 'A' : 'a');
867 desc
[descpos
++] = run
+ 'a';
868 desc
[descpos
] = '\0';
870 sfree(state
->common
->immutable
);
871 sfree(state
->common
);
879 static char *validate_desc(const game_params
*params
, const char *desc
)
884 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
886 rowspace
= params
->h
+ 1;
888 rowspace
= params
->w
+ 1;
890 if (*desc
&& isdigit((unsigned char)*desc
)) {
893 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
899 return "at least one column contains more numbers than will fit";
901 return "at least one row contains more numbers than will fit";
903 } while (*desc
++ == '.');
905 desc
++; /* expect a slash immediately */
908 if (desc
[-1] == '/') {
909 if (i
+1 == params
->w
+ params
->h
)
910 return "too many row/column specifications";
911 } else if (desc
[-1] == '\0' || desc
[-1] == ',') {
912 if (i
+1 < params
->w
+ params
->h
)
913 return "too few row/column specifications";
915 return "unrecognised character in game specification";
918 if (desc
[-1] == ',') {
920 * Optional extra piece of game description which fills in
921 * some grid squares as extra clues.
924 while (i
< params
->w
* params
->h
) {
925 int c
= (unsigned char)*desc
++;
926 if ((c
>= 'a' && c
<= 'z') ||
927 (c
>= 'A' && c
<= 'Z')) {
928 int len
= tolower(c
) - 'a';
930 if (len
< 25 && i
< params
->w
*params
->h
)
932 if (i
> params
->w
* params
->h
) {
933 return "too much data in clue-squares section";
936 return "too little data in clue-squares section";
938 return "unrecognised character in clue-squares section";
942 return "too much data in clue-squares section";
949 static game_state
*new_game(midend
*me
, const game_params
*params
,
954 game_state
*state
= snew(game_state
);
956 state
->common
= snew(game_state_common
);
957 state
->common
->refcount
= 1;
959 state
->common
->w
= params
->w
;
960 state
->common
->h
= params
->h
;
962 state
->grid
= snewn(state
->common
->w
* state
->common
->h
, unsigned char);
963 memset(state
->grid
, GRID_UNKNOWN
, state
->common
->w
* state
->common
->h
);
965 state
->common
->immutable
= snewn(state
->common
->w
* state
->common
->h
,
967 memset(state
->common
->immutable
, 0, state
->common
->w
* state
->common
->h
);
969 state
->common
->rowsize
= max(state
->common
->w
, state
->common
->h
);
970 state
->common
->rowdata
= snewn(state
->common
->rowsize
* (state
->common
->w
+ state
->common
->h
), int);
971 state
->common
->rowlen
= snewn(state
->common
->w
+ state
->common
->h
, int);
973 state
->completed
= state
->cheated
= FALSE
;
975 for (i
= 0; i
< params
->w
+ params
->h
; i
++) {
976 state
->common
->rowlen
[i
] = 0;
977 if (*desc
&& isdigit((unsigned char)*desc
)) {
980 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
981 state
->common
->rowdata
[state
->common
->rowsize
* i
+ state
->common
->rowlen
[i
]++] =
983 } while (*desc
++ == '.');
985 desc
++; /* expect a slash immediately */
989 if (desc
[-1] == ',') {
991 * Optional extra piece of game description which fills in
992 * some grid squares as extra clues.
995 while (i
< params
->w
* params
->h
) {
996 int c
= (unsigned char)*desc
++;
997 int full
= isupper(c
), len
= tolower(c
) - 'a';
999 if (len
< 25 && i
< params
->w
*params
->h
) {
1000 state
->grid
[i
] = full
? GRID_FULL
: GRID_EMPTY
;
1001 state
->common
->immutable
[i
] = TRUE
;
1010 static game_state
*dup_game(const game_state
*state
)
1012 game_state
*ret
= snew(game_state
);
1014 ret
->common
= state
->common
;
1015 ret
->common
->refcount
++;
1017 ret
->grid
= snewn(ret
->common
->w
* ret
->common
->h
, unsigned char);
1018 memcpy(ret
->grid
, state
->grid
, ret
->common
->w
* ret
->common
->h
);
1020 ret
->completed
= state
->completed
;
1021 ret
->cheated
= state
->cheated
;
1026 static void free_game(game_state
*state
)
1028 if (--state
->common
->refcount
== 0) {
1029 sfree(state
->common
->rowdata
);
1030 sfree(state
->common
->rowlen
);
1031 sfree(state
->common
->immutable
);
1032 sfree(state
->common
);
1038 static char *solve_game(const game_state
*state
, const game_state
*currstate
,
1039 const char *ai
, char **error
)
1041 unsigned char *matrix
;
1042 int w
= state
->common
->w
, h
= state
->common
->h
;
1046 unsigned char *workspace
;
1047 unsigned int *changed_h
, *changed_w
;
1051 * If we already have the solved state in ai, copy it out.
1057 matrix
= snewn(w
*h
, unsigned char);
1058 workspace
= snewn(max
*7, unsigned char);
1059 changed_h
= snewn(max
+1, unsigned int);
1060 changed_w
= snewn(max
+1, unsigned int);
1061 rowdata
= snewn(max
+1, int);
1063 ok
= solve_puzzle(state
, NULL
, w
, h
, matrix
, workspace
,
1064 changed_h
, changed_w
, rowdata
, 0);
1073 *error
= "Solving algorithm cannot complete this puzzle";
1077 ret
= snewn(w
*h
+2, char);
1079 for (i
= 0; i
< w
*h
; i
++) {
1080 assert(matrix
[i
] == BLOCK
|| matrix
[i
] == DOT
);
1081 ret
[i
+1] = (matrix
[i
] == BLOCK
? '1' : '0');
1090 static int game_can_format_as_text_now(const game_params
*params
)
1095 static char *game_text_format(const game_state
*state
)
1097 int w
= state
->common
->w
, h
= state
->common
->h
, i
, j
;
1098 int left_gap
= 0, top_gap
= 0, ch
= 2, cw
= 1, limit
= 1;
1100 int len
, topleft
, lw
, lh
, gw
, gh
; /* {line,grid}_{width,height} */
1103 for (i
= 0; i
< w
; ++i
) {
1104 top_gap
= max(top_gap
, state
->common
->rowlen
[i
]);
1105 for (j
= 0; j
< state
->common
->rowlen
[i
]; ++j
)
1106 while (state
->common
->rowdata
[i
*state
->common
->rowsize
+ j
] >= limit
) {
1111 for (i
= 0; i
< h
; ++i
) {
1112 int rowlen
= 0, predecessors
= FALSE
;
1113 for (j
= 0; j
< state
->common
->rowlen
[i
+w
]; ++j
) {
1114 int copy
= state
->common
->rowdata
[(i
+w
)*state
->common
->rowsize
+ j
];
1115 rowlen
+= predecessors
;
1116 predecessors
= TRUE
;
1117 do ++rowlen
; while (copy
/= 10);
1119 left_gap
= max(left_gap
, rowlen
);
1129 topleft
= lw
* top_gap
+ left_gap
;
1131 board
= snewn(len
+ 1, char);
1132 sprintf(board
, "%*s\n", len
- 2, "");
1134 for (i
= 0; i
< lh
; ++i
) {
1135 board
[lw
- 1 + i
*lw
] = '\n';
1136 if (i
< top_gap
) continue;
1137 board
[lw
- 2 + i
*lw
] = ((i
- top_gap
) % ch
? '|' : '+');
1140 for (i
= 0; i
< w
; ++i
) {
1141 for (j
= 0; j
< state
->common
->rowlen
[i
]; ++j
) {
1142 int cell
= topleft
+ i
*cw
+ 1 + lw
*(j
- state
->common
->rowlen
[i
]);
1143 int nch
= sprintf(board
+ cell
, "%*d", cw
- 1,
1144 state
->common
->rowdata
[i
*state
->common
->rowsize
+ j
]);
1145 board
[cell
+ nch
] = ' '; /* de-NUL-ify */
1149 buf
= snewn(left_gap
, char);
1150 for (i
= 0; i
< h
; ++i
) {
1151 char *p
= buf
, *start
= board
+ top_gap
*lw
+ left_gap
+ (i
*ch
+1)*lw
;
1152 for (j
= 0; j
< state
->common
->rowlen
[i
+w
]; ++j
) {
1153 if (p
> buf
) *p
++ = ' ';
1154 p
+= sprintf(p
, "%d", state
->common
->rowdata
[(i
+w
)*state
->common
->rowsize
+ j
]);
1156 memcpy(start
- (p
- buf
), buf
, p
- buf
);
1159 for (i
= 0; i
< w
; ++i
) {
1160 for (j
= 0; j
< h
; ++j
) {
1161 int cell
= topleft
+ i
*cw
+ j
*ch
*lw
;
1162 int center
= cell
+ cw
/2 + (ch
/2)*lw
;
1164 board
[cell
] = 0 ? center
: '+';
1165 for (dx
= 1; dx
< cw
; ++dx
) board
[cell
+ dx
] = '-';
1166 for (dy
= 1; dy
< ch
; ++dy
) board
[cell
+ dy
*lw
] = '|';
1167 if (state
->grid
[i
*w
+j
] == GRID_UNKNOWN
) continue;
1168 for (dx
= 1; dx
< cw
; ++dx
)
1169 for (dy
= 1; dy
< ch
; ++dy
)
1170 board
[cell
+ dx
+ dy
*lw
] =
1171 state
->grid
[i
*w
+j
] == GRID_FULL
? '#' : '.';
1175 memcpy(board
+ topleft
+ h
*ch
*lw
, board
+ topleft
, gw
- 1);
1188 int drag
, release
, state
;
1189 int cur_x
, cur_y
, cur_visible
;
1192 static game_ui
*new_ui(const game_state
*state
)
1196 ret
= snew(game_ui
);
1197 ret
->dragging
= FALSE
;
1198 ret
->cur_x
= ret
->cur_y
= ret
->cur_visible
= 0;
1203 static void free_ui(game_ui
*ui
)
1208 static char *encode_ui(const game_ui
*ui
)
1213 static void decode_ui(game_ui
*ui
, const char *encoding
)
1217 static void game_changed_state(game_ui
*ui
, const game_state
*oldstate
,
1218 const game_state
*newstate
)
1222 struct game_drawstate
{
1226 unsigned char *visible
, *numcolours
;
1230 static char *interpret_move(const game_state
*state
, game_ui
*ui
,
1231 const game_drawstate
*ds
,
1232 int x
, int y
, int button
)
1234 int control
= button
& MOD_CTRL
, shift
= button
& MOD_SHFT
;
1235 button
&= ~MOD_MASK
;
1237 x
= FROMCOORD(state
->common
->w
, x
);
1238 y
= FROMCOORD(state
->common
->h
, y
);
1240 if (x
>= 0 && x
< state
->common
->w
&& y
>= 0 && y
< state
->common
->h
&&
1241 (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
||
1242 button
== MIDDLE_BUTTON
)) {
1244 int currstate
= state
->grid
[y
* state
->common
->w
+ x
];
1247 ui
->dragging
= TRUE
;
1249 if (button
== LEFT_BUTTON
) {
1250 ui
->drag
= LEFT_DRAG
;
1251 ui
->release
= LEFT_RELEASE
;
1253 ui
->state
= (currstate
+ 2) % 3; /* FULL -> EMPTY -> UNKNOWN */
1255 ui
->state
= GRID_FULL
;
1257 } else if (button
== RIGHT_BUTTON
) {
1258 ui
->drag
= RIGHT_DRAG
;
1259 ui
->release
= RIGHT_RELEASE
;
1261 ui
->state
= (currstate
+ 1) % 3; /* EMPTY -> FULL -> UNKNOWN */
1263 ui
->state
= GRID_EMPTY
;
1265 } else /* if (button == MIDDLE_BUTTON) */ {
1266 ui
->drag
= MIDDLE_DRAG
;
1267 ui
->release
= MIDDLE_RELEASE
;
1268 ui
->state
= GRID_UNKNOWN
;
1271 ui
->drag_start_x
= ui
->drag_end_x
= x
;
1272 ui
->drag_start_y
= ui
->drag_end_y
= y
;
1273 ui
->cur_visible
= 0;
1275 return ""; /* UI activity occurred */
1278 if (ui
->dragging
&& button
== ui
->drag
) {
1280 * There doesn't seem much point in allowing a rectangle
1281 * drag; people will generally only want to drag a single
1282 * horizontal or vertical line, so we make that easy by
1285 * Exception: if we're _middle_-button dragging to tag
1286 * things as UNKNOWN, we may well want to trash an entire
1287 * area and start over!
1289 if (ui
->state
!= GRID_UNKNOWN
) {
1290 if (abs(x
- ui
->drag_start_x
) > abs(y
- ui
->drag_start_y
))
1291 y
= ui
->drag_start_y
;
1293 x
= ui
->drag_start_x
;
1298 if (x
>= state
->common
->w
) x
= state
->common
->w
- 1;
1299 if (y
>= state
->common
->h
) y
= state
->common
->h
- 1;
1304 return ""; /* UI activity occurred */
1307 if (ui
->dragging
&& button
== ui
->release
) {
1308 int x1
, x2
, y1
, y2
, xx
, yy
;
1309 int move_needed
= FALSE
;
1311 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
1312 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
1313 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
1314 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
1316 for (yy
= y1
; yy
<= y2
; yy
++)
1317 for (xx
= x1
; xx
<= x2
; xx
++)
1318 if (!state
->common
->immutable
[yy
* state
->common
->w
+ xx
] &&
1319 state
->grid
[yy
* state
->common
->w
+ xx
] != ui
->state
)
1322 ui
->dragging
= FALSE
;
1326 sprintf(buf
, "%c%d,%d,%d,%d",
1327 (char)(ui
->state
== GRID_FULL
? 'F' :
1328 ui
->state
== GRID_EMPTY
? 'E' : 'U'),
1329 x1
, y1
, x2
-x1
+1, y2
-y1
+1);
1332 return ""; /* UI activity occurred */
1335 if (IS_CURSOR_MOVE(button
)) {
1336 int x
= ui
->cur_x
, y
= ui
->cur_y
, newstate
;
1338 move_cursor(button
, &ui
->cur_x
, &ui
->cur_y
, state
->common
->w
, state
->common
->h
, 0);
1339 ui
->cur_visible
= 1;
1340 if (!control
&& !shift
) return "";
1342 newstate
= control
? shift
? GRID_UNKNOWN
: GRID_FULL
: GRID_EMPTY
;
1343 if (state
->grid
[y
* state
->common
->w
+ x
] == newstate
&&
1344 state
->grid
[ui
->cur_y
* state
->common
->w
+ ui
->cur_x
] == newstate
)
1347 sprintf(buf
, "%c%d,%d,%d,%d", control
? shift
? 'U' : 'F' : 'E',
1348 min(x
, ui
->cur_x
), min(y
, ui
->cur_y
),
1349 abs(x
- ui
->cur_x
) + 1, abs(y
- ui
->cur_y
) + 1);
1353 if (IS_CURSOR_SELECT(button
)) {
1354 int currstate
= state
->grid
[ui
->cur_y
* state
->common
->w
+ ui
->cur_x
];
1358 if (!ui
->cur_visible
) {
1359 ui
->cur_visible
= 1;
1363 if (button
== CURSOR_SELECT2
)
1364 newstate
= currstate
== GRID_UNKNOWN
? GRID_EMPTY
:
1365 currstate
== GRID_EMPTY
? GRID_FULL
: GRID_UNKNOWN
;
1367 newstate
= currstate
== GRID_UNKNOWN
? GRID_FULL
:
1368 currstate
== GRID_FULL
? GRID_EMPTY
: GRID_UNKNOWN
;
1370 sprintf(buf
, "%c%d,%d,%d,%d",
1371 (char)(newstate
== GRID_FULL
? 'F' :
1372 newstate
== GRID_EMPTY
? 'E' : 'U'),
1373 ui
->cur_x
, ui
->cur_y
, 1, 1);
1380 static game_state
*execute_move(const game_state
*from
, const char *move
)
1383 int x1
, x2
, y1
, y2
, xx
, yy
;
1386 if (move
[0] == 'S' &&
1387 strlen(move
) == from
->common
->w
* from
->common
->h
+ 1) {
1390 ret
= dup_game(from
);
1392 for (i
= 0; i
< ret
->common
->w
* ret
->common
->h
; i
++)
1393 ret
->grid
[i
] = (move
[i
+1] == '1' ? GRID_FULL
: GRID_EMPTY
);
1395 ret
->completed
= ret
->cheated
= TRUE
;
1398 } else if ((move
[0] == 'F' || move
[0] == 'E' || move
[0] == 'U') &&
1399 sscanf(move
+1, "%d,%d,%d,%d", &x1
, &y1
, &x2
, &y2
) == 4 &&
1400 x1
>= 0 && x2
>= 0 && x1
+x2
<= from
->common
->w
&&
1401 y1
>= 0 && y2
>= 0 && y1
+y2
<= from
->common
->h
) {
1405 val
= (move
[0] == 'F' ? GRID_FULL
:
1406 move
[0] == 'E' ? GRID_EMPTY
: GRID_UNKNOWN
);
1408 ret
= dup_game(from
);
1409 for (yy
= y1
; yy
< y2
; yy
++)
1410 for (xx
= x1
; xx
< x2
; xx
++)
1411 if (!ret
->common
->immutable
[yy
* ret
->common
->w
+ xx
])
1412 ret
->grid
[yy
* ret
->common
->w
+ xx
] = val
;
1415 * An actual change, so check to see if we've completed the
1418 if (!ret
->completed
) {
1419 int *rowdata
= snewn(ret
->common
->rowsize
, int);
1422 ret
->completed
= TRUE
;
1424 for (i
=0; i
<ret
->common
->w
; i
++) {
1425 len
= compute_rowdata(rowdata
, ret
->grid
+i
,
1426 ret
->common
->h
, ret
->common
->w
);
1427 if (len
!= ret
->common
->rowlen
[i
] ||
1428 memcmp(ret
->common
->rowdata
+i
*ret
->common
->rowsize
,
1429 rowdata
, len
* sizeof(int))) {
1430 ret
->completed
= FALSE
;
1434 for (i
=0; i
<ret
->common
->h
; i
++) {
1435 len
= compute_rowdata(rowdata
, ret
->grid
+i
*ret
->common
->w
,
1437 if (len
!= ret
->common
->rowlen
[i
+ret
->common
->w
] ||
1438 memcmp(ret
->common
->rowdata
+
1439 (i
+ret
->common
->w
)*ret
->common
->rowsize
,
1440 rowdata
, len
* sizeof(int))) {
1441 ret
->completed
= FALSE
;
1454 /* ----------------------------------------------------------------------
1455 * Error-checking during gameplay.
1459 * The difficulty in error-checking Pattern is to make the error check
1460 * _weak_ enough. The most obvious way would be to check each row and
1461 * column by calling (a modified form of) do_row() to recursively
1462 * analyse the row contents against the clue set and see if the
1463 * GRID_UNKNOWNs could be filled in in any way that would end up
1464 * correct. However, this turns out to be such a strong error check as
1465 * to constitute a spoiler in many situations: you make a typo while
1466 * trying to fill in one row, and not only does the row light up to
1467 * indicate an error, but several columns crossed by the move also
1468 * light up and draw your attention to deductions you hadn't even
1469 * noticed you could make.
1471 * So instead I restrict error-checking to 'complete runs' within a
1472 * row, by which I mean contiguous sequences of GRID_FULL bounded at
1473 * both ends by either GRID_EMPTY or the ends of the row. We identify
1474 * all the complete runs in a row, and verify that _those_ are
1475 * consistent with the row's clue list. Sequences of complete runs
1476 * separated by solid GRID_EMPTY are required to match contiguous
1477 * sequences in the clue list, whereas if there's at least one
1478 * GRID_UNKNOWN between any two complete runs then those two need not
1479 * be contiguous in the clue list.
1481 * To simplify the edge cases, I pretend that the clue list for the
1482 * row is extended with a 0 at each end, and I also pretend that the
1483 * grid data for the row is extended with a GRID_EMPTY and a
1484 * zero-length run at each end. This permits the contiguity checker to
1485 * handle the fiddly end effects (e.g. if the first contiguous
1486 * sequence of complete runs in the grid matches _something_ in the
1487 * clue list but not at the beginning, this is allowable iff there's a
1488 * GRID_UNKNOWN before the first one) with minimal faff, since the end
1489 * effects just drop out as special cases of the normal inter-run
1490 * handling (in this code the above case is not 'at the end of the
1491 * clue list' at all, but between the implicit initial zero run and
1492 * the first nonzero one).
1494 * We must also be a little careful about how we search for a
1495 * contiguous sequence of runs. In the clue list (1 1 2 1 2 3),
1496 * suppose we see a GRID_UNKNOWN and then a length-1 run. We search
1497 * for 1 in the clue list and find it at the very beginning. But now
1498 * suppose we find a length-2 run with no GRID_UNKNOWN before it. We
1499 * can't naively look at the next clue from the 1 we found, because
1500 * that'll be the second 1 and won't match. Instead, we must backtrack
1501 * by observing that the 2 we've just found must be contiguous with
1502 * the 1 we've already seen, so we search for the sequence (1 2) and
1503 * find it starting at the second 1. Now if we see a 3, we must
1504 * rethink again and search for (1 2 3).
1507 struct errcheck_state
{
1509 * rowdata and rowlen point at the clue data for this row in the
1515 * rowpos indicates the lowest position where it would be valid to
1516 * see our next run length. It might be equal to rowlen,
1517 * indicating that the next run would have to be the terminating 0.
1521 * ncontig indicates how many runs we've seen in a contiguous
1522 * block. This is taken into account when searching for the next
1523 * run we find, unless ncontig is zeroed out first by encountering
1529 static int errcheck_found_run(struct errcheck_state
*es
, int r
)
1531 /* Macro to handle the pretence that rowdata has a 0 at each end */
1532 #define ROWDATA(k) ((k)<0 || (k)>=es->rowlen ? 0 : es->rowdata[(k)])
1535 * See if we can find this new run length at a position where it
1536 * also matches the last 'ncontig' runs we've seen.
1539 for (newpos
= es
->rowpos
; newpos
<= es
->rowlen
; newpos
++) {
1541 if (ROWDATA(newpos
) != r
)
1544 for (i
= 1; i
<= es
->ncontig
; i
++)
1545 if (ROWDATA(newpos
- i
) != ROWDATA(es
->rowpos
- i
))
1548 es
->rowpos
= newpos
+1;
1560 static int check_errors(const game_state
*state
, int i
)
1562 int start
, step
, end
, j
;
1564 struct errcheck_state aes
, *es
= &aes
;
1566 es
->rowlen
= state
->common
->rowlen
[i
];
1567 es
->rowdata
= state
->common
->rowdata
+ state
->common
->rowsize
* i
;
1568 /* Pretend that we've already encountered the initial zero run */
1572 if (i
< state
->common
->w
) {
1574 step
= state
->common
->w
;
1575 end
= start
+ step
* state
->common
->h
;
1577 start
= (i
- state
->common
->w
) * state
->common
->w
;
1579 end
= start
+ step
* state
->common
->w
;
1583 for (j
= start
- step
; j
<= end
; j
+= step
) {
1584 if (j
< start
|| j
== end
)
1587 val
= state
->grid
[j
];
1589 if (val
== GRID_UNKNOWN
) {
1592 } else if (val
== GRID_FULL
) {
1595 } else if (val
== GRID_EMPTY
) {
1597 if (!errcheck_found_run(es
, runlen
))
1598 return TRUE
; /* error! */
1604 /* Signal end-of-row by sending errcheck_found_run the terminating
1605 * zero run, which will be marked as contiguous with the previous
1606 * run if and only if there hasn't been a GRID_UNKNOWN before. */
1607 if (!errcheck_found_run(es
, 0))
1608 return TRUE
; /* error at the last minute! */
1610 return FALSE
; /* no error */
1613 /* ----------------------------------------------------------------------
1617 static void game_compute_size(const game_params
*params
, int tilesize
,
1620 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1621 struct { int tilesize
; } ads
, *ds
= &ads
;
1622 ads
.tilesize
= tilesize
;
1624 *x
= SIZE(params
->w
);
1625 *y
= SIZE(params
->h
);
1628 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1629 const game_params
*params
, int tilesize
)
1631 ds
->tilesize
= tilesize
;
1634 static float *game_colours(frontend
*fe
, int *ncolours
)
1636 float *ret
= snewn(3 * NCOLOURS
, float);
1639 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1641 for (i
= 0; i
< 3; i
++) {
1642 ret
[COL_GRID
* 3 + i
] = 0.3F
;
1643 ret
[COL_UNKNOWN
* 3 + i
] = 0.5F
;
1644 ret
[COL_TEXT
* 3 + i
] = 0.0F
;
1645 ret
[COL_FULL
* 3 + i
] = 0.0F
;
1646 ret
[COL_EMPTY
* 3 + i
] = 1.0F
;
1648 ret
[COL_CURSOR
* 3 + 0] = 1.0F
;
1649 ret
[COL_CURSOR
* 3 + 1] = 0.25F
;
1650 ret
[COL_CURSOR
* 3 + 2] = 0.25F
;
1651 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1652 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1653 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1655 *ncolours
= NCOLOURS
;
1659 static game_drawstate
*game_new_drawstate(drawing
*dr
, const game_state
*state
)
1661 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1663 ds
->started
= FALSE
;
1664 ds
->w
= state
->common
->w
;
1665 ds
->h
= state
->common
->h
;
1666 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned char);
1667 ds
->tilesize
= 0; /* not decided yet */
1668 memset(ds
->visible
, 255, ds
->w
* ds
->h
);
1669 ds
->numcolours
= snewn(ds
->w
+ ds
->h
, unsigned char);
1670 memset(ds
->numcolours
, 255, ds
->w
+ ds
->h
);
1671 ds
->cur_x
= ds
->cur_y
= 0;
1676 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1682 static void grid_square(drawing
*dr
, game_drawstate
*ds
,
1683 int y
, int x
, int state
, int cur
)
1685 int xl
, xr
, yt
, yb
, dx
, dy
, dw
, dh
;
1687 draw_rect(dr
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
1688 TILE_SIZE
, TILE_SIZE
, COL_GRID
);
1690 xl
= (x
% 5 == 0 ? 1 : 0);
1691 yt
= (y
% 5 == 0 ? 1 : 0);
1692 xr
= (x
% 5 == 4 || x
== ds
->w
-1 ? 1 : 0);
1693 yb
= (y
% 5 == 4 || y
== ds
->h
-1 ? 1 : 0);
1695 dx
= TOCOORD(ds
->w
, x
) + 1 + xl
;
1696 dy
= TOCOORD(ds
->h
, y
) + 1 + yt
;
1697 dw
= TILE_SIZE
- xl
- xr
- 1;
1698 dh
= TILE_SIZE
- yt
- yb
- 1;
1700 draw_rect(dr
, dx
, dy
, dw
, dh
,
1701 (state
== GRID_FULL
? COL_FULL
:
1702 state
== GRID_EMPTY
? COL_EMPTY
: COL_UNKNOWN
));
1704 draw_rect_outline(dr
, dx
, dy
, dw
, dh
, COL_CURSOR
);
1705 draw_rect_outline(dr
, dx
+1, dy
+1, dw
-2, dh
-2, COL_CURSOR
);
1708 draw_update(dr
, TOCOORD(ds
->w
, x
), TOCOORD(ds
->h
, y
),
1709 TILE_SIZE
, TILE_SIZE
);
1713 * Draw the numbers for a single row or column.
1715 static void draw_numbers(drawing
*dr
, game_drawstate
*ds
,
1716 const game_state
*state
, int i
, int erase
, int colour
)
1718 int rowlen
= state
->common
->rowlen
[i
];
1719 int *rowdata
= state
->common
->rowdata
+ state
->common
->rowsize
* i
;
1724 if (i
< state
->common
->w
) {
1725 draw_rect(dr
, TOCOORD(state
->common
->w
, i
), 0,
1726 TILE_SIZE
, BORDER
+ TLBORDER(state
->common
->h
) * TILE_SIZE
,
1729 draw_rect(dr
, 0, TOCOORD(state
->common
->h
, i
- state
->common
->w
),
1730 BORDER
+ TLBORDER(state
->common
->w
) * TILE_SIZE
, TILE_SIZE
,
1736 * Normally I space the numbers out by the same distance as the
1737 * tile size. However, if there are more numbers than available
1738 * spaces, I have to squash them up a bit.
1740 if (i
< state
->common
->w
)
1741 nfit
= TLBORDER(state
->common
->h
);
1743 nfit
= TLBORDER(state
->common
->w
);
1744 nfit
= max(rowlen
, nfit
) - 1;
1747 for (j
= 0; j
< rowlen
; j
++) {
1751 if (i
< state
->common
->w
) {
1752 x
= TOCOORD(state
->common
->w
, i
);
1753 y
= BORDER
+ TILE_SIZE
* (TLBORDER(state
->common
->h
)-1);
1754 y
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(state
->common
->h
)-1) / nfit
;
1756 y
= TOCOORD(state
->common
->h
, i
- state
->common
->w
);
1757 x
= BORDER
+ TILE_SIZE
* (TLBORDER(state
->common
->w
)-1);
1758 x
-= ((rowlen
-j
-1)*TILE_SIZE
) * (TLBORDER(state
->common
->w
)-1) / nfit
;
1761 sprintf(str
, "%d", rowdata
[j
]);
1762 draw_text(dr
, x
+TILE_SIZE
/2, y
+TILE_SIZE
/2, FONT_VARIABLE
,
1763 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, colour
, str
);
1766 if (i
< state
->common
->w
) {
1767 draw_update(dr
, TOCOORD(state
->common
->w
, i
), 0,
1768 TILE_SIZE
, BORDER
+ TLBORDER(state
->common
->h
) * TILE_SIZE
);
1770 draw_update(dr
, 0, TOCOORD(state
->common
->h
, i
- state
->common
->w
),
1771 BORDER
+ TLBORDER(state
->common
->w
) * TILE_SIZE
, TILE_SIZE
);
1775 static void game_redraw(drawing
*dr
, game_drawstate
*ds
,
1776 const game_state
*oldstate
, const game_state
*state
,
1777 int dir
, const game_ui
*ui
,
1778 float animtime
, float flashtime
)
1786 * The initial contents of the window are not guaranteed
1787 * and can vary with front ends. To be on the safe side,
1788 * all games should start by drawing a big background-
1789 * colour rectangle covering the whole window.
1791 draw_rect(dr
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
), COL_BACKGROUND
);
1794 * Draw the grid outline.
1796 draw_rect(dr
, TOCOORD(ds
->w
, 0) - 1, TOCOORD(ds
->h
, 0) - 1,
1797 ds
->w
* TILE_SIZE
+ 3, ds
->h
* TILE_SIZE
+ 3,
1802 draw_update(dr
, 0, 0, SIZE(ds
->w
), SIZE(ds
->h
));
1806 x1
= min(ui
->drag_start_x
, ui
->drag_end_x
);
1807 x2
= max(ui
->drag_start_x
, ui
->drag_end_x
);
1808 y1
= min(ui
->drag_start_y
, ui
->drag_end_y
);
1809 y2
= max(ui
->drag_start_y
, ui
->drag_end_y
);
1811 x1
= x2
= y1
= y2
= -1; /* placate gcc warnings */
1814 if (ui
->cur_visible
) {
1815 cx
= ui
->cur_x
; cy
= ui
->cur_y
;
1819 cmoved
= (cx
!= ds
->cur_x
|| cy
!= ds
->cur_y
);
1822 * Now draw any grid squares which have changed since last
1825 for (i
= 0; i
< ds
->h
; i
++) {
1826 for (j
= 0; j
< ds
->w
; j
++) {
1830 * Work out what state this square should be drawn in,
1831 * taking any current drag operation into account.
1833 if (ui
->dragging
&& x1
<= j
&& j
<= x2
&& y1
<= i
&& i
<= y2
&&
1834 !state
->common
->immutable
[i
* state
->common
->w
+ j
])
1837 val
= state
->grid
[i
* state
->common
->w
+ j
];
1840 /* the cursor has moved; if we were the old or
1841 * the new cursor position we need to redraw. */
1842 if (j
== cx
&& i
== cy
) cc
= 1;
1843 if (j
== ds
->cur_x
&& i
== ds
->cur_y
) cc
= 1;
1847 * Briefly invert everything twice during a completion
1850 if (flashtime
> 0 &&
1851 (flashtime
<= FLASH_TIME
/3 || flashtime
>= FLASH_TIME
*2/3) &&
1852 val
!= GRID_UNKNOWN
)
1853 val
= (GRID_FULL
^ GRID_EMPTY
) ^ val
;
1855 if (ds
->visible
[i
* ds
->w
+ j
] != val
|| cc
) {
1856 grid_square(dr
, ds
, i
, j
, val
,
1857 (j
== cx
&& i
== cy
));
1858 ds
->visible
[i
* ds
->w
+ j
] = val
;
1862 ds
->cur_x
= cx
; ds
->cur_y
= cy
;
1865 * Redraw any numbers which have changed their colour due to error
1868 for (i
= 0; i
< state
->common
->w
+ state
->common
->h
; i
++) {
1869 int colour
= check_errors(state
, i
) ? COL_ERROR
: COL_TEXT
;
1870 if (ds
->numcolours
[i
] != colour
) {
1871 draw_numbers(dr
, ds
, state
, i
, TRUE
, colour
);
1872 ds
->numcolours
[i
] = colour
;
1877 static float game_anim_length(const game_state
*oldstate
,
1878 const game_state
*newstate
, int dir
, game_ui
*ui
)
1883 static float game_flash_length(const game_state
*oldstate
,
1884 const game_state
*newstate
, int dir
, game_ui
*ui
)
1886 if (!oldstate
->completed
&& newstate
->completed
&&
1887 !oldstate
->cheated
&& !newstate
->cheated
)
1892 static int game_status(const game_state
*state
)
1894 return state
->completed
? +1 : 0;
1897 static int game_timing_state(const game_state
*state
, game_ui
*ui
)
1902 static void game_print_size(const game_params
*params
, float *x
, float *y
)
1907 * I'll use 5mm squares by default.
1909 game_compute_size(params
, 500, &pw
, &ph
);
1914 static void game_print(drawing
*dr
, const game_state
*state
, int tilesize
)
1916 int w
= state
->common
->w
, h
= state
->common
->h
;
1917 int ink
= print_mono_colour(dr
, 0);
1920 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1921 game_drawstate ads
, *ds
= &ads
;
1922 game_set_size(dr
, ds
, NULL
, tilesize
);
1927 print_line_width(dr
, TILE_SIZE
/ 16);
1928 draw_rect_outline(dr
, TOCOORD(w
, 0), TOCOORD(h
, 0),
1929 w
*TILE_SIZE
, h
*TILE_SIZE
, ink
);
1934 for (x
= 1; x
< w
; x
++) {
1935 print_line_width(dr
, TILE_SIZE
/ (x
% 5 ? 128 : 24));
1936 draw_line(dr
, TOCOORD(w
, x
), TOCOORD(h
, 0),
1937 TOCOORD(w
, x
), TOCOORD(h
, h
), ink
);
1939 for (y
= 1; y
< h
; y
++) {
1940 print_line_width(dr
, TILE_SIZE
/ (y
% 5 ? 128 : 24));
1941 draw_line(dr
, TOCOORD(w
, 0), TOCOORD(h
, y
),
1942 TOCOORD(w
, w
), TOCOORD(h
, y
), ink
);
1948 for (i
= 0; i
< state
->common
->w
+ state
->common
->h
; i
++)
1949 draw_numbers(dr
, ds
, state
, i
, FALSE
, ink
);
1954 print_line_width(dr
, TILE_SIZE
/ 128);
1955 for (y
= 0; y
< h
; y
++)
1956 for (x
= 0; x
< w
; x
++) {
1957 if (state
->grid
[y
*w
+x
] == GRID_FULL
)
1958 draw_rect(dr
, TOCOORD(w
, x
), TOCOORD(h
, y
),
1959 TILE_SIZE
, TILE_SIZE
, ink
);
1960 else if (state
->grid
[y
*w
+x
] == GRID_EMPTY
)
1961 draw_circle(dr
, TOCOORD(w
, x
) + TILE_SIZE
/2,
1962 TOCOORD(h
, y
) + TILE_SIZE
/2,
1963 TILE_SIZE
/12, ink
, ink
);
1968 #define thegame pattern
1971 const struct game thegame
= {
1972 "Pattern", "games.pattern", "pattern",
1974 game_fetch_preset
, NULL
,
1979 TRUE
, game_configure
, custom_params
,
1987 TRUE
, game_can_format_as_text_now
, game_text_format
,
1995 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1998 game_free_drawstate
,
2003 TRUE
, FALSE
, game_print_size
, game_print
,
2004 FALSE
, /* wants_statusbar */
2005 FALSE
, game_timing_state
,
2006 REQUIRE_RBUTTON
, /* flags */
2009 #ifdef STANDALONE_SOLVER
2011 int main(int argc
, char **argv
)
2015 char *id
= NULL
, *desc
, *err
;
2017 while (--argc
> 0) {
2020 if (!strcmp(p
, "-v")) {
2023 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2032 fprintf(stderr
, "usage: %s <game_id>\n", argv
[0]);
2036 desc
= strchr(id
, ':');
2038 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2043 p
= default_params();
2044 decode_params(p
, id
);
2045 err
= validate_desc(p
, desc
);
2047 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2050 s
= new_game(NULL
, p
, desc
);
2053 int w
= p
->w
, h
= p
->h
, i
, j
, max
, cluewid
= 0;
2054 unsigned char *matrix
, *workspace
;
2055 unsigned int *changed_h
, *changed_w
;
2058 matrix
= snewn(w
*h
, unsigned char);
2060 workspace
= snewn(max
*7, unsigned char);
2061 changed_h
= snewn(max
+1, unsigned int);
2062 changed_w
= snewn(max
+1, unsigned int);
2063 rowdata
= snewn(max
+1, int);
2068 * Work out the maximum text width of the clue numbers
2069 * in a row or column, so we can print the solver's
2070 * working in a nicely lined up way.
2072 for (i
= 0; i
< (w
+h
); i
++) {
2074 for (thiswid
= -1, j
= 0; j
< s
->common
->rowlen
[i
]; j
++)
2077 s
->common
->rowdata
[s
->common
->rowsize
*i
+j
]);
2078 if (cluewid
< thiswid
)
2083 solve_puzzle(s
, NULL
, w
, h
, matrix
, workspace
,
2084 changed_h
, changed_w
, rowdata
, cluewid
);
2086 for (i
= 0; i
< h
; i
++) {
2087 for (j
= 0; j
< w
; j
++) {
2088 int c
= (matrix
[i
*w
+j
] == UNKNOWN
? '?' :
2089 matrix
[i
*w
+j
] == BLOCK
? '#' :
2090 matrix
[i
*w
+j
] == DOT
? '.' :
2103 #ifdef STANDALONE_PICTURE_GENERATOR
2106 * Main program for the standalone picture generator. To use it,
2107 * simply provide it with an XBM-format bitmap file (note XBM, not
2108 * XPM) on standard input, and it will output a game ID in return.
2111 * $ ./patternpicture < calligraphic-A.xbm
2112 * 15x15:2/4/2/2/2/3/3/3.1/3.1/3.1/11/14/12/6/1/2/2/3/4/5/1.3/2.3/1.3/2.3/1.4/9/1.1.3/2.2.3/5.4/3.2
2114 * That looks easy, of course - all the program has done is to count
2115 * up the clue numbers! But in fact, it's done more than that: it's
2116 * also checked that the result is uniquely soluble from just the
2117 * numbers. If it hadn't been, then it would have also left some
2118 * filled squares in the playing area as extra clues.
2120 * $ ./patternpicture < cube.xbm
2121 * 15x15:10/2.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.10/1.1.1/1.1.1/1.1.1/2.1/10/10/1.2/1.1.1/1.1.1/1.1.1/10.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.1.1/1.2/10,TNINzzzzGNzw
2123 * This enables a reasonably convenient design workflow for coming up
2124 * with pictorial Pattern puzzles which _are_ uniquely soluble without
2125 * those inelegant pre-filled squares. Fire up a bitmap editor (X11
2126 * bitmap(1) is good enough), save a trial .xbm, and then test it by
2127 * running a command along the lines of
2129 * $ ./pattern $(./patternpicture < test.xbm)
2131 * If the resulting window pops up with some pre-filled squares, then
2132 * that tells you which parts of the image are giving rise to
2133 * ambiguities, so try making tweaks in those areas, try the test
2134 * command again, and see if it helps. Once you have a design for
2135 * which the Pattern starting grid comes out empty, there's your game
2141 int main(int argc
, char **argv
)
2144 char *params
, *desc
;
2146 time_t seed
= time(NULL
);
2151 par
= default_params();
2153 decode_params(par
, argv
[1]); /* get difficulty */
2154 par
->w
= par
->h
= -1;
2157 * Now read an XBM file from standard input. This is simple and
2158 * hacky and will do very little error detection, so don't feed
2163 while (fgets(buf
, sizeof(buf
), stdin
)) {
2164 buf
[strcspn(buf
, "\r\n")] = '\0';
2165 if (!strncmp(buf
, "#define", 7)) {
2167 * Lines starting `#define' give the width and height.
2169 char *num
= buf
+ strlen(buf
);
2172 while (num
> buf
&& isdigit((unsigned char)num
[-1]))
2175 while (symend
> buf
&& isspace((unsigned char)symend
[-1]))
2178 if (symend
-5 >= buf
&& !strncmp(symend
-5, "width", 5))
2180 else if (symend
-6 >= buf
&& !strncmp(symend
-6, "height", 6))
2184 * Otherwise, break the string up into words and take
2185 * any word of the form `0x' plus hex digits to be a
2188 char *p
, *wordstart
;
2191 if (par
->w
< 0 || par
->h
< 0) {
2192 printf("failed to read width and height\n");
2195 picture
= snewn(par
->w
* par
->h
, unsigned char);
2196 for (i
= 0; i
< par
->w
* par
->h
; i
++)
2197 picture
[i
] = GRID_UNKNOWN
;
2202 while (*p
&& (*p
== ',' || isspace((unsigned char)*p
)))
2205 while (*p
&& !(*p
== ',' || *p
== '}' ||
2206 isspace((unsigned char)*p
)))
2211 if (wordstart
[0] == '0' &&
2212 (wordstart
[1] == 'x' || wordstart
[1] == 'X') &&
2213 !wordstart
[2 + strspn(wordstart
+2,
2214 "0123456789abcdefABCDEF")]) {
2215 unsigned long byte
= strtoul(wordstart
+2, NULL
, 16);
2216 for (i
= 0; i
< 8; i
++) {
2217 int bit
= (byte
>> i
) & 1;
2218 if (y
< par
->h
&& x
< par
->w
)
2219 picture
[y
* par
->w
+ x
] =
2220 bit
? GRID_FULL
: GRID_EMPTY
;
2233 for (i
= 0; i
< par
->w
* par
->h
; i
++)
2234 if (picture
[i
] == GRID_UNKNOWN
) {
2235 fprintf(stderr
, "failed to read enough bitmap data\n");
2239 rs
= random_new((void*)&seed
, sizeof(time_t));
2241 desc
= new_game_desc(par
, rs
, NULL
, FALSE
);
2242 params
= encode_params(par
, FALSE
);
2243 printf("%s:%s\n", params
, desc
);
2255 /* vim: set shiftwidth=4 tabstop=8: */