-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaxies.c
4484 lines (3931 loc) · 136 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.
*
* Future solver directions:
*
* - Non-local version of the exclave extension? Suppose you have an
* exclave with multiple potential paths back home, but all of them
* go through the same tile somewhere in the middle of the path.
* Then _that_ critical square can be assigned to the home dot,
* even if we don't yet know the details of the path from it to
* either existing region.
*
* - Permit non-simply-connected puzzle instances in sub-Unreasonable
* mode? Even the simplest case 5x3:ubb is graded Unreasonable at
* present, because we have no solution technique short of
* recursion that can handle it.
*
* The reasoning a human uses for that puzzle is to observe that
* the centre left square has to connect to the centre dot, so it
* must have _some_ path back there. It could go round either side
* of the dot in the way. But _whichever_ way it goes, that rules
* out the left dot extending to the squares above and below it,
* because if it did that, that would block _both_ routes back to
* the centre.
*
* But the exclave-extending deduction we have at present is only
* capable of extending an exclave with _one_ liberty. This has
* two, so the only technique we have available is to try them one
* by one via recursion.
*
* My vague plan to fix this would be to re-run the exclave
* extension on a per-dot basis (probably after working out a
* non-local version as described above): instead of trying to find
* all exclaves at once, try it for one exclave at a time, or
* perhaps all exclaves relating to a particular home dot H. The
* point of this is that then you could spot pairs of squares with
* _two_ possible dots, one of which is H, and which are opposite
* to each other with respect to their other dot D (such as the
* squares above/below the left dot in this example). And then you
* merge those into one vertex of the connectivity graph, on the
* grounds that they're either both H or both D - and _then_ you
* have an exclave with only one path back home, and can make
* progress.
*
* 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 <limits.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include "puzzles.h"
#ifdef DEBUGGING
#define solvep debug
#elif defined STANDALONE_SOLVER
static bool solver_show_working;
#define solvep(x) do { if (solver_show_working) { printf x; } } while(0)
#else
#define solvep(x) ((void)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;
bool 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. */
};
static bool check_complete(const game_state *state, DSF *dsf, int *colours);
static int solver_state_inner(game_state *state, int maxdiff, int depth);
static int solver_state(game_state *state, int maxdiff);
static int solver_obvious(game_state *state);
static int solver_obvious_dot(game_state *state, space *dot);
static space *space_opposite_dot(const game_state *state, const space *sp,
const space *dot);
static space *tile_opposite(const game_state *state, const space *sp);
static game_state *execute_move(const game_state *state, const char *move);
/* ----------------------------------------------------------
* 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 },
{ 10, 10, DIFF_UNREASONABLE },
{ 15, 15, DIFF_NORMAL },
{ 15, 15, DIFF_UNREASONABLE },
};
static bool 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, bool 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].u.string.sval = dupstr(buf);
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Difficulty";
ret[2].type = C_CHOICES;
ret[2].u.choices.choicenames = DIFFCONFIG;
ret[2].u.choices.selected = params->diff;
ret[3].name = NULL;
ret[3].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].u.string.sval);
ret->h = atoi(cfg[1].u.string.sval);
ret->diff = cfg[2].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->w < 3 || params->h < 3)
return "Width and height must both be at least 3";
if (params->w > INT_MAX / 2 || params->h > INT_MAX / 2 ||
params->w > (INT_MAX - params->w*2 - params->h*2 - 1) / 4 / params->h)
return "Width times height must not be unreasonably large";
/*
* 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 remove_assoc_with_opposite(game_state *state, space *tile) {
space *opposite;
if (!(tile->flags & F_TILE_ASSOC)) {
return;
}
opposite = tile_opposite(state, tile);
remove_assoc(state, tile);
if (opposite != NULL && opposite != tile) {
remove_assoc(state, opposite);
}
}
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 bool ok_to_add_assoc_with_opposite_internal(
const game_state *state, space *tile, space *opposite)
{
int *colors;
bool toret;
if (tile->type != s_tile)
return false;
if (tile->flags & F_DOT)
return false;
if (opposite == NULL)
return false;
if (opposite->flags & F_DOT)
return false;
toret = true;
colors = snewn(state->w * state->h, int);
check_complete(state, NULL, colors);
if (colors[(tile->y - 1)/2 * state->w + (tile->x - 1)/2])
toret = false;
if (colors[(opposite->y - 1)/2 * state->w + (opposite->x - 1)/2])
toret = false;
sfree(colors);
return toret;
}
#ifndef EDITOR
static bool ok_to_add_assoc_with_opposite(
const game_state *state, space *tile, space *dot)
{
space *opposite = space_opposite_dot(state, tile, dot);
return ok_to_add_assoc_with_opposite_internal(state, tile, opposite);
}
#endif
static void add_assoc_with_opposite(game_state *state, space *tile, space *dot) {
space *opposite = space_opposite_dot(state, tile, dot);
if(opposite && ok_to_add_assoc_with_opposite_internal(
state, tile, opposite))
{
remove_assoc_with_opposite(state, tile);
add_assoc(state, tile, dot);
remove_assoc_with_opposite(state, opposite);
add_assoc(state, opposite, dot);
}
}
#ifndef EDITOR
static space *sp2dot(const game_state *state, int x, int y)
{
space *sp = &SPACE(state, x, y);
if (!(sp->flags & F_TILE_ASSOC)) return NULL;
return &SPACE(state, sp->dotx, sp->doty);
}
#endif
#define IS_VERTICAL_EDGE(x) ((x % 2) == 0)
static bool game_can_format_as_text_now(const game_params *params)
{
return true;
}
static char *encode_game(const game_state *state);
static char *game_text_format(const game_state *state)
{
#ifdef EDITOR
game_params par;
char *params, *desc, *ret;
par.w = state->w;
par.h = state->h;
par.diff = DIFF_MAX; /* shouldn't be used */
params = encode_params(&par, false);
desc = encode_game(state);
ret = snewn(strlen(params) + strlen(desc) + 2, char);
sprintf(ret, "%s:%s", params, desc);
sfree(params);
sfree(desc);
return ret;
#else
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;
#endif
}
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, ret;
bool progress = false, impossible = false;
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 = true;
} else if (ret == 1) {
progress = true;
}
sp += 2;
}
}
return impossible ? -1 : progress ? 1 : 0;
}
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)
{
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 space *space_opposite_dot(const game_state *state, const space *sp,
const 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 space *tile_opposite(const game_state *state, const space *sp)
{
space *dot;
assert(sp->flags & F_TILE_ASSOC);
dot = &SPACE(state, sp->dotx, sp->doty);
return space_opposite_dot(state, sp, dot);
}
static bool dotfortile(game_state *state, space *tile, space *dot)
{
space *tile_opp = space_opposite_dot(state, tile, dot);
if (!tile_opp) return false; /* opposite would be off grid */
if (tile_opp->flags & F_TILE_ASSOC &&
(tile_opp->dotx != dot->x || tile_opp->doty != dot->y))
return false; /* opposite already associated with diff. dot */
return true;
}
static void adjacencies(game_state *state, space *sp, space **a1s, 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 bool outline_tile_fordot(game_state *state, space *tile, bool mark)
{
space *tadj[4], *eadj[4];
int i;
bool didsth = false, 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;
if (tadj[i]) {
if (!(tile->flags & F_TILE_ASSOC))
same = !(tadj[i]->flags & F_TILE_ASSOC);
else
same = ((tadj[i]->flags & F_TILE_ASSOC) &&
tile->dotx == tadj[i]->dotx &&
tile->doty == tadj[i]->doty);
} else
same = false;
if (!edge && !same) {
if (mark) eadj[i]->flags |= F_EDGE_SET;
didsth = true;
} else if (edge && same) {
if (mark) eadj[i]->flags &= ~F_EDGE_SET;
didsth = true;
}
}
return didsth;
}
static void tiles_from_edge(game_state *state, space *sp, 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,
bool issolve, int set_cdiff)
{
int movelen = 0, movesize = 256, x, y, len;
char *move = snewn(movesize, char), buf[80];
const char *sep = "";
char achar = issolve ? 'a' : 'A';
space *sps, *spd;
assert(src->sx == dest->sx && src->sy == dest->sy);
if (issolve) {
move[movelen++] = 'S';
sep = ";";
}
#ifdef EDITOR
if (set_cdiff >= 0) {
switch (set_cdiff) {
case DIFF_IMPOSSIBLE:
movelen += sprintf(move+movelen, "%sII", sep);
break;
case DIFF_AMBIGUOUS:
movelen += sprintf(move+movelen, "%sIA", sep);
break;
case DIFF_UNFINISHED:
movelen += sprintf(move+movelen, "%sIU", sep);
break;
default:
movelen += sprintf(move+movelen, "%si%c",
sep, galaxies_diffchars[set_cdiff]);
break;
}
sep = ";";
}
#endif
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 true if a dot here would not be too close to any other dots
* (and would avoid other game furniture). */
static bool dot_is_possible(const game_state *state, space *sp,
bool 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 false; /* colour mismatch */
}
}
#endif
if (!allow_assoc && (adj->flags & F_TILE_ASSOC))
return false;
if (dx != 0 || dy != 0) {
/* Other than our own square, no dots nearby. */
if (adj->flags & (F_DOT))
return false;
}
/* 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 false;
}
}
return true;
}
/* ----------------------------------------------------------
* 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, space);
state->completed = false;
state->used_solve = false;
for (x = 0; x < state->sx; x++) {
for (y = 0; y < state->sy; y++) {
space *sp = &SPACE(state, x, y);
memset(sp, 0, sizeof(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, bool 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(space));
game_update_dots(ret);
ret->me = state->me;
ret->cdiff = state->cdiff;