2 * guess.c: Mastermind clone.
14 #define FLASH_FRAME 0.5F
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,
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 {
35 int *pegs; /* 0 is 'empty' */
36 int *feedback; /* may well be unused */
41 pegrow *guesses; /* length params->nguesses */
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. */
59 ret->allow_multiple = 1;
64 static void free_params(game_params *params)
69 static game_params *dup_params(const game_params *params)
71 game_params *ret = snew(game_params);
72 *ret = *params; /* structure copy */
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))
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);
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);
112 params->ncolours = atoi(p);
113 while (*p && isdigit((unsigned char)*p)) p++;
117 params->npegs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
122 params->nguesses = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
127 params->allow_blank = 1;
131 params->allow_blank = 0;
135 params->allow_multiple = 1;
139 params->allow_multiple = 0;
148 static char *encode_params(const game_params *params, int full)
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");
159 static config_item *game_configure(const game_params *params)
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].u.string.sval = dupstr(buf);
171 ret[1].name = "Pegs per guess";
172 ret[1].type = C_STRING;
173 sprintf(buf, "%d", params->npegs);
174 ret[1].u.string.sval = dupstr(buf);
176 ret[2].name = "Guesses";
177 ret[2].type = C_STRING;
178 sprintf(buf, "%d", params->nguesses);
179 ret[2].u.string.sval = dupstr(buf);
181 ret[3].name = "Allow blanks";
182 ret[3].type = C_BOOLEAN;
183 ret[3].u.boolean.bval = params->allow_blank;
185 ret[4].name = "Allow duplicates";
186 ret[4].type = C_BOOLEAN;
187 ret[4].u.boolean.bval = params->allow_multiple;
195 static game_params *custom_params(const config_item *cfg)
197 game_params *ret = snew(game_params);
199 ret->ncolours = atoi(cfg[0].u.string.sval);
200 ret->npegs = atoi(cfg[1].u.string.sval);
201 ret->nguesses = atoi(cfg[2].u.string.sval);
203 ret->allow_blank = cfg[3].u.boolean.bval;
204 ret->allow_multiple = cfg[4].u.boolean.bval;
209 static char *validate_params(const game_params *params, int full)
211 if (params->ncolours < 2 || params->npegs < 2)
212 return "Trivial solutions are uninteresting";
213 /* NB as well as the no. of colours we define, max(ncolours) must
214 * also fit in an unsigned char; see new_game_desc. */
215 if (params->ncolours > 10)
216 return "Too many colours";
217 if (params->nguesses < 1)
218 return "Must have at least one guess";
219 if (!params->allow_multiple && params->ncolours < params->npegs)
220 return "Disallowing multiple colours requires at least as many colours as pegs";
224 static pegrow new_pegrow(int npegs)
226 pegrow pegs = snew(struct pegrow);
229 pegs->pegs = snewn(pegs->npegs, int);
230 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
231 pegs->feedback = snewn(pegs->npegs, int);
232 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
237 static pegrow dup_pegrow(pegrow pegs)
239 pegrow newpegs = new_pegrow(pegs->npegs);
241 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
242 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
247 static void invalidate_pegrow(pegrow pegs)
249 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
250 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
253 static void free_pegrow(pegrow pegs)
256 sfree(pegs->feedback);
260 static char *new_game_desc(const game_params *params, random_state *rs,
261 char **aux, int interactive)
263 unsigned char *bmp = snewn(params->npegs, unsigned char);
266 pegrow colcount = new_pegrow(params->ncolours);
268 for (i = 0; i < params->npegs; i++) {
270 c = random_upto(rs, params->ncolours);
271 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
273 bmp[i] = (unsigned char)(c+1);
275 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
277 ret = bin2hex(bmp, params->npegs);
279 free_pegrow(colcount);
283 static char *validate_desc(const game_params *params, const char *desc)
288 /* desc is just an (obfuscated) bitmap of the solution; check that
289 * it's the correct length and (when unobfuscated) contains only
290 * sensible colours. */
291 if (strlen(desc) != params->npegs * 2)
292 return "Game description is wrong length";
293 bmp = hex2bin(desc, params->npegs);
294 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
295 for (i = 0; i < params->npegs; i++) {
296 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
298 return "Game description is corrupted";
306 static game_state *new_game(midend *me, const game_params *params,
309 game_state *state = snew(game_state);
313 state->params = *params;
314 state->guesses = snewn(params->nguesses, pegrow);
315 for (i = 0; i < params->nguesses; i++)
316 state->guesses[i] = new_pegrow(params->npegs);
317 state->holds = snewn(params->npegs, int);
318 state->solution = new_pegrow(params->npegs);
320 bmp = hex2bin(desc, params->npegs);
321 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
322 for (i = 0; i < params->npegs; i++)
323 state->solution->pegs[i] = (int)bmp[i];
326 memset(state->holds, 0, sizeof(int) * params->npegs);
327 state->next_go = state->solved = 0;
332 static game_state *dup_game(const game_state *state)
334 game_state *ret = snew(game_state);
339 ret->guesses = snewn(state->params.nguesses, pegrow);
340 for (i = 0; i < state->params.nguesses; i++)
341 ret->guesses[i] = dup_pegrow(state->guesses[i]);
342 ret->holds = snewn(state->params.npegs, int);
343 memcpy(ret->holds, state->holds, sizeof(int) * state->params.npegs);
344 ret->solution = dup_pegrow(state->solution);
349 static void free_game(game_state *state)
353 free_pegrow(state->solution);
354 for (i = 0; i < state->params.nguesses; i++)
355 free_pegrow(state->guesses[i]);
357 sfree(state->guesses);
362 static char *solve_game(const game_state *state, const game_state *currstate,
363 const char *aux, char **error)
368 static int game_can_format_as_text_now(const game_params *params)
373 static char *game_text_format(const game_state *state)
378 static int is_markable(const game_params *params, pegrow pegs)
380 int i, nset = 0, nrequired, ret = 0;
381 pegrow colcount = new_pegrow(params->ncolours);
383 nrequired = params->allow_blank ? 1 : params->npegs;
385 for (i = 0; i < params->npegs; i++) {
386 int c = pegs->pegs[i];
388 colcount->pegs[c-1]++;
392 if (nset < nrequired) goto done;
394 if (!params->allow_multiple) {
395 for (i = 0; i < params->ncolours; i++) {
396 if (colcount->pegs[i] > 1) goto done;
401 free_pegrow(colcount);
407 pegrow curr_pegs; /* half-finished current move */
409 int colour_cur; /* position of up-down colour picker cursor */
410 int peg_cur; /* position of left-right peg picker cursor */
411 int display_cur, markable;
413 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
414 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
416 int show_labels; /* label the colours with letters */
420 static game_ui *new_ui(const game_state *state)
422 game_ui *ui = snew(game_ui);
423 memset(ui, 0, sizeof(game_ui));
424 ui->params = state->params; /* structure copy */
425 ui->curr_pegs = new_pegrow(state->params.npegs);
426 ui->holds = snewn(state->params.npegs, int);
427 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
432 static void free_ui(game_ui *ui)
435 free_pegrow(ui->hint);
436 free_pegrow(ui->curr_pegs);
441 static char *encode_ui(const game_ui *ui)
447 * For this game it's worth storing the contents of the current
448 * guess, and the current set of holds.
450 ret = snewn(40 * ui->curr_pegs->npegs, char);
453 for (i = 0; i < ui->curr_pegs->npegs; i++) {
454 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
455 ui->holds[i] ? "_" : "");
459 assert(p - ret < 40 * ui->curr_pegs->npegs);
460 return sresize(ret, p - ret, char);
463 static void decode_ui(game_ui *ui, const char *encoding)
466 const char *p = encoding;
467 for (i = 0; i < ui->curr_pegs->npegs; i++) {
468 ui->curr_pegs->pegs[i] = atoi(p);
469 while (*p && isdigit((unsigned char)*p)) p++;
471 /* NB: old versions didn't store holds */
478 ui->markable = is_markable(&ui->params, ui->curr_pegs);
481 static void game_changed_state(game_ui *ui, const game_state *oldstate,
482 const game_state *newstate)
486 if (newstate->next_go < oldstate->next_go) {
491 /* Implement holds, clear other pegs.
492 * This does something that is arguably the Right Thing even
494 for (i = 0; i < newstate->solution->npegs; i++) {
495 if (newstate->solved)
498 ui->holds[i] = newstate->holds[i];
499 if (newstate->solved || (newstate->next_go == 0) || !ui->holds[i]) {
500 ui->curr_pegs->pegs[i] = 0;
502 ui->curr_pegs->pegs[i] =
503 newstate->guesses[newstate->next_go-1]->pegs[i];
505 ui->markable = is_markable(&newstate->params, ui->curr_pegs);
506 /* Clean up cursor position */
507 if (!ui->markable && ui->peg_cur == newstate->solution->npegs)
511 #define PEGSZ (ds->pegsz)
512 #define PEGOFF (ds->pegsz + ds->gapsz)
513 #define HINTSZ (ds->hintsz)
514 #define HINTOFF (ds->hintsz + ds->gapsz)
516 #define GAP (ds->gapsz)
517 #define CGAP (ds->gapsz / 2)
519 #define PEGRAD (ds->pegrad)
520 #define HINTRAD (ds->hintrad)
522 #define COL_OX (ds->colx)
523 #define COL_OY (ds->coly)
524 #define COL_X(c) (COL_OX)
525 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
527 #define COL_H (ds->colours->npegs*PEGOFF)
529 #define GUESS_OX (ds->guessx)
530 #define GUESS_OY (ds->guessy)
531 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
532 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
533 #define GUESS_W (ds->solution->npegs*PEGOFF)
534 #define GUESS_H (ds->nguesses*PEGOFF)
536 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
537 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
538 #define HINT_X(g) HINT_OX
539 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
540 #define HINT_W ((ds->hintw*HINTOFF) - GAP)
541 #define HINT_H GUESS_H
543 #define SOLN_OX GUESS_OX
544 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
545 #define SOLN_W GUESS_W
546 #define SOLN_H PEGOFF
548 struct game_drawstate {
550 pegrow *guesses; /* same size as state->guesses */
551 pegrow solution; /* only displayed if state->solved */
552 pegrow colours; /* length ncolours, not npegs */
554 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
555 int pegrad, hintrad; /* radius of peg, hint */
557 int colx, coly; /* origin of colours vertical bar */
558 int guessx, guessy; /* origin of guesses */
559 int solnx, solny; /* origin of solution */
560 int hintw; /* no. of hint tiles we're wide per row */
561 int w, h, started, solved;
566 int drag_col, blit_ox, blit_oy;
569 static void set_peg(const game_params *params, game_ui *ui, int peg, int col)
571 ui->curr_pegs->pegs[peg] = col;
572 ui->markable = is_markable(params, ui->curr_pegs);
575 static int mark_pegs(pegrow guess, const pegrow solution, int ncols)
577 int nc_place = 0, nc_colour = 0, i, j;
579 assert(guess && solution && (guess->npegs == solution->npegs));
581 for (i = 0; i < guess->npegs; i++) {
582 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
585 /* slight bit of cleverness: we have the following formula, from
586 * http://mathworld.wolfram.com/Mastermind.html that gives:
588 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
590 * I think this is due to Knuth.
592 for (i = 1; i <= ncols; i++) {
593 int n_guess = 0, n_solution = 0;
594 for (j = 0; j < guess->npegs; j++) {
595 if (guess->pegs[j] == i) n_guess++;
596 if (solution->pegs[j] == i) n_solution++;
598 nc_colour += min(n_guess, n_solution);
600 nc_colour -= nc_place;
602 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
603 guess->npegs, nc_place, nc_colour));
604 assert((nc_colour + nc_place) <= guess->npegs);
606 memset(guess->feedback, 0, guess->npegs*sizeof(int));
607 for (i = 0, j = 0; i < nc_place; i++)
608 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
609 for (i = 0; i < nc_colour; i++)
610 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
615 static char *encode_move(const game_state *from, game_ui *ui)
620 len = ui->curr_pegs->npegs * 20 + 2;
621 buf = snewn(len, char);
625 for (i = 0; i < ui->curr_pegs->npegs; i++) {
626 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
627 ui->holds[i] ? "_" : "");
631 assert(p - buf <= len);
632 buf = sresize(buf, len, char);
637 static void compute_hint(const game_state *state, game_ui *ui)
639 /* Suggest the lexicographically first row consistent with all
640 * previous feedback. This is not only a useful hint, but also
641 * a reasonable strategy if applied consistently. If the user
642 * uses hints in every turn, they may be able to intuit this
643 * strategy, or one similar to it. I (Jonas Kölker) came up
644 * with something close to it without seeing it in action. */
646 /* Some performance characteristics: I want to ask for each n,
647 * how many solutions are guessed in exactly n guesses if you
648 * use the hint in each turn.
650 * With 4 pegs and 6 colours you get the following histogram:
652 * 1 guesses: 1 solution
653 * 2 guesses: 4 solutions
654 * 3 guesses: 25 solutions
655 * 4 guesses: 108 solutions
656 * 5 guesses: 305 solutions
657 * 6 guesses: 602 solutions
658 * 7 guesses: 196 solutions
659 * 8 guesses: 49 solutions
660 * 9 guesses: 6 solutions
661 * (note: the tenth guess is never necessary.)
663 * With 5 pegs and 8 colours you get the following histogram:
665 * 1 guesses: 1 solution
666 * 2 guesses: 5 solutions
667 * 3 guesses: 43 solutions
668 * 4 guesses: 278 solutions
669 * 5 guesses: 1240 solutions
670 * 6 guesses: 3515 solutions
671 * 7 guesses: 7564 solutions
672 * 8 guesses: 14086 solutions
673 * 9 guesses: 4614 solutions
674 * 10 guesses: 1239 solutions
675 * 11 guesses: 175 solutions
676 * 12 guesses: 7 solutions
677 * 13 guesses: 1 solution
679 * The solution which takes too many guesses is {8, 8, 5, 6, 7}.
680 * The game ID is c8p5g12Bm:4991e5e41a. */
682 int mincolour = 1, maxcolour = 0, i, j;
684 /* For large values of npegs and ncolours, the lexicographically
685 * next guess make take a while to find. Finding upper and
686 * lower limits on which colours we have to consider will speed
687 * this up, as will caching our progress from one invocation to
688 * the next. The latter strategy works, since if we have ruled
689 * out a candidate we will never reverse this judgment in the
690 * light of new information. Removing information, i.e. undo,
691 * will require us to backtrack somehow. We backtrack by fully
692 * forgetting our progress (and recomputing it if required). */
694 for (i = 0; i < state->next_go; ++i)
695 for (j = 0; j < state->params.npegs; ++j)
696 if (state->guesses[i]->pegs[j] > maxcolour)
697 maxcolour = state->guesses[i]->pegs[j];
698 maxcolour = min(maxcolour + 1, state->params.ncolours);
701 for (i = 0; i < state->next_go; ++i) {
702 if (state->guesses[i]->feedback[0])
704 for (j = 0; j < state->params.npegs; ++j)
705 if (state->guesses[i]->pegs[j] != mincolour)
708 goto increase_mincolour;
714 ui->hint = new_pegrow(state->params.npegs);
715 for (i = 0; i < state->params.npegs; ++i)
716 ui->hint->pegs[i] = 1;
719 while (ui->hint->pegs[0] <= state->params.ncolours) {
720 for (i = 0; i < state->next_go; ++i) {
721 mark_pegs(ui->hint, state->guesses[i], maxcolour);
722 for (j = 0; j < state->params.npegs; ++j)
723 if (ui->hint->feedback[j] != state->guesses[i]->feedback[j])
724 goto increment_pegrow;
726 /* a valid guess was found; install it and return */
727 for (i = 0; i < state->params.npegs; ++i)
728 ui->curr_pegs->pegs[i] = ui->hint->pegs[i];
731 ui->peg_cur = state->params.npegs;
736 for (i = ui->hint->npegs;
737 ++ui->hint->pegs[--i], i && ui->hint->pegs[i] > maxcolour;
738 ui->hint->pegs[i] = mincolour);
740 /* No solution is compatible with the given hints. Impossible! */
741 /* (hack new_game_desc to create invalid solutions to get here) */
743 /* For some values of npegs and ncolours, the hinting function takes a
744 * long time to complete. To visually indicate completion with failure,
745 * should it ever happen, update the ui in some trivial way. This gives
746 * the user a sense of broken(ish)ness and futility. */
747 if (!ui->display_cur) {
749 } else if (state->params.npegs == 1) {
752 ui->peg_cur = (ui->peg_cur + 1) % state->params.npegs;
756 static char *interpret_move(const game_state *from, game_ui *ui,
757 const game_drawstate *ds,
758 int x, int y, int button)
760 int over_col = 0; /* one-indexed */
761 int over_guess = -1; /* zero-indexed */
762 int over_past_guess_y = -1; /* zero-indexed */
763 int over_past_guess_x = -1; /* zero-indexed */
764 int over_hint = 0; /* zero or one */
767 int guess_ox = GUESS_X(from->next_go, 0);
768 int guess_oy = GUESS_Y(from->next_go, 0);
771 * Enable or disable labels on colours.
773 if (button == 'l' || button == 'L') {
774 ui->show_labels = !ui->show_labels;
778 if (from->solved) return NULL;
780 if (x >= COL_OX && x < (COL_OX + COL_W) &&
781 y >= COL_OY && y < (COL_OY + COL_H)) {
782 over_col = ((y - COL_OY) / PEGOFF) + 1;
783 assert(over_col >= 1 && over_col <= ds->colours->npegs);
784 } else if (x >= guess_ox &&
785 y >= guess_oy && y < (guess_oy + GUESS_H)) {
786 if (x < (guess_ox + GUESS_W)) {
787 over_guess = (x - guess_ox) / PEGOFF;
788 assert(over_guess >= 0 && over_guess < ds->solution->npegs);
792 } else if (x >= guess_ox && x < (guess_ox + GUESS_W) &&
793 y >= GUESS_OY && y < guess_oy) {
794 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
795 over_past_guess_x = (x - guess_ox) / PEGOFF;
796 assert(over_past_guess_y >= 0 && over_past_guess_y < from->next_go);
797 assert(over_past_guess_x >= 0 && over_past_guess_x < ds->solution->npegs);
799 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
800 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
801 over_past_guess_x, over_past_guess_y));
803 assert(ds->blit_peg);
806 if (button == LEFT_BUTTON) {
808 ui->drag_col = over_col;
810 debug(("Start dragging from colours"));
811 } else if (over_guess > -1) {
812 int col = ui->curr_pegs->pegs[over_guess];
815 ui->drag_opeg = over_guess;
816 debug(("Start dragging from a guess"));
818 } else if (over_past_guess_y > -1) {
820 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
824 debug(("Start dragging from a past guess"));
830 debug(("Start dragging, col = %d, (%d,%d)",
831 ui->drag_col, ui->drag_x, ui->drag_y));
834 } else if (button == LEFT_DRAG && ui->drag_col) {
837 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
839 } else if (button == LEFT_RELEASE && ui->drag_col) {
840 if (over_guess > -1) {
841 debug(("Dropping colour %d onto guess peg %d",
842 ui->drag_col, over_guess));
843 set_peg(&from->params, ui, over_guess, ui->drag_col);
845 if (ui->drag_opeg > -1) {
846 debug(("Removing colour %d from peg %d",
847 ui->drag_col, ui->drag_opeg));
848 set_peg(&from->params, ui, ui->drag_opeg, 0);
854 debug(("Stop dragging."));
856 } else if (button == RIGHT_BUTTON) {
857 if (over_guess > -1) {
858 /* we use ths feedback in the game_ui to signify
859 * 'carry this peg to the next guess as well'. */
860 ui->holds[over_guess] = 1 - ui->holds[over_guess];
863 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
864 /* NB this won't trigger if on the end of a drag; that's on
865 * purpose, in case you drop by mistake... */
866 ret = encode_move(from, ui);
870 if (button == CURSOR_UP || button == CURSOR_DOWN) {
872 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
874 if (button == CURSOR_UP && ui->colour_cur > 0)
877 } else if (button == 'h' || button == 'H' || button == '?') {
878 compute_hint(from, ui);
880 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
881 int maxcur = from->params.npegs;
882 if (ui->markable) maxcur++;
885 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
887 if (button == CURSOR_LEFT && ui->peg_cur > 0)
890 } else if (IS_CURSOR_SELECT(button)) {
892 if (ui->peg_cur == from->params.npegs) {
893 ret = encode_move(from, ui);
895 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
898 } else if (button == 'D' || button == 'd' || button == '\b') {
900 set_peg(&from->params, ui, ui->peg_cur, 0);
902 } else if (button == CURSOR_SELECT2) {
903 if (ui->peg_cur == from->params.npegs)
906 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
912 static game_state *execute_move(const game_state *from, const char *move)
918 if (!strcmp(move, "S")) {
919 ret = dup_game(from);
922 } else if (move[0] == 'G') {
925 ret = dup_game(from);
927 for (i = 0; i < from->solution->npegs; i++) {
929 int min_colour = from->params.allow_blank? 0 : 1;
930 if (val < min_colour || val > from->params.ncolours) {
934 ret->guesses[from->next_go]->pegs[i] = atoi(p);
935 while (*p && isdigit((unsigned char)*p)) p++;
944 nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
946 if (nc_place == ret->solution->npegs) {
947 ret->solved = +1; /* win! */
949 ret->next_go = from->next_go + 1;
950 if (ret->next_go >= ret->params.nguesses)
951 ret->solved = -1; /* lose, meaning we show the pegs. */
959 /* ----------------------------------------------------------------------
963 #define PEG_PREFER_SZ 32
965 /* next three are multipliers for pegsz. It will look much nicer if
966 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
968 #define PEG_HINT 0.35
972 static void game_compute_size(const game_params *params, int tilesize,
975 double hmul, vmul_c, vmul_g, vmul;
976 int hintw = (params->npegs+1)/2;
978 hmul = BORDER * 2.0 + /* border */
979 1.0 * 2.0 + /* vertical colour bar */
980 1.0 * params->npegs + /* guess pegs */
981 PEG_GAP * params->npegs + /* guess gaps */
982 PEG_HINT * hintw + /* hint pegs */
983 PEG_GAP * (hintw - 1); /* hint gaps */
985 vmul_c = BORDER * 2.0 + /* border */
986 1.0 * params->ncolours + /* colour pegs */
987 PEG_GAP * (params->ncolours - 1); /* colour gaps */
989 vmul_g = BORDER * 2.0 + /* border */
990 1.0 * (params->nguesses + 1) + /* guesses plus solution */
991 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
993 vmul = max(vmul_c, vmul_g);
995 *x = (int)ceil((double)tilesize * hmul);
996 *y = (int)ceil((double)tilesize * vmul);
999 static void game_set_size(drawing *dr, game_drawstate *ds,
1000 const game_params *params, int tilesize)
1004 ds->pegsz = tilesize;
1006 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
1007 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
1008 ds->border = (int)((double)ds->pegsz * BORDER);
1010 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
1011 ds->hintrad = (ds->hintsz-1)/2;
1013 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
1014 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
1015 guessh += ds->gapsz + ds->pegsz; /* solution */
1017 game_compute_size(params, tilesize, &ds->w, &ds->h);
1018 ds->colx = ds->border;
1019 ds->coly = (ds->h - colh) / 2;
1021 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
1022 ds->guessy = (ds->h - guessh) / 2;
1023 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
1025 assert(ds->pegsz > 0);
1026 assert(!ds->blit_peg); /* set_size is never called twice */
1027 ds->blit_peg = blitter_new(dr, ds->pegsz+2, ds->pegsz+2);
1030 static float *game_colours(frontend *fe, int *ncolours)
1032 float *ret = snewn(3 * NCOLOURS, float), max;
1035 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1038 ret[COL_1 * 3 + 0] = 1.0F;
1039 ret[COL_1 * 3 + 1] = 0.0F;
1040 ret[COL_1 * 3 + 2] = 0.0F;
1043 ret[COL_2 * 3 + 0] = 1.0F;
1044 ret[COL_2 * 3 + 1] = 1.0F;
1045 ret[COL_2 * 3 + 2] = 0.0F;
1048 ret[COL_3 * 3 + 0] = 0.0F;
1049 ret[COL_3 * 3 + 1] = 1.0F;
1050 ret[COL_3 * 3 + 2] = 0.0F;
1053 ret[COL_4 * 3 + 0] = 0.2F;
1054 ret[COL_4 * 3 + 1] = 0.3F;
1055 ret[COL_4 * 3 + 2] = 1.0F;
1058 ret[COL_5 * 3 + 0] = 1.0F;
1059 ret[COL_5 * 3 + 1] = 0.5F;
1060 ret[COL_5 * 3 + 2] = 0.0F;
1063 ret[COL_6 * 3 + 0] = 0.5F;
1064 ret[COL_6 * 3 + 1] = 0.0F;
1065 ret[COL_6 * 3 + 2] = 0.7F;
1068 ret[COL_7 * 3 + 0] = 0.5F;
1069 ret[COL_7 * 3 + 1] = 0.3F;
1070 ret[COL_7 * 3 + 2] = 0.3F;
1073 ret[COL_8 * 3 + 0] = 0.4F;
1074 ret[COL_8 * 3 + 1] = 0.8F;
1075 ret[COL_8 * 3 + 2] = 1.0F;
1078 ret[COL_9 * 3 + 0] = 0.7F;
1079 ret[COL_9 * 3 + 1] = 1.0F;
1080 ret[COL_9 * 3 + 2] = 0.7F;
1083 ret[COL_10 * 3 + 0] = 1.0F;
1084 ret[COL_10 * 3 + 1] = 0.6F;
1085 ret[COL_10 * 3 + 2] = 1.0F;
1087 ret[COL_FRAME * 3 + 0] = 0.0F;
1088 ret[COL_FRAME * 3 + 1] = 0.0F;
1089 ret[COL_FRAME * 3 + 2] = 0.0F;
1091 ret[COL_CURSOR * 3 + 0] = 0.0F;
1092 ret[COL_CURSOR * 3 + 1] = 0.0F;
1093 ret[COL_CURSOR * 3 + 2] = 0.0F;
1095 ret[COL_FLASH * 3 + 0] = 0.5F;
1096 ret[COL_FLASH * 3 + 1] = 1.0F;
1097 ret[COL_FLASH * 3 + 2] = 1.0F;
1099 ret[COL_HOLD * 3 + 0] = 1.0F;
1100 ret[COL_HOLD * 3 + 1] = 0.5F;
1101 ret[COL_HOLD * 3 + 2] = 0.5F;
1103 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
1104 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
1105 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
1107 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
1108 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
1109 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
1111 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
1112 * (which we hard-code as white) from COL_BACKGROUND (which
1113 * could default to white on some platforms).
1114 * Code borrowed from fifteen.c. */
1115 max = ret[COL_BACKGROUND*3];
1116 for (i = 1; i < 3; i++)
1117 if (ret[COL_BACKGROUND*3+i] > max)
1118 max = ret[COL_BACKGROUND*3+i];
1119 if (max * 1.2F > 1.0F) {
1120 for (i = 0; i < 3; i++)
1121 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
1124 /* We also want to be able to tell the difference between BACKGROUND
1125 * and EMPTY, for similar distinguishing-hint reasons. */
1126 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0F / 3.0F;
1127 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0F / 3.0F;
1128 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0F / 3.0F;
1130 *ncolours = NCOLOURS;
1134 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1136 struct game_drawstate *ds = snew(struct game_drawstate);
1139 memset(ds, 0, sizeof(struct game_drawstate));
1141 ds->guesses = snewn(state->params.nguesses, pegrow);
1142 ds->nguesses = state->params.nguesses;
1143 for (i = 0; i < state->params.nguesses; i++) {
1144 ds->guesses[i] = new_pegrow(state->params.npegs);
1145 invalidate_pegrow(ds->guesses[i]);
1147 ds->solution = new_pegrow(state->params.npegs);
1148 invalidate_pegrow(ds->solution);
1149 ds->colours = new_pegrow(state->params.ncolours);
1150 invalidate_pegrow(ds->colours);
1152 ds->hintw = (state->params.npegs+1)/2; /* must round up */
1154 ds->blit_peg = NULL;
1159 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1163 if (ds->blit_peg) blitter_free(dr, ds->blit_peg);
1164 free_pegrow(ds->colours);
1165 free_pegrow(ds->solution);
1166 for (i = 0; i < ds->nguesses; i++)
1167 free_pegrow(ds->guesses[i]);
1172 static void draw_peg(drawing *dr, game_drawstate *ds, int cx, int cy,
1173 int moving, int labelled, int col)
1176 * Some platforms antialias circles, which means we shouldn't
1177 * overwrite a circle of one colour with a circle of another
1178 * colour without erasing the background first. However, if the
1179 * peg is the one being dragged, we don't erase the background
1180 * because we _want_ it to alpha-blend nicely into whatever's
1184 draw_rect(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
1187 draw_circle(dr, cx+PEGRAD, cy+PEGRAD, PEGRAD,
1188 COL_EMPTY + col, (col ? COL_FRAME : COL_EMPTY));
1190 draw_rect(dr, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
1192 if (labelled && col) {
1194 buf[0] = 'a'-1 + col;
1196 draw_text(dr, cx+PEGRAD, cy+PEGRAD, FONT_VARIABLE, PEGRAD,
1197 ALIGN_HCENTRE|ALIGN_VCENTRE, COL_FRAME, buf);
1200 draw_update(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1203 static void draw_cursor(drawing *dr, game_drawstate *ds, int x, int y)
1205 draw_circle(dr, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, -1, COL_CURSOR);
1207 draw_update(dr, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1210 static void guess_redraw(drawing *dr, game_drawstate *ds, int guess,
1211 pegrow src, int *holds, int cur_col, int force,
1215 int rowx, rowy, i, scol;
1218 dest = ds->solution;
1222 dest = ds->guesses[guess];
1223 rowx = GUESS_X(guess,0);
1224 rowy = GUESS_Y(guess,0);
1226 if (src) assert(src->npegs == dest->npegs);
1228 for (i = 0; i < dest->npegs; i++) {
1229 scol = src ? src->pegs[i] : 0;
1232 if (holds && holds[i])
1236 if ((dest->pegs[i] != scol) || force) {
1237 draw_peg(dr, ds, rowx + PEGOFF * i, rowy, FALSE, labelled,
1242 draw_rect(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1243 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
1244 draw_update(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1247 draw_cursor(dr, ds, rowx + PEGOFF * i, rowy);
1249 dest->pegs[i] = scol;
1253 static void hint_redraw(drawing *dr, game_drawstate *ds, int guess,
1254 pegrow src, int force, int cursor, int markable)
1256 pegrow dest = ds->guesses[guess];
1257 int rowx, rowy, i, scol, col, hintlen;
1259 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
1261 if (src) assert(src->npegs == dest->npegs);
1263 hintlen = (dest->npegs + 1)/2;
1266 * Because of the possible presence of the cursor around this
1267 * entire section, we redraw all or none of it but never part.
1269 need_redraw = FALSE;
1271 for (i = 0; i < dest->npegs; i++) {
1272 scol = src ? src->feedback[i] : 0;
1273 if (i == 0 && cursor)
1275 if (i == 0 && markable)
1277 if ((scol != dest->feedback[i]) || force) {
1280 dest->feedback[i] = scol;
1284 int hinth = HINTSZ + GAP + HINTSZ;
1287 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1288 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1290 /* erase a large background rectangle */
1291 draw_rect(dr, hx, hy, hw, hh, COL_BACKGROUND);
1293 for (i = 0; i < dest->npegs; i++) {
1294 scol = src ? src->feedback[i] : 0;
1295 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1296 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1299 rowx = HINT_X(guess);
1300 rowy = HINT_Y(guess);
1302 rowx += HINTOFF * i;
1304 rowx += HINTOFF * (i - hintlen);
1308 draw_circle(dr, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, col,
1309 (col == emptycol ? emptycol : COL_FRAME));
1311 draw_rect(dr, rowx, rowy, HINTSZ, HINTSZ, col);
1316 x1 = hx + CGAP; y1 = hy + CGAP;
1317 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1318 draw_line(dr, x1, y1, x2, y1, COL_CURSOR);
1319 draw_line(dr, x2, y1, x2, y2, COL_CURSOR);
1320 draw_line(dr, x2, y2, x1, y2, COL_CURSOR);
1321 draw_line(dr, x1, y2, x1, y1, COL_CURSOR);
1324 draw_update(dr, hx, hy, hw, hh);
1328 static void currmove_redraw(drawing *dr, game_drawstate *ds, int guess, int col)
1330 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1332 draw_rect(dr, ox-off-1, oy, 2, PEGSZ, col);
1333 draw_update(dr, ox-off-1, oy, 2, PEGSZ);
1336 static void game_redraw(drawing *dr, game_drawstate *ds,
1337 const game_state *oldstate, const game_state *state,
1338 int dir, const game_ui *ui,
1339 float animtime, float flashtime)
1343 new_move = (state->next_go != ds->next_go) || !ds->started;
1346 draw_rect(dr, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1347 draw_rect(dr, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1348 draw_update(dr, 0, 0, ds->w, ds->h);
1351 if (ds->drag_col != 0) {
1352 debug(("Loading from blitter."));
1353 blitter_load(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1354 draw_update(dr, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1357 /* draw the colours */
1358 for (i = 0; i < state->params.ncolours; i++) {
1360 if (ui->display_cur && ui->colour_cur == i)
1362 if (ui->show_labels)
1364 if (ds->colours->pegs[i] != val) {
1365 draw_peg(dr, ds, COL_X(i), COL_Y(i), FALSE, ui->show_labels, i+1);
1367 draw_cursor(dr, ds, COL_X(i), COL_Y(i));
1368 ds->colours->pegs[i] = val;
1372 /* draw the guesses (so far) and the hints
1373 * (in reverse order to avoid trampling holds, and postponing the
1374 * next_go'th to not overrender the top of the circular cursor) */
1375 for (i = state->params.nguesses - 1; i >= 0; i--) {
1376 if (i < state->next_go || state->solved) {
1377 /* this info is stored in the game_state already */
1378 guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0,
1380 hint_redraw(dr, ds, i, state->guesses[i],
1381 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
1382 } else if (i > state->next_go) {
1383 /* we've not got here yet; it's blank. */
1384 guess_redraw(dr, ds, i, NULL, NULL, -1, 0, ui->show_labels);
1385 hint_redraw(dr, ds, i, NULL, 0, FALSE, FALSE);
1388 if (!state->solved) {
1389 /* this is the one we're on; the (incomplete) guess is stored in
1391 guess_redraw(dr, ds, state->next_go, ui->curr_pegs,
1392 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0,
1394 hint_redraw(dr, ds, state->next_go, NULL, 1,
1395 ui->display_cur && ui->peg_cur == state->params.npegs,
1399 /* draw the 'current move' and 'able to mark' sign. */
1401 currmove_redraw(dr, ds, ds->next_go, COL_BACKGROUND);
1403 currmove_redraw(dr, ds, state->next_go, COL_HOLD);
1405 /* draw the solution (or the big rectangle) */
1406 if ((!state->solved ^ !ds->solved) || !ds->started) {
1407 draw_rect(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1408 state->solved ? COL_BACKGROUND : COL_EMPTY);
1409 draw_update(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1412 guess_redraw(dr, ds, -1, state->solution, NULL, -1, !ds->solved,
1414 ds->solved = state->solved;
1416 ds->next_go = state->next_go;
1418 /* if ui->drag_col != 0, save the screen to the blitter,
1419 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1420 if (ui->drag_col != 0) {
1421 int ox = ui->drag_x - (PEGSZ/2);
1422 int oy = ui->drag_y - (PEGSZ/2);
1423 ds->blit_ox = ox - 1; ds->blit_oy = oy - 1;
1424 debug(("Saving to blitter at (%d,%d)", ds->blit_ox, ds->blit_oy));
1425 blitter_save(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1426 draw_peg(dr, ds, ox, oy, TRUE, ui->show_labels, ui->drag_col);
1428 ds->drag_col = ui->drag_col;
1433 static float game_anim_length(const game_state *oldstate,
1434 const game_state *newstate, int dir, game_ui *ui)
1439 static float game_flash_length(const game_state *oldstate,
1440 const game_state *newstate, int dir, game_ui *ui)
1445 static int game_status(const game_state *state)
1448 * We return nonzero whenever the solution has been revealed, even
1449 * (on spoiler grounds) if it wasn't guessed correctly. The
1450 * correct return value from this function is already in
1453 return state->solved;
1456 static int game_timing_state(const game_state *state, game_ui *ui)
1461 static void game_print_size(const game_params *params, float *x, float *y)
1465 static void game_print(drawing *dr, const game_state *state, int tilesize)
1470 #define thegame guess
1473 const struct game thegame = {
1474 "Guess", "games.guess", "guess",
1476 game_fetch_preset, NULL,
1481 TRUE, game_configure, custom_params,
1489 FALSE, game_can_format_as_text_now, game_text_format,
1497 PEG_PREFER_SZ, game_compute_size, game_set_size,
1500 game_free_drawstate,
1505 FALSE, FALSE, game_print_size, game_print,
1506 FALSE, /* wants_statusbar */
1507 FALSE, game_timing_state,
1511 /* vim: set shiftwidth=4 tabstop=8: */