-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackbox.c
1586 lines (1357 loc) · 47 KB
/
blackbox.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
/*
* blackbox.c: implementation of 'Black Box'.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include "puzzles.h"
#define PREFERRED_TILE_SIZE 32
#define FLASH_FRAME 0.2F
/* Terminology, for ease of reading various macros scattered about the place.
*
* The 'arena' is the inner area where the balls are placed. This is
* indexed from (0,0) to (w-1,h-1) but its offset in the grid is (1,1).
*
* The 'range' (firing range) is the bit around the edge where
* the lasers are fired from. This is indexed from 0 --> (2*(w+h) - 1),
* starting at the top left ((1,0) on the grid) and moving clockwise.
*
* The 'grid' is just the big array containing arena and range;
* locations (0,0), (0,w+1), (h+1,w+1) and (h+1,0) are unused.
*/
enum {
COL_BACKGROUND, COL_COVER, COL_LOCK,
COL_TEXT, COL_FLASHTEXT,
COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
COL_BALL, COL_WRONG, COL_BUTTON,
COL_CURSOR,
NCOLOURS
};
struct game_params {
int w, h;
int minballs, maxballs;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->w = ret->h = 8;
ret->minballs = ret->maxballs = 5;
return ret;
}
static const game_params blackbox_presets[] = {
{ 5, 5, 3, 3 },
{ 8, 8, 5, 5 },
{ 8, 8, 3, 6 },
{ 10, 10, 5, 5 },
{ 10, 10, 4, 10 }
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
char str[80];
game_params *ret;
if (i < 0 || i >= lenof(blackbox_presets))
return false;
ret = snew(game_params);
*ret = blackbox_presets[i];
if (ret->minballs == ret->maxballs)
sprintf(str, "%dx%d, %d balls",
ret->w, ret->h, ret->minballs);
else
sprintf(str, "%dx%d, %d-%d balls",
ret->w, ret->h, ret->minballs, ret->maxballs);
*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 *params, char const *string)
{
char const *p = string;
game_params *defs = default_params();
*params = *defs; free_params(defs);
while (*p) {
switch (*p++) {
case 'w':
params->w = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
break;
case 'h':
params->h = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
break;
case 'm':
params->minballs = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
break;
case 'M':
params->maxballs = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
break;
default:
;
}
}
}
static char *encode_params(const game_params *params, bool full)
{
char str[256];
sprintf(str, "w%dh%dm%dM%d",
params->w, params->h, params->minballs, params->maxballs);
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 = "No. of balls";
ret[2].type = C_STRING;
if (params->minballs == params->maxballs)
sprintf(buf, "%d", params->minballs);
else
sprintf(buf, "%d-%d", params->minballs, params->maxballs);
ret[2].u.string.sval = dupstr(buf);
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);
/* Allow 'a-b' for a range, otherwise assume a single number. */
if (sscanf(cfg[2].u.string.sval, "%d-%d",
&ret->minballs, &ret->maxballs) < 2)
ret->minballs = ret->maxballs = atoi(cfg[2].u.string.sval);
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->w < 2 || params->h < 2)
return "Width and height must both be at least two";
/* next one is just for ease of coding stuff into 'char'
* types, and could be worked around if required. */
if (params->w > 255 || params->h > 255)
return "Widths and heights greater than 255 are not supported";
if (params->minballs < 0)
return "Negative number of balls";
if (params->minballs < 1)
return "Number of balls must be at least one";
if (params->minballs > params->maxballs)
return "Minimum number of balls may not be greater than maximum";
if (params->minballs >= params->w * params->h)
return "Too many balls to fit in grid";
return NULL;
}
/*
* We store: width | height | ball1x | ball1y | [ ball2x | ball2y | [...] ]
* all stored as unsigned chars; validate_params has already
* checked this won't overflow an 8-bit char.
* Then we obfuscate it.
*/
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, bool interactive)
{
int nballs = params->minballs, i;
char *grid, *ret;
unsigned char *bmp;
if (params->maxballs > params->minballs)
nballs += random_upto(rs, params->maxballs - params->minballs + 1);
grid = snewn(params->w*params->h, char);
memset(grid, 0, params->w * params->h * sizeof(char));
bmp = snewn(nballs*2 + 2, unsigned char);
memset(bmp, 0, (nballs*2 + 2) * sizeof(unsigned char));
bmp[0] = params->w;
bmp[1] = params->h;
for (i = 0; i < nballs; i++) {
int x, y;
do {
x = random_upto(rs, params->w);
y = random_upto(rs, params->h);
} while (grid[y*params->w + x]);
grid[y*params->w + x] = 1;
bmp[(i+1)*2 + 0] = x;
bmp[(i+1)*2 + 1] = y;
}
sfree(grid);
obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, false);
ret = bin2hex(bmp, nballs*2 + 2);
sfree(bmp);
return ret;
}
static const char *validate_desc(const game_params *params, const char *desc)
{
int nballs, dlen = strlen(desc), i;
unsigned char *bmp;
const char *ret;
/* the bitmap is 2+(nballs*2) long; the hex version is double that. */
nballs = ((dlen/2)-2)/2;
if (dlen < 4 || dlen % 4 ||
nballs < params->minballs || nballs > params->maxballs)
return "Game description is wrong length";
bmp = hex2bin(desc, nballs*2 + 2);
obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, true);
ret = "Game description is corrupted";
/* check general grid size */
if (bmp[0] != params->w || bmp[1] != params->h)
goto done;
/* check each ball will fit on that grid */
for (i = 0; i < nballs; i++) {
int x = bmp[(i+1)*2 + 0], y = bmp[(i+1)*2 + 1];
if (x < 0 || y < 0 || x >= params->w || y >= params->h)
goto done;
}
ret = NULL;
done:
sfree(bmp);
return ret;
}
#define BALL_CORRECT 0x01
#define BALL_GUESS 0x02
#define BALL_LOCK 0x04
#define LASER_FLAGMASK 0x1f800
#define LASER_OMITTED 0x0800
#define LASER_REFLECT 0x1000
#define LASER_HIT 0x2000
#define LASER_WRONG 0x4000
#define LASER_FLASHED 0x8000
#define LASER_EMPTY (~0)
#define FLAG_CURSOR 0x10000 /* needs to be disjoint from both sets */
struct game_state {
int w, h, minballs, maxballs, nballs, nlasers;
unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */
unsigned int *exits; /* one per laser */
bool done; /* user has finished placing his own balls. */
int laserno; /* number of next laser to be fired. */
int nguesses, nright, nwrong, nmissed;
bool reveal, justwrong;
};
#define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
#define RANGECHECK(s,x) ((x) >= 0 && (x) < (s)->nlasers)
/* specify numbers because they must match array indexes. */
enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
struct offset { int x, y; };
static const struct offset offsets[] = {
{ 0, -1 }, /* up */
{ 1, 0 }, /* right */
{ 0, 1 }, /* down */
{ -1, 0 } /* left */
};
#ifdef DEBUGGING
static const char *dirstrs[] = {
"UP", "RIGHT", "DOWN", "LEFT"
};
#endif
static bool range2grid(const game_state *state, int rangeno, int *x, int *y,
int *direction)
{
if (rangeno < 0)
return false;
if (rangeno < state->w) {
/* top row; from (1,0) to (w,0) */
*x = rangeno + 1;
*y = 0;
*direction = DIR_DOWN;
return true;
}
rangeno -= state->w;
if (rangeno < state->h) {
/* RHS; from (w+1, 1) to (w+1, h) */
*x = state->w+1;
*y = rangeno + 1;
*direction = DIR_LEFT;
return true;
}
rangeno -= state->h;
if (rangeno < state->w) {
/* bottom row; from (1, h+1) to (w, h+1); counts backwards */
*x = (state->w - rangeno);
*y = state->h+1;
*direction = DIR_UP;
return true;
}
rangeno -= state->w;
if (rangeno < state->h) {
/* LHS; from (0, 1) to (0, h); counts backwards */
*x = 0;
*y = (state->h - rangeno);
*direction = DIR_RIGHT;
return true;
}
return false;
}
static bool grid2range(const game_state *state, int x, int y, int *rangeno)
{
int ret, x1 = state->w+1, y1 = state->h+1;
if (x > 0 && x < x1 && y > 0 && y < y1) return false; /* in arena */
if (x < 0 || x > x1 || y < 0 || y > y1) return false; /* outside grid */
if ((x == 0 || x == x1) && (y == 0 || y == y1))
return false; /* one of 4 corners */
if (y == 0) { /* top line */
ret = x - 1;
} else if (x == x1) { /* RHS */
ret = y - 1 + state->w;
} else if (y == y1) { /* Bottom [and counts backwards] */
ret = (state->w - x) + state->w + state->h;
} else { /* LHS [and counts backwards ] */
ret = (state->h-y) + state->w + state->w + state->h;
}
*rangeno = ret;
debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
return true;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
game_state *state = snew(game_state);
int dlen = strlen(desc), i;
unsigned char *bmp;
state->minballs = params->minballs;
state->maxballs = params->maxballs;
state->nballs = ((dlen/2)-2)/2;
bmp = hex2bin(desc, state->nballs*2 + 2);
obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, true);
state->w = bmp[0]; state->h = bmp[1];
state->nlasers = 2 * (state->w + state->h);
state->grid = snewn((state->w+2)*(state->h+2), unsigned int);
memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int));
state->exits = snewn(state->nlasers, unsigned int);
memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int));
for (i = 0; i < state->nballs; i++) {
GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT;
}
sfree(bmp);
state->done = false;
state->justwrong = false;
state->reveal = false;
state->nguesses = state->nright = state->nwrong = state->nmissed = 0;
state->laserno = 1;
return state;
}
#define XFER(x) ret->x = state->x
static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
XFER(w); XFER(h);
XFER(minballs); XFER(maxballs);
XFER(nballs); XFER(nlasers);
ret->grid = snewn((ret->w+2)*(ret->h+2), unsigned int);
memcpy(ret->grid, state->grid, (ret->w+2)*(ret->h+2) * sizeof(unsigned int));
ret->exits = snewn(ret->nlasers, unsigned int);
memcpy(ret->exits, state->exits, ret->nlasers * sizeof(unsigned int));
XFER(done);
XFER(laserno);
XFER(nguesses);
XFER(reveal);
XFER(justwrong);
XFER(nright); XFER(nwrong); XFER(nmissed);
return ret;
}
#undef XFER
static void free_game(game_state *state)
{
sfree(state->exits);
sfree(state->grid);
sfree(state);
}
static char *solve_game(const game_state *state, const game_state *currstate,
const char *aux, const char **error)
{
return dupstr("S");
}
struct game_ui {
int flash_laserno;
int errors;
bool newmove;
int cur_x, cur_y;
bool cur_visible;
int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */
};
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->flash_laserno = LASER_EMPTY;
ui->errors = 0;
ui->newmove = false;
ui->cur_x = ui->cur_y = 1;
ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
ui->flash_laser = 0;
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui);
}
static char *encode_ui(const game_ui *ui)
{
char buf[80];
/*
* The error counter needs preserving across a serialisation.
*/
sprintf(buf, "E%d", ui->errors);
return dupstr(buf);
}
static void decode_ui(game_ui *ui, const char *encoding,
const game_state *state)
{
sscanf(encoding, "E%d", &ui->errors);
}
static void game_changed_state(game_ui *ui, const game_state *oldstate,
const game_state *newstate)
{
/*
* If we've encountered a `justwrong' state as a result of
* actually making a move, increment the ui error counter.
*/
if (newstate->justwrong && ui->newmove)
ui->errors++;
ui->newmove = false;
}
static const char *current_key_label(const game_ui *ui,
const game_state *state, int button)
{
if (IS_CURSOR_SELECT(button) && ui->cur_visible && !state->reveal) {
int gx = ui->cur_x, gy = ui->cur_y, rangeno = -1;
if (gx == 0 && gy == 0 && button == CURSOR_SELECT) return "Check";
if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
/* Cursor somewhere in the arena. */
if (button == CURSOR_SELECT && !(GRID(state, gx,gy) & BALL_LOCK))
return (GRID(state, gx, gy) & BALL_GUESS) ? "Clear" : "Ball";
if (button == CURSOR_SELECT2)
return (GRID(state, gx, gy) & BALL_LOCK) ? "Unlock" : "Lock";
}
if (grid2range(state, gx, gy, &rangeno)) {
if (button == CURSOR_SELECT &&
state->exits[rangeno] == LASER_EMPTY)
return "Fire";
if (button == CURSOR_SELECT2) {
int n = 0;
/* Row or column lock or unlock. */
if (gy == 0 || gy > state->h) { /* Column lock */
for (gy = 1; gy <= state->h; gy++)
n += !!(GRID(state, gx, gy) & BALL_LOCK);
return n > state->h/2 ? "Unlock" : "Lock";
} else { /* Row lock */
for (gx = 1; gx <= state->w; gx++)
n += !!(GRID(state, gx, gy) & BALL_LOCK);
return n > state->w/2 ? "Unlock" : "Lock";
}
}
}
}
return "";
}
#define OFFSET(gx,gy,o) do { \
int off = (4 + (o) % 4) % 4; \
(gx) += offsets[off].x; \
(gy) += offsets[off].y; \
} while(0)
enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
/* Given a position and a direction, check whether we can see a ball in front
* of us, or to our front-left or front-right. */
static bool isball(game_state *state, int gx, int gy, int direction, int lookwhere)
{
debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction],
lookwhere == LOOK_LEFT ? "LEFT" :
lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT"));
OFFSET(gx,gy,direction);
if (lookwhere == LOOK_LEFT)
OFFSET(gx,gy,direction-1);
else if (lookwhere == LOOK_RIGHT)
OFFSET(gx,gy,direction+1);
else if (lookwhere != LOOK_FORWARD)
assert(!"unknown lookwhere");
debug(("isball, new (%d, %d)\n", gx, gy));
/* if we're off the grid (into the firing range) there's never a ball. */
if (gx < 1 || gy < 1 || gx > state->w || gy > state->h)
return false;
if (GRID(state, gx,gy) & BALL_CORRECT)
return true;
return false;
}
static int fire_laser_internal(game_state *state, int x, int y, int direction)
{
int unused, lno;
bool tmp;
tmp = grid2range(state, x, y, &lno);
assert(tmp);
/* deal with strange initial reflection rules (that stop
* you turning down the laser range) */
/* I've just chosen to prioritise instant-hit over instant-reflection;
* I can't find anywhere that gives me a definite algorithm for this. */
if (isball(state, x, y, direction, LOOK_FORWARD)) {
debug(("Instant hit at (%d, %d)\n", x, y));
return LASER_HIT; /* hit */
}
if (isball(state, x, y, direction, LOOK_LEFT) ||
isball(state, x, y, direction, LOOK_RIGHT)) {
debug(("Instant reflection at (%d, %d)\n", x, y));
return LASER_REFLECT; /* reflection */
}
/* move us onto the grid. */
OFFSET(x, y, direction);
while (1) {
debug(("fire_laser: looping at (%d, %d) pointing %s\n",
x, y, dirstrs[direction]));
if (grid2range(state, x, y, &unused)) {
int exitno;
tmp = grid2range(state, x, y, &exitno);
assert(tmp);
return (lno == exitno ? LASER_REFLECT : exitno);
}
/* paranoia. This obviously should never happen */
assert(!(GRID(state, x, y) & BALL_CORRECT));
if (isball(state, x, y, direction, LOOK_FORWARD)) {
/* we're facing a ball; send back a reflection. */
debug(("Ball ahead of (%d, %d)", x, y));
return LASER_HIT; /* hit */
}
if (isball(state, x, y, direction, LOOK_LEFT)) {
/* ball to our left; rotate clockwise and look again. */
debug(("Ball to left; turning clockwise.\n"));
direction += 1; direction %= 4;
continue;
}
if (isball(state, x, y, direction, LOOK_RIGHT)) {
/* ball to our right; rotate anti-clockwise and look again. */
debug(("Ball to rightl turning anti-clockwise.\n"));
direction += 3; direction %= 4;
continue;
}
/* ... otherwise, we have no balls ahead of us so just move one step. */
debug(("No balls; moving forwards.\n"));
OFFSET(x, y, direction);
}
}
static int laser_exit(game_state *state, int entryno)
{
int x, y, direction;
bool tmp;
tmp = range2grid(state, entryno, &x, &y, &direction);
assert(tmp);
return fire_laser_internal(state, x, y, direction);
}
static void fire_laser(game_state *state, int entryno)
{
int exitno, x, y, direction;
bool tmp;
tmp = range2grid(state, entryno, &x, &y, &direction);
assert(tmp);
exitno = fire_laser_internal(state, x, y, direction);
if (exitno == LASER_HIT || exitno == LASER_REFLECT) {
GRID(state, x, y) = state->exits[entryno] = exitno;
} else {
int newno = state->laserno++;
int xend, yend, unused;
tmp = range2grid(state, exitno, &xend, ¥d, &unused);
assert(tmp);
GRID(state, x, y) = GRID(state, xend, yend) = newno;
state->exits[entryno] = exitno;
state->exits[exitno] = entryno;
}
}
/* Checks that the guessed balls in the state match up with the real balls
* for all possible lasers (i.e. not just the ones that the player might
* have already guessed). This is required because any layout with >4 balls
* might have multiple valid solutions. Returns non-zero for a 'correct'
* (i.e. consistent) layout. */
static int check_guesses(game_state *state, bool cagey)
{
game_state *solution, *guesses;
int i, x, y, n, unused, tmp;
bool tmpb;
int ret = 0;
if (cagey) {
/*
* First, check that each laser the player has already
* fired is consistent with the layout. If not, show them
* one error they've made and reveal no further
* information.
*
* Failing that, check to see whether the player would have
* been able to fire any laser which distinguished the real
* solution from their guess. If so, show them one such
* laser and reveal no further information.
*/
guesses = dup_game(state);
/* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
for (x = 1; x <= state->w; x++) {
for (y = 1; y <= state->h; y++) {
GRID(guesses, x, y) &= ~BALL_CORRECT;
if (GRID(guesses, x, y) & BALL_GUESS)
GRID(guesses, x, y) |= BALL_CORRECT;
}
}
n = 0;
for (i = 0; i < guesses->nlasers; i++) {
if (guesses->exits[i] != LASER_EMPTY &&
guesses->exits[i] != laser_exit(guesses, i))
n++;
}
if (n) {
/*
* At least one of the player's existing lasers
* contradicts their ball placement. Pick a random one,
* highlight it, and return.
*
* A temporary random state is created from the current
* grid, so that repeating the same marking will give
* the same answer instead of a different one.
*/
random_state *rs = random_new((char *)guesses->grid,
(state->w+2)*(state->h+2) *
sizeof(unsigned int));
n = random_upto(rs, n);
random_free(rs);
for (i = 0; i < guesses->nlasers; i++) {
if (guesses->exits[i] != LASER_EMPTY &&
guesses->exits[i] != laser_exit(guesses, i) &&
n-- == 0) {
state->exits[i] |= LASER_WRONG;
tmp = laser_exit(state, i);
if (RANGECHECK(state, tmp))
state->exits[tmp] |= LASER_WRONG;
state->justwrong = true;
free_game(guesses);
return 0;
}
}
}
n = 0;
for (i = 0; i < guesses->nlasers; i++) {
if (guesses->exits[i] == LASER_EMPTY &&
laser_exit(state, i) != laser_exit(guesses, i))
n++;
}
if (n) {
/*
* At least one of the player's unfired lasers would
* demonstrate their ball placement to be wrong. Pick a
* random one, highlight it, and return.
*
* A temporary random state is created from the current
* grid, so that repeating the same marking will give
* the same answer instead of a different one.
*/
random_state *rs = random_new((char *)guesses->grid,
(state->w+2)*(state->h+2) *
sizeof(unsigned int));
n = random_upto(rs, n);
random_free(rs);
for (i = 0; i < guesses->nlasers; i++) {
if (guesses->exits[i] == LASER_EMPTY &&
laser_exit(state, i) != laser_exit(guesses, i) &&
n-- == 0) {
fire_laser(state, i);
state->exits[i] |= LASER_OMITTED;
tmp = laser_exit(state, i);
if (RANGECHECK(state, tmp))
state->exits[tmp] |= LASER_OMITTED;
state->justwrong = true;
free_game(guesses);
return 0;
}
}
}
free_game(guesses);
}
/* duplicate the state (to solution) */
solution = dup_game(state);
/* clear out the lasers of solution */
for (i = 0; i < solution->nlasers; i++) {
tmpb = range2grid(solution, i, &x, &y, &unused);
assert(tmpb);
GRID(solution, x, y) = 0;
solution->exits[i] = LASER_EMPTY;
}
/* duplicate solution to guess. */
guesses = dup_game(solution);
/* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
for (x = 1; x <= state->w; x++) {
for (y = 1; y <= state->h; y++) {
GRID(guesses, x, y) &= ~BALL_CORRECT;
if (GRID(guesses, x, y) & BALL_GUESS)
GRID(guesses, x, y) |= BALL_CORRECT;
}
}
/* for each laser (on both game_states), fire it if it hasn't been fired.
* If one has been fired (or received a hit) and another hasn't, we know
* the ball layouts didn't match and can short-circuit return. */
for (i = 0; i < solution->nlasers; i++) {
if (solution->exits[i] == LASER_EMPTY)
fire_laser(solution, i);
if (guesses->exits[i] == LASER_EMPTY)
fire_laser(guesses, i);
}
/* check each game_state's laser against the other; if any differ, return 0 */
ret = 1;
for (i = 0; i < solution->nlasers; i++) {
tmpb = range2grid(solution, i, &x, &y, &unused);
assert(tmpb);
if (solution->exits[i] != guesses->exits[i]) {
/* If the original state didn't have this shot fired,
* and it would be wrong between the guess and the solution,
* add it. */
if (state->exits[i] == LASER_EMPTY) {
state->exits[i] = solution->exits[i];
if (state->exits[i] == LASER_REFLECT ||
state->exits[i] == LASER_HIT)
GRID(state, x, y) = state->exits[i];
else {
/* add a new shot, incrementing state's laser count. */
int ex, ey, newno = state->laserno++;
tmpb = range2grid(state, state->exits[i], &ex, &ey, &unused);
assert(tmpb);
GRID(state, x, y) = newno;
GRID(state, ex, ey) = newno;
}
state->exits[i] |= LASER_OMITTED;
} else {
state->exits[i] |= LASER_WRONG;
}
ret = 0;
}
}
if (ret == 0 ||
state->nguesses < state->minballs ||
state->nguesses > state->maxballs) goto done;
/* fix up original state so the 'correct' balls end up matching the guesses,
* as we've just proved that they were equivalent. */
for (x = 1; x <= state->w; x++) {
for (y = 1; y <= state->h; y++) {
if (GRID(state, x, y) & BALL_GUESS)
GRID(state, x, y) |= BALL_CORRECT;
else
GRID(state, x, y) &= ~BALL_CORRECT;
}
}
done:
/* fill in nright and nwrong. */
state->nright = state->nwrong = state->nmissed = 0;
for (x = 1; x <= state->w; x++) {
for (y = 1; y <= state->h; y++) {
int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT);
if (bs == (BALL_GUESS | BALL_CORRECT))
state->nright++;
else if (bs == BALL_GUESS)
state->nwrong++;
else if (bs == BALL_CORRECT)
state->nmissed++;
}
}
free_game(solution);
free_game(guesses);
state->reveal = true;
return ret;
}
#define TILE_SIZE (ds->tilesize)
#define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
#define FROMDRAW(x) (((x) + (TILE_SIZE / 2)) / TILE_SIZE - 1)
#define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
(state)->nguesses <= (state)->maxballs && \
!(state)->reveal && !(state)->justwrong)
struct game_drawstate {
int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
unsigned int *grid; /* as the game_state grid */
bool started, reveal, isflash;
int flash_laserno;
};
static char *interpret_move(const game_state *state, game_ui *ui,
const game_drawstate *ds,
int x, int y, int button)
{
int gx = -1, gy = -1, rangeno = -1, wouldflash = 0;
enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
char buf[80], *nullret = NULL;
if (IS_CURSOR_MOVE(button)) {
int cx = ui->cur_x, cy = ui->cur_y;
move_cursor(button, &cx, &cy, state->w+2, state->h+2, false, NULL);
if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) ||
(cx == 0 && cy == state->h+1) ||
(cx == state->w+1 && cy == 0) ||
(cx == state->w+1 && cy == state->h+1))
return NULL; /* disallow moving cursor to corners. */
ui->cur_x = cx;
ui->cur_y = cy;
ui->cur_visible = true;
return MOVE_UI_UPDATE;
}
if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
gx = FROMDRAW(x);
gy = FROMDRAW(y);
ui->cur_visible = false;
wouldflash = 1;
} else if (button == LEFT_RELEASE) {
ui->flash_laser = 0;
return MOVE_UI_UPDATE;
} else if (IS_CURSOR_SELECT(button)) {
if (ui->cur_visible) {
gx = ui->cur_x;
gy = ui->cur_y;
ui->flash_laser = 0;
wouldflash = 2;
} else {
ui->cur_visible = true;
return MOVE_UI_UPDATE;
}
/* Fix up 'button' for the below logic. */
if (button == CURSOR_SELECT2) button = RIGHT_BUTTON;
else button = LEFT_BUTTON;
}
if (gx != -1 && gy != -1) {
if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
action = REVEAL;
if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
if (button == LEFT_BUTTON) {
if (!(GRID(state, gx,gy) & BALL_LOCK))
action = TOGGLE_BALL;
} else
action = TOGGLE_LOCK;
}
if (grid2range(state, gx, gy, &rangeno)) {
if (button == LEFT_BUTTON)
action = FIRE;
else if (gy == 0 || gy > state->h)
action = TOGGLE_COLUMN_LOCK; /* and use gx */
else
action = TOGGLE_ROW_LOCK; /* and use gy */
}
}
switch (action) {
case TOGGLE_BALL:
sprintf(buf, "T%d,%d", gx, gy);
break;
case TOGGLE_LOCK:
sprintf(buf, "LB%d,%d", gx, gy);
break;
case TOGGLE_COLUMN_LOCK:
sprintf(buf, "LC%d", gx);