Qt frontend build: fix use of XFLAGS
[sgt-puzzles/ydirson.git] / guess.c
bloba9fc1346e87daf55de1e2b8cefe8121b01b0c856
1 /*
2 * guess.c: Mastermind clone.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <ctype.h>
10 #include <math.h>
12 #include "puzzles.h"
14 #define FLASH_FRAME 0.5F
16 enum {
17 COL_BACKGROUND,
18 COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD,
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
25 struct game_params {
26 int ncolours, npegs, nguesses;
27 int allow_blank, allow_multiple;
30 #define FEEDBACK_CORRECTPLACE 1
31 #define FEEDBACK_CORRECTCOLOUR 2
33 typedef struct pegrow {
34 int npegs;
35 int *pegs; /* 0 is 'empty' */
36 int *feedback; /* may well be unused */
37 } *pegrow;
39 struct game_state {
40 game_params params;
41 pegrow *guesses; /* length params->nguesses */
42 int *holds;
43 pegrow solution;
44 int next_go; /* from 0 to nguesses-1;
45 if next_go == nguesses then they've lost. */
46 int solved; /* +1 = win, -1 = lose, 0 = still playing */
49 static game_params *default_params(void)
51 game_params *ret = snew(game_params);
53 /* AFAIK this is the canonical Mastermind ruleset. */
54 ret->ncolours = 6;
55 ret->npegs = 4;
56 ret->nguesses = 10;
58 ret->allow_blank = 0;
59 ret->allow_multiple = 1;
61 return ret;
64 static void free_params(game_params *params)
66 sfree(params);
69 static game_params *dup_params(game_params *params)
71 game_params *ret = snew(game_params);
72 *ret = *params; /* structure copy */
73 return ret;
76 static const struct {
77 char *name;
78 game_params params;
79 } guess_presets[] = {
80 {"Standard", {6, 4, 10, FALSE, TRUE}},
81 {"Super", {8, 5, 12, FALSE, TRUE}},
85 static int game_fetch_preset(int i, char **name, game_params **params)
87 if (i < 0 || i >= lenof(guess_presets))
88 return FALSE;
90 *name = dupstr(guess_presets[i].name);
92 * get round annoying const issues
95 game_params tmp = guess_presets[i].params;
96 *params = dup_params(&tmp);
99 return TRUE;
102 static void decode_params(game_params *params, char const *string)
104 char const *p = string;
105 game_params *defs = default_params();
107 *params = *defs; free_params(defs);
109 while (*p) {
110 switch (*p++) {
111 case 'c':
112 params->ncolours = atoi(p);
113 while (*p && isdigit((unsigned char)*p)) p++;
114 break;
116 case 'p':
117 params->npegs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
119 break;
121 case 'g':
122 params->nguesses = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
124 break;
126 case 'b':
127 params->allow_blank = 1;
128 break;
130 case 'B':
131 params->allow_blank = 0;
132 break;
134 case 'm':
135 params->allow_multiple = 1;
136 break;
138 case 'M':
139 params->allow_multiple = 0;
140 break;
142 default:
148 static char *encode_params(game_params *params, int full)
150 char data[256];
152 sprintf(data, "c%dp%dg%d%s%s",
153 params->ncolours, params->npegs, params->nguesses,
154 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
156 return dupstr(data);
159 static config_item *game_configure(game_params *params)
161 config_item *ret;
162 char buf[80];
164 ret = snewn(6, config_item);
166 ret[0].name = "Colours";
167 ret[0].type = C_STRING;
168 sprintf(buf, "%d", params->ncolours);
169 ret[0].sval = dupstr(buf);
170 ret[0].ival = 0;
172 ret[1].name = "Pegs per guess";
173 ret[1].type = C_STRING;
174 sprintf(buf, "%d", params->npegs);
175 ret[1].sval = dupstr(buf);
176 ret[1].ival = 0;
178 ret[2].name = "Guesses";
179 ret[2].type = C_STRING;
180 sprintf(buf, "%d", params->nguesses);
181 ret[2].sval = dupstr(buf);
182 ret[2].ival = 0;
184 ret[3].name = "Allow blanks";
185 ret[3].type = C_BOOLEAN;
186 ret[3].sval = NULL;
187 ret[3].ival = params->allow_blank;
189 ret[4].name = "Allow duplicates";
190 ret[4].type = C_BOOLEAN;
191 ret[4].sval = NULL;
192 ret[4].ival = params->allow_multiple;
194 ret[5].name = NULL;
195 ret[5].type = C_END;
196 ret[5].sval = NULL;
197 ret[5].ival = 0;
199 return ret;
202 static game_params *custom_params(config_item *cfg)
204 game_params *ret = snew(game_params);
206 ret->ncolours = atoi(cfg[0].sval);
207 ret->npegs = atoi(cfg[1].sval);
208 ret->nguesses = atoi(cfg[2].sval);
210 ret->allow_blank = cfg[3].ival;
211 ret->allow_multiple = cfg[4].ival;
213 return ret;
216 static char *validate_params(game_params *params, int full)
218 if (params->ncolours < 2 || params->npegs < 2)
219 return "Trivial solutions are uninteresting";
220 /* NB as well as the no. of colours we define, max(ncolours) must
221 * also fit in an unsigned char; see new_game_desc. */
222 if (params->ncolours > 10)
223 return "Too many colours";
224 if (params->nguesses < 1)
225 return "Must have at least one guess";
226 if (!params->allow_multiple && params->ncolours < params->npegs)
227 return "Disallowing multiple colours requires at least as many colours as pegs";
228 return NULL;
231 static pegrow new_pegrow(int npegs)
233 pegrow pegs = snew(struct pegrow);
235 pegs->npegs = npegs;
236 pegs->pegs = snewn(pegs->npegs, int);
237 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
238 pegs->feedback = snewn(pegs->npegs, int);
239 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
241 return pegs;
244 static pegrow dup_pegrow(pegrow pegs)
246 pegrow newpegs = new_pegrow(pegs->npegs);
248 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
249 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
251 return newpegs;
254 static void invalidate_pegrow(pegrow pegs)
256 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
257 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
260 static void free_pegrow(pegrow pegs)
262 sfree(pegs->pegs);
263 sfree(pegs->feedback);
264 sfree(pegs);
267 static char *new_game_desc(game_params *params, random_state *rs,
268 char **aux, int interactive)
270 unsigned char *bmp = snewn(params->npegs, unsigned char);
271 char *ret;
272 int i, c;
273 pegrow colcount = new_pegrow(params->ncolours);
275 for (i = 0; i < params->npegs; i++) {
276 newcol:
277 c = random_upto(rs, params->ncolours);
278 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
279 colcount->pegs[c]++;
280 bmp[i] = (unsigned char)(c+1);
282 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
284 ret = bin2hex(bmp, params->npegs);
285 sfree(bmp);
286 free_pegrow(colcount);
287 return ret;
290 static char *validate_desc(game_params *params, char *desc)
292 unsigned char *bmp;
293 int i;
295 /* desc is just an (obfuscated) bitmap of the solution; check that
296 * it's the correct length and (when unobfuscated) contains only
297 * sensible colours. */
298 if (strlen(desc) != params->npegs * 2)
299 return "Game description is wrong length";
300 bmp = hex2bin(desc, params->npegs);
301 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
302 for (i = 0; i < params->npegs; i++) {
303 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
304 sfree(bmp);
305 return "Game description is corrupted";
308 sfree(bmp);
310 return NULL;
313 static game_state *new_game(midend *me, game_params *params, char *desc)
315 game_state *state = snew(game_state);
316 unsigned char *bmp;
317 int i;
319 state->params = *params;
320 state->guesses = snewn(params->nguesses, pegrow);
321 for (i = 0; i < params->nguesses; i++)
322 state->guesses[i] = new_pegrow(params->npegs);
323 state->holds = snewn(params->npegs, int);
324 state->solution = new_pegrow(params->npegs);
326 bmp = hex2bin(desc, params->npegs);
327 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
328 for (i = 0; i < params->npegs; i++)
329 state->solution->pegs[i] = (int)bmp[i];
330 sfree(bmp);
332 memset(state->holds, 0, sizeof(int) * params->npegs);
333 state->next_go = state->solved = 0;
335 return state;
338 static game_state *dup_game(game_state *state)
340 game_state *ret = snew(game_state);
341 int i;
343 *ret = *state;
345 ret->guesses = snewn(state->params.nguesses, pegrow);
346 for (i = 0; i < state->params.nguesses; i++)
347 ret->guesses[i] = dup_pegrow(state->guesses[i]);
348 ret->holds = snewn(state->params.npegs, int);
349 memcpy(ret->holds, state->holds, sizeof(int) * state->params.npegs);
350 ret->solution = dup_pegrow(state->solution);
352 return ret;
355 static void free_game(game_state *state)
357 int i;
359 free_pegrow(state->solution);
360 for (i = 0; i < state->params.nguesses; i++)
361 free_pegrow(state->guesses[i]);
362 sfree(state->holds);
363 sfree(state->guesses);
365 sfree(state);
368 static char *solve_game(game_state *state, game_state *currstate,
369 char *aux, char **error)
371 return dupstr("S");
374 static int game_can_format_as_text_now(game_params *params)
376 return TRUE;
379 static char *game_text_format(game_state *state)
381 return NULL;
384 static int is_markable(game_params *params, pegrow pegs)
386 int i, nset = 0, nrequired, ret = 0;
387 pegrow colcount = new_pegrow(params->ncolours);
389 nrequired = params->allow_blank ? 1 : params->npegs;
391 for (i = 0; i < params->npegs; i++) {
392 int c = pegs->pegs[i];
393 if (c > 0) {
394 colcount->pegs[c-1]++;
395 nset++;
398 if (nset < nrequired) goto done;
400 if (!params->allow_multiple) {
401 for (i = 0; i < params->ncolours; i++) {
402 if (colcount->pegs[i] > 1) goto done;
405 ret = 1;
406 done:
407 free_pegrow(colcount);
408 return ret;
411 struct game_ui {
412 game_params params;
413 pegrow curr_pegs; /* half-finished current move */
414 int *holds;
415 int colour_cur; /* position of up-down colour picker cursor */
416 int peg_cur; /* position of left-right peg picker cursor */
417 int display_cur, markable;
419 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
420 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
422 int show_labels; /* label the colours with letters */
425 static game_ui *new_ui(game_state *state)
427 game_ui *ui = snew(game_ui);
428 memset(ui, 0, sizeof(game_ui));
429 ui->params = state->params; /* structure copy */
430 ui->curr_pegs = new_pegrow(state->params.npegs);
431 ui->holds = snewn(state->params.npegs, int);
432 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
433 ui->drag_opeg = -1;
434 return ui;
437 static void free_ui(game_ui *ui)
439 free_pegrow(ui->curr_pegs);
440 sfree(ui->holds);
441 sfree(ui);
444 static char *encode_ui(game_ui *ui)
446 char *ret, *p, *sep;
447 int i;
450 * For this game it's worth storing the contents of the current
451 * guess, and the current set of holds.
453 ret = snewn(40 * ui->curr_pegs->npegs, char);
454 p = ret;
455 sep = "";
456 for (i = 0; i < ui->curr_pegs->npegs; i++) {
457 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
458 ui->holds[i] ? "_" : "");
459 sep = ",";
461 *p++ = '\0';
462 assert(p - ret < 40 * ui->curr_pegs->npegs);
463 return sresize(ret, p - ret, char);
466 static void decode_ui(game_ui *ui, char *encoding)
468 int i;
469 char *p = encoding;
470 for (i = 0; i < ui->curr_pegs->npegs; i++) {
471 ui->curr_pegs->pegs[i] = atoi(p);
472 while (*p && isdigit((unsigned char)*p)) p++;
473 if (*p == '_') {
474 /* NB: old versions didn't store holds */
475 ui->holds[i] = 1;
476 p++;
477 } else
478 ui->holds[i] = 0;
479 if (*p == ',') p++;
481 ui->markable = is_markable(&ui->params, ui->curr_pegs);
484 static void game_changed_state(game_ui *ui, game_state *oldstate,
485 game_state *newstate)
487 int i;
489 /* Implement holds, clear other pegs.
490 * This does something that is arguably the Right Thing even
491 * for undo. */
492 for (i = 0; i < newstate->solution->npegs; i++) {
493 if (newstate->solved)
494 ui->holds[i] = 0;
495 else
496 ui->holds[i] = newstate->holds[i];
497 if (newstate->solved || (newstate->next_go == 0) || !ui->holds[i]) {
498 ui->curr_pegs->pegs[i] = 0;
499 } else
500 ui->curr_pegs->pegs[i] =
501 newstate->guesses[newstate->next_go-1]->pegs[i];
503 ui->markable = is_markable(&newstate->params, ui->curr_pegs);
504 /* Clean up cursor position */
505 if (!ui->markable && ui->peg_cur == newstate->solution->npegs)
506 ui->peg_cur--;
509 #define PEGSZ (ds->pegsz)
510 #define PEGOFF (ds->pegsz + ds->gapsz)
511 #define HINTSZ (ds->hintsz)
512 #define HINTOFF (ds->hintsz + ds->gapsz)
514 #define GAP (ds->gapsz)
515 #define CGAP (ds->gapsz / 2)
517 #define PEGRAD (ds->pegrad)
518 #define HINTRAD (ds->hintrad)
520 #define COL_OX (ds->colx)
521 #define COL_OY (ds->coly)
522 #define COL_X(c) (COL_OX)
523 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
524 #define COL_W PEGOFF
525 #define COL_H (ds->colours->npegs*PEGOFF)
527 #define GUESS_OX (ds->guessx)
528 #define GUESS_OY (ds->guessy)
529 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
530 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
531 #define GUESS_W (ds->solution->npegs*PEGOFF)
532 #define GUESS_H (ds->nguesses*PEGOFF)
534 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
535 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
536 #define HINT_X(g) HINT_OX
537 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
538 #define HINT_W ((ds->hintw*HINTOFF) - GAP)
539 #define HINT_H GUESS_H
541 #define SOLN_OX GUESS_OX
542 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
543 #define SOLN_W GUESS_W
544 #define SOLN_H PEGOFF
546 struct game_drawstate {
547 int nguesses;
548 pegrow *guesses; /* same size as state->guesses */
549 pegrow solution; /* only displayed if state->solved */
550 pegrow colours; /* length ncolours, not npegs */
552 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
553 int pegrad, hintrad; /* radius of peg, hint */
554 int border;
555 int colx, coly; /* origin of colours vertical bar */
556 int guessx, guessy; /* origin of guesses */
557 int solnx, solny; /* origin of solution */
558 int hintw; /* no. of hint tiles we're wide per row */
559 int w, h, started, solved;
561 int next_go;
563 blitter *blit_peg;
564 int drag_col, blit_ox, blit_oy;
567 static void set_peg(game_params *params, game_ui *ui, int peg, int col)
569 ui->curr_pegs->pegs[peg] = col;
570 ui->markable = is_markable(params, ui->curr_pegs);
573 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
575 int nc_place = 0, nc_colour = 0, i, j;
577 assert(guess && solution && (guess->npegs == solution->npegs));
579 for (i = 0; i < guess->npegs; i++) {
580 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
583 /* slight bit of cleverness: we have the following formula, from
584 * http://mathworld.wolfram.com/Mastermind.html that gives:
586 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
588 * I think this is due to Knuth.
590 for (i = 1; i <= ncols; i++) {
591 int n_guess = 0, n_solution = 0;
592 for (j = 0; j < guess->npegs; j++) {
593 if (guess->pegs[j] == i) n_guess++;
594 if (solution->pegs[j] == i) n_solution++;
596 nc_colour += min(n_guess, n_solution);
598 nc_colour -= nc_place;
600 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
601 guess->npegs, nc_place, nc_colour));
602 assert((nc_colour + nc_place) <= guess->npegs);
604 memset(guess->feedback, 0, guess->npegs*sizeof(int));
605 for (i = 0, j = 0; i < nc_place; i++)
606 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
607 for (i = 0; i < nc_colour; i++)
608 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
610 return nc_place;
613 static char *encode_move(game_state *from, game_ui *ui)
615 char *buf, *p, *sep;
616 int len, i;
618 len = ui->curr_pegs->npegs * 20 + 2;
619 buf = snewn(len, char);
620 p = buf;
621 *p++ = 'G';
622 sep = "";
623 for (i = 0; i < ui->curr_pegs->npegs; i++) {
624 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
625 ui->holds[i] ? "_" : "");
626 sep = ",";
628 *p++ = '\0';
629 assert(p - buf <= len);
630 buf = sresize(buf, len, char);
632 return buf;
635 static char *interpret_move(game_state *from, game_ui *ui, game_drawstate *ds,
636 int x, int y, int button)
638 int over_col = 0; /* one-indexed */
639 int over_guess = -1; /* zero-indexed */
640 int over_past_guess_y = -1; /* zero-indexed */
641 int over_past_guess_x = -1; /* zero-indexed */
642 int over_hint = 0; /* zero or one */
643 char *ret = NULL;
645 int guess_ox = GUESS_X(from->next_go, 0);
646 int guess_oy = GUESS_Y(from->next_go, 0);
649 * Enable or disable labels on colours.
651 if (button == 'l' || button == 'L') {
652 ui->show_labels = !ui->show_labels;
653 return "";
656 if (from->solved) return NULL;
658 if (x >= COL_OX && x < (COL_OX + COL_W) &&
659 y >= COL_OY && y < (COL_OY + COL_H)) {
660 over_col = ((y - COL_OY) / PEGOFF) + 1;
661 assert(over_col >= 1 && over_col <= ds->colours->npegs);
662 } else if (x >= guess_ox &&
663 y >= guess_oy && y < (guess_oy + GUESS_H)) {
664 if (x < (guess_ox + GUESS_W)) {
665 over_guess = (x - guess_ox) / PEGOFF;
666 assert(over_guess >= 0 && over_guess < ds->solution->npegs);
667 } else {
668 over_hint = 1;
670 } else if (x >= guess_ox && x < (guess_ox + GUESS_W) &&
671 y >= GUESS_OY && y < guess_oy) {
672 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
673 over_past_guess_x = (x - guess_ox) / PEGOFF;
674 assert(over_past_guess_y >= 0 && over_past_guess_y < from->next_go);
675 assert(over_past_guess_x >= 0 && over_past_guess_x < ds->solution->npegs);
677 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
678 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
679 over_past_guess_x, over_past_guess_y));
681 assert(ds->blit_peg);
683 /* mouse input */
684 if (button == LEFT_BUTTON) {
685 if (over_col > 0) {
686 ui->drag_col = over_col;
687 ui->drag_opeg = -1;
688 debug(("Start dragging from colours"));
689 } else if (over_guess > -1) {
690 int col = ui->curr_pegs->pegs[over_guess];
691 if (col) {
692 ui->drag_col = col;
693 ui->drag_opeg = over_guess;
694 debug(("Start dragging from a guess"));
696 } else if (over_past_guess_y > -1) {
697 int col =
698 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
699 if (col) {
700 ui->drag_col = col;
701 ui->drag_opeg = -1;
702 debug(("Start dragging from a past guess"));
705 if (ui->drag_col) {
706 ui->drag_x = x;
707 ui->drag_y = y;
708 debug(("Start dragging, col = %d, (%d,%d)",
709 ui->drag_col, ui->drag_x, ui->drag_y));
710 ret = "";
712 } else if (button == LEFT_DRAG && ui->drag_col) {
713 ui->drag_x = x;
714 ui->drag_y = y;
715 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
716 ret = "";
717 } else if (button == LEFT_RELEASE && ui->drag_col) {
718 if (over_guess > -1) {
719 debug(("Dropping colour %d onto guess peg %d",
720 ui->drag_col, over_guess));
721 set_peg(&from->params, ui, over_guess, ui->drag_col);
722 } else {
723 if (ui->drag_opeg > -1) {
724 debug(("Removing colour %d from peg %d",
725 ui->drag_col, ui->drag_opeg));
726 set_peg(&from->params, ui, ui->drag_opeg, 0);
729 ui->drag_col = 0;
730 ui->drag_opeg = -1;
731 ui->display_cur = 0;
732 debug(("Stop dragging."));
733 ret = "";
734 } else if (button == RIGHT_BUTTON) {
735 if (over_guess > -1) {
736 /* we use ths feedback in the game_ui to signify
737 * 'carry this peg to the next guess as well'. */
738 ui->holds[over_guess] = 1 - ui->holds[over_guess];
739 ret = "";
741 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
742 /* NB this won't trigger if on the end of a drag; that's on
743 * purpose, in case you drop by mistake... */
744 ret = encode_move(from, ui);
747 /* keyboard input */
748 if (button == CURSOR_UP || button == CURSOR_DOWN) {
749 ui->display_cur = 1;
750 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
751 ui->colour_cur++;
752 if (button == CURSOR_UP && ui->colour_cur > 0)
753 ui->colour_cur--;
754 ret = "";
755 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
756 int maxcur = from->params.npegs;
757 if (ui->markable) maxcur++;
759 ui->display_cur = 1;
760 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
761 ui->peg_cur++;
762 if (button == CURSOR_LEFT && ui->peg_cur > 0)
763 ui->peg_cur--;
764 ret = "";
765 } else if (IS_CURSOR_SELECT(button)) {
766 ui->display_cur = 1;
767 if (ui->peg_cur == from->params.npegs) {
768 ret = encode_move(from, ui);
769 } else {
770 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
771 ret = "";
773 } else if (button == 'D' || button == 'd' || button == '\b') {
774 ui->display_cur = 1;
775 set_peg(&from->params, ui, ui->peg_cur, 0);
776 ret = "";
777 } else if (button == 'H' || button == 'h') {
778 ui->display_cur = 1;
779 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
780 ret = "";
782 return ret;
785 static game_state *execute_move(game_state *from, char *move)
787 int i, nc_place;
788 game_state *ret;
789 char *p;
791 if (!strcmp(move, "S")) {
792 ret = dup_game(from);
793 ret->solved = -1;
794 return ret;
795 } else if (move[0] == 'G') {
796 p = move+1;
798 ret = dup_game(from);
800 for (i = 0; i < from->solution->npegs; i++) {
801 int val = atoi(p);
802 int min_colour = from->params.allow_blank? 0 : 1;
803 if (val < min_colour || val > from->params.ncolours) {
804 free_game(ret);
805 return NULL;
807 ret->guesses[from->next_go]->pegs[i] = atoi(p);
808 while (*p && isdigit((unsigned char)*p)) p++;
809 if (*p == '_') {
810 ret->holds[i] = 1;
811 p++;
812 } else
813 ret->holds[i] = 0;
814 if (*p == ',') p++;
817 nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
819 if (nc_place == ret->solution->npegs) {
820 ret->solved = +1; /* win! */
821 } else {
822 ret->next_go = from->next_go + 1;
823 if (ret->next_go >= ret->params.nguesses)
824 ret->solved = -1; /* lose, meaning we show the pegs. */
827 return ret;
828 } else
829 return NULL;
832 /* ----------------------------------------------------------------------
833 * Drawing routines.
836 #define PEG_PREFER_SZ 32
838 /* next three are multipliers for pegsz. It will look much nicer if
839 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
840 #define PEG_GAP 0.10
841 #define PEG_HINT 0.35
843 #define BORDER 0.5
845 static void game_compute_size(game_params *params, int tilesize,
846 int *x, int *y)
848 double hmul, vmul_c, vmul_g, vmul;
849 int hintw = (params->npegs+1)/2;
851 hmul = BORDER * 2.0 + /* border */
852 1.0 * 2.0 + /* vertical colour bar */
853 1.0 * params->npegs + /* guess pegs */
854 PEG_GAP * params->npegs + /* guess gaps */
855 PEG_HINT * hintw + /* hint pegs */
856 PEG_GAP * (hintw - 1); /* hint gaps */
858 vmul_c = BORDER * 2.0 + /* border */
859 1.0 * params->ncolours + /* colour pegs */
860 PEG_GAP * (params->ncolours - 1); /* colour gaps */
862 vmul_g = BORDER * 2.0 + /* border */
863 1.0 * (params->nguesses + 1) + /* guesses plus solution */
864 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
866 vmul = max(vmul_c, vmul_g);
868 *x = (int)ceil((double)tilesize * hmul);
869 *y = (int)ceil((double)tilesize * vmul);
872 static void game_set_size(drawing *dr, game_drawstate *ds,
873 game_params *params, int tilesize)
875 int colh, guessh;
877 ds->pegsz = tilesize;
879 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
880 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
881 ds->border = (int)((double)ds->pegsz * BORDER);
883 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
884 ds->hintrad = (ds->hintsz-1)/2;
886 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
887 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
888 guessh += ds->gapsz + ds->pegsz; /* solution */
890 game_compute_size(params, tilesize, &ds->w, &ds->h);
891 ds->colx = ds->border;
892 ds->coly = (ds->h - colh) / 2;
894 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
895 ds->guessy = (ds->h - guessh) / 2;
896 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
898 assert(ds->pegsz > 0);
899 assert(!ds->blit_peg); /* set_size is never called twice */
900 ds->blit_peg = blitter_new(dr, ds->pegsz, ds->pegsz);
903 static float *game_colours(frontend *fe, int *ncolours)
905 float *ret = snewn(3 * NCOLOURS, float), max;
906 int i;
908 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
910 /* red */
911 ret[COL_1 * 3 + 0] = 1.0F;
912 ret[COL_1 * 3 + 1] = 0.0F;
913 ret[COL_1 * 3 + 2] = 0.0F;
915 /* yellow */
916 ret[COL_2 * 3 + 0] = 1.0F;
917 ret[COL_2 * 3 + 1] = 1.0F;
918 ret[COL_2 * 3 + 2] = 0.0F;
920 /* green */
921 ret[COL_3 * 3 + 0] = 0.0F;
922 ret[COL_3 * 3 + 1] = 1.0F;
923 ret[COL_3 * 3 + 2] = 0.0F;
925 /* blue */
926 ret[COL_4 * 3 + 0] = 0.2F;
927 ret[COL_4 * 3 + 1] = 0.3F;
928 ret[COL_4 * 3 + 2] = 1.0F;
930 /* orange */
931 ret[COL_5 * 3 + 0] = 1.0F;
932 ret[COL_5 * 3 + 1] = 0.5F;
933 ret[COL_5 * 3 + 2] = 0.0F;
935 /* purple */
936 ret[COL_6 * 3 + 0] = 0.5F;
937 ret[COL_6 * 3 + 1] = 0.0F;
938 ret[COL_6 * 3 + 2] = 0.7F;
940 /* brown */
941 ret[COL_7 * 3 + 0] = 0.5F;
942 ret[COL_7 * 3 + 1] = 0.3F;
943 ret[COL_7 * 3 + 2] = 0.3F;
945 /* light blue */
946 ret[COL_8 * 3 + 0] = 0.4F;
947 ret[COL_8 * 3 + 1] = 0.8F;
948 ret[COL_8 * 3 + 2] = 1.0F;
950 /* light green */
951 ret[COL_9 * 3 + 0] = 0.7F;
952 ret[COL_9 * 3 + 1] = 1.0F;
953 ret[COL_9 * 3 + 2] = 0.7F;
955 /* pink */
956 ret[COL_10 * 3 + 0] = 1.0F;
957 ret[COL_10 * 3 + 1] = 0.6F;
958 ret[COL_10 * 3 + 2] = 1.0F;
960 ret[COL_FRAME * 3 + 0] = 0.0F;
961 ret[COL_FRAME * 3 + 1] = 0.0F;
962 ret[COL_FRAME * 3 + 2] = 0.0F;
964 ret[COL_CURSOR * 3 + 0] = 0.0F;
965 ret[COL_CURSOR * 3 + 1] = 0.0F;
966 ret[COL_CURSOR * 3 + 2] = 0.0F;
968 ret[COL_FLASH * 3 + 0] = 0.5F;
969 ret[COL_FLASH * 3 + 1] = 1.0F;
970 ret[COL_FLASH * 3 + 2] = 1.0F;
972 ret[COL_HOLD * 3 + 0] = 1.0F;
973 ret[COL_HOLD * 3 + 1] = 0.5F;
974 ret[COL_HOLD * 3 + 2] = 0.5F;
976 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
977 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
978 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
980 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
981 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
982 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
984 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
985 * (which we hard-code as white) from COL_BACKGROUND (which
986 * could default to white on some platforms).
987 * Code borrowed from fifteen.c. */
988 max = ret[COL_BACKGROUND*3];
989 for (i = 1; i < 3; i++)
990 if (ret[COL_BACKGROUND*3+i] > max)
991 max = ret[COL_BACKGROUND*3+i];
992 if (max * 1.2F > 1.0F) {
993 for (i = 0; i < 3; i++)
994 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
997 /* We also want to be able to tell the difference between BACKGROUND
998 * and EMPTY, for similar distinguishing-hint reasons. */
999 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0F / 3.0F;
1000 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0F / 3.0F;
1001 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0F / 3.0F;
1003 *ncolours = NCOLOURS;
1004 return ret;
1007 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1009 struct game_drawstate *ds = snew(struct game_drawstate);
1010 int i;
1012 memset(ds, 0, sizeof(struct game_drawstate));
1014 ds->guesses = snewn(state->params.nguesses, pegrow);
1015 ds->nguesses = state->params.nguesses;
1016 for (i = 0; i < state->params.nguesses; i++) {
1017 ds->guesses[i] = new_pegrow(state->params.npegs);
1018 invalidate_pegrow(ds->guesses[i]);
1020 ds->solution = new_pegrow(state->params.npegs);
1021 invalidate_pegrow(ds->solution);
1022 ds->colours = new_pegrow(state->params.ncolours);
1023 invalidate_pegrow(ds->colours);
1025 ds->hintw = (state->params.npegs+1)/2; /* must round up */
1027 ds->blit_peg = NULL;
1029 return ds;
1032 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1034 int i;
1036 if (ds->blit_peg) blitter_free(dr, ds->blit_peg);
1037 free_pegrow(ds->colours);
1038 free_pegrow(ds->solution);
1039 for (i = 0; i < ds->nguesses; i++)
1040 free_pegrow(ds->guesses[i]);
1041 sfree(ds->guesses);
1042 sfree(ds);
1045 static void draw_peg(drawing *dr, game_drawstate *ds, int cx, int cy,
1046 int moving, int labelled, int col)
1049 * Some platforms antialias circles, which means we shouldn't
1050 * overwrite a circle of one colour with a circle of another
1051 * colour without erasing the background first. However, if the
1052 * peg is the one being dragged, we don't erase the background
1053 * because we _want_ it to alpha-blend nicely into whatever's
1054 * behind it.
1056 if (!moving)
1057 draw_rect(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
1058 COL_BACKGROUND);
1059 if (PEGRAD > 0) {
1060 draw_circle(dr, cx+PEGRAD, cy+PEGRAD, PEGRAD,
1061 COL_EMPTY + col, (col ? COL_FRAME : COL_EMPTY));
1062 } else
1063 draw_rect(dr, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
1065 if (labelled && col) {
1066 char buf[2];
1067 buf[0] = 'a'-1 + col;
1068 buf[1] = '\0';
1069 draw_text(dr, cx+PEGRAD, cy+PEGRAD, FONT_VARIABLE, PEGRAD,
1070 ALIGN_HCENTRE|ALIGN_VCENTRE, COL_FRAME, buf);
1073 draw_update(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1076 static void draw_cursor(drawing *dr, game_drawstate *ds, int x, int y)
1078 draw_circle(dr, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, -1, COL_CURSOR);
1080 draw_update(dr, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1083 static void guess_redraw(drawing *dr, game_drawstate *ds, int guess,
1084 pegrow src, int *holds, int cur_col, int force,
1085 int labelled)
1087 pegrow dest;
1088 int rowx, rowy, i, scol;
1090 if (guess == -1) {
1091 dest = ds->solution;
1092 rowx = SOLN_OX;
1093 rowy = SOLN_OY;
1094 } else {
1095 dest = ds->guesses[guess];
1096 rowx = GUESS_X(guess,0);
1097 rowy = GUESS_Y(guess,0);
1099 if (src) assert(src->npegs == dest->npegs);
1101 for (i = 0; i < dest->npegs; i++) {
1102 scol = src ? src->pegs[i] : 0;
1103 if (i == cur_col)
1104 scol |= 0x1000;
1105 if (holds && holds[i])
1106 scol |= 0x2000;
1107 if (labelled)
1108 scol |= 0x4000;
1109 if ((dest->pegs[i] != scol) || force) {
1110 draw_peg(dr, ds, rowx + PEGOFF * i, rowy, FALSE, labelled,
1111 scol &~ 0x7000);
1113 * Hold marker.
1115 draw_rect(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1116 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
1117 draw_update(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1118 PEGSZ, 2);
1119 if (scol & 0x1000)
1120 draw_cursor(dr, ds, rowx + PEGOFF * i, rowy);
1122 dest->pegs[i] = scol;
1126 static void hint_redraw(drawing *dr, game_drawstate *ds, int guess,
1127 pegrow src, int force, int cursor, int markable)
1129 pegrow dest = ds->guesses[guess];
1130 int rowx, rowy, i, scol, col, hintlen;
1131 int need_redraw;
1132 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
1134 if (src) assert(src->npegs == dest->npegs);
1136 hintlen = (dest->npegs + 1)/2;
1139 * Because of the possible presence of the cursor around this
1140 * entire section, we redraw all or none of it but never part.
1142 need_redraw = FALSE;
1144 for (i = 0; i < dest->npegs; i++) {
1145 scol = src ? src->feedback[i] : 0;
1146 if (i == 0 && cursor)
1147 scol |= 0x1000;
1148 if (i == 0 && markable)
1149 scol |= 0x2000;
1150 if ((scol != dest->feedback[i]) || force) {
1151 need_redraw = TRUE;
1153 dest->feedback[i] = scol;
1156 if (need_redraw) {
1157 int hinth = HINTSZ + GAP + HINTSZ;
1158 int hx,hy,hw,hh;
1160 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1161 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1163 /* erase a large background rectangle */
1164 draw_rect(dr, hx, hy, hw, hh, COL_BACKGROUND);
1166 for (i = 0; i < dest->npegs; i++) {
1167 scol = src ? src->feedback[i] : 0;
1168 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1169 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1170 emptycol);
1172 rowx = HINT_X(guess);
1173 rowy = HINT_Y(guess);
1174 if (i < hintlen) {
1175 rowx += HINTOFF * i;
1176 } else {
1177 rowx += HINTOFF * (i - hintlen);
1178 rowy += HINTOFF;
1180 if (HINTRAD > 0) {
1181 draw_circle(dr, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, col,
1182 (col == emptycol ? emptycol : COL_FRAME));
1183 } else {
1184 draw_rect(dr, rowx, rowy, HINTSZ, HINTSZ, col);
1187 if (cursor) {
1188 int x1,y1,x2,y2;
1189 x1 = hx + CGAP; y1 = hy + CGAP;
1190 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1191 draw_line(dr, x1, y1, x2, y1, COL_CURSOR);
1192 draw_line(dr, x2, y1, x2, y2, COL_CURSOR);
1193 draw_line(dr, x2, y2, x1, y2, COL_CURSOR);
1194 draw_line(dr, x1, y2, x1, y1, COL_CURSOR);
1197 draw_update(dr, hx, hy, hw, hh);
1201 static void currmove_redraw(drawing *dr, game_drawstate *ds, int guess, int col)
1203 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1205 draw_rect(dr, ox-off-1, oy, 2, PEGSZ, col);
1206 draw_update(dr, ox-off-1, oy, 2, PEGSZ);
1209 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
1210 game_state *state, int dir, game_ui *ui,
1211 float animtime, float flashtime)
1213 int i, new_move;
1215 new_move = (state->next_go != ds->next_go) || !ds->started;
1217 if (!ds->started) {
1218 draw_rect(dr, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1219 draw_rect(dr, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1220 draw_update(dr, 0, 0, ds->w, ds->h);
1223 if (ds->drag_col != 0) {
1224 debug(("Loading from blitter."));
1225 blitter_load(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1226 draw_update(dr, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1229 /* draw the colours */
1230 for (i = 0; i < state->params.ncolours; i++) {
1231 int val = i+1;
1232 if (ui->display_cur && ui->colour_cur == i)
1233 val |= 0x1000;
1234 if (ui->show_labels)
1235 val |= 0x2000;
1236 if (ds->colours->pegs[i] != val) {
1237 draw_peg(dr, ds, COL_X(i), COL_Y(i), FALSE, ui->show_labels, i+1);
1238 if (val & 0x1000)
1239 draw_cursor(dr, ds, COL_X(i), COL_Y(i));
1240 ds->colours->pegs[i] = val;
1244 /* draw the guesses (so far) and the hints
1245 * (in reverse order to avoid trampling holds) */
1246 for (i = state->params.nguesses - 1; i >= 0; i--) {
1247 if (state->next_go > i || state->solved) {
1248 /* this info is stored in the game_state already */
1249 guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0,
1250 ui->show_labels);
1251 hint_redraw(dr, ds, i, state->guesses[i],
1252 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
1253 } else if (state->next_go == i) {
1254 /* this is the one we're on; the (incomplete) guess is
1255 * stored in the game_ui. */
1256 guess_redraw(dr, ds, i, ui->curr_pegs,
1257 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0,
1258 ui->show_labels);
1259 hint_redraw(dr, ds, i, NULL, 1,
1260 ui->display_cur && ui->peg_cur == state->params.npegs,
1261 ui->markable);
1262 } else {
1263 /* we've not got here yet; it's blank. */
1264 guess_redraw(dr, ds, i, NULL, NULL, -1, 0, ui->show_labels);
1265 hint_redraw(dr, ds, i, NULL, 0, FALSE, FALSE);
1269 /* draw the 'current move' and 'able to mark' sign. */
1270 if (new_move)
1271 currmove_redraw(dr, ds, ds->next_go, COL_BACKGROUND);
1272 if (!state->solved)
1273 currmove_redraw(dr, ds, state->next_go, COL_HOLD);
1275 /* draw the solution (or the big rectangle) */
1276 if ((!state->solved ^ !ds->solved) || !ds->started) {
1277 draw_rect(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1278 state->solved ? COL_BACKGROUND : COL_EMPTY);
1279 draw_update(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1281 if (state->solved)
1282 guess_redraw(dr, ds, -1, state->solution, NULL, -1, !ds->solved,
1283 ui->show_labels);
1284 ds->solved = state->solved;
1286 ds->next_go = state->next_go;
1288 /* if ui->drag_col != 0, save the screen to the blitter,
1289 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1290 if (ui->drag_col != 0) {
1291 int ox = ui->drag_x - (PEGSZ/2);
1292 int oy = ui->drag_y - (PEGSZ/2);
1293 debug(("Saving to blitter at (%d,%d)", ox, oy));
1294 blitter_save(dr, ds->blit_peg, ox, oy);
1295 draw_peg(dr, ds, ox, oy, TRUE, ui->show_labels, ui->drag_col);
1297 ds->blit_ox = ox; ds->blit_oy = oy;
1299 ds->drag_col = ui->drag_col;
1301 ds->started = 1;
1304 static float game_anim_length(game_state *oldstate, game_state *newstate,
1305 int dir, game_ui *ui)
1307 return 0.0F;
1310 static float game_flash_length(game_state *oldstate, game_state *newstate,
1311 int dir, game_ui *ui)
1313 return 0.0F;
1316 static int game_status(game_state *state)
1319 * We return nonzero whenever the solution has been revealed, even
1320 * (on spoiler grounds) if it wasn't guessed correctly. The
1321 * correct return value from this function is already in
1322 * state->solved.
1324 return state->solved;
1327 static int game_timing_state(game_state *state, game_ui *ui)
1329 return TRUE;
1332 static void game_print_size(game_params *params, float *x, float *y)
1336 static void game_print(drawing *dr, game_state *state, int tilesize)
1340 #ifdef COMBINED
1341 #define thegame guess
1342 #endif
1344 const struct game thegame = {
1345 "Guess", "games.guess", "guess",
1346 default_params,
1347 game_fetch_preset,
1348 decode_params,
1349 encode_params,
1350 free_params,
1351 dup_params,
1352 TRUE, game_configure, custom_params,
1353 validate_params,
1354 new_game_desc,
1355 validate_desc,
1356 new_game,
1357 dup_game,
1358 free_game,
1359 TRUE, solve_game,
1360 FALSE, game_can_format_as_text_now, game_text_format,
1361 new_ui,
1362 free_ui,
1363 encode_ui,
1364 decode_ui,
1365 game_changed_state,
1366 interpret_move,
1367 execute_move,
1368 PEG_PREFER_SZ, game_compute_size, game_set_size,
1369 game_colours,
1370 game_new_drawstate,
1371 game_free_drawstate,
1372 game_redraw,
1373 game_anim_length,
1374 game_flash_length,
1375 game_status,
1376 FALSE, FALSE, game_print_size, game_print,
1377 FALSE, /* wants_statusbar */
1378 FALSE, game_timing_state,
1379 0, /* flags */
1382 /* vim: set shiftwidth=4 tabstop=8: */