-
Notifications
You must be signed in to change notification settings - Fork 24
/
galaxies.c
3908 lines (3403 loc) · 115 KB
/
galaxies.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* galaxies.c: implementation of 'Tentai Show' from Nikoli,
* also sometimes called 'Spiral Galaxies'.
*
* Notes:
*
* Grid is stored as size (2n-1), holding edges as well as spaces
* (and thus vertices too, at edge intersections).
*
* Any dot will thus be positioned at one of our grid points,
* which saves any faffing with half-of-a-square stuff.
*
* Edges have on/off state; obviously the actual edges of the
* board are fixed to on, and everything else starts as off.
*
* TTD:
* Cleverer solver
* Think about how to display remote groups of tiles?
*
* Bugs:
*
* Notable puzzle IDs:
*
* Nikoli's example [web site has wrong highlighting]
* (at http://www.nikoli.co.jp/en/puzzles/astronomical_show/):
* 5x5:eBbbMlaBbOEnf
*
* The 'spiral galaxies puzzles are NP-complete' paper
* (at http://www.stetson.edu/~efriedma/papers/spiral.pdf):
* 7x7:chpgdqqqoezdddki
*
* Puzzle competition pdf examples
* (at http://www.puzzleratings.org/Yurekli2006puz.pdf):
* 6x6:EDbaMucCohbrecEi
* 10x10:beFbufEEzowDlxldibMHezBQzCdcFzjlci
* 13x13:dCemIHFFkJajjgDfdbdBzdzEgjccoPOcztHjBczLDjczqktJjmpreivvNcggFi
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#ifdef DEBUGGING
#define solvep debug
#else
int solver_show_working;
#define solvep(x) do { if (solver_show_working) { printf x; } } while(0)
#endif
#ifdef STANDALONE_PICTURE_GENERATOR
/*
* Dirty hack to enable the generator to construct a game ID which
* solves to a specified black-and-white bitmap. We define a global
* variable here which gives the desired colour of each square, and
* we arrange that the grid generator never merges squares of
* different colours.
*
* The bitmap as stored here is a simple int array (at these sizes
* it isn't worth doing fiddly bit-packing). picture[y*w+x] is 1
* iff the pixel at (x,y) is intended to be black.
*
* (It might be nice to be able to specify some pixels as
* don't-care, to give the generator more leeway. But that might be
* fiddly.)
*/
static int *picture;
#endif
enum {
COL_BACKGROUND,
COL_WHITEBG,
COL_BLACKBG,
COL_WHITEDOT,
COL_BLACKDOT,
COL_GRID,
COL_EDGE,
COL_ARROW,
COL_CURSOR,
NCOLOURS
};
#define DIFFLIST(A) \
A(NORMAL,Normal,n) \
A(UNREASONABLE,Unreasonable,u)
#define ENUM(upper,title,lower) DIFF_ ## upper,
#define TITLE(upper,title,lower) #title,
#define ENCODE(upper,title,lower) #lower
#define CONFIG(upper,title,lower) ":" #title
enum { DIFFLIST(ENUM)
DIFF_IMPOSSIBLE, DIFF_AMBIGUOUS, DIFF_UNFINISHED, DIFF_MAX };
static char const *const galaxies_diffnames[] = {
DIFFLIST(TITLE) "Impossible", "Ambiguous", "Unfinished" };
static char const galaxies_diffchars[] = DIFFLIST(ENCODE);
#define DIFFCONFIG DIFFLIST(CONFIG)
struct game_params {
/* X and Y is the area of the board as seen by
* the user, not the (2n+1) area the game uses. */
int w, h, diff;
};
enum { s_tile, s_edge, s_vertex };
#define F_DOT 1 /* there's a dot here */
#define F_EDGE_SET 2 /* the edge is set */
#define F_TILE_ASSOC 4 /* this tile is associated with a dot. */
#define F_DOT_BLACK 8 /* (ui only) dot is black. */
#define F_MARK 16 /* scratch flag */
#define F_REACHABLE 32
#define F_SCRATCH 64
#define F_MULTIPLE 128
#define F_DOT_HOLD 256
#define F_GOOD 512
typedef struct space {
int x, y; /* its position */
int type;
unsigned int flags;
int dotx, doty; /* if flags & F_TILE_ASSOC */
int nassoc; /* if flags & F_DOT */
} space;
#define INGRID(s,x,y) ((x) >= 0 && (y) >= 0 && \
(x) < (state)->sx && (y) < (state)->sy)
#define INUI(s,x,y) ((x) > 0 && (y) > 0 && \
(x) < ((state)->sx-1) && (y) < ((state)->sy-1))
#define GRID(s,g,x,y) ((s)->g[((y)*(s)->sx)+(x)])
#define SPACE(s,x,y) GRID(s,grid,x,y)
struct game_state {
int w, h; /* size from params */
int sx, sy; /* allocated size, (2x-1)*(2y-1) */
space *grid;
int completed, used_solve;
int ndots;
space **dots;
midend *me; /* to call supersede_game_desc */
int cdiff; /* difficulty of current puzzle (for status bar),
or -1 if stale. */
};
/* ----------------------------------------------------------
* Game parameters and presets
*/
/* make up some sensible default sizes */
#define DEFAULT_PRESET 0
static const game_params galaxies_presets[] = {
{ 7, 7, DIFF_NORMAL },
{ 7, 7, DIFF_UNREASONABLE },
{ 10, 10, DIFF_NORMAL },
{ 15, 15, DIFF_NORMAL },
};
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char buf[80];
if (i < 0 || i >= lenof(galaxies_presets))
return FALSE;
ret = snew(game_params);
*ret = galaxies_presets[i]; /* structure copy */
sprintf(buf, "%dx%d %s", ret->w, ret->h,
galaxies_diffnames[ret->diff]);
if (name) *name = dupstr(buf);
*params = ret;
return TRUE;
}
static game_params *default_params(void)
{
game_params *ret;
game_fetch_preset(DEFAULT_PRESET, NULL, &ret);
return ret;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *params, char const *string)
{
params->h = params->w = atoi(string);
params->diff = DIFF_NORMAL;
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
params->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'd') {
int i;
string++;
for (i = 0; i <= DIFF_UNREASONABLE; i++)
if (*string == galaxies_diffchars[i])
params->diff = i;
if (*string) string++;
}
}
static char *encode_params(const game_params *params, int full)
{
char str[80];
sprintf(str, "%dx%d", params->w, params->h);
if (full)
sprintf(str + strlen(str), "d%c", galaxies_diffchars[params->diff]);
return dupstr(str);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(4, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].sval = dupstr(buf);
ret[0].ival = 0;
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].sval = dupstr(buf);
ret[1].ival = 0;
ret[2].name = "Difficulty";
ret[2].type = C_CHOICES;
ret[2].sval = DIFFCONFIG;
ret[2].ival = params->diff;
ret[3].name = NULL;
ret[3].type = C_END;
ret[3].sval = NULL;
ret[3].ival = 0;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].sval);
ret->h = atoi(cfg[1].sval);
ret->diff = cfg[2].ival;
return ret;
}
static char *validate_params(const game_params *params, int full)
{
if (params->w < 3 || params->h < 3)
return "Width and height must both be at least 3";
/*
* This shouldn't be able to happen at all, since decode_params
* and custom_params will never generate anything that isn't
* within range.
*/
assert(params->diff <= DIFF_UNREASONABLE);
return NULL;
}
/* ----------------------------------------------------------
* Game utility functions.
*/
static void add_dot(space *space) {
assert(!(space->flags & F_DOT));
space->flags |= F_DOT;
space->nassoc = 0;
}
static void remove_dot(space *space) {
assert(space->flags & F_DOT);
space->flags &= ~F_DOT;
}
static void remove_assoc(const game_state *state, space *tile) {
if (tile->flags & F_TILE_ASSOC) {
SPACE(state, tile->dotx, tile->doty).nassoc--;
tile->flags &= ~F_TILE_ASSOC;
tile->dotx = -1;
tile->doty = -1;
}
}
static void add_assoc(const game_state *state, space *tile, space *dot) {
remove_assoc(state, tile);
#ifdef STANDALONE_PICTURE_GENERATOR
if (picture)
assert(!picture[(tile->y/2) * state->w + (tile->x/2)] ==
!(dot->flags & F_DOT_BLACK));
#endif
tile->flags |= F_TILE_ASSOC;
tile->dotx = dot->x;
tile->doty = dot->y;
dot->nassoc++;
/*debug(("add_assoc sp %d %d --> dot %d,%d, new nassoc %d.\n",
tile->x, tile->y, dot->x, dot->y, dot->nassoc));*/
}
static struct space *sp2dot(const game_state *state, int x, int y)
{
struct space *sp = &SPACE(state, x, y);
if (!(sp->flags & F_TILE_ASSOC)) return NULL;
return &SPACE(state, sp->dotx, sp->doty);
}
#define IS_VERTICAL_EDGE(x) ((x % 2) == 0)
static int game_can_format_as_text_now(const game_params *params)
{
return TRUE;
}
static char *game_text_format(const game_state *state)
{
int maxlen = (state->sx+1)*state->sy, x, y;
char *ret, *p;
space *sp;
ret = snewn(maxlen+1, char);
p = ret;
for (y = 0; y < state->sy; y++) {
for (x = 0; x < state->sx; x++) {
sp = &SPACE(state, x, y);
if (sp->flags & F_DOT)
*p++ = 'o';
#if 0
else if (sp->flags & (F_REACHABLE|F_MULTIPLE|F_MARK))
*p++ = (sp->flags & F_MULTIPLE) ? 'M' :
(sp->flags & F_REACHABLE) ? 'R' : 'X';
#endif
else {
switch (sp->type) {
case s_tile:
if (sp->flags & F_TILE_ASSOC) {
space *dot = sp2dot(state, sp->x, sp->y);
if (dot && dot->flags & F_DOT)
*p++ = (dot->flags & F_DOT_BLACK) ? 'B' : 'W';
else
*p++ = '?'; /* association with not-a-dot. */
} else
*p++ = ' ';
break;
case s_vertex:
*p++ = '+';
break;
case s_edge:
if (sp->flags & F_EDGE_SET)
*p++ = (IS_VERTICAL_EDGE(x)) ? '|' : '-';
else
*p++ = ' ';
break;
default:
assert(!"shouldn't get here!");
}
}
}
*p++ = '\n';
}
assert(p - ret == maxlen);
*p = '\0';
return ret;
}
static void dbg_state(const game_state *state)
{
#ifdef DEBUGGING
char *temp = game_text_format(state);
debug(("%s\n", temp));
sfree(temp);
#endif
}
/* Space-enumeration callbacks should all return 1 for 'progress made',
* -1 for 'impossible', and 0 otherwise. */
typedef int (*space_cb)(game_state *state, space *sp, void *ctx);
#define IMPOSSIBLE_QUITS 1
static int foreach_sub(game_state *state, space_cb cb, unsigned int f,
void *ctx, int startx, int starty)
{
int x, y, progress = 0, impossible = 0, ret;
space *sp;
for (y = starty; y < state->sy; y += 2) {
sp = &SPACE(state, startx, y);
for (x = startx; x < state->sx; x += 2) {
ret = cb(state, sp, ctx);
if (ret == -1) {
if (f & IMPOSSIBLE_QUITS) return -1;
impossible = -1;
} else if (ret == 1) {
progress = 1;
}
sp += 2;
}
}
return impossible ? -1 : progress;
}
static int foreach_tile(game_state *state, space_cb cb, unsigned int f,
void *ctx)
{
return foreach_sub(state, cb, f, ctx, 1, 1);
}
static int foreach_edge(game_state *state, space_cb cb, unsigned int f,
void *ctx)
{
int ret1, ret2;
ret1 = foreach_sub(state, cb, f, ctx, 0, 1);
ret2 = foreach_sub(state, cb, f, ctx, 1, 0);
if (ret1 == -1 || ret2 == -1) return -1;
return (ret1 || ret2) ? 1 : 0;
}
#if 0
static int foreach_vertex(game_state *state, space_cb cb, unsigned int f,
void *ctx)
{
return foreach_sub(state, cb, f, ctx, 0, 0);
}
#endif
#if 0
static int is_same_assoc(game_state *state,
int x1, int y1, int x2, int y2)
{
struct space *s1, *s2;
if (!INGRID(state, x1, y1) || !INGRID(state, x2, y2))
return 0;
s1 = &SPACE(state, x1, y1);
s2 = &SPACE(state, x2, y2);
assert(s1->type == s_tile && s2->type == s_tile);
if ((s1->flags & F_TILE_ASSOC) && (s2->flags & F_TILE_ASSOC) &&
s1->dotx == s2->dotx && s1->doty == s2->doty)
return 1;
return 0; /* 0 if not same or not both associated. */
}
#endif
#if 0
static int edges_into_vertex(game_state *state,
int x, int y)
{
int dx, dy, nx, ny, count = 0;
assert(SPACE(state, x, y).type == s_vertex);
for (dx = -1; dx <= 1; dx++) {
for (dy = -1; dy <= 1; dy++) {
if (dx != 0 && dy != 0) continue;
if (dx == 0 && dy == 0) continue;
nx = x+dx; ny = y+dy;
if (!INGRID(state, nx, ny)) continue;
assert(SPACE(state, nx, ny).type == s_edge);
if (SPACE(state, nx, ny).flags & F_EDGE_SET)
count++;
}
}
return count;
}
#endif
static struct space *space_opposite_dot(struct game_state *state,
struct space *sp, struct space *dot)
{
int dx, dy, tx, ty;
space *sp2;
dx = sp->x - dot->x;
dy = sp->y - dot->y;
tx = dot->x - dx;
ty = dot->y - dy;
if (!INGRID(state, tx, ty)) return NULL;
sp2 = &SPACE(state, tx, ty);
assert(sp2->type == sp->type);
return sp2;
}
static struct space *tile_opposite(struct game_state *state, struct space *sp)
{
struct space *dot;
assert(sp->flags & F_TILE_ASSOC);
dot = &SPACE(state, sp->dotx, sp->doty);
return space_opposite_dot(state, sp, dot);
}
static int dotfortile(game_state *state, space *tile, space *dot)
{
space *tile_opp = space_opposite_dot(state, tile, dot);
if (!tile_opp) return 0; /* opposite would be off grid */
if (tile_opp->flags & F_TILE_ASSOC &&
(tile_opp->dotx != dot->x || tile_opp->doty != dot->y))
return 0; /* opposite already associated with diff. dot */
return 1;
}
static void adjacencies(struct game_state *state, struct space *sp,
struct space **a1s, struct space **a2s)
{
int dxs[4] = {-1, 1, 0, 0}, dys[4] = {0, 0, -1, 1};
int n, x, y;
/* this function needs optimising. */
for (n = 0; n < 4; n++) {
x = sp->x+dxs[n];
y = sp->y+dys[n];
if (INGRID(state, x, y)) {
a1s[n] = &SPACE(state, x, y);
x += dxs[n]; y += dys[n];
if (INGRID(state, x, y))
a2s[n] = &SPACE(state, x, y);
else
a2s[n] = NULL;
} else {
a1s[n] = a2s[n] = NULL;
}
}
}
static int outline_tile_fordot(game_state *state, space *tile, int mark)
{
struct space *tadj[4], *eadj[4];
int i, didsth = 0, edge, same;
assert(tile->type == s_tile);
adjacencies(state, tile, eadj, tadj);
for (i = 0; i < 4; i++) {
if (!eadj[i]) continue;
edge = (eadj[i]->flags & F_EDGE_SET) ? 1 : 0;
if (tadj[i]) {
if (!(tile->flags & F_TILE_ASSOC))
same = (tadj[i]->flags & F_TILE_ASSOC) ? 0 : 1;
else
same = ((tadj[i]->flags & F_TILE_ASSOC) &&
tile->dotx == tadj[i]->dotx &&
tile->doty == tadj[i]->doty) ? 1 : 0;
} else
same = 0;
if (!edge && !same) {
if (mark) eadj[i]->flags |= F_EDGE_SET;
didsth = 1;
} else if (edge && same) {
if (mark) eadj[i]->flags &= ~F_EDGE_SET;
didsth = 1;
}
}
return didsth;
}
static void tiles_from_edge(struct game_state *state,
struct space *sp, struct space **ts)
{
int xs[2], ys[2];
if (IS_VERTICAL_EDGE(sp->x)) {
xs[0] = sp->x-1; ys[0] = sp->y;
xs[1] = sp->x+1; ys[1] = sp->y;
} else {
xs[0] = sp->x; ys[0] = sp->y-1;
xs[1] = sp->x; ys[1] = sp->y+1;
}
ts[0] = INGRID(state, xs[0], ys[0]) ? &SPACE(state, xs[0], ys[0]) : NULL;
ts[1] = INGRID(state, xs[1], ys[1]) ? &SPACE(state, xs[1], ys[1]) : NULL;
}
/* Returns a move string for use by 'solve', including the initial
* 'S' if issolve is true. */
static char *diff_game(const game_state *src, const game_state *dest,
int issolve)
{
int movelen = 0, movesize = 256, x, y, len;
char *move = snewn(movesize, char), buf[80], *sep = "";
char achar = issolve ? 'a' : 'A';
space *sps, *spd;
assert(src->sx == dest->sx && src->sy == dest->sy);
if (issolve) {
move[movelen++] = 'S';
sep = ";";
}
move[movelen] = '\0';
for (x = 0; x < src->sx; x++) {
for (y = 0; y < src->sy; y++) {
sps = &SPACE(src, x, y);
spd = &SPACE(dest, x, y);
assert(sps->type == spd->type);
len = 0;
if (sps->type == s_tile) {
if ((sps->flags & F_TILE_ASSOC) &&
(spd->flags & F_TILE_ASSOC)) {
if (sps->dotx != spd->dotx ||
sps->doty != spd->doty)
/* Both associated; change association, if different */
len = sprintf(buf, "%s%c%d,%d,%d,%d", sep,
(int)achar, x, y, spd->dotx, spd->doty);
} else if (sps->flags & F_TILE_ASSOC)
/* Only src associated; remove. */
len = sprintf(buf, "%sU%d,%d", sep, x, y);
else if (spd->flags & F_TILE_ASSOC)
/* Only dest associated; add. */
len = sprintf(buf, "%s%c%d,%d,%d,%d", sep,
(int)achar, x, y, spd->dotx, spd->doty);
} else if (sps->type == s_edge) {
if ((sps->flags & F_EDGE_SET) != (spd->flags & F_EDGE_SET))
/* edge flags are different; flip them. */
len = sprintf(buf, "%sE%d,%d", sep, x, y);
}
if (len) {
if (movelen + len >= movesize) {
movesize = movelen + len + 256;
move = sresize(move, movesize, char);
}
strcpy(move + movelen, buf);
movelen += len;
sep = ";";
}
}
}
debug(("diff_game src then dest:\n"));
dbg_state(src);
dbg_state(dest);
debug(("diff string %s\n", move));
return move;
}
/* Returns 1 if a dot here would not be too close to any other dots
* (and would avoid other game furniture). */
static int dot_is_possible(game_state *state, space *sp, int allow_assoc)
{
int bx = 0, by = 0, dx, dy;
space *adj;
#ifdef STANDALONE_PICTURE_GENERATOR
int col = -1;
#endif
switch (sp->type) {
case s_tile:
bx = by = 1; break;
case s_edge:
if (IS_VERTICAL_EDGE(sp->x)) {
bx = 2; by = 1;
} else {
bx = 1; by = 2;
}
break;
case s_vertex:
bx = by = 2; break;
}
for (dx = -bx; dx <= bx; dx++) {
for (dy = -by; dy <= by; dy++) {
if (!INGRID(state, sp->x+dx, sp->y+dy)) continue;
adj = &SPACE(state, sp->x+dx, sp->y+dy);
#ifdef STANDALONE_PICTURE_GENERATOR
/*
* Check that all the squares we're looking at have the
* same colour.
*/
if (picture) {
if (adj->type == s_tile) {
int c = picture[(adj->y / 2) * state->w + (adj->x / 2)];
if (col < 0)
col = c;
if (c != col)
return 0; /* colour mismatch */
}
}
#endif
if (!allow_assoc && (adj->flags & F_TILE_ASSOC))
return 0;
if (dx != 0 || dy != 0) {
/* Other than our own square, no dots nearby. */
if (adj->flags & (F_DOT))
return 0;
}
/* We don't want edges within our rectangle
* (but don't care about edges on the edge) */
if (abs(dx) < bx && abs(dy) < by &&
adj->flags & F_EDGE_SET)
return 0;
}
}
return 1;
}
/* ----------------------------------------------------------
* Game generation, structure creation, and descriptions.
*/
static game_state *blank_game(int w, int h)
{
game_state *state = snew(game_state);
int x, y;
state->w = w;
state->h = h;
state->sx = (w*2)+1;
state->sy = (h*2)+1;
state->grid = snewn(state->sx * state->sy, struct space);
state->completed = state->used_solve = 0;
for (x = 0; x < state->sx; x++) {
for (y = 0; y < state->sy; y++) {
struct space *sp = &SPACE(state, x, y);
memset(sp, 0, sizeof(struct space));
sp->x = x;
sp->y = y;
if ((x % 2) == 0 && (y % 2) == 0)
sp->type = s_vertex;
else if ((x % 2) == 0 || (y % 2) == 0) {
sp->type = s_edge;
if (x == 0 || y == 0 || x == state->sx-1 || y == state->sy-1)
sp->flags |= F_EDGE_SET;
} else
sp->type = s_tile;
}
}
state->ndots = 0;
state->dots = NULL;
state->me = NULL; /* filled in by new_game. */
state->cdiff = -1;
return state;
}
static void game_update_dots(game_state *state)
{
int i, n, sz = state->sx * state->sy;
if (state->dots) sfree(state->dots);
state->ndots = 0;
for (i = 0; i < sz; i++) {
if (state->grid[i].flags & F_DOT) state->ndots++;
}
state->dots = snewn(state->ndots, space *);
n = 0;
for (i = 0; i < sz; i++) {
if (state->grid[i].flags & F_DOT)
state->dots[n++] = &state->grid[i];
}
}
static void clear_game(game_state *state, int cleardots)
{
int x, y;
/* don't erase edge flags around outline! */
for (x = 1; x < state->sx-1; x++) {
for (y = 1; y < state->sy-1; y++) {
if (cleardots)
SPACE(state, x, y).flags = 0;
else
SPACE(state, x, y).flags &= (F_DOT|F_DOT_BLACK);
}
}
if (cleardots) game_update_dots(state);
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = blank_game(state->w, state->h);
ret->completed = state->completed;
ret->used_solve = state->used_solve;
memcpy(ret->grid, state->grid,
ret->sx*ret->sy*sizeof(struct space));
game_update_dots(ret);
ret->me = state->me;
ret->cdiff = state->cdiff;
return ret;
}
static void free_game(game_state *state)
{
if (state->dots) sfree(state->dots);
sfree(state->grid);
sfree(state);
}
/* Game description is a sequence of letters representing the number
* of spaces (a = 0, y = 24) before the next dot; a-y for a white dot,
* and A-Y for a black dot. 'z' is 25 spaces (and no dot).
*
* I know it's a bitch to generate by hand, so we provide
* an edit mode.
*/
static char *encode_game(game_state *state)
{
char *desc, *p;
int run, x, y, area;
unsigned int f;
area = (state->sx-2) * (state->sy-2);
desc = snewn(area, char);
p = desc;
run = 0;
for (y = 1; y < state->sy-1; y++) {
for (x = 1; x < state->sx-1; x++) {
f = SPACE(state, x, y).flags;
/* a/A is 0 spaces between, b/B is 1 space, ...
* y/Y is 24 spaces, za/zA is 25 spaces, ...
* It's easier to count from 0 because we then
* don't have to special-case the top left-hand corner
* (which could be a dot with 0 spaces before it). */
if (!(f & F_DOT))
run++;
else {
while (run > 24) {
*p++ = 'z';
run -= 25;
}
*p++ = ((f & F_DOT_BLACK) ? 'A' : 'a') + run;
run = 0;
}
}
}
assert(p - desc < area);
*p++ = '\0';
desc = sresize(desc, p - desc, char);
return desc;
}
struct movedot {
int op;
space *olddot, *newdot;
};
enum { MD_CHECK, MD_MOVE };
static int movedot_cb(game_state *state, space *tile, void *vctx)
{
struct movedot *md = (struct movedot *)vctx;
space *newopp = NULL;
assert(tile->type == s_tile);
assert(md->olddot && md->newdot);
if (!(tile->flags & F_TILE_ASSOC)) return 0;
if (tile->dotx != md->olddot->x || tile->doty != md->olddot->y)
return 0;
newopp = space_opposite_dot(state, tile, md->newdot);
switch (md->op) {
case MD_CHECK:
/* If the tile is associated with the old dot, check its
* opposite wrt the _new_ dot is empty or same assoc. */
if (!newopp) return -1; /* no new opposite */
if (newopp->flags & F_TILE_ASSOC) {
if (newopp->dotx != md->olddot->x ||
newopp->doty != md->olddot->y)
return -1; /* associated, but wrong dot. */
}
#ifdef STANDALONE_PICTURE_GENERATOR
if (picture) {
/*
* Reject if either tile and the dot don't match in colour.
*/
if (!(picture[(tile->y/2) * state->w + (tile->x/2)]) ^
!(md->newdot->flags & F_DOT_BLACK))
return -1;
if (!(picture[(newopp->y/2) * state->w + (newopp->x/2)]) ^
!(md->newdot->flags & F_DOT_BLACK))
return -1;
}
#endif
break;
case MD_MOVE:
/* Move dot associations: anything that was associated
* with the old dot, and its opposite wrt the new dot,
* become associated with the new dot. */
assert(newopp);
debug(("Associating %d,%d and %d,%d with new dot %d,%d.\n",
tile->x, tile->y, newopp->x, newopp->y,
md->newdot->x, md->newdot->y));
add_assoc(state, tile, md->newdot);
add_assoc(state, newopp, md->newdot);
return 1; /* we did something! */
}
return 0;
}
/* For the given dot, first see if we could expand it into all the given
* extra spaces (by checking for empty spaces on the far side), and then
* see if we can move the dot to shift the CoG to include the new spaces.
*/
static int dot_expand_or_move(game_state *state, space *dot,
space **toadd, int nadd)
{
space *tileopp;
int i, ret, nnew, cx, cy;
struct movedot md;
debug(("dot_expand_or_move: %d tiles for dot %d,%d\n",
nadd, dot->x, dot->y));
for (i = 0; i < nadd; i++)
debug(("dot_expand_or_move: dot %d,%d\n",
toadd[i]->x, toadd[i]->y));
assert(dot->flags & F_DOT);
#ifdef STANDALONE_PICTURE_GENERATOR
if (picture) {
/*
* Reject the expansion totally if any of the new tiles are
* the wrong colour.
*/
for (i = 0; i < nadd; i++) {
if (!(picture[(toadd[i]->y/2) * state->w + (toadd[i]->x/2)]) ^
!(dot->flags & F_DOT_BLACK))
return 0;
}
}
#endif
/* First off, could we just expand the current dot's tile to cover
* the space(s) passed in and their opposites? */
for (i = 0; i < nadd; i++) {
tileopp = space_opposite_dot(state, toadd[i], dot);
if (!tileopp) goto noexpand;
if (tileopp->flags & F_TILE_ASSOC) goto noexpand;
#ifdef STANDALONE_PICTURE_GENERATOR
if (picture) {
/*
* The opposite tiles have to be the right colour as well.
*/
if (!(picture[(tileopp->y/2) * state->w + (tileopp->x/2)]) ^
!(dot->flags & F_DOT_BLACK))