-
Notifications
You must be signed in to change notification settings - Fork 24
/
flip.c
1314 lines (1151 loc) · 35.2 KB
/
flip.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
/*
* flip.c: Puzzle involving lighting up all the squares on a grid,
* where each click toggles an overlapping set of lights.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "tree234.h"
enum {
COL_BACKGROUND,
COL_WRONG,
COL_RIGHT,
COL_GRID,
COL_DIAG,
COL_HINT,
COL_CURSOR,
NCOLOURS
};
#define PREFERRED_TILE_SIZE 48
#define TILE_SIZE (ds->tilesize)
#define BORDER (TILE_SIZE / 2)
#define COORD(x) ( (x) * TILE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
#define ANIM_TIME 0.25F
#define FLASH_FRAME 0.07F
/*
* Possible ways to decide which lights are toggled by each click.
* Essentially, each of these describes a means of inventing a
* matrix over GF(2).
*/
enum {
CROSSES, RANDOM
};
struct game_params {
int w, h;
int matrix_type;
};
/*
* This structure is shared between all the game_states describing
* a particular game, so it's reference-counted.
*/
struct matrix {
int refcount;
unsigned char *matrix; /* array of (w*h) by (w*h) */
};
struct game_state {
int w, h;
int moves, completed, cheated, hints_active;
unsigned char *grid; /* array of w*h */
struct matrix *matrix;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 5;
ret->matrix_type = CROSSES;
return ret;
}
static const struct game_params flip_presets[] = {
{3, 3, CROSSES},
{4, 4, CROSSES},
{5, 5, CROSSES},
{3, 3, RANDOM},
{4, 4, RANDOM},
{5, 5, RANDOM},
};
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char str[80];
if (i < 0 || i >= lenof(flip_presets))
return FALSE;
ret = snew(game_params);
*ret = flip_presets[i];
sprintf(str, "%dx%d %s", ret->w, ret->h,
ret->matrix_type == CROSSES ? "Crosses" : "Random");
*name = dupstr(str);
*params = ret;
return TRUE;
}
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 *ret, char const *string)
{
ret->w = ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
ret->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'r') {
string++;
ret->matrix_type = RANDOM;
} else if (*string == 'c') {
string++;
ret->matrix_type = CROSSES;
}
}
static char *encode_params(const game_params *params, int full)
{
char data[256];
sprintf(data, "%dx%d%s", params->w, params->h,
!full ? "" : params->matrix_type == CROSSES ? "c" : "r");
return dupstr(data);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret = snewn(4, config_item);
char buf[80];
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 = "Shape type";
ret[2].type = C_CHOICES;
ret[2].sval = ":Crosses:Random";
ret[2].ival = params->matrix_type;
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->matrix_type = cfg[2].ival;
return ret;
}
static char *validate_params(const game_params *params, int full)
{
if (params->w <= 0 || params->h <= 0)
return "Width and height must both be greater than zero";
return NULL;
}
static char *encode_bitmap(unsigned char *bmp, int len)
{
int slen = (len + 3) / 4;
char *ret;
int i;
ret = snewn(slen + 1, char);
for (i = 0; i < slen; i++) {
int j, v;
v = 0;
for (j = 0; j < 4; j++)
if (i*4+j < len && bmp[i*4+j])
v |= 8 >> j;
ret[i] = "0123456789abcdef"[v];
}
ret[slen] = '\0';
return ret;
}
static void decode_bitmap(unsigned char *bmp, int len, const char *hex)
{
int slen = (len + 3) / 4;
int i;
for (i = 0; i < slen; i++) {
int j, v, c = hex[i];
if (c >= '0' && c <= '9')
v = c - '0';
else if (c >= 'A' && c <= 'F')
v = c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
v = c - 'a' + 10;
else
v = 0; /* shouldn't happen */
for (j = 0; j < 4; j++) {
if (i*4+j < len) {
if (v & (8 >> j))
bmp[i*4+j] = 1;
else
bmp[i*4+j] = 0;
}
}
}
}
/*
* Structure used during random matrix generation, and a compare
* function to permit storage in a tree234.
*/
struct sq {
int cx, cy; /* coords of click square */
int x, y; /* coords of output square */
/*
* Number of click squares which currently affect this output
* square.
*/
int coverage;
/*
* Number of output squares currently affected by this click
* square.
*/
int ominosize;
};
#define SORT(field) do { \
if (a->field < b->field) \
return -1; \
else if (a->field > b->field) \
return +1; \
} while (0)
/*
* Compare function for choosing the next square to add. We must
* sort by coverage, then by omino size, then everything else.
*/
static int sqcmp_pick(void *av, void *bv)
{
struct sq *a = (struct sq *)av;
struct sq *b = (struct sq *)bv;
SORT(coverage);
SORT(ominosize);
SORT(cy);
SORT(cx);
SORT(y);
SORT(x);
return 0;
}
/*
* Compare function for adjusting the coverage figures after a
* change. We sort first by coverage and output square, then by
* everything else.
*/
static int sqcmp_cov(void *av, void *bv)
{
struct sq *a = (struct sq *)av;
struct sq *b = (struct sq *)bv;
SORT(coverage);
SORT(y);
SORT(x);
SORT(ominosize);
SORT(cy);
SORT(cx);
return 0;
}
/*
* Compare function for adjusting the omino sizes after a change.
* We sort first by omino size and input square, then by everything
* else.
*/
static int sqcmp_osize(void *av, void *bv)
{
struct sq *a = (struct sq *)av;
struct sq *b = (struct sq *)bv;
SORT(ominosize);
SORT(cy);
SORT(cx);
SORT(coverage);
SORT(y);
SORT(x);
return 0;
}
static void addsq(tree234 *t, int w, int h, int cx, int cy,
int x, int y, unsigned char *matrix)
{
int wh = w * h;
struct sq *sq;
int i;
if (x < 0 || x >= w || y < 0 || y >= h)
return;
if (abs(x-cx) > 1 || abs(y-cy) > 1)
return;
if (matrix[(cy*w+cx) * wh + y*w+x])
return;
sq = snew(struct sq);
sq->cx = cx;
sq->cy = cy;
sq->x = x;
sq->y = y;
sq->coverage = sq->ominosize = 0;
for (i = 0; i < wh; i++) {
if (matrix[i * wh + y*w+x])
sq->coverage++;
if (matrix[(cy*w+cx) * wh + i])
sq->ominosize++;
}
if (add234(t, sq) != sq)
sfree(sq); /* already there */
}
static void addneighbours(tree234 *t, int w, int h, int cx, int cy,
int x, int y, unsigned char *matrix)
{
addsq(t, w, h, cx, cy, x-1, y, matrix);
addsq(t, w, h, cx, cy, x+1, y, matrix);
addsq(t, w, h, cx, cy, x, y-1, matrix);
addsq(t, w, h, cx, cy, x, y+1, matrix);
}
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
int w = params->w, h = params->h, wh = w * h;
int i, j;
unsigned char *matrix, *grid;
char *mbmp, *gbmp, *ret;
matrix = snewn(wh * wh, unsigned char);
grid = snewn(wh, unsigned char);
/*
* First set up the matrix.
*/
switch (params->matrix_type) {
case CROSSES:
for (i = 0; i < wh; i++) {
int ix = i % w, iy = i / w;
for (j = 0; j < wh; j++) {
int jx = j % w, jy = j / w;
if (abs(jx - ix) + abs(jy - iy) <= 1)
matrix[i*wh+j] = 1;
else
matrix[i*wh+j] = 0;
}
}
break;
case RANDOM:
while (1) {
tree234 *pick, *cov, *osize;
int limit;
pick = newtree234(sqcmp_pick);
cov = newtree234(sqcmp_cov);
osize = newtree234(sqcmp_osize);
memset(matrix, 0, wh * wh);
for (i = 0; i < wh; i++) {
matrix[i*wh+i] = 1;
}
for (i = 0; i < wh; i++) {
int ix = i % w, iy = i / w;
addneighbours(pick, w, h, ix, iy, ix, iy, matrix);
addneighbours(cov, w, h, ix, iy, ix, iy, matrix);
addneighbours(osize, w, h, ix, iy, ix, iy, matrix);
}
/*
* Repeatedly choose a square to add to the matrix,
* until we have enough. I'll arbitrarily choose our
* limit to be the same as the total number of set bits
* in the crosses matrix.
*/
limit = 4*wh - 2*(w+h); /* centre squares already present */
while (limit-- > 0) {
struct sq *sq, *sq2, sqlocal;
int k;
/*
* Find the lowest element in the pick tree.
*/
sq = index234(pick, 0);
/*
* Find the highest element with the same coverage
* and omino size, by setting all other elements to
* lots.
*/
sqlocal = *sq;
sqlocal.cx = sqlocal.cy = sqlocal.x = sqlocal.y = wh;
sq = findrelpos234(pick, &sqlocal, NULL, REL234_LT, &k);
assert(sq != 0);
/*
* Pick at random from all elements up to k of the
* pick tree.
*/
k = random_upto(rs, k+1);
sq = delpos234(pick, k);
del234(cov, sq);
del234(osize, sq);
/*
* Add this square to the matrix.
*/
matrix[(sq->cy * w + sq->cx) * wh + (sq->y * w + sq->x)] = 1;
/*
* Correct the matrix coverage field of any sq
* which points at this output square.
*/
sqlocal = *sq;
sqlocal.cx = sqlocal.cy = sqlocal.ominosize = -1;
while ((sq2 = findrel234(cov, &sqlocal, NULL,
REL234_GT)) != NULL &&
sq2->coverage == sq->coverage &&
sq2->x == sq->x && sq2->y == sq->y) {
del234(pick, sq2);
del234(cov, sq2);
del234(osize, sq2);
sq2->coverage++;
add234(pick, sq2);
add234(cov, sq2);
add234(osize, sq2);
}
/*
* Correct the omino size field of any sq which
* points at this input square.
*/
sqlocal = *sq;
sqlocal.x = sqlocal.y = sqlocal.coverage = -1;
while ((sq2 = findrel234(osize, &sqlocal, NULL,
REL234_GT)) != NULL &&
sq2->ominosize == sq->ominosize &&
sq2->cx == sq->cx && sq2->cy == sq->cy) {
del234(pick, sq2);
del234(cov, sq2);
del234(osize, sq2);
sq2->ominosize++;
add234(pick, sq2);
add234(cov, sq2);
add234(osize, sq2);
}
/*
* The sq we actually picked out of the tree is
* finished with; but its neighbours now need to
* appear.
*/
addneighbours(pick, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
addneighbours(cov, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
addneighbours(osize, w,h, sq->cx,sq->cy, sq->x,sq->y, matrix);
sfree(sq);
}
/*
* Free all remaining sq structures.
*/
{
struct sq *sq;
while ((sq = delpos234(pick, 0)) != NULL)
sfree(sq);
}
freetree234(pick);
freetree234(cov);
freetree234(osize);
/*
* Finally, check to see if any two matrix rows are
* exactly identical. If so, this is not an acceptable
* matrix, and we give up and go round again.
*
* I haven't been immediately able to think of a
* plausible means of algorithmically avoiding this
* situation (by, say, making a small perturbation to
* an offending matrix), so for the moment I'm just
* going to deal with it by throwing the whole thing
* away. I suspect this will lead to scalability
* problems (since most of the things happening in
* these matrices are local, the chance of _some_
* neighbourhood having two identical regions will
* increase with the grid area), but so far this puzzle
* seems to be really hard at large sizes so I'm not
* massively worried yet. Anyone needs this done
* better, they're welcome to submit a patch.
*/
for (i = 0; i < wh; i++) {
for (j = 0; j < wh; j++)
if (i != j &&
!memcmp(matrix + i * wh, matrix + j * wh, wh))
break;
if (j < wh)
break;
}
if (i == wh)
break; /* no matches found */
}
break;
}
/*
* Now invent a random initial set of lights.
*
* At first glance it looks as if it might be quite difficult
* to choose equiprobably from all soluble light sets. After
* all, soluble light sets are those in the image space of the
* transformation matrix; so first we'd have to identify that
* space and its dimension, then pick a random coordinate for
* each basis vector and recombine. Lot of fiddly matrix
* algebra there.
*
* However, vector spaces are nicely orthogonal and relieve us
* of all that difficulty. For every point in the image space,
* there are precisely as many points in the input space that
* map to it as there are elements in the kernel of the
* transformation matrix (because adding any kernel element to
* the input does not change the output, and because any two
* inputs mapping to the same output must differ by an element
* of the kernel because that's what the kernel _is_); and
* these cosets are all disjoint (obviously, since no input
* point can map to more than one output point) and cover the
* whole space (equally obviously, because no input point can
* map to fewer than one output point!).
*
* So the input space contains the same number of points for
* each point in the output space; thus, we can simply choose
* equiprobably from elements of the _input_ space, and filter
* the result through the transformation matrix in the obvious
* way, and we thereby guarantee to choose equiprobably from
* all the output points. Phew!
*/
while (1) {
memset(grid, 0, wh);
for (i = 0; i < wh; i++) {
int v = random_upto(rs, 2);
if (v) {
for (j = 0; j < wh; j++)
grid[j] ^= matrix[i*wh+j];
}
}
/*
* Ensure we don't have the starting state already!
*/
for (i = 0; i < wh; i++)
if (grid[i])
break;
if (i < wh)
break;
}
/*
* Now encode the matrix and the starting grid as a game
* description. We'll do this by concatenating two great big
* hex bitmaps.
*/
mbmp = encode_bitmap(matrix, wh*wh);
gbmp = encode_bitmap(grid, wh);
ret = snewn(strlen(mbmp) + strlen(gbmp) + 2, char);
sprintf(ret, "%s,%s", mbmp, gbmp);
sfree(mbmp);
sfree(gbmp);
sfree(matrix);
sfree(grid);
return ret;
}
static char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w, h = params->h, wh = w * h;
int mlen = (wh*wh+3)/4, glen = (wh+3)/4;
if (strspn(desc, "0123456789abcdefABCDEF") != mlen)
return "Matrix description is wrong length";
if (desc[mlen] != ',')
return "Expected comma after matrix description";
if (strspn(desc+mlen+1, "0123456789abcdefABCDEF") != glen)
return "Grid description is wrong length";
if (desc[mlen+1+glen])
return "Unexpected data after grid description";
return NULL;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
int w = params->w, h = params->h, wh = w * h;
int mlen = (wh*wh+3)/4;
game_state *state = snew(game_state);
state->w = w;
state->h = h;
state->completed = FALSE;
state->cheated = FALSE;
state->hints_active = FALSE;
state->moves = 0;
state->matrix = snew(struct matrix);
state->matrix->refcount = 1;
state->matrix->matrix = snewn(wh*wh, unsigned char);
decode_bitmap(state->matrix->matrix, wh*wh, desc);
state->grid = snewn(wh, unsigned char);
decode_bitmap(state->grid, wh, desc + mlen + 1);
return state;
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
ret->w = state->w;
ret->h = state->h;
ret->completed = state->completed;
ret->cheated = state->cheated;
ret->hints_active = state->hints_active;
ret->moves = state->moves;
ret->matrix = state->matrix;
state->matrix->refcount++;
ret->grid = snewn(ret->w * ret->h, unsigned char);
memcpy(ret->grid, state->grid, ret->w * ret->h);
return ret;
}
static void free_game(game_state *state)
{
sfree(state->grid);
if (--state->matrix->refcount <= 0) {
sfree(state->matrix->matrix);
sfree(state->matrix);
}
sfree(state);
}
static void rowxor(unsigned char *row1, unsigned char *row2, int len)
{
int i;
for (i = 0; i < len; i++)
row1[i] ^= row2[i];
}
static char *solve_game(const game_state *state, const game_state *currstate,
const char *aux, char **error)
{
int w = state->w, h = state->h, wh = w * h;
unsigned char *equations, *solution, *shortest;
int *und, nund;
int rowsdone, colsdone;
int i, j, k, len, bestlen;
char *ret;
/*
* Set up a list of simultaneous equations. Each one is of
* length (wh+1) and has wh coefficients followed by a value.
*/
equations = snewn((wh + 1) * wh, unsigned char);
for (i = 0; i < wh; i++) {
for (j = 0; j < wh; j++)
equations[i * (wh+1) + j] = currstate->matrix->matrix[j*wh+i];
equations[i * (wh+1) + wh] = currstate->grid[i] & 1;
}
/*
* Perform Gaussian elimination over GF(2).
*/
rowsdone = colsdone = 0;
nund = 0;
und = snewn(wh, int);
do {
/*
* Find the leftmost column which has a 1 in it somewhere
* outside the first `rowsdone' rows.
*/
j = -1;
for (i = colsdone; i < wh; i++) {
for (j = rowsdone; j < wh; j++)
if (equations[j * (wh+1) + i])
break;
if (j < wh)
break; /* found one */
/*
* This is a column which will not have an equation
* controlling it. Mark it as undetermined.
*/
und[nund++] = i;
}
/*
* If there wasn't one, then we've finished: all remaining
* equations are of the form 0 = constant. Check to see if
* any of them wants 0 to be equal to 1; this is the
* condition which indicates an insoluble problem
* (therefore _hopefully_ one typed in by a user!).
*/
if (i == wh) {
for (j = rowsdone; j < wh; j++)
if (equations[j * (wh+1) + wh]) {
*error = "No solution exists for this position";
sfree(equations);
sfree(und);
return NULL;
}
break;
}
/*
* We've found a 1. It's in column i, and the topmost 1 in
* that column is in row j. Do a row-XOR to move it up to
* the topmost row if it isn't already there.
*/
assert(j != -1);
if (j > rowsdone)
rowxor(equations + rowsdone*(wh+1), equations + j*(wh+1), wh+1);
/*
* Do row-XORs to eliminate that 1 from all rows below the
* topmost row.
*/
for (j = rowsdone + 1; j < wh; j++)
if (equations[j*(wh+1) + i])
rowxor(equations + j*(wh+1),
equations + rowsdone*(wh+1), wh+1);
/*
* Mark this row and column as done.
*/
rowsdone++;
colsdone = i+1;
/*
* If we've done all the rows, terminate.
*/
} while (rowsdone < wh);
/*
* If we reach here, we have the ability to produce a solution.
* So we go through _all_ possible solutions (each
* corresponding to a set of arbitrary choices of those
* components not directly determined by an equation), and pick
* one requiring the smallest number of flips.
*/
solution = snewn(wh, unsigned char);
shortest = snewn(wh, unsigned char);
memset(solution, 0, wh);
bestlen = wh + 1;
while (1) {
/*
* Find a solution based on the current values of the
* undetermined variables.
*/
for (j = rowsdone; j-- ;) {
int v;
/*
* Find the leftmost set bit in this equation.
*/
for (i = 0; i < wh; i++)
if (equations[j * (wh+1) + i])
break;
assert(i < wh); /* there must have been one! */
/*
* Compute this variable using the rest.
*/
v = equations[j * (wh+1) + wh];
for (k = i+1; k < wh; k++)
if (equations[j * (wh+1) + k])
v ^= solution[k];
solution[i] = v;
}
/*
* Compare this solution to the current best one, and
* replace the best one if this one is shorter.
*/
len = 0;
for (i = 0; i < wh; i++)
if (solution[i])
len++;
if (len < bestlen) {
bestlen = len;
memcpy(shortest, solution, wh);
}
/*
* Now increment the binary number given by the
* undetermined variables: turn all 1s into 0s until we see
* a 0, at which point we turn it into a 1.
*/
for (i = 0; i < nund; i++) {
solution[und[i]] = !solution[und[i]];
if (solution[und[i]])
break;
}
/*
* If we didn't find a 0 at any point, we have wrapped
* round and are back at the start, i.e. we have enumerated
* all solutions.
*/
if (i == nund)
break;
}
/*
* We have a solution. Produce a move string encoding the
* solution.
*/
ret = snewn(wh + 2, char);
ret[0] = 'S';
for (i = 0; i < wh; i++)
ret[i+1] = shortest[i] ? '1' : '0';
ret[wh+1] = '\0';
sfree(shortest);
sfree(solution);
sfree(equations);
sfree(und);
return ret;
}
static int game_can_format_as_text_now(const game_params *params)
{
return TRUE;
}
static char *game_text_format(const game_state *state)
{
return NULL;
}
struct game_ui {
int cx, cy, cdraw;
};
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->cx = ui->cy = ui->cdraw = 0;
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui);
}
static char *encode_ui(const game_ui *ui)
{
return NULL;
}
static void decode_ui(game_ui *ui, const char *encoding)
{
}
static void game_changed_state(game_ui *ui, const game_state *oldstate,
const game_state *newstate)
{
}
struct game_drawstate {
int w, h, started;
unsigned char *tiles;
int tilesize;
};
static char *interpret_move(const game_state *state, game_ui *ui,
const game_drawstate *ds,
int x, int y, int button)
{
int w = state->w, h = state->h, wh = w * h;
char buf[80], *nullret = NULL;
if (button == LEFT_BUTTON || IS_CURSOR_SELECT(button)) {
int tx, ty;
if (button == LEFT_BUTTON) {
tx = FROMCOORD(x), ty = FROMCOORD(y);
ui->cdraw = 0;
} else {
tx = ui->cx; ty = ui->cy;
ui->cdraw = 1;
}
nullret = "";
if (tx >= 0 && tx < w && ty >= 0 && ty < h) {
/*
* It's just possible that a manually entered game ID
* will have at least one square do nothing whatsoever.
* If so, we avoid encoding a move at all.
*/
int i = ty*w+tx, j, makemove = FALSE;
for (j = 0; j < wh; j++) {
if (state->matrix->matrix[i*wh+j])
makemove = TRUE;
}
if (makemove) {
sprintf(buf, "M%d,%d", tx, ty);
return dupstr(buf);
} else {
return NULL;
}
}
}
else if (IS_CURSOR_MOVE(button)) {
int dx = 0, dy = 0;
switch (button) {
case CURSOR_UP: dy = -1; break;
case CURSOR_DOWN: dy = 1; break;
case CURSOR_RIGHT: dx = 1; break;
case CURSOR_LEFT: dx = -1; break;
default: assert(!"shouldn't get here");
}
ui->cx += dx; ui->cy += dy;
ui->cx = min(max(ui->cx, 0), state->w - 1);
ui->cy = min(max(ui->cy, 0), state->h - 1);
ui->cdraw = 1;
nullret = "";
}
return nullret;
}
static game_state *execute_move(const game_state *from, const char *move)
{
int w = from->w, h = from->h, wh = w * h;
game_state *ret;
int x, y;
if (move[0] == 'S' && strlen(move) == wh+1) {
int i;
ret = dup_game(from);
ret->hints_active = TRUE;
ret->cheated = TRUE;
for (i = 0; i < wh; i++) {
ret->grid[i] &= ~2;
if (move[i+1] != '0')
ret->grid[i] |= 2;
}
return ret;
} else if (move[0] == 'M' &&
sscanf(move+1, "%d,%d", &x, &y) == 2 &&
x >= 0 && x < w && y >= 0 && y < h) {
int i, j, done;
ret = dup_game(from);
if (!ret->completed)
ret->moves++;
i = y * w + x;
done = TRUE;
for (j = 0; j < wh; j++) {
ret->grid[j] ^= ret->matrix->matrix[i*wh+j];
if (ret->grid[j] & 1)
done = FALSE;
}
ret->grid[i] ^= 2; /* toggle hint */
if (done) {
ret->completed = TRUE;
ret->hints_active = FALSE;
}