-
Notifications
You must be signed in to change notification settings - Fork 24
/
flood.c
1370 lines (1200 loc) · 37 KB
/
flood.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
/*
* flood.c: puzzle in which you make a grid all the same colour by
* repeatedly flood-filling the top left corner, and try to do so in
* as few moves as possible.
*/
/*
* Possible further work:
*
* - UI: perhaps we should only permit clicking on regions that can
* actually be reached by the next flood-fill - i.e. a click is
* only interpreted as a move if it would cause the clicked-on
* square to become part of the controlled area. This provides a
* hint in cases where you mistakenly thought that would happen,
* and protects you against typos in cases where you just
* mis-aimed.
*
* - UI: perhaps mark the fill square in some way? Or even mark the
* whole connected component _containing_ the fill square. Pro:
* that would make it easier to tell apart cases where almost all
* the yellow squares in the grid are part of the target component
* (hence, yellow is _done_ and you never have to fill in that
* colour again) from cases where there's still one yellow square
* that's only diagonally adjacent and hence will need coming back
* to. Con: but it would almost certainly be ugly.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
enum {
COL_BACKGROUND, COL_SEPARATOR,
COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10,
COL_HIGHLIGHT, COL_LOWLIGHT,
NCOLOURS
};
struct game_params {
int w, h;
int colours;
int leniency;
};
/* Just in case I want to make this changeable later, I'll put the
* coordinates of the flood-fill point here so that it'll be easy to
* find everywhere later that has to change. */
#define FILLX 0
#define FILLY 0
typedef struct soln {
int refcount;
int nmoves;
char *moves;
} soln;
struct game_state {
int w, h, colours;
int moves, movelimit;
int complete;
char *grid;
int cheated;
int solnpos;
soln *soln;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 12;
ret->colours = 6;
ret->leniency = 5;
return ret;
}
static const struct {
struct game_params preset;
const char *name;
} flood_presets[] = {
/* Default 12x12 size, three difficulty levels. */
{{12, 12, 6, 5}, "12x12 Easy"},
{{12, 12, 6, 2}, "12x12 Medium"},
{{12, 12, 6, 0}, "12x12 Hard"},
/* Larger puzzles, leaving off Easy in the expectation that people
* wanting a bigger grid will have played it enough to find Easy
* easy. */
{{16, 16, 6, 2}, "16x16 Medium"},
{{16, 16, 6, 0}, "16x16 Hard"},
/* A couple of different colour counts. It seems generally not too
* hard with fewer colours (probably because fewer choices), so no
* extra moves for these modes. */
{{12, 12, 3, 0}, "12x12, 3 colours"},
{{12, 12, 4, 0}, "12x12, 4 colours"},
};
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
if (i < 0 || i >= lenof(flood_presets))
return FALSE;
ret = snew(game_params);
*ret = flood_presets[i].preset;
*name = dupstr(flood_presets[i].name);
*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++;
}
while (*string) {
if (*string == 'c') {
string++;
ret->colours = atoi(string);
while (string[1] && isdigit((unsigned char)string[1])) string++;
} else if (*string == 'm') {
string++;
ret->leniency = atoi(string);
while (string[1] && isdigit((unsigned char)string[1])) string++;
}
string++;
}
}
static char *encode_params(const game_params *params, int full)
{
char buf[256];
sprintf(buf, "%dx%d", params->w, params->h);
if (full)
sprintf(buf + strlen(buf), "c%dm%d",
params->colours, params->leniency);
return dupstr(buf);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(5, 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 = "Colours";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->colours);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;
ret[3].name = "Extra moves permitted";
ret[3].type = C_STRING;
sprintf(buf, "%d", params->leniency);
ret[3].sval = dupstr(buf);
ret[3].ival = 0;
ret[4].name = NULL;
ret[4].type = C_END;
ret[4].sval = NULL;
ret[4].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->colours = atoi(cfg[2].sval);
ret->leniency = atoi(cfg[3].sval);
return ret;
}
static char *validate_params(const game_params *params, int full)
{
if (params->w < 2 && params->h < 2)
return "Grid must contain at least two squares";
if (params->colours < 3 || params->colours > 10)
return "Must have between 3 and 10 colours";
if (params->leniency < 0)
return "Leniency must be non-negative";
return NULL;
}
#if 0
/*
* Bodge to permit varying the recursion depth for testing purposes.
To test two Floods against each other:
paste <(./flood.1 --generate 100 12x12c6m0#12345 | cut -f2 -d,) <(./flood.2 --generate 100 12x12c6m0#12345 | cut -f2 -d,) | awk '{print $2-$1}' | sort -n | uniq -c | awk '{print $2,$1}' | tee z
and then run gnuplot and plot "z".
*/
static int rdepth = 0;
#define RECURSION_DEPTH (rdepth)
void check_recursion_depth(void)
{
if (!rdepth) {
const char *depthstr = getenv("FLOOD_DEPTH");
rdepth = depthstr ? atoi(depthstr) : 1;
rdepth = rdepth > 0 ? rdepth : 1;
}
}
#else
/*
* Last time I empirically checked this, depth 3 was a noticeable
* improvement on 2, but 4 only negligibly better than 3.
*/
#define RECURSION_DEPTH 3
#define check_recursion_depth() (void)0
#endif
struct solver_scratch {
int *queue[2];
int *dist;
char *grid, *grid2;
char *rgrids;
};
static struct solver_scratch *new_scratch(int w, int h)
{
int wh = w*h;
struct solver_scratch *scratch = snew(struct solver_scratch);
check_recursion_depth();
scratch->queue[0] = snewn(wh, int);
scratch->queue[1] = snewn(wh, int);
scratch->dist = snewn(wh, int);
scratch->grid = snewn(wh, char);
scratch->grid2 = snewn(wh, char);
scratch->rgrids = snewn(wh * RECURSION_DEPTH, char);
return scratch;
}
static void free_scratch(struct solver_scratch *scratch)
{
sfree(scratch->queue[0]);
sfree(scratch->queue[1]);
sfree(scratch->dist);
sfree(scratch->grid);
sfree(scratch->grid2);
sfree(scratch->rgrids);
sfree(scratch);
}
#if 0
/* Diagnostic routines you can uncomment if you need them */
void dump_grid(int w, int h, const char *grid, const char *titlefmt, ...)
{
int x, y;
if (titlefmt) {
va_list ap;
va_start(ap, titlefmt);
vprintf(titlefmt, ap);
va_end(ap);
printf(":\n");
} else {
printf("Grid:\n");
}
for (y = 0; y < h; y++) {
printf(" ");
for (x = 0; x < w; x++) {
printf("%1x", grid[y*w+x]);
}
printf("\n");
}
}
void dump_dist(int w, int h, const int *dists, const char *titlefmt, ...)
{
int x, y;
if (titlefmt) {
va_list ap;
va_start(ap, titlefmt);
vprintf(titlefmt, ap);
va_end(ap);
printf(":\n");
} else {
printf("Distances:\n");
}
for (y = 0; y < h; y++) {
printf(" ");
for (x = 0; x < w; x++) {
printf("%3d", dists[y*w+x]);
}
printf("\n");
}
}
#endif
/*
* Search a grid to find the most distant square(s). Return their
* distance and the number of them, and also the number of squares in
* the current controlled set (i.e. at distance zero).
*/
static void search(int w, int h, char *grid, int x0, int y0,
struct solver_scratch *scratch,
int *rdist, int *rnumber, int *rcontrol)
{
int wh = w*h;
int i, qcurr, qhead, qtail, qnext, currdist, remaining;
for (i = 0; i < wh; i++)
scratch->dist[i] = -1;
scratch->queue[0][0] = y0*w+x0;
scratch->queue[1][0] = y0*w+x0;
scratch->dist[y0*w+x0] = 0;
currdist = 0;
qcurr = 0;
qtail = 0;
qhead = 1;
qnext = 1;
remaining = wh - 1;
while (1) {
if (qtail == qhead) {
/* Switch queues. */
if (currdist == 0)
*rcontrol = qhead;
currdist++;
qcurr ^= 1; /* switch queues */
qhead = qnext;
qtail = 0;
qnext = 0;
#if 0
printf("switch queue, new dist %d, queue %d\n", currdist, qhead);
#endif
} else if (remaining == 0 && qnext == 0) {
break;
} else {
int pos = scratch->queue[qcurr][qtail++];
int y = pos / w;
int x = pos % w;
#if 0
printf("checking neighbours of %d,%d\n", x, y);
#endif
int dir;
for (dir = 0; dir < 4; dir++) {
int y1 = y + (dir == 1 ? 1 : dir == 3 ? -1 : 0);
int x1 = x + (dir == 0 ? 1 : dir == 2 ? -1 : 0);
if (0 <= x1 && x1 < w && 0 <= y1 && y1 < h) {
int pos1 = y1*w+x1;
#if 0
printf("trying %d,%d: colours %d-%d dist %d\n", x1, y1,
grid[pos], grid[pos1], scratch->dist[pos]);
#endif
if (scratch->dist[pos1] == -1 &&
((grid[pos1] == grid[pos] &&
scratch->dist[pos] == currdist) ||
(grid[pos1] != grid[pos] &&
scratch->dist[pos] == currdist - 1))) {
#if 0
printf("marking %d,%d dist %d\n", x1, y1, currdist);
#endif
scratch->queue[qcurr][qhead++] = pos1;
scratch->queue[qcurr^1][qnext++] = pos1;
scratch->dist[pos1] = currdist;
remaining--;
}
}
}
}
}
*rdist = currdist;
*rnumber = qhead;
if (currdist == 0)
*rcontrol = qhead;
}
/*
* Enact a flood-fill move on a grid.
*/
static void fill(int w, int h, char *grid, int x0, int y0, char newcolour,
int *queue)
{
char oldcolour;
int qhead, qtail;
oldcolour = grid[y0*w+x0];
assert(oldcolour != newcolour);
grid[y0*w+x0] = newcolour;
queue[0] = y0*w+x0;
qtail = 0;
qhead = 1;
while (qtail < qhead) {
int pos = queue[qtail++];
int y = pos / w;
int x = pos % w;
int dir;
for (dir = 0; dir < 4; dir++) {
int y1 = y + (dir == 1 ? 1 : dir == 3 ? -1 : 0);
int x1 = x + (dir == 0 ? 1 : dir == 2 ? -1 : 0);
if (0 <= x1 && x1 < w && 0 <= y1 && y1 < h) {
int pos1 = y1*w+x1;
if (grid[pos1] == oldcolour) {
grid[pos1] = newcolour;
queue[qhead++] = pos1;
}
}
}
}
}
/*
* Detect a completed grid.
*/
static int completed(int w, int h, char *grid)
{
int wh = w*h;
int i;
for (i = 1; i < wh; i++)
if (grid[i] != grid[0])
return FALSE;
return TRUE;
}
/*
* Try out every possible move on a grid, and choose whichever one
* reduced the result of search() by the most.
*/
static char choosemove_recurse(int w, int h, char *grid, int x0, int y0,
int maxmove, struct solver_scratch *scratch,
int depth, int *rbestdist, int *rbestnumber, int *rbestcontrol)
{
int wh = w*h;
char move, bestmove;
int dist, number, control, bestdist, bestnumber, bestcontrol;
char *tmpgrid;
assert(0 <= depth && depth < RECURSION_DEPTH);
tmpgrid = scratch->rgrids + depth*wh;
bestdist = wh + 1;
bestnumber = 0;
bestcontrol = 0;
bestmove = -1;
#if 0
dump_grid(w, h, grid, "before choosemove_recurse %d", depth);
#endif
for (move = 0; move < maxmove; move++) {
if (grid[y0*w+x0] == move)
continue;
memcpy(tmpgrid, grid, wh * sizeof(*grid));
fill(w, h, tmpgrid, x0, y0, move, scratch->queue[0]);
if (completed(w, h, tmpgrid)) {
/*
* A move that wins is immediately the best, so stop
* searching. Record what depth of recursion that happened
* at, so that higher levels will choose a move that gets
* to a winning position sooner.
*/
*rbestdist = -1;
*rbestnumber = depth;
*rbestcontrol = wh;
return move;
}
if (depth < RECURSION_DEPTH-1) {
choosemove_recurse(w, h, tmpgrid, x0, y0, maxmove, scratch,
depth+1, &dist, &number, &control);
} else {
#if 0
dump_grid(w, h, tmpgrid, "after move %d at depth %d",
move, depth);
#endif
search(w, h, tmpgrid, x0, y0, scratch, &dist, &number, &control);
#if 0
dump_dist(w, h, scratch->dist, "after move %d at depth %d",
move, depth);
printf("move %d at depth %d: %d at %d\n",
depth, move, number, dist);
#endif
}
if (dist < bestdist ||
(dist == bestdist &&
(number < bestnumber ||
(number == bestnumber &&
(control > bestcontrol))))) {
bestdist = dist;
bestnumber = number;
bestcontrol = control;
bestmove = move;
}
}
#if 0
printf("best at depth %d was %d (%d at %d, %d controlled)\n",
depth, bestmove, bestnumber, bestdist, bestcontrol);
#endif
*rbestdist = bestdist;
*rbestnumber = bestnumber;
*rbestcontrol = bestcontrol;
return bestmove;
}
static char choosemove(int w, int h, char *grid, int x0, int y0,
int maxmove, struct solver_scratch *scratch)
{
int tmp0, tmp1, tmp2;
return choosemove_recurse(w, h, grid, x0, y0, maxmove, scratch,
0, &tmp0, &tmp1, &tmp2);
}
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, moves;
char *desc;
struct solver_scratch *scratch;
scratch = new_scratch(w, h);
/*
* Invent a random grid.
*/
for (i = 0; i < wh; i++)
scratch->grid[i] = random_upto(rs, params->colours);
/*
* Run the solver, and count how many moves it uses.
*/
memcpy(scratch->grid2, scratch->grid, wh * sizeof(*scratch->grid2));
moves = 0;
check_recursion_depth();
while (!completed(w, h, scratch->grid2)) {
char move = choosemove(w, h, scratch->grid2, FILLX, FILLY,
params->colours, scratch);
fill(w, h, scratch->grid2, FILLX, FILLY, move, scratch->queue[0]);
moves++;
}
/*
* Adjust for difficulty.
*/
moves += params->leniency;
/*
* Encode the game id.
*/
desc = snewn(wh + 40, char);
for (i = 0; i < wh; i++) {
char colour = scratch->grid[i];
char textcolour = (colour > 9 ? 'A' : '0') + colour;
desc[i] = textcolour;
}
sprintf(desc+i, ",%d", moves);
free_scratch(scratch);
return desc;
}
static char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w, h = params->h, wh = w*h;
int i;
for (i = 0; i < wh; i++) {
char c = *desc++;
if (c == 0)
return "Not enough data in grid description";
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'A' && c <= 'Z')
c = 10 + (c - 'A');
else
return "Bad character in grid description";
if (c < 0 || c >= params->colours)
return "Colour out of range in grid description";
}
if (*desc != ',')
return "Expected ',' after grid description";
desc++;
if (desc[strspn(desc, "0123456789")])
return "Badly formatted move limit 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;
game_state *state = snew(game_state);
int i;
state->w = w;
state->h = h;
state->colours = params->colours;
state->moves = 0;
state->grid = snewn(wh, char);
for (i = 0; i < wh; i++) {
char c = *desc++;
assert(c);
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'A' && c <= 'Z')
c = 10 + (c - 'A');
else
assert(!"bad colour");
state->grid[i] = c;
}
assert(*desc == ',');
desc++;
state->movelimit = atoi(desc);
state->complete = FALSE;
state->cheated = FALSE;
state->solnpos = 0;
state->soln = NULL;
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->colours = state->colours;
ret->moves = state->moves;
ret->movelimit = state->movelimit;
ret->complete = state->complete;
ret->grid = snewn(state->w * state->h, char);
memcpy(ret->grid, state->grid, state->w * state->h * sizeof(*ret->grid));
ret->cheated = state->cheated;
ret->soln = state->soln;
if (ret->soln)
ret->soln->refcount++;
ret->solnpos = state->solnpos;
return ret;
}
static void free_game(game_state *state)
{
if (state->soln && --state->soln->refcount == 0) {
sfree(state->soln->moves);
sfree(state->soln);
}
sfree(state->grid);
sfree(state);
}
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;
char *moves, *ret, *p;
int i, len, nmoves;
char buf[256];
struct solver_scratch *scratch;
if (currstate->complete)
return NULL;
/*
* Find the best solution our solver can give.
*/
moves = snewn(wh, char); /* sure to be enough */
nmoves = 0;
scratch = new_scratch(w, h);
memcpy(scratch->grid2, currstate->grid, wh * sizeof(*scratch->grid2));
check_recursion_depth();
while (!completed(w, h, scratch->grid2)) {
char move = choosemove(w, h, scratch->grid2, FILLX, FILLY,
currstate->colours, scratch);
fill(w, h, scratch->grid2, FILLX, FILLY, move, scratch->queue[0]);
assert(nmoves < wh);
moves[nmoves++] = move;
}
free_scratch(scratch);
/*
* Encode it as a move string.
*/
len = 1; /* trailing NUL */
for (i = 0; i < nmoves; i++)
len += sprintf(buf, ",%d", moves[i]);
ret = snewn(len, char);
p = ret;
for (i = 0; i < nmoves; i++)
p += sprintf(p, "%c%d", (i==0 ? 'S' : ','), moves[i]);
assert(p - ret == len - 1);
sfree(moves);
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)
{
int w = state->w, h = state->h;
char *ret, *p;
int x, y, len;
len = h * (w+1); /* +1 for newline after each row */
ret = snewn(len+1, char); /* and +1 for terminating \0 */
p = ret;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
char colour = state->grid[y*w+x];
char textcolour = (colour > 9 ? 'A' : '0') + colour;
*p++ = textcolour;
}
*p++ = '\n';
}
assert(p - ret == len);
*p = '\0';
return ret;
}
struct game_ui {
int cursor_visible;
int cx, cy;
enum { VICTORY, DEFEAT } flash_type;
};
static game_ui *new_ui(const game_state *state)
{
struct game_ui *ui = snew(struct game_ui);
ui->cursor_visible = FALSE;
ui->cx = FILLX;
ui->cy = FILLY;
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 started;
int tilesize;
int *grid;
};
#define TILESIZE (ds->tilesize)
#define PREFERRED_TILESIZE 32
#define BORDER (TILESIZE / 2)
#define SEP_WIDTH (TILESIZE / 32)
#define CURSOR_INSET (TILESIZE / 8)
#define HIGHLIGHT_WIDTH (TILESIZE / 10)
#define COORD(x) ( (x) * TILESIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
#define VICTORY_FLASH_FRAME 0.03F
#define DEFEAT_FLASH_FRAME 0.10F
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;
int tx = -1, ty = -1, move = -1;
if (button == LEFT_BUTTON) {
tx = FROMCOORD(x);
ty = FROMCOORD(y);
ui->cursor_visible = FALSE;
} else if (button == CURSOR_LEFT && ui->cx > 0) {
ui->cx--;
ui->cursor_visible = TRUE;
return "";
} else if (button == CURSOR_RIGHT && ui->cx+1 < w) {
ui->cx++;
ui->cursor_visible = TRUE;
return "";
} else if (button == CURSOR_UP && ui->cy > 0) {
ui->cy--;
ui->cursor_visible = TRUE;
return "";
} else if (button == CURSOR_DOWN && ui->cy+1 < h) {
ui->cy++;
ui->cursor_visible = TRUE;
return "";
} else if (button == CURSOR_SELECT) {
tx = ui->cx;
ty = ui->cy;
} else if (button == CURSOR_SELECT2 &&
state->soln && state->solnpos < state->soln->nmoves) {
move = state->soln->moves[state->solnpos];
} else {
return NULL;
}
if (tx >= 0 && tx < w && ty >= 0 && ty < h &&
state->grid[0] != state->grid[ty*w+tx])
move = state->grid[ty*w+tx];
if (move >= 0 && !state->complete) {
char buf[256];
sprintf(buf, "M%d", move);
return dupstr(buf);
}
return NULL;
}
static game_state *execute_move(const game_state *state, const char *move)
{
game_state *ret;
int c;
if (move[0] == 'M' &&
sscanf(move+1, "%d", &c) == 1 &&
c >= 0 &&
!state->complete) {
int *queue = snewn(state->w * state->h, int);
ret = dup_game(state);
fill(ret->w, ret->h, ret->grid, FILLX, FILLY, c, queue);
ret->moves++;
ret->complete = completed(ret->w, ret->h, ret->grid);
if (ret->soln) {
/*
* If this move is the correct next one in the stored
* solution path, advance solnpos.
*/
if (c == ret->soln->moves[ret->solnpos] &&
ret->solnpos+1 < ret->soln->nmoves) {
ret->solnpos++;
} else {
/*
* Otherwise, the user has strayed from the path or
* else the path has come to an end; either way, the
* path is no longer valid.
*/
ret->soln->refcount--;
assert(ret->soln->refcount > 0);/* `state' at least still exists */
ret->soln = NULL;
ret->solnpos = 0;
}
}
sfree(queue);
return ret;
} else if (*move == 'S') {
soln *sol;
const char *p;
int i;
/*
* This is a solve move, so we don't actually _change_ the
* grid but merely set up a stored solution path.
*/
move++;
sol = snew(soln);
sol->nmoves = 1;
for (p = move; *p; p++) {
if (*p == ',')
sol->nmoves++;
}
sol->moves = snewn(sol->nmoves, char);
for (i = 0, p = move; i < sol->nmoves; i++) {
assert(*p);
sol->moves[i] = atoi(p);
p += strspn(p, "0123456789");
if (*p) {
assert(*p == ',');
p++;
}
}
ret = dup_game(state);
ret->cheated = TRUE;
if (ret->soln && --ret->soln->refcount == 0) {
sfree(ret->soln->moves);
sfree(ret->soln);
}
ret->soln = sol;
ret->solnpos = 0;
sol->refcount = 1;
return ret;
}
return NULL;
}
/* ----------------------------------------------------------------------
* Drawing routines.
*/
static void game_compute_size(const game_params *params, int tilesize,
int *x, int *y)
{
/* Ick: fake up `ds->tilesize' for macro expansion purposes */
struct { int tilesize; } ads, *ds = &ads;
ads.tilesize = tilesize;
*x = BORDER * 2 + TILESIZE * params->w;
*y = BORDER * 2 + TILESIZE * params->h;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
}
static float *game_colours(frontend *fe, int *ncolours)
{
float *ret = snewn(3 * NCOLOURS, float);
game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
ret[COL_SEPARATOR * 3 + 0] = 0.0F;
ret[COL_SEPARATOR * 3 + 1] = 0.0F;
ret[COL_SEPARATOR * 3 + 2] = 0.0F;
/* red */
ret[COL_1 * 3 + 0] = 1.0F;
ret[COL_1 * 3 + 1] = 0.0F;
ret[COL_1 * 3 + 2] = 0.0F;
/* yellow */
ret[COL_2 * 3 + 0] = 1.0F;
ret[COL_2 * 3 + 1] = 1.0F;
ret[COL_2 * 3 + 2] = 0.0F;
/* green */
ret[COL_3 * 3 + 0] = 0.0F;
ret[COL_3 * 3 + 1] = 1.0F;
ret[COL_3 * 3 + 2] = 0.0F;
/* blue */
ret[COL_4 * 3 + 0] = 0.2F;
ret[COL_4 * 3 + 1] = 0.3F;
ret[COL_4 * 3 + 2] = 1.0F;
/* orange */