-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentifier.c
2766 lines (2535 loc) · 88.1 KB
/
identifier.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
/*
Identifier
Author: Anders Holst ([email protected])
This game is similar to Battleship where two players try to sink each others
ships as fast as possible by firing at squares in each others boards.
However, there are two main differences: The "ships" have unknown shapes (in
the default setting), and the goal is to identify their shapes and positions.
I.e. you don't have to explicitly "sink" them by hitting all squares, but as
soon as you know where they are located you are done.
Each shape must be connected, i.e each black square should be possible
to reach from any other black square by moving vertically or
horizontally on black squares. Diagonal touching is not enough to be
connected. Different shapes (of same or different form) placed on the
board may not touch each other, not even diagonally.
Parameters:
There are three game modes:
In Duel mode you are playing against the computer. First you prepare your
board according to the agreed specification, then you start playing, where
in turn you and the computer guess at each others squares. Whenever one of
you know the other's shapes and locations, the full guess is shown. The other
player will still have a chance to continue playing to see how many extra
moves were needed.
In Single mode you will only guess on the computer generated board. A goal
number of guesses is given, that you could try to beat.
In Puzzle mode, which is the trickiest one, you get a board with a set of
(empty) squares uncovered, and your task is to find the only way to place
shapes of the agreed specification into that board. There is no way to get
further clues - you have to stare at the board until you know where they are
all located.
There are also three different fleet types to choose from: In the default
setting, "Unknown", you only know the number of black squares in each shape.
In the "Random" setting, some random shapes of the specified size will be
generated, and those are to be used. Finally, the "Standard fleet" setting
is the most boring, and threwn in just in case there is someone who would
prefer the standard straiht line shapes of Battleships instead of arbitrary
shapes.
You can also specify which symmetry transformation are allowed on the shapes:
rotations, mirroring, both, or none. Default is that all transformations are
allowed.
Finally you can specify the shape configuration, in terms of how many copies
of each shape and of which level, i.e how many black squares they consist of.
For example "2*6,3*4" means 2 copies of a shape of 6 squares and 3 copies of
a shape of 4 squares. Maximum shape size is 12 (for which it takes several
minutes to compile the dictionary of all shapes).
Controls:
Either using the mouse buttons or the arrow keys you can select a square in
any of the two boards. Again pressing the left or right mouse button, or the
space bar, you can toggle a black or white mark on the selected square. For
example, in duel mode it can be used to mark where your ships are on the top
board that the computer will guess on. On the lower board you can use it to
mark the squares you are certain of so far.
To guess on a square in the lower board you use the return key. Also, to
answer a computer guess in the upper board, make sure it is correctly marked
white or black and then press return.
With the numbers 1-6 you can also give the selected square an auxilary color.
0 erases that color. The auxilary color has no meaning in the game, it can be
used any way you like, for your memory as with "pencil marks". For example, if
you don't trust that the computer will not cheat by looking at your white and
black marks, you can use the colors instead in some obscure way that you decide.
Make sure you remember what the colors mean, because if you answer wrong in
such a way that the result will be inconsistent, you have lost. (Just to make
it perfectly clear, you can safely use the white and black marks, because the
computer will *not* cheat. It does not need to, since it uses an efficient
Bayesian optimization algorithm, maximizing the expected entropy decrease at
each step, which is difficult for anyone to beat.)
Gameplay:
Duel mode is most complex, so it s described here. Much in the other modes
will then be similar. In Duel mode (but not the others) you have to prepare
your own board, for the computer to guess. Prepare it in the upper boars,
either using white and black marks (black for the shape squares and white
outside), or the colors 1 - 6 or arbitrary meaning, or on a paper on the
side if you don't trust the program. At the bottom of the window, the shape
configuration to use is shown. For Unknown fleet type, it only says how
many copies of shapes of how many black squares, and you design your shapes
yourself, and the computer designs its shapes of the same sizes. For Random
fleet type, some random shapes are drawn and displayed at the bottom. You
and the computer will use these same shapes. In Standard fleet type, only
straight line shapes will be used (but you can affect the counts and sizes
of them by setting the parameters). Depending on the symmetry settings the
shapes may be rotated or mirrored.
When you are done preparing, press Start. First it is your turn. Select a
square in the lower board on which you want to ask, and press Return. It
will then be revealed as black (hit) or white (miss), and your turn count
increases.
Then it is the computer's turn. One square in the upper board will be
highlighted. ake sure it has the right black or white mark on it (with the
left or right mouse button, or the space bar), and then press Return. The
computer's turn count increases.
So it continues until either you or the computer is sure of the solution.
When you are certain what the computer's board look like, make sure that
all squares where you think shapes are located are marked with black.
Whites are not mandatory to mark, thus unmarked squares will be treated as
white. When the right number of black squares are marked, you will be able
to press Guess, and the true answer will be revealed. If you made any
mistake, the difference between your guess and the true answer will be
highlighted with red.
When the computer is certain, it reveals the whole solution. It will not
make a mistake, so if you don't agree, the fault is on your side. You will
have a chance to continue guessing to see how far behind you were. Likewise,
if you are done before the computer, the computer will continue to ask and
if you like you can continue to answer to see how far behind it was.
Play in Single mode is similar, but you need not prepare a board yourself,
but only guess on the computer generated board. There is a goal turn count
provided (which is how long it would have taken the computer to solve it,
plus a handicap of two, since the computer has a very efficient optimizer
which is hard to beat). As soon as you are sure of the solution, make sure
all the black squares are marked, and press Guess.
Puzzle mode is the most tricky, but at the same time the least controls.
You get a board with some white squares revealed, and have to figure out
the only way to fit in the shapes of the given specification on the board.
You can not ask for specific squares. You have to figure it all out, mark
all the shape squares with black, and press Guess to see if you were right.
Good luck!
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
/* ---------- Game generation ---------- */
#define ID_ON 2
#define ID_OFF 1
#define ID_UNKNOWN 0
#define ID_BLOCKED 3
#define ID_ROT0 0
#define ID_ROT0_MIR 1
#define ID_ROT90 2
#define ID_ROT90_MIR 3
#define ID_ROT180 4
#define ID_ROT180_MIR 5
#define ID_ROT270 6
#define ID_ROT270_MIR 7
#define ID_REFL_ORIG 1
#define ID_REFL_ROT 85
#define ID_REFL_MIR 153
#define ID_REFL_ALL 255
#define ID_REFL_SWAP 102
#define COMPLEXITY_LIMIT 64
#define MAKE_BOARD_TRIALS 200
#define PI_STRING "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89452821E638D01377BE5466CF34E90C6CC0AC29B7C97C50DD3F84D5B5B54709179216D5D98979FB1BD1310BA698DFB5AC2FFD72DBD01ADFB7B8E1AFED6A267E96BA7C9045F12C7F9924A19947B3916CF70801F2E2858EFC16636920D871574E69"
static int imax(int a, int b) { return (a>b ? a : b); };
static int imin(int a, int b) { return (a<b ? a : b); };
typedef struct Shape {
int width, height;
char* pix;
} Shape;
int ShapeWidth(Shape* sh, int reflbit)
{
return (ID_REFL_SWAP & reflbit ? sh->height : sh->width);
}
int ShapeHeight(Shape* sh, int reflbit)
{
return (ID_REFL_SWAP & reflbit ? sh->width : sh->height);
}
char ShapePix(Shape* sh, int x, int y, int reflbit)
{
if (ID_REFL_SWAP & reflbit)
return sh->pix[(60&reflbit ? sh->height-x : x+1)*(sh->width+2)+(240&reflbit ? sh->width-y : y+1)];
else
return sh->pix[(60&reflbit ? sh->width-x : x+1)+(240&reflbit ? sh->height-y : y+1)*(sh->width+2)];
};
void SetShapePix(Shape* sh, int x, int y, int reflbit, char val)
{
if (ID_REFL_SWAP & reflbit)
sh->pix[(60&reflbit ? sh->height-x : x+1)*(sh->width+2)+(240&reflbit ? sh->width-y : y+1)] = val;
else
sh->pix[(60&reflbit ? sh->width-x : x+1)+(240&reflbit ? sh->height-y : y+1)*(sh->width+2)] = val;
};
typedef struct ShapeConfig {
int numcomp;
int symmetry;
int maxlev;
int* lev;
int* mult;
int* id;
} ShapeConfig;
typedef struct ShapeAnswer {
int refcount;
int* shapeind;
int** shapex;
int** shapey;
int** shapeb;
} ShapeAnswer;
typedef struct ShapeDict {
int maxlevel;
int toplevel;
int totnum;
int reflmask;
int* len;
Shape*** shapes;
} ShapeDict;
int ShapeDictNum(ShapeDict* sd, int lev)
{
return sd->len[lev-1];
}
Shape* ShapeDictGet(ShapeDict* sd, int lev, int ind)
{
return sd->shapes[lev-1][ind];
}
typedef struct DictStatistics {
ShapeDict* dict;
ShapeConfig* conf;
ShapeAnswer* answer;
int num; /* Total number of shapes in all comp levels */
int bsize;
Shape* board;
int* lenposs;
int* numposs;
int* smask;
int* np1;
int* np2;
char** poss;
double* entr;
} DictStatistics;
typedef struct DictHyperIndex {
DictStatistics* stat;
Shape* origboard;
Shape* shape;
int comp;
int mult;
int shind0, nshape;
int* orignumposs;
int nposs;
int shind;
int* pos;
int* xvec;
int* yvec;
int* bvec;
} DictHyperIndex;
Shape* make_unit_shape()
{
Shape* shape = snew(Shape);
shape->width = shape->height = 1;
shape->pix = snewn(9, char);
for (int i=0; i<9; i++)
shape->pix[i] = (i==4 ? ID_ON : ID_OFF);
return shape;
}
Shape* make_empty_board(int w, int h)
{
Shape* shape = snew(Shape);
shape->width = w;
shape->height = h;
shape->pix = snewn((w+2)*(h+2), char);
for (int i=0; i<(w+2)*(h+2); i++)
shape->pix[i] = ID_UNKNOWN;
return shape;
}
Shape* make_incr_shape(Shape* initial, int addx, int addy)
{
Shape* shape = snew(Shape);
int npix;
if (addx == -1 || addx == initial->width)
shape->width = initial->width+1, shape->height = initial->height;
else if (addy == -1 || addy == initial->height)
shape->width = initial->width, shape->height = initial->height+1;
else
shape->width = initial->width, shape->height = initial->height;
npix = (shape->width+2) * (shape->height+2);
shape->pix = snewn(npix, char);
for (int i=0, j=0; j<npix; j++)
if (addx == -1 ? j%(shape->width+2)==0 :
addx == initial->width ? j%(shape->width+2)==(shape->width+1) :
addy == -1 ? j<(shape->width+2) :
addy == initial->height ? j>=npix-(shape->width+2) :
0)
shape->pix[j] = ID_UNKNOWN;
else
shape->pix[j] = initial->pix[i++];
if (addx == -1) addx = 0;
if (addy == -1) addy = 0;
for (int y=-1; y<=1; y++)
for (int x=-1; x<=1; x++) {
int ind = (addy + 1 + y)*(shape->width+2) + addx + x + 1;
if (shape->pix[ind] != ID_ON)
shape->pix[ind] = ID_OFF;
}
shape->pix[(addy+1)*(shape->width+2) + (addx+1)] = ID_ON;
return shape;
}
int can_incr_shape(Shape* initial, int addx, int addy)
{
return (initial->pix[(addy+1)*(initial->width+2) + (addx+1)] != ID_ON &&
((addx < initial->width && initial->pix[(addy+1)*(initial->width+2) + (addx+2)] == ID_ON) ||
(addy < initial->height && initial->pix[(addy+2)*(initial->width+2) + (addx+1)] == ID_ON) ||
(addx != -1 && initial->pix[(addy+1)*(initial->width+2) + (addx)] == ID_ON) ||
(addy != -1 && initial->pix[(addy)*(initial->width+2) + (addx+1)] == ID_ON)));
}
void free_shape(Shape* shape)
{
sfree(shape->pix);
sfree(shape);
}
void reset_board(Shape* board, char val)
{
for (int y=0; y<board->height; y++)
for (int x=0; x<board->width; x++)
SetShapePix(board, x, y, 1, val);
}
void copy_board(Shape* shape0, Shape* shape)
{
if (shape0->width == shape->width && shape0->height == shape->height) {
int sz = (shape0->width+2)*(shape0->height+2);
for (int i=0; i<sz; i++)
shape->pix[i] = shape0->pix[i];
}
}
int count_board(Shape* board, char val)
{
int count = 0;
for (int y=0; y<board->height; y++)
for (int x=0; x<board->width; x++)
if (ShapePix(board, x, y, 1) == val)
count++;
return count;
}
Shape* copy_shape(Shape* shape)
{
Shape* ret = snew(Shape);
int n = (shape->width + 2) * (shape->height + 2);
ret->width = shape->width;
ret->height = shape->height;
ret->pix = snewn(n, char);
for (int i=0; i<n; i++)
ret->pix[i] = shape->pix[i];
return ret;
}
int same_shape(Shape* sh1, Shape* sh2, int reflmask)
{
int smask = 0;
if (sh1->width == sh2->width && sh1->height == sh2->height)
smask |= ID_REFL_MIR;
if (sh1->width == sh2->height && sh1->height == sh2->width)
smask |= ID_REFL_SWAP;
smask &= reflmask;
if (!smask)
return 0;
for (int b=1, i=0; i<8; i++, b<<=1)
if (smask&b) {
for (int x=0; x<sh1->width; x++)
for (int y=0; y<sh1->height; y++)
if (ShapePix(sh1, x, y, 1) != ShapePix(sh2, x, y, b))
goto donext;
return 1;
donext:
;
}
return 0;
}
ShapeDict* init_shape_dictionary(int maxlev, int reflmask)
{
ShapeDict* dict = snew(ShapeDict);
dict->maxlevel = maxlev;
dict->toplevel = 1;
dict->reflmask = reflmask;
dict->len = snewn(maxlev, int);
dict->shapes = snewn(maxlev, Shape**);
dict->shapes[0] = snewn(1, Shape*);
dict->shapes[0][0] = make_unit_shape();
dict->len[0] = 1;
dict->totnum = 1;
return dict;
}
void extend_shape_dictionary(ShapeDict* dict, int lev)
{
int worksize = 1000;
int nr, newsize, i, j, k, x, y;
Shape** work = snewn(worksize, Shape*);
Shape** newwork;
Shape* shape;
for (i=dict->toplevel; i<lev; i++) {
nr = 0;
for (j=0; j<dict->len[i-1]; j++)
for (x=-1; x<=dict->shapes[i-1][j]->width; x++)
for (y=-1; y<=dict->shapes[i-1][j]->height; y++)
if (can_incr_shape(dict->shapes[i-1][j], x, y)) {
shape = make_incr_shape(dict->shapes[i-1][j], x, y);
for (k=0; k<nr; k++)
if (same_shape(work[k], shape, dict->reflmask))
break;
if (k<nr) {
free_shape(shape);
continue;
} else {
if (nr == worksize) {
newsize = 2*worksize;
newwork = snewn(newsize, Shape*);
for (k=0; k<worksize; k++)
newwork[k] = work[k];
sfree(work);
work = newwork;
worksize = newsize;
}
work[nr++] = shape;
}
}
dict->shapes[i] = snewn(nr, Shape*);
for (k=0; k<nr; k++)
dict->shapes[i][k] = work[k];
dict->len[i] = nr;
dict->totnum += nr;
}
dict->toplevel = lev;
sfree(work);
}
void free_shape_dictionary(ShapeDict* dict)
{
int i, j;
for (i=0; i<dict->maxlevel; i++) {
for (j=0; j<dict->len[i]; j++)
free_shape(dict->shapes[i][j]);
sfree(dict->shapes[i]);
}
sfree(dict->shapes);
sfree(dict->len);
sfree(dict);
}
/*
Symmetry relations used below
1 3 5 7
0 2 4 6 10011001 =153 10101010 =85 11111111 =255
-: B C D A 10001000 =17 10101010 =85 10101010 =85, 11001100
A B C D
|: D A B C 10001000 =17 10101010 =85 10101010 =85, 11001100
A B C D
+: B A B A 10000000 =1 10100000 =5 10100000 =5, 11000000
A B A B
\: C D A B 10011001 =153 10101010 =85 10011001 =153, 10101010 =85, 11110000
A B C D
/: A B C D 10011001 =153 10101010 =85 10011001 =153, 10101010 =85, 01010101
A B C D
X: A B A B 10010000 =9 10100000 =5 10010000 =9, 10100000 =5
A B A B
O: C D C D 10010000 =9 10100000 =5 11110000 =15
A B A B
C: B B B B 10010000 =9 10000000 =1 11000000 =3
A A A A
*/
void calc_needed_positions(Shape* shape, Shape* board, int reflmask, int* smask, int* nr1, int* nr2, int* np1, int* np2)
{
/* Find the symmetry class of the shape */
int s1=0, s2=0;
if (reflmask==ID_REFL_ORIG)
*smask = 1, *nr1 = 1, *nr2 = 0;
else {
if (same_shape(shape, shape, 128)) s1++;
if (same_shape(shape, shape, 8)) s1++;
if (same_shape(shape, shape, 32)) s2++;
if (same_shape(shape, shape, 2)) s2++;
if (s1+s2) {
if (s1+s2 == 4)
*smask = 1, *nr1 = 1, *nr2 = 0;
else if (s1) {
if (s1 == 2)
if (reflmask == ID_REFL_MIR)
*smask = 1, *nr1 = 1, *nr2 = 0;
else
*smask = 5, *nr1 = 1, *nr2 = 1;
else
if (reflmask == ID_REFL_MIR)
*smask = 17, *nr1 = 2, *nr2 = 0;
else
*smask = 85, *nr1 = 2, *nr2 = 2;
} else {
if (s2 == 2)
if (reflmask == ID_REFL_ROT)
*smask = 5, *nr1 = 1, *nr2 = 1;
else
*smask = 9, *nr1 = 2, *nr2 = 0;
else
if (reflmask == ID_REFL_ROT)
*smask = 85, *nr1 = 2, *nr2 = 2;
else
*smask = 153, *nr1 = 4, *nr2 = 0;
}
} else if (same_shape(shape, shape, 4)) {
if (reflmask == ID_REFL_MIR)
*smask = 9, *nr1 = 2, *nr2 = 0;
else if (reflmask == ID_REFL_ROT)
*smask = 1, *nr1 = 1, *nr2 = 0;
else
*smask = 9, *nr1 = 2, *nr2 = 0;
} else if (same_shape(shape, shape, 16)) {
if (reflmask == ID_REFL_MIR)
*smask = 9, *nr1 = 2, *nr2 = 0;
else if (reflmask == ID_REFL_ROT)
*smask = 5, *nr1 = 1, *nr2 = 1;
else
*smask = 15, *nr1 = 2, *nr2 = 2;
} else {
*smask = reflmask;
if (reflmask == ID_REFL_MIR)
*nr1 = 4, *nr2 = 0;
else if (reflmask == ID_REFL_ROT)
*nr1 = 2, *nr2 = 2;
else
*nr1 = 4, *nr2 = 4;
}
}
*np1 = (board->width - shape->width + 1)*(board->height - shape->height + 1);
*np2 = (board->width - shape->height + 1)*(board->height - shape->width + 1);
if (*np1 < 0) *np1 = 0;
if (*np2 < 0) *np2 = 0;
}
void mark_inconsistent(Shape* board, Shape* shape, int smask, int np1, int np2, char* poss)
{
int bpix, spix;
for (int y=0; y<board->height; y++)
for (int x=0; x<board->width; x++)
if ((bpix = ShapePix(board, x, y, 1)) != ID_UNKNOWN) {
int ir1=0, ir2=0;
for (int b=1, i=0; i<8; i++, b<<=1)
if (smask&b) {
for (int yy = imax(y+ShapeHeight(shape, b)-board->height, -1); yy <= imin(ShapeHeight(shape, b), y); yy++)
for (int xx = imax(x+ShapeWidth(shape, b)-board->width, -1); xx <= imin(ShapeWidth(shape, b), x); xx++)
if ((spix = ShapePix(shape, xx, yy, b)) != ID_UNKNOWN && spix != bpix)
poss[np1*ir1 + np2*ir2 + (y-yy) * (board->width-ShapeWidth(shape, b)+1) + (x-xx)] = 0;
if (ID_REFL_SWAP & b)
ir2++;
else
ir1++;
}
}
}
void accumulate_possibilities(Shape* board, Shape* shape, int smask, int np1, int np2, char* poss, int* bpos, int* bneg)
{
char spix;
for (int y=0; y<board->height; y++)
for (int x=0; x<board->width; x++)
if (ShapePix(board, x,y,1) == ID_UNKNOWN) {
int ir1=0, ir2=0;
for (int b=1, i=0; i<8; i++, b<<=1)
if (smask&b) {
for (int yy = imax(y+ShapeHeight(shape, b)-board->height, -1); yy <= imin(ShapeHeight(shape, b), y); yy++)
for (int xx = imax(x+ShapeWidth(shape, b)-board->width, -1); xx <= imin(ShapeWidth(shape, b), x); xx++)
if ((spix = ShapePix(shape, xx,yy,b)) != ID_UNKNOWN &&
poss[np1*ir1 + np2*ir2 + (y-yy) * (board->width-ShapeWidth(shape, b)+1) + (x-xx)] != 0) {
if (spix == ID_ON)
bpos[y*board->width + x]++;
else
bneg[y*board->width + x]++;
}
if (ID_REFL_SWAP & b)
ir2++;
else
ir1++;
}
}
}
int check_inconsistent(Shape* board, Shape* shape, int bit, int x, int y)
{
char px;
for (int yy=-1; yy<=ShapeHeight(shape, bit); yy++)
for (int xx=-1; xx<=ShapeWidth(shape, bit); xx++)
if ((px = ShapePix(shape, xx, yy, bit)) != ID_UNKNOWN)
if (ShapePix(board, xx+x, yy+y, 1) != px && ShapePix(board, xx+x, yy+y, 1) != ID_UNKNOWN)
return 1;
return 0;
}
void copy_to_board(Shape* board, Shape* shape, int bit, int x, int y, char val)
{
char px;
for (int yy=-1; yy<=ShapeHeight(shape, bit); yy++)
for (int xx=-1; xx<=ShapeWidth(shape, bit); xx++)
if ((px = ShapePix(shape, xx, yy, bit)) != ID_UNKNOWN && xx+x >= 0 && xx+x < ShapeWidth(board, 1) && yy+y >= 0 && yy+y < ShapeHeight(board, 1))
SetShapePix(board, xx+x, yy+y, 1, (px==ID_ON ? val : px));
}
int add_random_board_shape(Shape* board, Shape* shape, int reflmask, int num, random_state *rs)
{
int smask, nr1, nr2, np1, np2;
calc_needed_positions(shape, board, reflmask, &smask, &nr1, &nr2, &np1, &np2);
int b, i, ww;
int possnum, posslen = nr1*np1+nr2*np2;
char* poss = snewn(posslen, char);
int pick;
for (int i=0; i<posslen; i++) poss[i] = 1;
while (num--) {
mark_inconsistent(board, shape, smask, np1, np2, poss);
for (possnum=0, i=0; i<posslen; i++)
if (poss[i]) possnum++;
if (possnum == 0) {
sfree(poss);
return 0;
}
pick = random_upto(rs, possnum) + 1;
for (i=0; i<posslen; i++)
if (poss[i]) {
pick--;
if (!pick) break;
}
pick=i;
for (b=1, i=0; i<8; i++, b<<=1)
if (smask & b) {
if (pick >= (ID_REFL_SWAP & b ? np2 : np1))
pick -= (ID_REFL_SWAP & b ? np2 : np1);
else
break;
}
ww = (board->width - ShapeWidth(shape, b) + 1);
copy_to_board(board, shape, b, pick%ww, pick/ww, ID_BLOCKED);
}
sfree(poss);
return 1;
}
Shape* make_random_board(ShapeDict* dict, ShapeConfig* conf, int w, int h, int storeshapes, random_state *rs)
{
int sameshape[conf->numcomp];
int numsame = 0;
int ind;
Shape* shape;
Shape* board = make_empty_board(w, h);
for (int i=0; i<conf->numcomp; i++) {
if (conf->id[i] > -1) {
ind = conf->id[i];
sameshape[0] = ind;
numsame = 0;
} else if (i>0 && conf->lev[i] == conf->lev[i-1]) {
/* Prevent the same shape in different components */
int j;
numsame++;
if (ShapeDictNum(dict, conf->lev[i]) <= numsame) {
free_shape(board);
return 0;
}
ind = random_upto(rs, ShapeDictNum(dict, conf->lev[i])-numsame);
for (j=0; j<numsame; j++)
if (ind>=sameshape[j])
ind++;
for (j=numsame-1; j>=0 && sameshape[j]>ind; j--)
sameshape[j+1] = sameshape[j];
sameshape[j+1] = ind;
} else {
ind = random_upto(rs, ShapeDictNum(dict, conf->lev[i]));
sameshape[0] = ind;
numsame = 0;
}
if (storeshapes)
conf->id[i] = ind;
shape = ShapeDictGet(dict, conf->lev[i], ind);
if (!add_random_board_shape(board, shape, dict->reflmask, conf->mult[i], rs)) {
free_shape(board);
return 0;
}
}
for (int i=0; i<(board->width+2)*(board->height+2); i++)
if (board->pix[i] == ID_BLOCKED)
board->pix[i] = ID_ON;
else
board->pix[i] = ID_OFF;
return board;
}
/*
Shape* try_make_random_board(ShapeDict* dict, ShapeConfig* conf, int w, int h, int maxiter, random_state *rs)
{
Shape* board;
int iter = 0;
while (!(board = make_random_board(dict, conf, w, h, rs)) && iter++ < maxiter);
return board;
}
*/
void print_shape(Shape* shape, int reflbit)
{
for (int y=0; y<ShapeHeight(shape, reflbit); y++) {
for (int x=0; x<ShapeWidth(shape, reflbit); x++)
printf(ShapePix(shape, x,y,reflbit) == ID_ON ? "# " : ShapePix(shape, x,y,reflbit) == ID_BLOCKED ? "X " : " ");
printf("\n");
}
}
void print_dictionary(ShapeDict* dict, int lev)
{
for (int k=0; k<dict->len[lev-1]; k++) {
printf("\n%d:\n", k);
print_shape(dict->shapes[lev-1][k], 1);
}
}
ShapeAnswer* init_shape_answer(ShapeConfig* conf)
{
int i;
ShapeAnswer* ans = snew(ShapeAnswer);
ans->refcount = 1;
ans->shapeind = snewn(conf->numcomp, int);
ans->shapex = snewn(conf->numcomp, int*);
ans->shapey = snewn(conf->numcomp, int*);
ans->shapeb = snewn(conf->numcomp, int*);
for (i=0; i<conf->numcomp; i++) {
ans->shapeind[i] = -1;
ans->shapex[i] = snewn(conf->mult[i], int);
ans->shapey[i] = snewn(conf->mult[i], int);
ans->shapeb[i] = snewn(conf->mult[i], int);
}
return ans;
}
void free_shape_answer(ShapeAnswer* ans, int numcomp)
{
int i;
for (i=0; i<numcomp; i++) {
sfree(ans->shapex[i]);
sfree(ans->shapey[i]);
sfree(ans->shapeb[i]);
}
sfree(ans->shapex);
sfree(ans->shapey);
sfree(ans->shapeb);
sfree(ans->shapeind);
sfree(ans);
}
void dict_statistics_constrain_shapes(DictStatistics* stat)
{
for (int k=0, j=0; k<stat->conf->numcomp; j+=ShapeDictNum(stat->dict, stat->conf->lev[k]), k++)
if (stat->conf->id[k] != -1) {
int jk, n = ShapeDictNum(stat->dict, stat->conf->lev[k]);
for (jk=0; jk<n; jk++)
if (jk != stat->conf->id[k])
stat->numposs[j+jk] = 0;
jk = stat->conf->id[k];
if (jk < n)
for (int kk=k+1; kk<stat->conf->numcomp && stat->conf->lev[k] == stat->conf->lev[kk]; kk++)
stat->numposs[j + n*(kk-k) + jk] = 0;
}
}
void dict_statistics_break_symmetry(DictStatistics* stat)
{
/* This takes care of breaking symmetry if two components have same lev and mult */
for (int k=0, j=0; k<stat->conf->numcomp-1; j+=ShapeDictNum(stat->dict, stat->conf->lev[k]), k++)
if (stat->conf->lev[k] == stat->conf->lev[k+1] &&
stat->conf->id[k] == -1 &&
stat->conf->mult[k] == stat->conf->mult[k+1]) {
int jk, n = ShapeDictNum(stat->dict, stat->conf->lev[k]);
for (jk=0; jk<n && !stat->numposs[j+jk]; jk++)
stat->numposs[j+n+jk] = 0;
if (jk<n) stat->numposs[j+n+jk] = 0;
}
for (int k=stat->conf->numcomp-1, j=stat->num-ShapeDictNum(stat->dict, stat->conf->lev[k]); k>0; k--, j-=ShapeDictNum(stat->dict, stat->conf->lev[k]))
if (stat->conf->lev[k] == stat->conf->lev[k-1] &&
stat->conf->id[k-1] == -1 &&
stat->conf->mult[k] == stat->conf->mult[k-1]) {
int jk, n = ShapeDictNum(stat->dict, stat->conf->lev[k]);
for (jk=n-1; jk>=0 && !stat->numposs[j+jk]; jk--)
stat->numposs[j-n+jk] = 0;
if (jk>=0) stat->numposs[j-n+jk] = 0;
}
}
DictStatistics* init_dict_statistics(ShapeDict* dict, ShapeConfig* conf, int w, int h)
{
int nr1, nr2, np1, np2, tot;
DictStatistics* stat = snew(DictStatistics);
stat->dict = dict;
stat->conf = conf;
stat->answer = init_shape_answer(conf);
stat->num = 0;
for (int i=0; i<conf->numcomp; i++)
stat->num += ShapeDictNum(dict, conf->lev[i]);
stat->bsize = w * h;
stat->board = make_empty_board(w, h);
stat->lenposs = snewn(stat->num, int);
stat->numposs = snewn(stat->num, int);
stat->smask = snewn(stat->num, int);
stat->np1 = snewn(stat->num, int);
stat->np2 = snewn(stat->num, int);
stat->poss = snewn(stat->num, char*);
for (int j=0, jk=0, k=0; j<stat->num; j++, jk++) {
if (jk==ShapeDictNum(dict, conf->lev[k]))
jk = 0, k++;
calc_needed_positions(ShapeDictGet(dict, conf->lev[k], jk), stat->board, dict->reflmask, &stat->smask[j], &nr1, &nr2, &np1, &np2);
tot = nr1*np1 + nr2*np2;
stat->lenposs[j] = stat->numposs[j] = tot;
stat->np1[j] = np1;
stat->np2[j] = np2;
stat->poss[j] = snewn(tot, char);
for (int i=0; i<tot; i++) stat->poss[j][i] = 1;
}
dict_statistics_constrain_shapes(stat);
stat->entr = snewn(stat->bsize, double);
for (int i=0; i<stat->bsize; i++) stat->entr[i] = 0.0;
return stat;
}
void free_dict_statistics(DictStatistics* stat)
{
stat->answer->refcount--;
if (!stat->answer->refcount)
free_shape_answer(stat->answer, stat->conf->numcomp);
for (int i=0; i<stat->num; i++) {
sfree(stat->poss[i]);
}
sfree(stat->lenposs);
sfree(stat->numposs);
sfree(stat->poss);
sfree(stat->smask);
sfree(stat->np1);
sfree(stat->np2);
sfree(stat->entr);
free_shape(stat->board);
sfree(stat);
}
DictStatistics* copy_dict_statistics(DictStatistics* stat0)
{
int nr1, nr2, np1, np2, tot;
DictStatistics* stat = snew(DictStatistics);
stat->dict = stat0->dict;
stat->conf = stat0->conf;
stat->answer = stat0->answer;
stat->answer->refcount++;
stat->num = stat0->num;
stat->bsize = stat0->bsize;
stat->board = copy_shape(stat0->board);
stat->lenposs = snewn(stat->num, int);
stat->numposs = snewn(stat->num, int);
stat->smask = snewn(stat->num, int);
stat->np1 = snewn(stat->num, int);
stat->np2 = snewn(stat->num, int);
stat->poss = snewn(stat->num, char*);
for (int j=0, jk=0, k=0; j<stat->num; j++, jk++) {
if (jk==ShapeDictNum(stat->dict, stat->conf->lev[k]))
jk = 0, k++;
calc_needed_positions(ShapeDictGet(stat->dict, stat->conf->lev[k], jk), stat->board, stat->dict->reflmask, &stat->smask[j], &nr1, &nr2, &np1, &np2);
tot = nr1*np1 + nr2*np2;
stat->lenposs[j] = stat0->lenposs[j];
stat->numposs[j] = stat0->numposs[j];
stat->np1[j] = stat0->np1[j];
stat->np2[j] = stat0->np2[j];
stat->poss[j] = snewn(tot, char);
for (int i=0; i<tot; i++) stat->poss[j][i] = stat0->poss[j][i];
}
stat->entr = snewn(stat->bsize, double);
for (int i=0; i<stat->bsize; i++) stat->entr[i] = stat0->entr[i];
return stat;
}
void dict_statistics_update_poss(DictStatistics* stat, int x, int y, char val)
{
int spix;
SetShapePix(stat->board, x, y, 1, val);
for (int j=0, jk=0, k=0; j<stat->num; j++, jk++) {
if (jk==ShapeDictNum(stat->dict, stat->conf->lev[k]))
jk = 0, k++;
if (stat->numposs[j]) {
Shape* shape = ShapeDictGet(stat->dict, stat->conf->lev[k], jk);
int ir1=0, ir2=0;
for (int b=1, i=0; i<8; i++, b<<=1)
if (stat->smask[j]&b) {
for (int yy = imax(y+ShapeHeight(shape, b)-stat->board->height, -1); yy <= imin(ShapeHeight(shape, b), y); yy++)
for (int xx = imax(x+ShapeWidth(shape, b)-stat->board->width, -1); xx <= imin(ShapeWidth(shape, b), x); xx++)
if ((spix = ShapePix(shape, xx,yy,b)) != ID_UNKNOWN && spix != val) {
int ind = stat->np1[j]*ir1 + stat->np2[j]*ir2 + (y-yy) * (stat->board->width-ShapeWidth(shape, b)+1) + (x-xx);
if (stat->poss[j][ind])
stat->poss[j][ind] = 0, stat->numposs[j]--;
}
if (stat->numposs[j] < stat->conf->mult[k])
stat->numposs[j] = 0;
if (ID_REFL_SWAP & b)
ir2++;
else
ir1++;
}
}
}
dict_statistics_break_symmetry(stat);
}
static DictHyperIndex* make_hyper_index(DictStatistics* st, int ci)
{
int i, maxnposs = 0;
DictHyperIndex* dhi = snew(DictHyperIndex);
dhi->stat = st;
dhi->comp = ci;
dhi->shape = 0;
dhi->shind = -1;
dhi->shind0 = 0;
for (i=0; i<dhi->comp; i++)
dhi->shind0 += ShapeDictNum(dhi->stat->dict, dhi->stat->conf->lev[i]);
dhi->nshape = ShapeDictNum(dhi->stat->dict, dhi->stat->conf->lev[dhi->comp]);
dhi->mult = dhi->stat->conf->mult[dhi->comp];
dhi->origboard = copy_shape(dhi->stat->board);
dhi->orignumposs = snewn(dhi->stat->num, int);
for (i=0; i<dhi->stat->num; i++)
dhi->orignumposs[i] = dhi->stat->numposs[i];
dhi->nposs = 0;
dhi->pos = snewn(dhi->mult, int);
for (i=0; i<dhi->nshape; i++)
if (dhi->stat->numposs[dhi->shind0+i] > maxnposs)
maxnposs = dhi->stat->numposs[dhi->shind0+i];
dhi->xvec = snewn(maxnposs, int);
dhi->yvec = snewn(maxnposs, int);
dhi->bvec = snewn(maxnposs, int);
return dhi;
}
static void free_hyper_index(DictHyperIndex* dhi)
{
free_shape(dhi->origboard);
sfree(dhi->orignumposs);
sfree(dhi->pos);
sfree(dhi->xvec);
sfree(dhi->yvec);
sfree(dhi->bvec);
sfree(dhi);
}
static int next_hyper_index(DictHyperIndex* dhi)
{
int smask, np1, np2, ii;
while (1) {
if (dhi->shind != -1)
for (ii=dhi->mult-1; ii>=0 && dhi->pos[ii]==dhi->nposs+ii-dhi->mult; ii--);
else
ii = -1;
if (ii >= 0) {
dhi->pos[ii]++;
for (ii++; ii<dhi->mult; ii++)
dhi->pos[ii] = dhi->pos[ii-1]+1;