New infrastructure feature. Games are now permitted to be
[sgt/puzzles] / blackbox.c
1 /*
2 * blackbox.c: implementation of 'Black Box'.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <ctype.h>
10 #include <math.h>
11
12 #include "puzzles.h"
13
14 #define PREFERRED_TILE_SIZE 32
15 #define FLASH_FRAME 0.2F
16
17 /* Terminology, for ease of reading various macros scattered about the place.
18 *
19 * The 'arena' is the inner area where the balls are placed. This is
20 * indexed from (0,0) to (w-1,h-1) but its offset in the grid is (1,1).
21 *
22 * The 'range' (firing range) is the bit around the edge where
23 * the lasers are fired from. This is indexed from 0 --> (2*(w+h) - 1),
24 * starting at the top left ((1,0) on the grid) and moving clockwise.
25 *
26 * The 'grid' is just the big array containing arena and range;
27 * locations (0,0), (0,w+1), (h+1,w+1) and (h+1,0) are unused.
28 */
29
30 enum {
31 COL_BACKGROUND, COL_COVER, COL_LOCK,
32 COL_TEXT, COL_FLASHTEXT,
33 COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
34 COL_BALL, COL_WRONG, COL_BUTTON,
35 COL_LASER, COL_DIMLASER,
36 NCOLOURS
37 };
38
39 struct game_params {
40 int w, h;
41 int minballs, maxballs;
42 };
43
44 static game_params *default_params(void)
45 {
46 game_params *ret = snew(game_params);
47
48 ret->w = ret->h = 8;
49 ret->minballs = ret->maxballs = 5;
50
51 return ret;
52 }
53
54 static const game_params blackbox_presets[] = {
55 { 5, 5, 3, 3 },
56 { 8, 8, 5, 5 },
57 { 8, 8, 3, 6 },
58 { 10, 10, 5, 5 },
59 { 10, 10, 4, 10 }
60 };
61
62 static int game_fetch_preset(int i, char **name, game_params **params)
63 {
64 char str[80];
65 game_params *ret;
66
67 if (i < 0 || i >= lenof(blackbox_presets))
68 return FALSE;
69
70 ret = snew(game_params);
71 *ret = blackbox_presets[i];
72
73 if (ret->minballs == ret->maxballs)
74 sprintf(str, "%dx%d, %d balls",
75 ret->w, ret->h, ret->minballs);
76 else
77 sprintf(str, "%dx%d, %d-%d balls",
78 ret->w, ret->h, ret->minballs, ret->maxballs);
79
80 *name = dupstr(str);
81 *params = ret;
82 return TRUE;
83 }
84
85 static void free_params(game_params *params)
86 {
87 sfree(params);
88 }
89
90 static game_params *dup_params(game_params *params)
91 {
92 game_params *ret = snew(game_params);
93 *ret = *params; /* structure copy */
94 return ret;
95 }
96
97 static void decode_params(game_params *params, char const *string)
98 {
99 char const *p = string;
100 game_params *defs = default_params();
101
102 *params = *defs; free_params(defs);
103
104 while (*p) {
105 switch (*p++) {
106 case 'w':
107 params->w = atoi(p);
108 while (*p && isdigit((unsigned char)*p)) p++;
109 break;
110
111 case 'h':
112 params->h = atoi(p);
113 while (*p && isdigit((unsigned char)*p)) p++;
114 break;
115
116 case 'm':
117 params->minballs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
119 break;
120
121 case 'M':
122 params->maxballs = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
124 break;
125
126 default:
127 ;
128 }
129 }
130 }
131
132 static char *encode_params(game_params *params, int full)
133 {
134 char str[256];
135
136 sprintf(str, "w%dh%dm%dM%d",
137 params->w, params->h, params->minballs, params->maxballs);
138 return dupstr(str);
139 }
140
141 static config_item *game_configure(game_params *params)
142 {
143 config_item *ret;
144 char buf[80];
145
146 ret = snewn(4, config_item);
147
148 ret[0].name = "Width";
149 ret[0].type = C_STRING;
150 sprintf(buf, "%d", params->w);
151 ret[0].sval = dupstr(buf);
152 ret[0].ival = 0;
153
154 ret[1].name = "Height";
155 ret[1].type = C_STRING;
156 sprintf(buf, "%d", params->h);
157 ret[1].sval = dupstr(buf);
158 ret[1].ival = 0;
159
160 ret[2].name = "No. of balls";
161 ret[2].type = C_STRING;
162 if (params->minballs == params->maxballs)
163 sprintf(buf, "%d", params->minballs);
164 else
165 sprintf(buf, "%d-%d", params->minballs, params->maxballs);
166 ret[2].sval = dupstr(buf);
167 ret[2].ival = 0;
168
169 ret[3].name = NULL;
170 ret[3].type = C_END;
171 ret[3].sval = NULL;
172 ret[3].ival = 0;
173
174 return ret;
175 }
176
177 static game_params *custom_params(config_item *cfg)
178 {
179 game_params *ret = snew(game_params);
180
181 ret->w = atoi(cfg[0].sval);
182 ret->h = atoi(cfg[1].sval);
183
184 /* Allow 'a-b' for a range, otherwise assume a single number. */
185 if (sscanf(cfg[2].sval, "%d-%d", &ret->minballs, &ret->maxballs) < 2)
186 ret->minballs = ret->maxballs = atoi(cfg[2].sval);
187
188 return ret;
189 }
190
191 static char *validate_params(game_params *params, int full)
192 {
193 if (params->w < 2 || params->h < 2)
194 return "Width and height must both be at least two";
195 /* next one is just for ease of coding stuff into 'char'
196 * types, and could be worked around if required. */
197 if (params->w > 255 || params->h > 255)
198 return "Widths and heights greater than 255 are not supported";
199 if (params->minballs > params->maxballs)
200 return "Minimum number of balls may not be greater than maximum";
201 if (params->minballs >= params->w * params->h)
202 return "Too many balls to fit in grid";
203 return NULL;
204 }
205
206 /*
207 * We store: width | height | ball1x | ball1y | [ ball2x | ball2y | [...] ]
208 * all stored as unsigned chars; validate_params has already
209 * checked this won't overflow an 8-bit char.
210 * Then we obfuscate it.
211 */
212
213 static char *new_game_desc(game_params *params, random_state *rs,
214 char **aux, int interactive)
215 {
216 int nballs = params->minballs, i;
217 char *grid, *ret;
218 unsigned char *bmp;
219
220 if (params->maxballs > params->minballs)
221 nballs += random_upto(rs, params->maxballs - params->minballs + 1);
222
223 grid = snewn(params->w*params->h, char);
224 memset(grid, 0, params->w * params->h * sizeof(char));
225
226 bmp = snewn(nballs*2 + 2, unsigned char);
227 memset(bmp, 0, (nballs*2 + 2) * sizeof(unsigned char));
228
229 bmp[0] = params->w;
230 bmp[1] = params->h;
231
232 for (i = 0; i < nballs; i++) {
233 int x, y;
234
235 do {
236 x = random_upto(rs, params->w);
237 y = random_upto(rs, params->h);
238 } while (grid[y*params->w + x]);
239
240 grid[y*params->w + x] = 1;
241
242 bmp[(i+1)*2 + 0] = x;
243 bmp[(i+1)*2 + 1] = y;
244 }
245 sfree(grid);
246
247 obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, FALSE);
248 ret = bin2hex(bmp, nballs*2 + 2);
249 sfree(bmp);
250
251 return ret;
252 }
253
254 static char *validate_desc(game_params *params, char *desc)
255 {
256 int nballs, dlen = strlen(desc), i;
257 unsigned char *bmp;
258 char *ret;
259
260 /* the bitmap is 2+(nballs*2) long; the hex version is double that. */
261 nballs = ((dlen/2)-2)/2;
262
263 if (dlen < 4 || dlen % 4 ||
264 nballs < params->minballs || nballs > params->maxballs)
265 return "Game description is wrong length";
266
267 bmp = hex2bin(desc, nballs*2 + 2);
268 obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, TRUE);
269 ret = "Game description is corrupted";
270 /* check general grid size */
271 if (bmp[0] != params->w || bmp[1] != params->h)
272 goto done;
273 /* check each ball will fit on that grid */
274 for (i = 0; i < nballs; i++) {
275 int x = bmp[(i+1)*2 + 0], y = bmp[(i+1)*2 + 1];
276 if (x < 0 || y < 0 || x >= params->w || y >= params->h)
277 goto done;
278 }
279 ret = NULL;
280
281 done:
282 sfree(bmp);
283 return ret;
284 }
285
286 #define BALL_CORRECT 0x01
287 #define BALL_GUESS 0x02
288 #define BALL_LOCK 0x04
289
290 #define LASER_FLAGMASK 0xf800
291 #define LASER_OMITTED 0x0800
292 #define LASER_REFLECT 0x1000
293 #define LASER_HIT 0x2000
294 #define LASER_WRONG 0x4000
295 #define LASER_FLASHED 0x8000
296 #define LASER_EMPTY (~0)
297
298 struct game_state {
299 int w, h, minballs, maxballs, nballs, nlasers;
300 unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */
301 unsigned int *exits; /* one per laser */
302 int done; /* user has finished placing his own balls. */
303 int laserno; /* number of next laser to be fired. */
304 int nguesses, reveal, justwrong, nright, nwrong, nmissed;
305 };
306
307 #define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
308
309 #define RANGECHECK(s,x) ((x) >= 0 && (x) <= (s)->nlasers)
310
311 /* specify numbers because they must match array indexes. */
312 enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
313
314 struct offset { int x, y; };
315
316 static const struct offset offsets[] = {
317 { 0, -1 }, /* up */
318 { 1, 0 }, /* right */
319 { 0, 1 }, /* down */
320 { -1, 0 } /* left */
321 };
322
323 #ifdef DEBUGGING
324 static const char *dirstrs[] = {
325 "UP", "RIGHT", "DOWN", "LEFT"
326 };
327 #endif
328
329 static int range2grid(game_state *state, int rangeno, int *x, int *y, int *direction)
330 {
331 if (rangeno < 0)
332 return 0;
333
334 if (rangeno < state->w) {
335 /* top row; from (1,0) to (w,0) */
336 *x = rangeno + 1;
337 *y = 0;
338 *direction = DIR_DOWN;
339 return 1;
340 }
341 rangeno -= state->w;
342 if (rangeno < state->h) {
343 /* RHS; from (w+1, 1) to (w+1, h) */
344 *x = state->w+1;
345 *y = rangeno + 1;
346 *direction = DIR_LEFT;
347 return 1;
348 }
349 rangeno -= state->h;
350 if (rangeno < state->w) {
351 /* bottom row; from (1, h+1) to (w, h+1); counts backwards */
352 *x = (state->w - rangeno);
353 *y = state->h+1;
354 *direction = DIR_UP;
355 return 1;
356 }
357 rangeno -= state->w;
358 if (rangeno < state->h) {
359 /* LHS; from (0, 1) to (0, h); counts backwards */
360 *x = 0;
361 *y = (state->h - rangeno);
362 *direction = DIR_RIGHT;
363 return 1;
364 }
365 return 0;
366 }
367
368 static int grid2range(game_state *state, int x, int y, int *rangeno)
369 {
370 int ret, x1 = state->w+1, y1 = state->h+1;
371
372 if (x > 0 && x < x1 && y > 0 && y < y1) return 0; /* in arena */
373 if (x < 0 || x > y1 || y < 0 || y > y1) return 0; /* outside grid */
374
375 if ((x == 0 || x == x1) && (y == 0 || y == y1))
376 return 0; /* one of 4 corners */
377
378 if (y == 0) { /* top line */
379 ret = x - 1;
380 } else if (x == x1) { /* RHS */
381 ret = y - 1 + state->w;
382 } else if (y == y1) { /* Bottom [and counts backwards] */
383 ret = (state->w - x) + state->w + state->h;
384 } else { /* LHS [and counts backwards ] */
385 ret = (state->h-y) + state->w + state->w + state->h;
386 }
387 *rangeno = ret;
388 debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
389 return 1;
390 }
391
392 static game_state *new_game(midend *me, game_params *params, char *desc)
393 {
394 game_state *state = snew(game_state);
395 int dlen = strlen(desc), i;
396 unsigned char *bmp;
397
398 state->minballs = params->minballs;
399 state->maxballs = params->maxballs;
400 state->nballs = ((dlen/2)-2)/2;
401
402 bmp = hex2bin(desc, state->nballs*2 + 2);
403 obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, TRUE);
404
405 state->w = bmp[0]; state->h = bmp[1];
406 state->nlasers = 2 * (state->w + state->h);
407
408 state->grid = snewn((state->w+2)*(state->h+2), unsigned int);
409 memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int));
410
411 state->exits = snewn(state->nlasers, unsigned int);
412 memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int));
413
414 for (i = 0; i < state->nballs; i++) {
415 GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT;
416 }
417 sfree(bmp);
418
419 state->done = state->nguesses = state->reveal = state->justwrong =
420 state->nright = state->nwrong = state->nmissed = 0;
421 state->laserno = 1;
422
423 return state;
424 }
425
426 #define XFER(x) ret->x = state->x
427
428 static game_state *dup_game(game_state *state)
429 {
430 game_state *ret = snew(game_state);
431
432 XFER(w); XFER(h);
433 XFER(minballs); XFER(maxballs);
434 XFER(nballs); XFER(nlasers);
435
436 ret->grid = snewn((ret->w+2)*(ret->h+2), unsigned int);
437 memcpy(ret->grid, state->grid, (ret->w+2)*(ret->h+2) * sizeof(unsigned int));
438 ret->exits = snewn(ret->nlasers, unsigned int);
439 memcpy(ret->exits, state->exits, ret->nlasers * sizeof(unsigned int));
440
441 XFER(done);
442 XFER(laserno);
443 XFER(nguesses);
444 XFER(reveal);
445 XFER(justwrong);
446 XFER(nright); XFER(nwrong); XFER(nmissed);
447
448 return ret;
449 }
450
451 #undef XFER
452
453 static void free_game(game_state *state)
454 {
455 sfree(state->exits);
456 sfree(state->grid);
457 sfree(state);
458 }
459
460 static char *solve_game(game_state *state, game_state *currstate,
461 char *aux, char **error)
462 {
463 return dupstr("S");
464 }
465
466 static int game_can_format_as_text_now(game_params *params)
467 {
468 return TRUE;
469 }
470
471 static char *game_text_format(game_state *state)
472 {
473 return NULL;
474 }
475
476 struct game_ui {
477 int flash_laserno;
478 int errors, newmove;
479 };
480
481 static game_ui *new_ui(game_state *state)
482 {
483 game_ui *ui = snew(game_ui);
484 ui->flash_laserno = LASER_EMPTY;
485 ui->errors = 0;
486 ui->newmove = FALSE;
487 return ui;
488 }
489
490 static void free_ui(game_ui *ui)
491 {
492 sfree(ui);
493 }
494
495 static char *encode_ui(game_ui *ui)
496 {
497 char buf[80];
498 /*
499 * The error counter needs preserving across a serialisation.
500 */
501 sprintf(buf, "E%d", ui->errors);
502 return dupstr(buf);
503 }
504
505 static void decode_ui(game_ui *ui, char *encoding)
506 {
507 sscanf(encoding, "E%d", &ui->errors);
508 }
509
510 static void game_changed_state(game_ui *ui, game_state *oldstate,
511 game_state *newstate)
512 {
513 /*
514 * If we've encountered a `justwrong' state as a result of
515 * actually making a move, increment the ui error counter.
516 */
517 if (newstate->justwrong && ui->newmove)
518 ui->errors++;
519 ui->newmove = FALSE;
520 }
521
522 #define OFFSET(gx,gy,o) do { \
523 int off = (4 + (o) % 4) % 4; \
524 (gx) += offsets[off].x; \
525 (gy) += offsets[off].y; \
526 } while(0)
527
528 enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
529
530 /* Given a position and a direction, check whether we can see a ball in front
531 * of us, or to our front-left or front-right. */
532 static int isball(game_state *state, int gx, int gy, int direction, int lookwhere)
533 {
534 debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction],
535 lookwhere == LOOK_LEFT ? "LEFT" :
536 lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT"));
537 OFFSET(gx,gy,direction);
538 if (lookwhere == LOOK_LEFT)
539 OFFSET(gx,gy,direction-1);
540 else if (lookwhere == LOOK_RIGHT)
541 OFFSET(gx,gy,direction+1);
542 else if (lookwhere != LOOK_FORWARD)
543 assert(!"unknown lookwhere");
544
545 debug(("isball, new (%d, %d)\n", gx, gy));
546
547 /* if we're off the grid (into the firing range) there's never a ball. */
548 if (gx < 1 || gy < 1 || gx > state->h || gy > state->w)
549 return 0;
550
551 if (GRID(state, gx,gy) & BALL_CORRECT)
552 return 1;
553
554 return 0;
555 }
556
557 static int fire_laser_internal(game_state *state, int x, int y, int direction)
558 {
559 int unused, lno, tmp;
560
561 tmp = grid2range(state, x, y, &lno);
562 assert(tmp);
563
564 /* deal with strange initial reflection rules (that stop
565 * you turning down the laser range) */
566
567 /* I've just chosen to prioritise instant-hit over instant-reflection;
568 * I can't find anywhere that gives me a definite algorithm for this. */
569 if (isball(state, x, y, direction, LOOK_FORWARD)) {
570 debug(("Instant hit at (%d, %d)\n", x, y));
571 return LASER_HIT; /* hit */
572 }
573
574 if (isball(state, x, y, direction, LOOK_LEFT) ||
575 isball(state, x, y, direction, LOOK_RIGHT)) {
576 debug(("Instant reflection at (%d, %d)\n", x, y));
577 return LASER_REFLECT; /* reflection */
578 }
579 /* move us onto the grid. */
580 OFFSET(x, y, direction);
581
582 while (1) {
583 debug(("fire_laser: looping at (%d, %d) pointing %s\n",
584 x, y, dirstrs[direction]));
585 if (grid2range(state, x, y, &unused)) {
586 int exitno;
587
588 tmp = grid2range(state, x, y, &exitno);
589 assert(tmp);
590
591 return (lno == exitno ? LASER_REFLECT : exitno);
592 }
593 /* paranoia. This obviously should never happen */
594 assert(!(GRID(state, x, y) & BALL_CORRECT));
595
596 if (isball(state, x, y, direction, LOOK_FORWARD)) {
597 /* we're facing a ball; send back a reflection. */
598 debug(("Ball ahead of (%d, %d)", x, y));
599 return LASER_HIT; /* hit */
600 }
601
602 if (isball(state, x, y, direction, LOOK_LEFT)) {
603 /* ball to our left; rotate clockwise and look again. */
604 debug(("Ball to left; turning clockwise.\n"));
605 direction += 1; direction %= 4;
606 continue;
607 }
608 if (isball(state, x, y, direction, LOOK_RIGHT)) {
609 /* ball to our right; rotate anti-clockwise and look again. */
610 debug(("Ball to rightl turning anti-clockwise.\n"));
611 direction += 3; direction %= 4;
612 continue;
613 }
614 /* ... otherwise, we have no balls ahead of us so just move one step. */
615 debug(("No balls; moving forwards.\n"));
616 OFFSET(x, y, direction);
617 }
618 }
619
620 static int laser_exit(game_state *state, int entryno)
621 {
622 int tmp, x, y, direction;
623
624 tmp = range2grid(state, entryno, &x, &y, &direction);
625 assert(tmp);
626
627 return fire_laser_internal(state, x, y, direction);
628 }
629
630 static void fire_laser(game_state *state, int entryno)
631 {
632 int tmp, exitno, x, y, direction;
633
634 tmp = range2grid(state, entryno, &x, &y, &direction);
635 assert(tmp);
636
637 exitno = fire_laser_internal(state, x, y, direction);
638
639 if (exitno == LASER_HIT || exitno == LASER_REFLECT) {
640 GRID(state, x, y) = state->exits[entryno] = exitno;
641 } else {
642 int newno = state->laserno++;
643 int xend, yend, unused;
644 tmp = range2grid(state, exitno, &xend, &yend, &unused);
645 assert(tmp);
646 GRID(state, x, y) = GRID(state, xend, yend) = newno;
647 state->exits[entryno] = exitno;
648 state->exits[exitno] = entryno;
649 }
650 }
651
652 /* Checks that the guessed balls in the state match up with the real balls
653 * for all possible lasers (i.e. not just the ones that the player might
654 * have already guessed). This is required because any layout with >4 balls
655 * might have multiple valid solutions. Returns non-zero for a 'correct'
656 * (i.e. consistent) layout. */
657 static int check_guesses(game_state *state, int cagey)
658 {
659 game_state *solution, *guesses;
660 int i, x, y, n, unused, tmp;
661 int ret = 0;
662
663 if (cagey) {
664 /*
665 * First, check that each laser the player has already
666 * fired is consistent with the layout. If not, show them
667 * one error they've made and reveal no further
668 * information.
669 *
670 * Failing that, check to see whether the player would have
671 * been able to fire any laser which distinguished the real
672 * solution from their guess. If so, show them one such
673 * laser and reveal no further information.
674 */
675 guesses = dup_game(state);
676 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
677 for (x = 1; x <= state->w; x++) {
678 for (y = 1; y <= state->h; y++) {
679 GRID(guesses, x, y) &= ~BALL_CORRECT;
680 if (GRID(guesses, x, y) & BALL_GUESS)
681 GRID(guesses, x, y) |= BALL_CORRECT;
682 }
683 }
684 n = 0;
685 for (i = 0; i < guesses->nlasers; i++) {
686 if (guesses->exits[i] != LASER_EMPTY &&
687 guesses->exits[i] != laser_exit(guesses, i))
688 n++;
689 }
690 if (n) {
691 /*
692 * At least one of the player's existing lasers
693 * contradicts their ball placement. Pick a random one,
694 * highlight it, and return.
695 *
696 * A temporary random state is created from the current
697 * grid, so that repeating the same marking will give
698 * the same answer instead of a different one.
699 */
700 random_state *rs = random_new((char *)guesses->grid,
701 (state->w+2)*(state->h+2) *
702 sizeof(unsigned int));
703 n = random_upto(rs, n);
704 random_free(rs);
705 for (i = 0; i < guesses->nlasers; i++) {
706 if (guesses->exits[i] != LASER_EMPTY &&
707 guesses->exits[i] != laser_exit(guesses, i) &&
708 n-- == 0) {
709 state->exits[i] |= LASER_WRONG;
710 tmp = laser_exit(state, i);
711 if (RANGECHECK(state, tmp))
712 state->exits[tmp] |= LASER_WRONG;
713 state->justwrong = TRUE;
714 free_game(guesses);
715 return 0;
716 }
717 }
718 }
719 n = 0;
720 for (i = 0; i < guesses->nlasers; i++) {
721 if (guesses->exits[i] == LASER_EMPTY &&
722 laser_exit(state, i) != laser_exit(guesses, i))
723 n++;
724 }
725 if (n) {
726 /*
727 * At least one of the player's unfired lasers would
728 * demonstrate their ball placement to be wrong. Pick a
729 * random one, highlight it, and return.
730 *
731 * A temporary random state is created from the current
732 * grid, so that repeating the same marking will give
733 * the same answer instead of a different one.
734 */
735 random_state *rs = random_new((char *)guesses->grid,
736 (state->w+2)*(state->h+2) *
737 sizeof(unsigned int));
738 n = random_upto(rs, n);
739 random_free(rs);
740 for (i = 0; i < guesses->nlasers; i++) {
741 if (guesses->exits[i] == LASER_EMPTY &&
742 laser_exit(state, i) != laser_exit(guesses, i) &&
743 n-- == 0) {
744 fire_laser(state, i);
745 state->exits[i] |= LASER_OMITTED;
746 tmp = laser_exit(state, i);
747 if (RANGECHECK(state, tmp))
748 state->exits[tmp] |= LASER_OMITTED;
749 state->justwrong = TRUE;
750 free_game(guesses);
751 return 0;
752 }
753 }
754 }
755 free_game(guesses);
756 }
757
758 /* duplicate the state (to solution) */
759 solution = dup_game(state);
760
761 /* clear out the lasers of solution */
762 for (i = 0; i < solution->nlasers; i++) {
763 tmp = range2grid(solution, i, &x, &y, &unused);
764 assert(tmp);
765 GRID(solution, x, y) = 0;
766 solution->exits[i] = LASER_EMPTY;
767 }
768
769 /* duplicate solution to guess. */
770 guesses = dup_game(solution);
771
772 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
773 for (x = 1; x <= state->w; x++) {
774 for (y = 1; y <= state->h; y++) {
775 GRID(guesses, x, y) &= ~BALL_CORRECT;
776 if (GRID(guesses, x, y) & BALL_GUESS)
777 GRID(guesses, x, y) |= BALL_CORRECT;
778 }
779 }
780
781 /* for each laser (on both game_states), fire it if it hasn't been fired.
782 * If one has been fired (or received a hit) and another hasn't, we know
783 * the ball layouts didn't match and can short-circuit return. */
784 for (i = 0; i < solution->nlasers; i++) {
785 if (solution->exits[i] == LASER_EMPTY)
786 fire_laser(solution, i);
787 if (guesses->exits[i] == LASER_EMPTY)
788 fire_laser(guesses, i);
789 }
790
791 /* check each game_state's laser against the other; if any differ, return 0 */
792 ret = 1;
793 for (i = 0; i < solution->nlasers; i++) {
794 tmp = range2grid(solution, i, &x, &y, &unused);
795 assert(tmp);
796
797 if (solution->exits[i] != guesses->exits[i]) {
798 /* If the original state didn't have this shot fired,
799 * and it would be wrong between the guess and the solution,
800 * add it. */
801 if (state->exits[i] == LASER_EMPTY) {
802 state->exits[i] = solution->exits[i];
803 if (state->exits[i] == LASER_REFLECT ||
804 state->exits[i] == LASER_HIT)
805 GRID(state, x, y) = state->exits[i];
806 else {
807 /* add a new shot, incrementing state's laser count. */
808 int ex, ey, newno = state->laserno++;
809 tmp = range2grid(state, state->exits[i], &ex, &ey, &unused);
810 assert(tmp);
811 GRID(state, x, y) = newno;
812 GRID(state, ex, ey) = newno;
813 }
814 state->exits[i] |= LASER_OMITTED;
815 } else {
816 state->exits[i] |= LASER_WRONG;
817 }
818 ret = 0;
819 }
820 }
821 if (ret == 0 ||
822 state->nguesses < state->minballs ||
823 state->nguesses > state->maxballs) goto done;
824
825 /* fix up original state so the 'correct' balls end up matching the guesses,
826 * as we've just proved that they were equivalent. */
827 for (x = 1; x <= state->w; x++) {
828 for (y = 1; y <= state->h; y++) {
829 if (GRID(state, x, y) & BALL_GUESS)
830 GRID(state, x, y) |= BALL_CORRECT;
831 else
832 GRID(state, x, y) &= ~BALL_CORRECT;
833 }
834 }
835
836 done:
837 /* fill in nright and nwrong. */
838 state->nright = state->nwrong = state->nmissed = 0;
839 for (x = 1; x <= state->w; x++) {
840 for (y = 1; y <= state->h; y++) {
841 int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT);
842 if (bs == (BALL_GUESS | BALL_CORRECT))
843 state->nright++;
844 else if (bs == BALL_GUESS)
845 state->nwrong++;
846 else if (bs == BALL_CORRECT)
847 state->nmissed++;
848 }
849 }
850 free_game(solution);
851 free_game(guesses);
852 state->reveal = 1;
853 return ret;
854 }
855
856 #define TILE_SIZE (ds->tilesize)
857
858 #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
859 #define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE)
860
861 #define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
862 (state)->nguesses <= (state)->maxballs && \
863 !(state)->reveal && !(state)->justwrong)
864
865 struct game_drawstate {
866 int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
867 unsigned int *grid; /* as the game_state grid */
868 int started, reveal;
869 int flash_laserno, isflash;
870 };
871
872 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
873 int x, int y, int button)
874 {
875 int gx = -1, gy = -1, rangeno = -1;
876 enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
877 TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
878 char buf[80], *nullret = NULL;
879
880 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
881 gx = FROMDRAW(x);
882 gy = FROMDRAW(y);
883 if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
884 action = REVEAL;
885 if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
886 if (button == LEFT_BUTTON) {
887 if (!(GRID(state, gx,gy) & BALL_LOCK))
888 action = TOGGLE_BALL;
889 } else
890 action = TOGGLE_LOCK;
891 }
892 if (grid2range(state, gx, gy, &rangeno)) {
893 if (button == LEFT_BUTTON)
894 action = FIRE;
895 else if (gy == 0 || gy > state->h)
896 action = TOGGLE_COLUMN_LOCK; /* and use gx */
897 else
898 action = TOGGLE_ROW_LOCK; /* and use gy */
899 }
900 } else if (button == LEFT_RELEASE) {
901 ui->flash_laserno = LASER_EMPTY;
902 return "";
903 }
904
905 switch (action) {
906 case TOGGLE_BALL:
907 sprintf(buf, "T%d,%d", gx, gy);
908 break;
909
910 case TOGGLE_LOCK:
911 sprintf(buf, "LB%d,%d", gx, gy);
912 break;
913
914 case TOGGLE_COLUMN_LOCK:
915 sprintf(buf, "LC%d", gx);
916 break;
917
918 case TOGGLE_ROW_LOCK:
919 sprintf(buf, "LR%d", gy);
920 break;
921
922 case FIRE:
923 if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
924 return nullret;
925 ui->flash_laserno = rangeno;
926 nullret = "";
927 if (state->exits[rangeno] != LASER_EMPTY)
928 return "";
929 sprintf(buf, "F%d", rangeno);
930 break;
931
932 case REVEAL:
933 if (!CAN_REVEAL(state)) return nullret;
934 sprintf(buf, "R");
935 break;
936
937 default:
938 return nullret;
939 }
940 if (state->reveal) return nullret;
941 ui->newmove = TRUE;
942 return dupstr(buf);
943 }
944
945 static game_state *execute_move(game_state *from, char *move)
946 {
947 game_state *ret = dup_game(from);
948 int gx = -1, gy = -1, rangeno = -1;
949
950 if (ret->justwrong) {
951 int i;
952 ret->justwrong = FALSE;
953 for (i = 0; i < ret->nlasers; i++)
954 if (ret->exits[i] != LASER_EMPTY)
955 ret->exits[i] &= ~(LASER_OMITTED | LASER_WRONG);
956 }
957
958 if (!strcmp(move, "S")) {
959 check_guesses(ret, FALSE);
960 return ret;
961 }
962
963 if (from->reveal) goto badmove;
964 if (!*move) goto badmove;
965
966 switch (move[0]) {
967 case 'T':
968 sscanf(move+1, "%d,%d", &gx, &gy);
969 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
970 goto badmove;
971 if (GRID(ret, gx, gy) & BALL_GUESS) {
972 ret->nguesses--;
973 GRID(ret, gx, gy) &= ~BALL_GUESS;
974 } else {
975 ret->nguesses++;
976 GRID(ret, gx, gy) |= BALL_GUESS;
977 }
978 break;
979
980 case 'F':
981 sscanf(move+1, "%d", &rangeno);
982 if (ret->exits[rangeno] != LASER_EMPTY)
983 goto badmove;
984 if (!RANGECHECK(ret, rangeno))
985 goto badmove;
986 fire_laser(ret, rangeno);
987 break;
988
989 case 'R':
990 if (ret->nguesses < ret->minballs ||
991 ret->nguesses > ret->maxballs)
992 goto badmove;
993 check_guesses(ret, TRUE);
994 break;
995
996 case 'L':
997 {
998 int lcount = 0;
999 if (strlen(move) < 2) goto badmove;
1000 switch (move[1]) {
1001 case 'B':
1002 sscanf(move+2, "%d,%d", &gx, &gy);
1003 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
1004 goto badmove;
1005 GRID(ret, gx, gy) ^= BALL_LOCK;
1006 break;
1007
1008 #define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0)
1009 #define SETLOCKIF(c) do { \
1010 if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \
1011 else GRID(ret, gx, gy) |= BALL_LOCK; \
1012 } while(0)
1013
1014 case 'C':
1015 sscanf(move+2, "%d", &gx);
1016 if (gx < 1 || gx > ret->w) goto badmove;
1017 for (gy = 1; gy <= ret->h; gy++) { COUNTLOCK; }
1018 for (gy = 1; gy <= ret->h; gy++) { SETLOCKIF(ret->h/2); }
1019 break;
1020
1021 case 'R':
1022 sscanf(move+2, "%d", &gy);
1023 if (gy < 1 || gy > ret->h) goto badmove;
1024 for (gx = 1; gx <= ret->w; gx++) { COUNTLOCK; }
1025 for (gx = 1; gx <= ret->w; gx++) { SETLOCKIF(ret->w/2); }
1026 break;
1027
1028 #undef COUNTLOCK
1029 #undef SETLOCKIF
1030
1031 default:
1032 goto badmove;
1033 }
1034 }
1035 break;
1036
1037 default:
1038 goto badmove;
1039 }
1040
1041 return ret;
1042
1043 badmove:
1044 free_game(ret);
1045 return NULL;
1046 }
1047
1048 /* ----------------------------------------------------------------------
1049 * Drawing routines.
1050 */
1051
1052 static void game_compute_size(game_params *params, int tilesize,
1053 int *x, int *y)
1054 {
1055 /* Border is ts/2, to make things easier.
1056 * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
1057 * across, and similarly height + 2 + 1 tiles down. */
1058 *x = (params->w + 3) * tilesize;
1059 *y = (params->h + 3) * tilesize;
1060 }
1061
1062 static void game_set_size(drawing *dr, game_drawstate *ds,
1063 game_params *params, int tilesize)
1064 {
1065 ds->tilesize = tilesize;
1066 ds->crad = (tilesize-1)/2;
1067 ds->rrad = (3*tilesize)/8;
1068 }
1069
1070 static float *game_colours(frontend *fe, int *ncolours)
1071 {
1072 float *ret = snewn(3 * NCOLOURS, float);
1073 int i;
1074
1075 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
1076
1077 ret[COL_BALL * 3 + 0] = 0.0F;
1078 ret[COL_BALL * 3 + 1] = 0.0F;
1079 ret[COL_BALL * 3 + 2] = 0.0F;
1080
1081 ret[COL_WRONG * 3 + 0] = 1.0F;
1082 ret[COL_WRONG * 3 + 1] = 0.0F;
1083 ret[COL_WRONG * 3 + 2] = 0.0F;
1084
1085 ret[COL_BUTTON * 3 + 0] = 0.0F;
1086 ret[COL_BUTTON * 3 + 1] = 1.0F;
1087 ret[COL_BUTTON * 3 + 2] = 0.0F;
1088
1089 ret[COL_LASER * 3 + 0] = 1.0F;
1090 ret[COL_LASER * 3 + 1] = 0.0F;
1091 ret[COL_LASER * 3 + 2] = 0.0F;
1092
1093 ret[COL_DIMLASER * 3 + 0] = 0.5F;
1094 ret[COL_DIMLASER * 3 + 1] = 0.0F;
1095 ret[COL_DIMLASER * 3 + 2] = 0.0F;
1096
1097 for (i = 0; i < 3; i++) {
1098 ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
1099 ret[COL_LOCK * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.7F;
1100 ret[COL_COVER * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.5F;
1101 ret[COL_TEXT * 3 + i] = 0.0F;
1102 }
1103
1104 ret[COL_FLASHTEXT * 3 + 0] = 0.0F;
1105 ret[COL_FLASHTEXT * 3 + 1] = 1.0F;
1106 ret[COL_FLASHTEXT * 3 + 2] = 0.0F;
1107
1108 *ncolours = NCOLOURS;
1109 return ret;
1110 }
1111
1112 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1113 {
1114 struct game_drawstate *ds = snew(struct game_drawstate);
1115
1116 ds->tilesize = 0;
1117 ds->w = state->w; ds->h = state->h;
1118 ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
1119 memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
1120 ds->started = ds->reveal = 0;
1121 ds->flash_laserno = LASER_EMPTY;
1122 ds->isflash = 0;
1123
1124 return ds;
1125 }
1126
1127 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1128 {
1129 sfree(ds->grid);
1130 sfree(ds);
1131 }
1132
1133 static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds,
1134 int ax, int ay, int force, int isflash)
1135 {
1136 int gx = ax+1, gy = ay+1;
1137 int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
1138 int dx = TODRAW(gx), dy = TODRAW(gy);
1139
1140 if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
1141 int bcol, bg;
1142
1143 bg = (gs->reveal ? COL_BACKGROUND :
1144 (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
1145
1146 draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg);
1147 draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
1148
1149 if (gs->reveal) {
1150 /* Guessed balls are always black; if they're incorrect they'll
1151 * have a red cross added later.
1152 * Missing balls are red. */
1153 if (gs_tile & BALL_GUESS) {
1154 bcol = isflash ? bg : COL_BALL;
1155 } else if (gs_tile & BALL_CORRECT) {
1156 bcol = isflash ? bg : COL_WRONG;
1157 } else {
1158 bcol = bg;
1159 }
1160 } else {
1161 /* guesses are black/black, all else background. */
1162 if (gs_tile & BALL_GUESS) {
1163 bcol = COL_BALL;
1164 } else {
1165 bcol = bg;
1166 }
1167 }
1168
1169 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
1170 bcol, bcol);
1171
1172 if (gs->reveal &&
1173 (gs_tile & BALL_GUESS) &&
1174 !(gs_tile & BALL_CORRECT)) {
1175 int x1 = dx + 3, y1 = dy + 3;
1176 int x2 = dx + TILE_SIZE - 3, y2 = dy + TILE_SIZE-3;
1177 int coords[8];
1178
1179 /* Incorrect guess; draw a red cross over the ball. */
1180 coords[0] = x1-1;
1181 coords[1] = y1+1;
1182 coords[2] = x1+1;
1183 coords[3] = y1-1;
1184 coords[4] = x2+1;
1185 coords[5] = y2-1;
1186 coords[6] = x2-1;
1187 coords[7] = y2+1;
1188 draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
1189 coords[0] = x2+1;
1190 coords[1] = y1+1;
1191 coords[2] = x2-1;
1192 coords[3] = y1-1;
1193 coords[4] = x1-1;
1194 coords[5] = y2-1;
1195 coords[6] = x1+1;
1196 coords[7] = y2+1;
1197 draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
1198 }
1199 draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
1200 }
1201 GRID(ds,gx,gy) = gs_tile;
1202 }
1203
1204 static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds,
1205 game_ui *ui, int lno, int force)
1206 {
1207 int gx, gy, dx, dy, unused;
1208 int wrong, omitted, reflect, hit, laserval, flash = 0, tmp;
1209 unsigned int gs_tile, ds_tile, exitno;
1210
1211 tmp = range2grid(gs, lno, &gx, &gy, &unused);
1212 assert(tmp);
1213 gs_tile = GRID(gs, gx, gy);
1214 ds_tile = GRID(ds, gx, gy);
1215 dx = TODRAW(gx);
1216 dy = TODRAW(gy);
1217
1218 wrong = gs->exits[lno] & LASER_WRONG;
1219 omitted = gs->exits[lno] & LASER_OMITTED;
1220 exitno = gs->exits[lno] & ~LASER_FLAGMASK;
1221
1222 reflect = gs_tile & LASER_REFLECT;
1223 hit = gs_tile & LASER_HIT;
1224 laserval = gs_tile & ~LASER_FLAGMASK;
1225
1226 if (lno == ui->flash_laserno)
1227 gs_tile |= LASER_FLASHED;
1228 else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
1229 if (exitno == ui->flash_laserno)
1230 gs_tile |= LASER_FLASHED;
1231 }
1232 if (gs_tile & LASER_FLASHED) flash = 1;
1233
1234 gs_tile |= wrong | omitted;
1235
1236 if (gs_tile != ds_tile || force) {
1237 draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
1238 draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
1239
1240 if (gs_tile &~ (LASER_WRONG | LASER_OMITTED)) {
1241 char str[10];
1242 int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
1243
1244 if (reflect || hit)
1245 sprintf(str, "%s", reflect ? "R" : "H");
1246 else
1247 sprintf(str, "%d", laserval);
1248
1249 if (wrong) {
1250 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1251 ds->rrad,
1252 COL_WRONG, COL_WRONG);
1253 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1254 ds->rrad - TILE_SIZE/16,
1255 COL_BACKGROUND, COL_WRONG);
1256 }
1257
1258 draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1259 FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
1260 tcol, str);
1261 }
1262 draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
1263 }
1264 GRID(ds, gx, gy) = gs_tile;
1265 }
1266
1267
1268 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
1269 game_state *state, int dir, game_ui *ui,
1270 float animtime, float flashtime)
1271 {
1272 int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0;
1273
1274 if (flashtime > 0) {
1275 int frame = (int)(flashtime / FLASH_FRAME);
1276 isflash = (frame % 2) == 0;
1277 debug(("game_redraw: flashtime = %f", flashtime));
1278 }
1279
1280 if (!ds->started) {
1281 int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
1282 int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
1283
1284 draw_rect(dr, 0, 0,
1285 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3),
1286 COL_BACKGROUND);
1287
1288 /* clockwise around the outline starting at pt behind (1,1). */
1289 draw_line(dr, x0+ts, y0+ts, x0+ts, y0, COL_HIGHLIGHT);
1290 draw_line(dr, x0+ts, y0, x1-ts, y0, COL_HIGHLIGHT);
1291 draw_line(dr, x1-ts, y0, x1-ts, y0+ts, COL_LOWLIGHT);
1292 draw_line(dr, x1-ts, y0+ts, x1, y0+ts, COL_HIGHLIGHT);
1293 draw_line(dr, x1, y0+ts, x1, y1-ts, COL_LOWLIGHT);
1294 draw_line(dr, x1, y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
1295 draw_line(dr, x1-ts, y1-ts, x1-ts, y1, COL_LOWLIGHT);
1296 draw_line(dr, x1-ts, y1, x0+ts, y1, COL_LOWLIGHT);
1297 draw_line(dr, x0+ts, y1, x0+ts, y1-ts, COL_HIGHLIGHT);
1298 draw_line(dr, x0+ts, y1-ts, x0, y1-ts, COL_LOWLIGHT);
1299 draw_line(dr, x0, y1-ts, x0, y0+ts, COL_HIGHLIGHT);
1300 draw_line(dr, x0, y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
1301 /* phew... */
1302
1303 draw_update(dr, 0, 0,
1304 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
1305 force = 1;
1306 ds->started = 1;
1307 }
1308
1309 if (isflash != ds->isflash) force = 1;
1310
1311 /* draw the arena */
1312 for (x = 0; x < state->w; x++) {
1313 for (y = 0; y < state->h; y++) {
1314 draw_arena_tile(dr, state, ds, x, y, force, isflash);
1315 }
1316 }
1317
1318 /* draw the lasers */
1319 for (i = 0; i < 2*(state->w+state->h); i++) {
1320 draw_laser_tile(dr, state, ds, ui, i, force);
1321 }
1322
1323 /* draw the 'finish' button */
1324 if (CAN_REVEAL(state)) {
1325 clip(dr, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
1326 draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
1327 COL_BUTTON, COL_BALL);
1328 unclip(dr);
1329 } else {
1330 draw_rect(dr, TODRAW(0), TODRAW(0),
1331 TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
1332 }
1333 draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
1334 ds->reveal = state->reveal;
1335 ds->flash_laserno = ui->flash_laserno;
1336 ds->isflash = isflash;
1337
1338 {
1339 char buf[256];
1340
1341 if (ds->reveal) {
1342 if (state->nwrong == 0 &&
1343 state->nmissed == 0 &&
1344 state->nright >= state->minballs)
1345 sprintf(buf, "CORRECT!");
1346 else
1347 sprintf(buf, "%d wrong and %d missed balls.",
1348 state->nwrong, state->nmissed);
1349 } else if (state->justwrong) {
1350 sprintf(buf, "Wrong! Guess again.");
1351 } else {
1352 if (state->nguesses > state->maxballs)
1353 sprintf(buf, "%d too many balls marked.",
1354 state->nguesses - state->maxballs);
1355 else if (state->nguesses <= state->maxballs &&
1356 state->nguesses >= state->minballs)
1357 sprintf(buf, "Click button to verify guesses.");
1358 else if (state->maxballs == state->minballs)
1359 sprintf(buf, "Balls marked: %d / %d",
1360 state->nguesses, state->minballs);
1361 else
1362 sprintf(buf, "Balls marked: %d / %d-%d.",
1363 state->nguesses, state->minballs, state->maxballs);
1364 }
1365 if (ui->errors) {
1366 sprintf(buf + strlen(buf), " (%d error%s)",
1367 ui->errors, ui->errors > 1 ? "s" : "");
1368 }
1369 status_bar(dr, buf);
1370 }
1371 }
1372
1373 static float game_anim_length(game_state *oldstate, game_state *newstate,
1374 int dir, game_ui *ui)
1375 {
1376 return 0.0F;
1377 }
1378
1379 static float game_flash_length(game_state *oldstate, game_state *newstate,
1380 int dir, game_ui *ui)
1381 {
1382 if (!oldstate->reveal && newstate->reveal)
1383 return 4.0F * FLASH_FRAME;
1384 else
1385 return 0.0F;
1386 }
1387
1388 static int game_timing_state(game_state *state, game_ui *ui)
1389 {
1390 return TRUE;
1391 }
1392
1393 static void game_print_size(game_params *params, float *x, float *y)
1394 {
1395 }
1396
1397 static void game_print(drawing *dr, game_state *state, int tilesize)
1398 {
1399 }
1400
1401 #ifdef COMBINED
1402 #define thegame blackbox
1403 #endif
1404
1405 const struct game thegame = {
1406 "Black Box", "games.blackbox", "blackbox",
1407 default_params,
1408 game_fetch_preset,
1409 decode_params,
1410 encode_params,
1411 free_params,
1412 dup_params,
1413 TRUE, game_configure, custom_params,
1414 validate_params,
1415 new_game_desc,
1416 validate_desc,
1417 new_game,
1418 dup_game,
1419 free_game,
1420 TRUE, solve_game,
1421 FALSE, game_can_format_as_text_now, game_text_format,
1422 new_ui,
1423 free_ui,
1424 encode_ui,
1425 decode_ui,
1426 game_changed_state,
1427 interpret_move,
1428 execute_move,
1429 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1430 game_colours,
1431 game_new_drawstate,
1432 game_free_drawstate,
1433 game_redraw,
1434 game_anim_length,
1435 game_flash_length,
1436 FALSE, FALSE, game_print_size, game_print,
1437 TRUE, /* wants_statusbar */
1438 FALSE, game_timing_state,
1439 REQUIRE_RBUTTON, /* flags */
1440 };
1441
1442 /* vim: set shiftwidth=4 tabstop=8: */