Fix another display/UI glitch which triggered if you filled a row
[sgt/puzzles] / guess.c
CommitLineData
74476385 1/*
2 * guess.c: Mastermind clone.
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 FLASH_FRAME 0.5F
15
16enum {
17 COL_BACKGROUND,
c6203e43 18 COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD,
74476385 19 COL_EMPTY, /* must be COL_1 - 1 */
20 COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10,
21 COL_CORRECTPLACE, COL_CORRECTCOLOUR,
22 NCOLOURS
23};
24
25struct game_params {
26 int ncolours, npegs, nguesses;
c6203e43 27 int allow_blank, allow_multiple;
74476385 28};
29
30#define FEEDBACK_CORRECTPLACE 1
31#define FEEDBACK_CORRECTCOLOUR 2
32
33typedef struct pegrow {
34 int npegs;
35 int *pegs; /* 0 is 'empty' */
36 int *feedback; /* may well be unused */
37} *pegrow;
38
39struct game_state {
40 game_params params;
41 pegrow *guesses; /* length params->nguesses */
42 pegrow solution;
43 int next_go; /* from 0 to nguesses-1;
44 if next_go == nguesses then they've lost. */
45 int solved;
46};
47
48static game_params *default_params(void)
49{
50 game_params *ret = snew(game_params);
51
52 /* AFAIK this is the canonical Mastermind ruleset. */
53 ret->ncolours = 6;
54 ret->npegs = 4;
55 ret->nguesses = 10;
56
c6203e43 57 ret->allow_blank = 0;
58 ret->allow_multiple = 1;
59
74476385 60 return ret;
61}
62
63static int game_fetch_preset(int i, char **name, game_params **params)
64{
65 return FALSE;
66}
67
68static void free_params(game_params *params)
69{
70 sfree(params);
71}
72
73static game_params *dup_params(game_params *params)
74{
75 game_params *ret = snew(game_params);
76 *ret = *params; /* structure copy */
77 return ret;
78}
79
80static void decode_params(game_params *params, char const *string)
81{
82 char const *p = string;
83 game_params *defs = default_params();
84
85 *params = *defs; free_params(defs);
86
87 while (*p) {
88 switch (*p++) {
89 case 'c':
90 params->ncolours = atoi(p);
91 while (*p && isdigit((unsigned char)*p)) p++;
92 break;
93
94 case 'p':
95 params->npegs = atoi(p);
96 while (*p && isdigit((unsigned char)*p)) p++;
97 break;
98
99 case 'g':
100 params->nguesses = atoi(p);
101 while (*p && isdigit((unsigned char)*p)) p++;
102 break;
103
c6203e43 104 case 'b':
105 params->allow_blank = 1;
106 break;
107
108 case 'B':
109 params->allow_blank = 0;
110 break;
111
112 case 'm':
113 params->allow_multiple = 1;
114 break;
115
116 case 'M':
117 params->allow_multiple = 0;
118 break;
119
74476385 120 default:
121 ;
122 }
123 }
124}
125
126static char *encode_params(game_params *params, int full)
127{
128 char data[256];
129
c6203e43 130 sprintf(data, "c%dp%dg%d%s%s",
131 params->ncolours, params->npegs, params->nguesses,
132 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
74476385 133
134 return dupstr(data);
135}
136
137static config_item *game_configure(game_params *params)
138{
139 config_item *ret;
140 char buf[80];
141
c6203e43 142 ret = snewn(6, config_item);
74476385 143
c6203e43 144 ret[0].name = "Colours";
74476385 145 ret[0].type = C_STRING;
146 sprintf(buf, "%d", params->ncolours);
147 ret[0].sval = dupstr(buf);
148 ret[0].ival = 0;
149
c6203e43 150 ret[1].name = "Pegs per guess";
74476385 151 ret[1].type = C_STRING;
152 sprintf(buf, "%d", params->npegs);
153 ret[1].sval = dupstr(buf);
154 ret[1].ival = 0;
155
c6203e43 156 ret[2].name = "Guesses";
74476385 157 ret[2].type = C_STRING;
158 sprintf(buf, "%d", params->nguesses);
159 ret[2].sval = dupstr(buf);
160 ret[2].ival = 0;
161
c6203e43 162 ret[3].name = "Allow blanks";
163 ret[3].type = C_BOOLEAN;
74476385 164 ret[3].sval = NULL;
c6203e43 165 ret[3].ival = params->allow_blank;
166
167 ret[4].name = "Allow duplicates";
168 ret[4].type = C_BOOLEAN;
169 ret[4].sval = NULL;
170 ret[4].ival = params->allow_multiple;
171
172 ret[5].name = NULL;
173 ret[5].type = C_END;
174 ret[5].sval = NULL;
175 ret[5].ival = 0;
74476385 176
177 return ret;
178}
179
180static game_params *custom_params(config_item *cfg)
181{
182 game_params *ret = snew(game_params);
183
184 ret->ncolours = atoi(cfg[0].sval);
185 ret->npegs = atoi(cfg[1].sval);
186 ret->nguesses = atoi(cfg[2].sval);
187
c6203e43 188 ret->allow_blank = cfg[3].ival;
189 ret->allow_multiple = cfg[4].ival;
190
74476385 191 return ret;
192}
193
194static char *validate_params(game_params *params)
195{
196 if (params->ncolours < 2 || params->npegs < 2)
197 return "Trivial solutions are uninteresting";
198 /* NB as well as the no. of colours we define, max(ncolours) must
199 * also fit in an unsigned char; see new_game_desc. */
200 if (params->ncolours > 10)
201 return "Too many colours";
202 if (params->nguesses < 1)
203 return "Must have at least one guess";
c6203e43 204 if (!params->allow_multiple && params->ncolours < params->npegs)
205 return "Disallowing multiple colours requires at least as many colours as pegs";
74476385 206 return NULL;
207}
208
209static pegrow new_pegrow(int npegs)
210{
211 pegrow pegs = snew(struct pegrow);
212
213 pegs->npegs = npegs;
214 pegs->pegs = snewn(pegs->npegs, int);
215 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
216 pegs->feedback = snewn(pegs->npegs, int);
217 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
218
219 return pegs;
220}
221
222static pegrow dup_pegrow(pegrow pegs)
223{
6b98d12c 224 pegrow newpegs = new_pegrow(pegs->npegs);
74476385 225
74476385 226 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
74476385 227 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
228
229 return newpegs;
230}
231
232static void invalidate_pegrow(pegrow pegs)
233{
234 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
235 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
236}
237
238static void free_pegrow(pegrow pegs)
239{
240 sfree(pegs->pegs);
241 sfree(pegs->feedback);
242 sfree(pegs);
243}
244
245static char *new_game_desc(game_params *params, random_state *rs,
246 game_aux_info **aux, int interactive)
247{
248 unsigned char *bmp = snewn(params->npegs, unsigned char);
249 char *ret;
c6203e43 250 int i, c;
251 pegrow colcount = new_pegrow(params->ncolours);
252
253 for (i = 0; i < params->npegs; i++) {
254newcol:
255 c = random_upto(rs, params->ncolours);
256 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
257 colcount->pegs[c]++;
258 bmp[i] = (unsigned char)(c+1);
259 }
74476385 260 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
261
262 ret = bin2hex(bmp, params->npegs);
263 sfree(bmp);
c6203e43 264 free_pegrow(colcount);
74476385 265 return ret;
266}
267
268static void game_free_aux_info(game_aux_info *aux)
269{
270 assert(!"Shouldn't happen");
271}
272
273static char *validate_desc(game_params *params, char *desc)
274{
c6203e43 275 unsigned char *bmp;
276 int i;
277
278 /* desc is just an (obfuscated) bitmap of the solution; check that
279 * it's the correct length and (when unobfuscated) contains only
280 * sensible colours. */
74476385 281 if (strlen(desc) != params->npegs * 2)
282 return "Game description is wrong length";
c6203e43 283 bmp = hex2bin(desc, params->npegs);
284 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
285 for (i = 0; i < params->npegs; i++) {
286 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
287 sfree(bmp);
288 return "Game description is corrupted";
289 }
290 }
291 sfree(bmp);
292
74476385 293 return NULL;
294}
295
296static game_state *new_game(midend_data *me, game_params *params, char *desc)
297{
298 game_state *state = snew(game_state);
299 unsigned char *bmp;
300 int i;
301
302 state->params = *params;
303 state->guesses = snewn(params->nguesses, pegrow);
304 for (i = 0; i < params->nguesses; i++)
305 state->guesses[i] = new_pegrow(params->npegs);
306 state->solution = new_pegrow(params->npegs);
307
308 bmp = hex2bin(desc, params->npegs);
309 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
310 for (i = 0; i < params->npegs; i++)
311 state->solution->pegs[i] = (int)bmp[i];
312 sfree(bmp);
313
314 state->next_go = state->solved = 0;
315
316 return state;
317}
318
319static game_state *dup_game(game_state *state)
320{
321 game_state *ret = snew(game_state);
322 int i;
323
324 *ret = *state;
6b98d12c 325
74476385 326 ret->guesses = snewn(state->params.nguesses, pegrow);
327 for (i = 0; i < state->params.nguesses; i++)
328 ret->guesses[i] = dup_pegrow(state->guesses[i]);
329 ret->solution = dup_pegrow(state->solution);
330
331 return ret;
332}
333
334static void free_game(game_state *state)
335{
336 int i;
337
338 free_pegrow(state->solution);
339 for (i = 0; i < state->params.nguesses; i++)
340 free_pegrow(state->guesses[i]);
341 sfree(state->guesses);
342
343 sfree(state);
344}
345
346static game_state *solve_game(game_state *state, game_state *currstate,
347 game_aux_info *aux, char **error)
348{
349 game_state *ret = dup_game(currstate);
350 ret->solved = 1;
351 return ret;
352}
353
354static char *game_text_format(game_state *state)
355{
356 return NULL;
357}
358
359struct game_ui {
360 pegrow curr_pegs; /* half-finished current move */
361 int *holds;
362 int colour_cur; /* position of up-down colour picker cursor */
363 int peg_cur; /* position of left-right peg picker cursor */
364 int display_cur, markable;
365
366 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
c6203e43 367 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
74476385 368};
369
370static game_ui *new_ui(game_state *state)
371{
372 game_ui *ui = snew(struct game_ui);
373 memset(ui, 0, sizeof(struct game_ui));
374 ui->curr_pegs = new_pegrow(state->params.npegs);
375 ui->holds = snewn(state->params.npegs, int);
376 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
c6203e43 377 ui->drag_opeg = -1;
74476385 378 return ui;
379}
380
381static void free_ui(game_ui *ui)
382{
383 free_pegrow(ui->curr_pegs);
384 sfree(ui->holds);
385 sfree(ui);
386}
387
388static void game_changed_state(game_ui *ui, game_state *oldstate,
389 game_state *newstate)
390{
391 int i;
392
393 /* just clear the row-in-progress when we have an undo/redo. */
394 for (i = 0; i < ui->curr_pegs->npegs; i++)
395 ui->curr_pegs->pegs[i] = 0;
75953cd4 396 ui->markable = FALSE;
74476385 397}
398
399#define PEGSZ (ds->pegsz)
400#define PEGOFF (ds->pegsz + ds->gapsz)
401#define HINTSZ (ds->hintsz)
402#define HINTOFF (ds->hintsz + ds->gapsz)
403
66732e7a 404#define CGAP (ds->gapsz / 2)
405
74476385 406#define PEGRAD (ds->pegrad)
407#define HINTRAD (ds->hintrad)
408
409#define COL_OX (ds->colx)
410#define COL_OY (ds->coly)
411#define COL_X(c) (COL_OX)
412#define COL_Y(c) (COL_OY + (c)*PEGOFF)
413#define COL_W PEGOFF
414#define COL_H (ds->colours->npegs*PEGOFF)
415
416#define GUESS_OX (ds->guessx)
417#define GUESS_OY (ds->guessy)
418#define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
419#define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
420#define GUESS_W (ds->solution->npegs*PEGOFF)
421#define GUESS_H (ds->nguesses*PEGOFF)
422
423#define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
424#define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
425#define HINT_X(g) HINT_OX
426#define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
427#define HINT_W (ds->hintw*HINTOFF)
428#define HINT_H GUESS_H
429
430#define SOLN_OX GUESS_OX
431#define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
432#define SOLN_W GUESS_W
433#define SOLN_H PEGOFF
434
435struct game_drawstate {
436 int nguesses;
437 pegrow *guesses; /* same size as state->guesses */
438 pegrow solution; /* only displayed if state->solved */
439 pegrow colours; /* length ncolours, not npegs */
440
74476385 441 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
442 int pegrad, hintrad; /* radius of peg, hint */
443 int border;
444 int colx, coly; /* origin of colours vertical bar */
445 int guessx, guessy; /* origin of guesses */
446 int solnx, solny; /* origin of solution */
447 int hintw; /* no. of hint tiles we're wide per row */
448 int w, h, started, solved;
449
74476385 450 int next_go;
451
452 blitter *blit_peg;
453 int drag_col, blit_ox, blit_oy;
454};
455
c6203e43 456static int is_markable(game_params *params, pegrow pegs)
74476385 457{
c6203e43 458 int i, nset = 0, nrequired, ret = 0;
459 pegrow colcount = new_pegrow(params->ncolours);
74476385 460
c6203e43 461 nrequired = params->allow_blank ? 1 : params->npegs;
462
463 for (i = 0; i < params->npegs; i++) {
6b98d12c 464 int c = pegs->pegs[i];
465 if (c > 0) {
466 colcount->pegs[c-1]++;
c6203e43 467 nset++;
468 }
469 }
470 if (nset < nrequired) goto done;
74476385 471
c6203e43 472 if (!params->allow_multiple) {
473 for (i = 0; i < params->ncolours; i++) {
474 if (colcount->pegs[i] > 1) goto done;
475 }
74476385 476 }
c6203e43 477 ret = 1;
478done:
479 free_pegrow(colcount);
480 return ret;
481}
482
483static void set_peg(game_params *params, game_ui *ui, int peg, int col)
484{
485 ui->curr_pegs->pegs[peg] = col;
486 ui->markable = is_markable(params, ui->curr_pegs);
74476385 487}
488
489static int mark_pegs(pegrow guess, pegrow solution, int ncols)
490{
491 int nc_place = 0, nc_colour = 0, i, j;
492
493 assert(guess && solution && (guess->npegs == solution->npegs));
494
495 for (i = 0; i < guess->npegs; i++) {
496 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
497 }
498
499 /* slight bit of cleverness: we have the following formula, from
500 * http://mathworld.wolfram.com/Mastermind.html that gives:
501 *
502 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
503 *
504 * I think this is due to Knuth.
505 */
506 for (i = 1; i <= ncols; i++) {
507 int n_guess = 0, n_solution = 0;
508 for (j = 0; j < guess->npegs; j++) {
509 if (guess->pegs[j] == i) n_guess++;
510 if (solution->pegs[j] == i) n_solution++;
511 }
512 nc_colour += min(n_guess, n_solution);
513 }
514 nc_colour -= nc_place;
515
516 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
517 guess->npegs, nc_place, nc_colour));
518 assert((nc_colour + nc_place) <= guess->npegs);
519
520 memset(guess->feedback, 0, guess->npegs*sizeof(int));
521 for (i = 0, j = 0; i < nc_place; i++)
522 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
523 for (i = 0; i < nc_colour; i++)
524 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
525
526 return nc_place;
527}
528
529static game_state *mark_move(game_state *from, game_ui *ui)
530{
531 int i, ncleared = 0, nc_place;
532 game_state *to = dup_game(from);
533
534 for (i = 0; i < to->solution->npegs; i++) {
535 to->guesses[from->next_go]->pegs[i] = ui->curr_pegs->pegs[i];
536 }
537 nc_place = mark_pegs(to->guesses[from->next_go], to->solution, to->params.ncolours);
538
539 if (nc_place == to->solution->npegs) {
540 to->solved = 1; /* win! */
541 } else {
542 to->next_go = from->next_go + 1;
543 if (to->next_go >= to->params.nguesses)
544 to->solved = 1; /* 'lose' so we show the pegs. */
545 }
546
547 for (i = 0; i < to->solution->npegs; i++) {
548 if (!ui->holds[i] || to->solved) {
549 ui->curr_pegs->pegs[i] = 0;
550 ncleared++;
551 }
552 if (to->solved) ui->holds[i] = 0;
553 }
c6203e43 554 ui->markable = is_markable(&from->params, ui->curr_pegs);
555 if (!ui->markable && ui->peg_cur == to->solution->npegs)
556 ui->peg_cur--;
74476385 557
558 return to;
559}
560
561static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
562 int x, int y, int button)
563{
564 int over_col = 0; /* one-indexed */
565 int over_guess = -1; /* zero-indexed */
e98cc9b7 566 int over_past_guess_y = -1; /* zero-indexed */
567 int over_past_guess_x = -1; /* zero-indexed */
74476385 568 int over_hint = 0; /* zero or one */
569 game_state *ret = NULL;
570
571 int guess_ox = GUESS_X(from->next_go, 0);
572 int guess_oy = GUESS_Y(from->next_go, 0);
573
574 if (from->solved) return NULL;
575
576 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
577 y >= COL_OY && y <= (COL_OY + COL_H)) {
578 over_col = ((y - COL_OY) / PEGOFF) + 1;
579 } else if (x >= guess_ox &&
580 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
581 if (x <= (guess_ox + GUESS_W)) {
582 over_guess = (x - guess_ox) / PEGOFF;
583 } else {
584 over_hint = 1;
585 }
e98cc9b7 586 } else if (x >= guess_ox &&
587 y >= GUESS_OY && y < guess_oy) {
588 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
589 over_past_guess_x = (x - guess_ox) / PEGOFF;
74476385 590 }
e98cc9b7 591 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
c6203e43 592 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
593 over_past_guess_x, over_past_guess_y));
74476385 594
595 assert(ds->blit_peg);
596
597 /* mouse input */
598 if (button == LEFT_BUTTON) {
599 if (over_col > 0) {
600 ui->drag_col = over_col;
c6203e43 601 ui->drag_opeg = -1;
74476385 602 debug(("Start dragging from colours"));
603 } else if (over_guess > -1) {
604 int col = ui->curr_pegs->pegs[over_guess];
605 if (col) {
606 ui->drag_col = col;
c6203e43 607 ui->drag_opeg = over_guess;
74476385 608 debug(("Start dragging from a guess"));
609 }
e98cc9b7 610 } else if (over_past_guess_y > -1) {
611 int col =
612 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
613 if (col) {
614 ui->drag_col = col;
c6203e43 615 ui->drag_opeg = -1;
e98cc9b7 616 debug(("Start dragging from a past guess"));
617 }
74476385 618 }
619 if (ui->drag_col) {
620 ui->drag_x = x;
621 ui->drag_y = y;
622 debug(("Start dragging, col = %d, (%d,%d)",
623 ui->drag_col, ui->drag_x, ui->drag_y));
624 ret = from;
625 }
626 } else if (button == LEFT_DRAG && ui->drag_col) {
627 ui->drag_x = x;
628 ui->drag_y = y;
629 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
630 ret = from;
631 } else if (button == LEFT_RELEASE && ui->drag_col) {
632 if (over_guess > -1) {
633 debug(("Dropping colour %d onto guess peg %d",
634 ui->drag_col, over_guess));
c6203e43 635 set_peg(&from->params, ui, over_guess, ui->drag_col);
636 } else {
637 if (ui->drag_opeg > -1) {
638 debug(("Removing colour %d from peg %d",
639 ui->drag_col, ui->drag_opeg));
640 set_peg(&from->params, ui, ui->drag_opeg, 0);
641 }
74476385 642 }
643 ui->drag_col = 0;
c6203e43 644 ui->drag_opeg = -1;
eb6acfe7 645 ui->display_cur = 0;
74476385 646 debug(("Stop dragging."));
647 ret = from;
648 } else if (button == RIGHT_BUTTON) {
649 if (over_guess > -1) {
650 /* we use ths feedback in the game_ui to signify
651 * 'carry this peg to the next guess as well'. */
652 ui->holds[over_guess] = 1 - ui->holds[over_guess];
653 ret = from;
654 }
655 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
656 /* NB this won't trigger if on the end of a drag; that's on
657 * purpose, in case you drop by mistake... */
658 ret = mark_move(from, ui);
659 }
660
661 /* keyboard input */
662 if (button == CURSOR_UP || button == CURSOR_DOWN) {
663 ui->display_cur = 1;
664 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
665 ui->colour_cur++;
666 if (button == CURSOR_UP && ui->colour_cur > 0)
667 ui->colour_cur--;
668 ret = from;
669 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
670 int maxcur = from->params.npegs;
671 if (ui->markable) maxcur++;
672
673 ui->display_cur = 1;
674 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
675 ui->peg_cur++;
676 if (button == CURSOR_LEFT && ui->peg_cur > 0)
677 ui->peg_cur--;
678 ret = from;
679 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
680 button == '\n') {
681 if (ui->peg_cur == from->params.npegs) {
682 ret = mark_move(from, ui);
683 } else {
c6203e43 684 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
74476385 685 ret = from;
686 }
badb093d 687 } else if (button == 'H' || button == 'h') {
688 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
689 ret = from;
74476385 690 }
691 return ret;
692}
693
694/* ----------------------------------------------------------------------
695 * Drawing routines.
696 */
697
698#define PEG_PREFER_SZ 32
699
700/* next three are multipliers for pegsz. It will look much nicer if
701 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
702#define PEG_GAP 0.10
703#define PEG_HINT 0.35
704
705#define BORDER 0.5
706
707static void game_size(game_params *params, game_drawstate *ds,
708 int *x, int *y, int expand)
709{
710 double hmul, vmul_c, vmul_g, vmul, szx, szy;
711 int sz, colh, guessh;
712
713 hmul = BORDER * 2.0 + /* border */
714 1.0 * 2.0 + /* vertical colour bar */
715 1.0 * params->npegs + /* guess pegs */
716 PEG_GAP * params->npegs + /* guess gaps */
717 PEG_HINT * ds->hintw + /* hint pegs */
718 PEG_GAP * (ds->hintw - 1); /* hint gaps */
719
720 vmul_c = BORDER * 2.0 + /* border */
721 1.0 * params->ncolours + /* colour pegs */
722 PEG_GAP * (params->ncolours - 1); /* colour gaps */
723
724 vmul_g = BORDER * 2.0 + /* border */
725 1.0 * (params->nguesses + 1) + /* guesses plus solution */
726 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
727
728 vmul = max(vmul_c, vmul_g);
729
730 szx = *x / hmul;
731 szy = *y / vmul;
732 sz = max(min((int)szx, (int)szy), 1);
733 if (expand)
734 ds->pegsz = sz;
735 else
736 ds->pegsz = min(sz, PEG_PREFER_SZ);
737
738 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
739 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
740 ds->border = (int)((double)ds->pegsz * BORDER);
741
742 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
743 ds->hintrad = (ds->hintsz-1)/2;
744
813ca264 745 *x = (int)ceil((double)ds->pegsz * hmul);
746 *y = (int)ceil((double)ds->pegsz * vmul);
74476385 747 ds->w = *x; ds->h = *y;
748
749 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
750 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
751 guessh += ds->gapsz + ds->pegsz; /* solution */
752
753 ds->colx = ds->border;
754 ds->coly = (*y - colh) / 2;
755
756 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
757 ds->guessy = (*y - guessh) / 2;
758 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
759
760 if (ds->pegsz > 0) {
761 if (ds->blit_peg) blitter_free(ds->blit_peg);
762 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
763 }
764}
765
766static float *game_colours(frontend *fe, game_state *state, int *ncolours)
767{
768 float *ret = snewn(3 * NCOLOURS, float);
769
770 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
771
e98cc9b7 772 /* red */
773 ret[COL_1 * 3 + 0] = 1.0F;
74476385 774 ret[COL_1 * 3 + 1] = 0.0F;
e98cc9b7 775 ret[COL_1 * 3 + 2] = 0.0F;
74476385 776
e98cc9b7 777 /* yellow (toned down a bit due to pale grey background) */
778 ret[COL_2 * 3 + 0] = 0.7F;
779 ret[COL_2 * 3 + 1] = 0.7F;
74476385 780 ret[COL_2 * 3 + 2] = 0.0F;
781
e98cc9b7 782 /* green (also toned down) */
783 ret[COL_3 * 3 + 0] = 0.0F;
784 ret[COL_3 * 3 + 1] = 0.5F;
74476385 785 ret[COL_3 * 3 + 2] = 0.0F;
786
e98cc9b7 787 /* blue */
788 ret[COL_4 * 3 + 0] = 0.0F;
789 ret[COL_4 * 3 + 1] = 0.0F;
790 ret[COL_4 * 3 + 2] = 1.0F;
74476385 791
e98cc9b7 792 /* orange */
74476385 793 ret[COL_5 * 3 + 0] = 1.0F;
e98cc9b7 794 ret[COL_5 * 3 + 1] = 0.5F;
795 ret[COL_5 * 3 + 2] = 0.0F;
796
797 /* purple */
798 ret[COL_6 * 3 + 0] = 0.5F;
799 ret[COL_6 * 3 + 1] = 0.0F;
800 ret[COL_6 * 3 + 2] = 0.7F;
801
802 /* brown */
803 ret[COL_7 * 3 + 0] = 0.4F;
804 ret[COL_7 * 3 + 1] = 0.2F;
805 ret[COL_7 * 3 + 2] = 0.2F;
806
807 /* light blue */
808 ret[COL_8 * 3 + 0] = 0.4F;
809 ret[COL_8 * 3 + 1] = 0.7F;
810 ret[COL_8 * 3 + 2] = 1.0F;
811
812 /* light green */
813 ret[COL_9 * 3 + 0] = 0.5F;
814 ret[COL_9 * 3 + 1] = 0.8F;
74476385 815 ret[COL_9 * 3 + 2] = 0.5F;
816
e98cc9b7 817 /* pink */
74476385 818 ret[COL_10 * 3 + 0] = 1.0F;
e98cc9b7 819 ret[COL_10 * 3 + 1] = 0.6F;
74476385 820 ret[COL_10 * 3 + 2] = 1.0F;
821
822 ret[COL_FRAME * 3 + 0] = 0.0F;
823 ret[COL_FRAME * 3 + 1] = 0.0F;
824 ret[COL_FRAME * 3 + 2] = 0.0F;
825
c6203e43 826 ret[COL_CURSOR * 3 + 0] = 0.0F;
827 ret[COL_CURSOR * 3 + 1] = 0.0F;
828 ret[COL_CURSOR * 3 + 2] = 0.0F;
74476385 829
830 ret[COL_FLASH * 3 + 0] = 0.5F;
831 ret[COL_FLASH * 3 + 1] = 1.0F;
832 ret[COL_FLASH * 3 + 2] = 1.0F;
833
834 ret[COL_HOLD * 3 + 0] = 1.0F;
835 ret[COL_HOLD * 3 + 1] = 0.5F;
836 ret[COL_HOLD * 3 + 2] = 0.5F;
837
838 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
839 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
840 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
841
e98cc9b7 842 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
74476385 843 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
844 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
845
846 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
847 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
848 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
849
850 *ncolours = NCOLOURS;
851 return ret;
852}
853
854static game_drawstate *game_new_drawstate(game_state *state)
855{
856 struct game_drawstate *ds = snew(struct game_drawstate);
857 int i;
858
859 memset(ds, 0, sizeof(struct game_drawstate));
860
861 ds->guesses = snewn(state->params.nguesses, pegrow);
862 ds->nguesses = state->params.nguesses;
863 for (i = 0; i < state->params.nguesses; i++) {
864 ds->guesses[i] = new_pegrow(state->params.npegs);
865 invalidate_pegrow(ds->guesses[i]);
866 }
867 ds->solution = new_pegrow(state->params.npegs);
868 invalidate_pegrow(ds->solution);
869 ds->colours = new_pegrow(state->params.ncolours);
870 invalidate_pegrow(ds->colours);
871
872 ds->hintw = (state->params.npegs+1)/2; /* must round up */
873
74476385 874 ds->blit_peg = NULL;
875
876 return ds;
877}
878
879static void game_free_drawstate(game_drawstate *ds)
880{
881 int i;
882
883 if (ds->blit_peg) blitter_free(ds->blit_peg);
884 free_pegrow(ds->colours);
885 free_pegrow(ds->solution);
886 for (i = 0; i < ds->nguesses; i++)
887 free_pegrow(ds->guesses[i]);
74476385 888 sfree(ds->guesses);
889 sfree(ds);
890}
891
385f402f 892static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
893 int moving, int col)
74476385 894{
385f402f 895 /*
896 * Some platforms antialias circles, which means we shouldn't
897 * overwrite a circle of one colour with a circle of another
898 * colour without erasing the background first. However, if the
899 * peg is the one being dragged, we don't erase the background
900 * because we _want_ it to alpha-blend nicely into whatever's
901 * behind it.
902 */
903 if (!moving)
66732e7a 904 draw_rect(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
905 COL_BACKGROUND);
28094c81 906 if (PEGRAD > 0) {
74476385 907 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
28094c81 908 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
909 } else
74476385 910 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
66732e7a 911 draw_update(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
912}
913
914static void draw_cursor(frontend *fe, game_drawstate *ds, int x, int y)
915{
916 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, 0, COL_CURSOR);
917
918 draw_update(fe, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
74476385 919}
920
921static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
66732e7a 922 pegrow src, int *holds, int cur_col, int force)
74476385 923{
924 pegrow dest;
925 int rowx, rowy, i, scol;
926
927 if (guess == -1) {
928 dest = ds->solution;
929 rowx = SOLN_OX;
930 rowy = SOLN_OY;
931 } else {
932 dest = ds->guesses[guess];
933 rowx = GUESS_X(guess,0);
934 rowy = GUESS_Y(guess,0);
935 }
936 if (src) assert(src->npegs == dest->npegs);
937
938 for (i = 0; i < dest->npegs; i++) {
939 scol = src ? src->pegs[i] : 0;
66732e7a 940 if (i == cur_col)
941 scol |= 0x1000;
942 if (holds && holds[i])
943 scol |= 0x2000;
944 if ((dest->pegs[i] != scol) || force) {
945 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000);
946 /*
947 * Hold marker.
948 */
949 draw_rect(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
950 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
951 draw_update(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
952 PEGSZ, 2);
953 if (scol & 0x1000)
954 draw_cursor(fe, ds, rowx + PEGOFF * i, rowy);
955 }
74476385 956 dest->pegs[i] = scol;
957 }
958}
959
960static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
75953cd4 961 pegrow src, int force, int cursor, int markable)
74476385 962{
963 pegrow dest = ds->guesses[guess];
964 int rowx, rowy, i, scol, col, hintlen;
66732e7a 965 int need_redraw;
75953cd4 966 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
74476385 967
968 if (src) assert(src->npegs == dest->npegs);
969
970 hintlen = (dest->npegs + 1)/2;
971
66732e7a 972 /*
973 * Because of the possible presence of the cursor around this
974 * entire section, we redraw all or none of it but never part.
975 */
976 need_redraw = FALSE;
977
74476385 978 for (i = 0; i < dest->npegs; i++) {
979 scol = src ? src->feedback[i] : 0;
66732e7a 980 if (i == 0 && cursor)
981 scol |= 0x1000;
75953cd4 982 if (i == 0 && markable)
983 scol |= 0x2000;
74476385 984 if ((scol != dest->feedback[i]) || force) {
66732e7a 985 need_redraw = TRUE;
986 }
987 dest->feedback[i] = scol;
988 }
989
990 if (need_redraw) {
991 /* erase a large background rectangle */
992 draw_rect(fe, GUESS_X(guess, dest->npegs)-CGAP,
993 GUESS_Y(guess, dest->npegs)-CGAP,
994 PEGSZ+CGAP*2, PEGSZ+CGAP*2, COL_BACKGROUND);
995
996 for (i = 0; i < dest->npegs; i++) {
997 scol = src ? src->feedback[i] : 0;
998 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
999 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1000 emptycol);
1001
74476385 1002 rowx = HINT_X(guess);
1003 rowy = HINT_Y(guess);
1004 if (i < hintlen) {
1005 rowx += HINTOFF * i;
1006 } else {
1007 rowx += HINTOFF * (i - hintlen);
1008 rowy += HINTOFF;
1009 }
28094c81 1010 if (HINTRAD > 0) {
74476385 1011 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
28094c81 1012 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
1013 } else {
74476385 1014 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
28094c81 1015 }
74476385 1016 }
66732e7a 1017 if (cursor)
1018 draw_cursor(fe, ds, GUESS_X(guess, dest->npegs),
1019 GUESS_Y(guess, dest->npegs));
74476385 1020
66732e7a 1021 draw_update(fe, GUESS_X(guess, dest->npegs)-CGAP,
1022 GUESS_Y(guess, dest->npegs)-CGAP,
1023 PEGSZ+CGAP*2, PEGSZ+CGAP*2);
74476385 1024 }
1025}
1026
1027static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
1028{
1029 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1030
1031 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1032 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1033}
1034
74476385 1035static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1036 game_state *state, int dir, game_ui *ui,
1037 float animtime, float flashtime)
1038{
66732e7a 1039 int i, new_move, last_go;
74476385 1040
1041 new_move = (state->next_go != ds->next_go) || !ds->started;
1042 last_go = (state->next_go == state->params.nguesses-1);
1043
1044 if (!ds->started) {
1045 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1046 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1047 draw_update(fe, 0, 0, ds->w, ds->h);
1048 }
1049
1050 if (ds->drag_col != 0) {
1051 debug(("Loading from blitter."));
1052 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1053 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1054 }
1055
1056 /* draw the colours */
1057 for (i = 0; i < state->params.ncolours; i++) {
66732e7a 1058 int val = i+1;
1059 if (ui->display_cur && ui->colour_cur == i)
1060 val |= 0x1000;
1061 if (ds->colours->pegs[i] != val) {
385f402f 1062 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
66732e7a 1063 if (val & 0x1000)
1064 draw_cursor(fe, ds, COL_X(i), COL_Y(i));
1065 ds->colours->pegs[i] = val;
74476385 1066 }
1067 }
1068
1069 /* draw the guesses (so far) and the hints */
1070 for (i = 0; i < state->params.nguesses; i++) {
1071 if (state->next_go > i || state->solved) {
1072 /* this info is stored in the game_state already */
66732e7a 1073 guess_redraw(fe, ds, i, state->guesses[i], NULL, -1, 0);
74476385 1074 hint_redraw(fe, ds, i, state->guesses[i],
75953cd4 1075 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
74476385 1076 } else if (state->next_go == i) {
1077 /* this is the one we're on; the (incomplete) guess is
1078 * stored in the game_ui. */
66732e7a 1079 guess_redraw(fe, ds, i, ui->curr_pegs,
1080 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0);
1081 hint_redraw(fe, ds, i, NULL, 1,
1082 ui->display_cur && ui->peg_cur == state->params.npegs,
75953cd4 1083 ui->markable);
74476385 1084 } else {
1085 /* we've not got here yet; it's blank. */
66732e7a 1086 guess_redraw(fe, ds, i, NULL, NULL, -1, 0);
75953cd4 1087 hint_redraw(fe, ds, i, NULL, 0, FALSE, FALSE);
74476385 1088 }
1089 }
1090
74476385 1091 /* draw the 'current move' and 'able to mark' sign. */
1092 if (new_move)
1093 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1094 if (!state->solved)
1095 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1096
1097 /* draw the solution (or the big rectangle) */
1098 if ((state->solved != ds->solved) || !ds->started) {
1099 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1100 state->solved ? COL_BACKGROUND : COL_EMPTY);
1101 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1102 }
1103 if (state->solved)
66732e7a 1104 guess_redraw(fe, ds, -1, state->solution, NULL, -1, !ds->solved);
74476385 1105 ds->solved = state->solved;
1106
74476385 1107 ds->next_go = state->next_go;
1108
1109 /* if ui->drag_col != 0, save the screen to the blitter,
1110 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1111 if (ui->drag_col != 0) {
1112 int ox = ui->drag_x - (PEGSZ/2);
1113 int oy = ui->drag_y - (PEGSZ/2);
1114 debug(("Saving to blitter at (%d,%d)", ox, oy));
1115 blitter_save(fe, ds->blit_peg, ox, oy);
385f402f 1116 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
74476385 1117
1118 ds->blit_ox = ox; ds->blit_oy = oy;
1119 }
1120 ds->drag_col = ui->drag_col;
1121
1122 ds->started = 1;
1123}
1124
1125static float game_anim_length(game_state *oldstate, game_state *newstate,
1126 int dir, game_ui *ui)
1127{
1128 return 0.0F;
1129}
1130
1131static float game_flash_length(game_state *oldstate, game_state *newstate,
1132 int dir, game_ui *ui)
1133{
1134 return 0.0F;
1135}
1136
1137static int game_wants_statusbar(void)
1138{
1139 return FALSE;
1140}
1141
1142static int game_timing_state(game_state *state)
1143{
1144 return TRUE;
1145}
1146
1147#ifdef COMBINED
1148#define thegame guess
1149#endif
1150
1151const struct game thegame = {
61072f7b 1152 "Guess", "games.guess",
74476385 1153 default_params,
1154 game_fetch_preset,
1155 decode_params,
1156 encode_params,
1157 free_params,
1158 dup_params,
1159 TRUE, game_configure, custom_params,
1160 validate_params,
1161 new_game_desc,
1162 game_free_aux_info,
1163 validate_desc,
1164 new_game,
1165 dup_game,
1166 free_game,
1167 TRUE, solve_game,
1168 FALSE, game_text_format,
1169 new_ui,
1170 free_ui,
1171 game_changed_state,
1172 make_move,
1173 game_size,
1174 game_colours,
1175 game_new_drawstate,
1176 game_free_drawstate,
1177 game_redraw,
1178 game_anim_length,
1179 game_flash_length,
1180 game_wants_statusbar,
1181 FALSE, game_timing_state,
1182 0, /* mouse_priorities */
1183};
1184
1185/* vim: set shiftwidth=4 tabstop=8: */