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 */
43 int next_go; /* from 0 to nguesses-1;
44 if next_go == nguesses then they've lost. */
48 static game_params *default_params(void)
50 game_params *ret = snew(game_params);
52 /* AFAIK this is the canonical Mastermind ruleset. */
58 ret->allow_multiple = 1;
63 static void free_params(game_params *params)
68 static game_params *dup_params(game_params *params)
70 game_params *ret = snew(game_params);
71 *ret = *params; /* structure copy */
79 {"Standard", {6, 4, 10, FALSE, TRUE}},
80 {"Super", {8, 5, 12, FALSE, TRUE}},
84 static int game_fetch_preset(int i, char **name, game_params **params)
86 if (i < 0 || i >= lenof(guess_presets))
89 *name = dupstr(guess_presets[i].name);
91 * get round annoying const issues
94 game_params tmp = guess_presets[i].params;
95 *params = dup_params(&tmp);
101 static void decode_params(game_params *params, char const *string)
103 char const *p = string;
104 game_params *defs = default_params();
106 *params = *defs; free_params(defs);
111 params->ncolours = atoi(p);
112 while (*p && isdigit((unsigned char)*p)) p++;
116 params->npegs = atoi(p);
117 while (*p && isdigit((unsigned char)*p)) p++;
121 params->nguesses = atoi(p);
122 while (*p && isdigit((unsigned char)*p)) p++;
126 params->allow_blank = 1;
130 params->allow_blank = 0;
134 params->allow_multiple = 1;
138 params->allow_multiple = 0;
147 static char *encode_params(game_params *params, int full)
151 sprintf(data, "c%dp%dg%d%s%s",
152 params->ncolours, params->npegs, params->nguesses,
153 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
158 static config_item *game_configure(game_params *params)
163 ret = snewn(6, config_item);
165 ret[0].name = "Colours";
166 ret[0].type = C_STRING;
167 sprintf(buf, "%d", params->ncolours);
168 ret[0].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].sval = dupstr(buf);
177 ret[2].name = "Guesses";
178 ret[2].type = C_STRING;
179 sprintf(buf, "%d", params->nguesses);
180 ret[2].sval = dupstr(buf);
183 ret[3].name = "Allow blanks";
184 ret[3].type = C_BOOLEAN;
186 ret[3].ival = params->allow_blank;
188 ret[4].name = "Allow duplicates";
189 ret[4].type = C_BOOLEAN;
191 ret[4].ival = params->allow_multiple;
201 static game_params *custom_params(config_item *cfg)
203 game_params *ret = snew(game_params);
205 ret->ncolours = atoi(cfg[0].sval);
206 ret->npegs = atoi(cfg[1].sval);
207 ret->nguesses = atoi(cfg[2].sval);
209 ret->allow_blank = cfg[3].ival;
210 ret->allow_multiple = cfg[4].ival;
215 static char *validate_params(game_params *params)
217 if (params->ncolours < 2 || params->npegs < 2)
218 return "Trivial solutions are uninteresting";
219 /* NB as well as the no. of colours we define, max(ncolours) must
220 * also fit in an unsigned char; see new_game_desc. */
221 if (params->ncolours > 10)
222 return "Too many colours";
223 if (params->nguesses < 1)
224 return "Must have at least one guess";
225 if (!params->allow_multiple && params->ncolours < params->npegs)
226 return "Disallowing multiple colours requires at least as many colours as pegs";
230 static pegrow new_pegrow(int npegs)
232 pegrow pegs = snew(struct pegrow);
235 pegs->pegs = snewn(pegs->npegs, int);
236 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
237 pegs->feedback = snewn(pegs->npegs, int);
238 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
243 static pegrow dup_pegrow(pegrow pegs)
245 pegrow newpegs = new_pegrow(pegs->npegs);
247 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
248 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
253 static void invalidate_pegrow(pegrow pegs)
255 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
256 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
259 static void free_pegrow(pegrow pegs)
262 sfree(pegs->feedback);
266 static char *new_game_desc(game_params *params, random_state *rs,
267 game_aux_info **aux, int interactive)
269 unsigned char *bmp = snewn(params->npegs, unsigned char);
272 pegrow colcount = new_pegrow(params->ncolours);
274 for (i = 0; i < params->npegs; i++) {
276 c = random_upto(rs, params->ncolours);
277 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
279 bmp[i] = (unsigned char)(c+1);
281 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
283 ret = bin2hex(bmp, params->npegs);
285 free_pegrow(colcount);
289 static void game_free_aux_info(game_aux_info *aux)
291 assert(!"Shouldn't happen");
294 static char *validate_desc(game_params *params, char *desc)
299 /* desc is just an (obfuscated) bitmap of the solution; check that
300 * it's the correct length and (when unobfuscated) contains only
301 * sensible colours. */
302 if (strlen(desc) != params->npegs * 2)
303 return "Game description is wrong length";
304 bmp = hex2bin(desc, params->npegs);
305 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
306 for (i = 0; i < params->npegs; i++) {
307 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
309 return "Game description is corrupted";
317 static game_state *new_game(midend_data *me, game_params *params, char *desc)
319 game_state *state = snew(game_state);
323 state->params = *params;
324 state->guesses = snewn(params->nguesses, pegrow);
325 for (i = 0; i < params->nguesses; i++)
326 state->guesses[i] = new_pegrow(params->npegs);
327 state->solution = new_pegrow(params->npegs);
329 bmp = hex2bin(desc, params->npegs);
330 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
331 for (i = 0; i < params->npegs; i++)
332 state->solution->pegs[i] = (int)bmp[i];
335 state->next_go = state->solved = 0;
340 static game_state *dup_game(game_state *state)
342 game_state *ret = snew(game_state);
347 ret->guesses = snewn(state->params.nguesses, pegrow);
348 for (i = 0; i < state->params.nguesses; i++)
349 ret->guesses[i] = dup_pegrow(state->guesses[i]);
350 ret->solution = dup_pegrow(state->solution);
355 static void free_game(game_state *state)
359 free_pegrow(state->solution);
360 for (i = 0; i < state->params.nguesses; i++)
361 free_pegrow(state->guesses[i]);
362 sfree(state->guesses);
367 static char *solve_game(game_state *state, game_state *currstate,
368 game_aux_info *aux, char **error)
373 static char *game_text_format(game_state *state)
379 pegrow curr_pegs; /* half-finished current move */
381 int colour_cur; /* position of up-down colour picker cursor */
382 int peg_cur; /* position of left-right peg picker cursor */
383 int display_cur, markable;
385 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
386 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
389 static game_ui *new_ui(game_state *state)
391 game_ui *ui = snew(struct game_ui);
392 memset(ui, 0, sizeof(struct game_ui));
393 ui->curr_pegs = new_pegrow(state->params.npegs);
394 ui->holds = snewn(state->params.npegs, int);
395 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
400 static void free_ui(game_ui *ui)
402 free_pegrow(ui->curr_pegs);
407 char *encode_ui(game_ui *ui)
412 void decode_ui(game_ui *ui, char *encoding)
416 static void game_changed_state(game_ui *ui, game_state *oldstate,
417 game_state *newstate)
421 /* just clear the row-in-progress when we have an undo/redo. */
422 for (i = 0; i < ui->curr_pegs->npegs; i++)
423 ui->curr_pegs->pegs[i] = 0;
424 ui->markable = FALSE;
427 #define PEGSZ (ds->pegsz)
428 #define PEGOFF (ds->pegsz + ds->gapsz)
429 #define HINTSZ (ds->hintsz)
430 #define HINTOFF (ds->hintsz + ds->gapsz)
432 #define GAP (ds->gapsz)
433 #define CGAP (ds->gapsz / 2)
435 #define PEGRAD (ds->pegrad)
436 #define HINTRAD (ds->hintrad)
438 #define COL_OX (ds->colx)
439 #define COL_OY (ds->coly)
440 #define COL_X(c) (COL_OX)
441 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
443 #define COL_H (ds->colours->npegs*PEGOFF)
445 #define GUESS_OX (ds->guessx)
446 #define GUESS_OY (ds->guessy)
447 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
448 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
449 #define GUESS_W (ds->solution->npegs*PEGOFF)
450 #define GUESS_H (ds->nguesses*PEGOFF)
452 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
453 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
454 #define HINT_X(g) HINT_OX
455 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
456 #define HINT_W ((ds->hintw*HINTOFF) - GAP)
457 #define HINT_H GUESS_H
459 #define SOLN_OX GUESS_OX
460 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
461 #define SOLN_W GUESS_W
462 #define SOLN_H PEGOFF
464 struct game_drawstate {
466 pegrow *guesses; /* same size as state->guesses */
467 pegrow solution; /* only displayed if state->solved */
468 pegrow colours; /* length ncolours, not npegs */
470 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
471 int pegrad, hintrad; /* radius of peg, hint */
473 int colx, coly; /* origin of colours vertical bar */
474 int guessx, guessy; /* origin of guesses */
475 int solnx, solny; /* origin of solution */
476 int hintw; /* no. of hint tiles we're wide per row */
477 int w, h, started, solved;
482 int drag_col, blit_ox, blit_oy;
485 static int is_markable(game_params *params, pegrow pegs)
487 int i, nset = 0, nrequired, ret = 0;
488 pegrow colcount = new_pegrow(params->ncolours);
490 nrequired = params->allow_blank ? 1 : params->npegs;
492 for (i = 0; i < params->npegs; i++) {
493 int c = pegs->pegs[i];
495 colcount->pegs[c-1]++;
499 if (nset < nrequired) goto done;
501 if (!params->allow_multiple) {
502 for (i = 0; i < params->ncolours; i++) {
503 if (colcount->pegs[i] > 1) goto done;
508 free_pegrow(colcount);
512 static void set_peg(game_params *params, game_ui *ui, int peg, int col)
514 ui->curr_pegs->pegs[peg] = col;
515 ui->markable = is_markable(params, ui->curr_pegs);
518 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
520 int nc_place = 0, nc_colour = 0, i, j;
522 assert(guess && solution && (guess->npegs == solution->npegs));
524 for (i = 0; i < guess->npegs; i++) {
525 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
528 /* slight bit of cleverness: we have the following formula, from
529 * http://mathworld.wolfram.com/Mastermind.html that gives:
531 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
533 * I think this is due to Knuth.
535 for (i = 1; i <= ncols; i++) {
536 int n_guess = 0, n_solution = 0;
537 for (j = 0; j < guess->npegs; j++) {
538 if (guess->pegs[j] == i) n_guess++;
539 if (solution->pegs[j] == i) n_solution++;
541 nc_colour += min(n_guess, n_solution);
543 nc_colour -= nc_place;
545 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
546 guess->npegs, nc_place, nc_colour));
547 assert((nc_colour + nc_place) <= guess->npegs);
549 memset(guess->feedback, 0, guess->npegs*sizeof(int));
550 for (i = 0, j = 0; i < nc_place; i++)
551 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
552 for (i = 0; i < nc_colour; i++)
553 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
558 static char *encode_move(game_state *from, game_ui *ui)
564 len = ui->curr_pegs->npegs * 20 + 2;
565 buf = snewn(len, char);
569 for (i = 0; i < ui->curr_pegs->npegs; i++) {
570 p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]);
574 assert(p - buf <= len);
575 buf = sresize(buf, len, char);
577 tmppegs = dup_pegrow(ui->curr_pegs);
578 solved = mark_pegs(tmppegs, from->solution, from->params.ncolours);
579 solved = (solved == from->params.ncolours);
580 free_pegrow(tmppegs);
582 for (i = 0; i < from->solution->npegs; i++) {
583 if (!ui->holds[i] || solved) {
584 ui->curr_pegs->pegs[i] = 0;
586 if (solved) ui->holds[i] = 0;
588 ui->markable = is_markable(&from->params, ui->curr_pegs);
589 if (!ui->markable && ui->peg_cur == from->solution->npegs)
595 static char *interpret_move(game_state *from, game_ui *ui, game_drawstate *ds,
596 int x, int y, int button)
598 int over_col = 0; /* one-indexed */
599 int over_guess = -1; /* zero-indexed */
600 int over_past_guess_y = -1; /* zero-indexed */
601 int over_past_guess_x = -1; /* zero-indexed */
602 int over_hint = 0; /* zero or one */
605 int guess_ox = GUESS_X(from->next_go, 0);
606 int guess_oy = GUESS_Y(from->next_go, 0);
608 if (from->solved) return NULL;
610 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
611 y >= COL_OY && y <= (COL_OY + COL_H)) {
612 over_col = ((y - COL_OY) / PEGOFF) + 1;
613 } else if (x >= guess_ox &&
614 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
615 if (x <= (guess_ox + GUESS_W)) {
616 over_guess = (x - guess_ox) / PEGOFF;
620 } else if (x >= guess_ox &&
621 y >= GUESS_OY && y < guess_oy) {
622 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
623 over_past_guess_x = (x - guess_ox) / PEGOFF;
625 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
626 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
627 over_past_guess_x, over_past_guess_y));
629 assert(ds->blit_peg);
632 if (button == LEFT_BUTTON) {
634 ui->drag_col = over_col;
636 debug(("Start dragging from colours"));
637 } else if (over_guess > -1) {
638 int col = ui->curr_pegs->pegs[over_guess];
641 ui->drag_opeg = over_guess;
642 debug(("Start dragging from a guess"));
644 } else if (over_past_guess_y > -1) {
646 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
650 debug(("Start dragging from a past guess"));
656 debug(("Start dragging, col = %d, (%d,%d)",
657 ui->drag_col, ui->drag_x, ui->drag_y));
660 } else if (button == LEFT_DRAG && ui->drag_col) {
663 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
665 } else if (button == LEFT_RELEASE && ui->drag_col) {
666 if (over_guess > -1) {
667 debug(("Dropping colour %d onto guess peg %d",
668 ui->drag_col, over_guess));
669 set_peg(&from->params, ui, over_guess, ui->drag_col);
671 if (ui->drag_opeg > -1) {
672 debug(("Removing colour %d from peg %d",
673 ui->drag_col, ui->drag_opeg));
674 set_peg(&from->params, ui, ui->drag_opeg, 0);
680 debug(("Stop dragging."));
682 } else if (button == RIGHT_BUTTON) {
683 if (over_guess > -1) {
684 /* we use ths feedback in the game_ui to signify
685 * 'carry this peg to the next guess as well'. */
686 ui->holds[over_guess] = 1 - ui->holds[over_guess];
689 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
690 /* NB this won't trigger if on the end of a drag; that's on
691 * purpose, in case you drop by mistake... */
692 ret = encode_move(from, ui);
696 if (button == CURSOR_UP || button == CURSOR_DOWN) {
698 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
700 if (button == CURSOR_UP && ui->colour_cur > 0)
703 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
704 int maxcur = from->params.npegs;
705 if (ui->markable) maxcur++;
708 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
710 if (button == CURSOR_LEFT && ui->peg_cur > 0)
713 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
716 if (ui->peg_cur == from->params.npegs) {
717 ret = encode_move(from, ui);
719 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
722 } else if (button == 'H' || button == 'h') {
724 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
730 static game_state *execute_move(game_state *from, char *move)
736 if (!strcmp(move, "S")) {
737 ret = dup_game(from);
740 } else if (move[0] == 'G') {
743 ret = dup_game(from);
745 for (i = 0; i < from->solution->npegs; i++) {
747 if (val <= 0 || val > from->params.ncolours) {
751 ret->guesses[from->next_go]->pegs[i] = atoi(p);
752 while (*p && isdigit((unsigned char)*p)) p++;
756 nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
758 if (nc_place == ret->solution->npegs) {
759 ret->solved = 1; /* win! */
761 ret->next_go = from->next_go + 1;
762 if (ret->next_go >= ret->params.nguesses)
763 ret->solved = 1; /* 'lose' so we show the pegs. */
771 /* ----------------------------------------------------------------------
775 #define PEG_PREFER_SZ 32
777 /* next three are multipliers for pegsz. It will look much nicer if
778 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
780 #define PEG_HINT 0.35
784 static void game_size(game_params *params, game_drawstate *ds,
785 int *x, int *y, int expand)
787 double hmul, vmul_c, vmul_g, vmul, szx, szy;
788 int sz, colh, guessh;
790 hmul = BORDER * 2.0 + /* border */
791 1.0 * 2.0 + /* vertical colour bar */
792 1.0 * params->npegs + /* guess pegs */
793 PEG_GAP * params->npegs + /* guess gaps */
794 PEG_HINT * ds->hintw + /* hint pegs */
795 PEG_GAP * (ds->hintw - 1); /* hint gaps */
797 vmul_c = BORDER * 2.0 + /* border */
798 1.0 * params->ncolours + /* colour pegs */
799 PEG_GAP * (params->ncolours - 1); /* colour gaps */
801 vmul_g = BORDER * 2.0 + /* border */
802 1.0 * (params->nguesses + 1) + /* guesses plus solution */
803 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
805 vmul = max(vmul_c, vmul_g);
809 sz = max(min((int)szx, (int)szy), 1);
813 ds->pegsz = min(sz, PEG_PREFER_SZ);
815 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
816 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
817 ds->border = (int)((double)ds->pegsz * BORDER);
819 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
820 ds->hintrad = (ds->hintsz-1)/2;
822 *x = (int)ceil((double)ds->pegsz * hmul);
823 *y = (int)ceil((double)ds->pegsz * vmul);
824 ds->w = *x; ds->h = *y;
826 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
827 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
828 guessh += ds->gapsz + ds->pegsz; /* solution */
830 ds->colx = ds->border;
831 ds->coly = (*y - colh) / 2;
833 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
834 ds->guessy = (*y - guessh) / 2;
835 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
838 if (ds->blit_peg) blitter_free(ds->blit_peg);
839 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
843 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
845 float *ret = snewn(3 * NCOLOURS, float), max;
848 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
851 ret[COL_1 * 3 + 0] = 1.0F;
852 ret[COL_1 * 3 + 1] = 0.0F;
853 ret[COL_1 * 3 + 2] = 0.0F;
855 /* yellow (toned down a bit due to pale grey background) */
856 ret[COL_2 * 3 + 0] = 0.7F;
857 ret[COL_2 * 3 + 1] = 0.7F;
858 ret[COL_2 * 3 + 2] = 0.0F;
860 /* green (also toned down) */
861 ret[COL_3 * 3 + 0] = 0.0F;
862 ret[COL_3 * 3 + 1] = 0.5F;
863 ret[COL_3 * 3 + 2] = 0.0F;
866 ret[COL_4 * 3 + 0] = 0.0F;
867 ret[COL_4 * 3 + 1] = 0.0F;
868 ret[COL_4 * 3 + 2] = 1.0F;
871 ret[COL_5 * 3 + 0] = 1.0F;
872 ret[COL_5 * 3 + 1] = 0.5F;
873 ret[COL_5 * 3 + 2] = 0.0F;
876 ret[COL_6 * 3 + 0] = 0.5F;
877 ret[COL_6 * 3 + 1] = 0.0F;
878 ret[COL_6 * 3 + 2] = 0.7F;
881 ret[COL_7 * 3 + 0] = 0.4F;
882 ret[COL_7 * 3 + 1] = 0.2F;
883 ret[COL_7 * 3 + 2] = 0.2F;
886 ret[COL_8 * 3 + 0] = 0.4F;
887 ret[COL_8 * 3 + 1] = 0.7F;
888 ret[COL_8 * 3 + 2] = 1.0F;
891 ret[COL_9 * 3 + 0] = 0.5F;
892 ret[COL_9 * 3 + 1] = 0.8F;
893 ret[COL_9 * 3 + 2] = 0.5F;
896 ret[COL_10 * 3 + 0] = 1.0F;
897 ret[COL_10 * 3 + 1] = 0.6F;
898 ret[COL_10 * 3 + 2] = 1.0F;
900 ret[COL_FRAME * 3 + 0] = 0.0F;
901 ret[COL_FRAME * 3 + 1] = 0.0F;
902 ret[COL_FRAME * 3 + 2] = 0.0F;
904 ret[COL_CURSOR * 3 + 0] = 0.0F;
905 ret[COL_CURSOR * 3 + 1] = 0.0F;
906 ret[COL_CURSOR * 3 + 2] = 0.0F;
908 ret[COL_FLASH * 3 + 0] = 0.5F;
909 ret[COL_FLASH * 3 + 1] = 1.0F;
910 ret[COL_FLASH * 3 + 2] = 1.0F;
912 ret[COL_HOLD * 3 + 0] = 1.0F;
913 ret[COL_HOLD * 3 + 1] = 0.5F;
914 ret[COL_HOLD * 3 + 2] = 0.5F;
916 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
917 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
918 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
920 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
921 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
922 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
924 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
925 * (which we hard-code as white) from COL_BACKGROUND (which
926 * could default to white on some platforms).
927 * Code borrowed from fifteen.c. */
928 max = ret[COL_BACKGROUND*3];
929 for (i = 1; i < 3; i++)
930 if (ret[COL_BACKGROUND*3+i] > max)
931 max = ret[COL_BACKGROUND*3+i];
932 if (max * 1.2F > 1.0F) {
933 for (i = 0; i < 3; i++)
934 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
937 /* We also want to be able to tell the difference between BACKGROUND
938 * and EMPTY, for similar distinguishing-hint reasons. */
939 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
940 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
941 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
943 *ncolours = NCOLOURS;
947 static game_drawstate *game_new_drawstate(game_state *state)
949 struct game_drawstate *ds = snew(struct game_drawstate);
952 memset(ds, 0, sizeof(struct game_drawstate));
954 ds->guesses = snewn(state->params.nguesses, pegrow);
955 ds->nguesses = state->params.nguesses;
956 for (i = 0; i < state->params.nguesses; i++) {
957 ds->guesses[i] = new_pegrow(state->params.npegs);
958 invalidate_pegrow(ds->guesses[i]);
960 ds->solution = new_pegrow(state->params.npegs);
961 invalidate_pegrow(ds->solution);
962 ds->colours = new_pegrow(state->params.ncolours);
963 invalidate_pegrow(ds->colours);
965 ds->hintw = (state->params.npegs+1)/2; /* must round up */
972 static void game_free_drawstate(game_drawstate *ds)
976 if (ds->blit_peg) blitter_free(ds->blit_peg);
977 free_pegrow(ds->colours);
978 free_pegrow(ds->solution);
979 for (i = 0; i < ds->nguesses; i++)
980 free_pegrow(ds->guesses[i]);
985 static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
989 * Some platforms antialias circles, which means we shouldn't
990 * overwrite a circle of one colour with a circle of another
991 * colour without erasing the background first. However, if the
992 * peg is the one being dragged, we don't erase the background
993 * because we _want_ it to alpha-blend nicely into whatever's
997 draw_rect(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
1000 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
1001 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
1003 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
1004 draw_update(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1007 static void draw_cursor(frontend *fe, game_drawstate *ds, int x, int y)
1009 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, 0, COL_CURSOR);
1011 draw_update(fe, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1014 static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
1015 pegrow src, int *holds, int cur_col, int force)
1018 int rowx, rowy, i, scol;
1021 dest = ds->solution;
1025 dest = ds->guesses[guess];
1026 rowx = GUESS_X(guess,0);
1027 rowy = GUESS_Y(guess,0);
1029 if (src) assert(src->npegs == dest->npegs);
1031 for (i = 0; i < dest->npegs; i++) {
1032 scol = src ? src->pegs[i] : 0;
1035 if (holds && holds[i])
1037 if ((dest->pegs[i] != scol) || force) {
1038 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000);
1042 draw_rect(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1043 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
1044 draw_update(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1047 draw_cursor(fe, ds, rowx + PEGOFF * i, rowy);
1049 dest->pegs[i] = scol;
1053 static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
1054 pegrow src, int force, int cursor, int markable)
1056 pegrow dest = ds->guesses[guess];
1057 int rowx, rowy, i, scol, col, hintlen;
1059 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
1061 if (src) assert(src->npegs == dest->npegs);
1063 hintlen = (dest->npegs + 1)/2;
1066 * Because of the possible presence of the cursor around this
1067 * entire section, we redraw all or none of it but never part.
1069 need_redraw = FALSE;
1071 for (i = 0; i < dest->npegs; i++) {
1072 scol = src ? src->feedback[i] : 0;
1073 if (i == 0 && cursor)
1075 if (i == 0 && markable)
1077 if ((scol != dest->feedback[i]) || force) {
1080 dest->feedback[i] = scol;
1084 int hinth = HINTSZ + GAP + HINTSZ;
1087 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1088 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1090 /* erase a large background rectangle */
1091 draw_rect(fe, hx, hy, hw, hh, COL_BACKGROUND);
1093 for (i = 0; i < dest->npegs; i++) {
1094 scol = src ? src->feedback[i] : 0;
1095 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1096 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1099 rowx = HINT_X(guess);
1100 rowy = HINT_Y(guess);
1102 rowx += HINTOFF * i;
1104 rowx += HINTOFF * (i - hintlen);
1108 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
1109 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
1111 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
1116 x1 = hx + CGAP; y1 = hy + CGAP;
1117 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1118 draw_line(fe, x1, y1, x2, y1, COL_CURSOR);
1119 draw_line(fe, x2, y1, x2, y2, COL_CURSOR);
1120 draw_line(fe, x2, y2, x1, y2, COL_CURSOR);
1121 draw_line(fe, x1, y2, x1, y1, COL_CURSOR);
1124 draw_update(fe, hx, hy, hw, hh);
1128 static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
1130 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1132 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1133 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1136 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1137 game_state *state, int dir, game_ui *ui,
1138 float animtime, float flashtime)
1140 int i, new_move, last_go;
1142 new_move = (state->next_go != ds->next_go) || !ds->started;
1143 last_go = (state->next_go == state->params.nguesses-1);
1146 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1147 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1148 draw_update(fe, 0, 0, ds->w, ds->h);
1151 if (ds->drag_col != 0) {
1152 debug(("Loading from blitter."));
1153 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1154 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1157 /* draw the colours */
1158 for (i = 0; i < state->params.ncolours; i++) {
1160 if (ui->display_cur && ui->colour_cur == i)
1162 if (ds->colours->pegs[i] != val) {
1163 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
1165 draw_cursor(fe, ds, COL_X(i), COL_Y(i));
1166 ds->colours->pegs[i] = val;
1170 /* draw the guesses (so far) and the hints */
1171 for (i = 0; i < state->params.nguesses; i++) {
1172 if (state->next_go > i || state->solved) {
1173 /* this info is stored in the game_state already */
1174 guess_redraw(fe, ds, i, state->guesses[i], NULL, -1, 0);
1175 hint_redraw(fe, ds, i, state->guesses[i],
1176 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
1177 } else if (state->next_go == i) {
1178 /* this is the one we're on; the (incomplete) guess is
1179 * stored in the game_ui. */
1180 guess_redraw(fe, ds, i, ui->curr_pegs,
1181 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0);
1182 hint_redraw(fe, ds, i, NULL, 1,
1183 ui->display_cur && ui->peg_cur == state->params.npegs,
1186 /* we've not got here yet; it's blank. */
1187 guess_redraw(fe, ds, i, NULL, NULL, -1, 0);
1188 hint_redraw(fe, ds, i, NULL, 0, FALSE, FALSE);
1192 /* draw the 'current move' and 'able to mark' sign. */
1194 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1196 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1198 /* draw the solution (or the big rectangle) */
1199 if ((state->solved != ds->solved) || !ds->started) {
1200 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1201 state->solved ? COL_BACKGROUND : COL_EMPTY);
1202 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1205 guess_redraw(fe, ds, -1, state->solution, NULL, -1, !ds->solved);
1206 ds->solved = state->solved;
1208 ds->next_go = state->next_go;
1210 /* if ui->drag_col != 0, save the screen to the blitter,
1211 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1212 if (ui->drag_col != 0) {
1213 int ox = ui->drag_x - (PEGSZ/2);
1214 int oy = ui->drag_y - (PEGSZ/2);
1215 debug(("Saving to blitter at (%d,%d)", ox, oy));
1216 blitter_save(fe, ds->blit_peg, ox, oy);
1217 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
1219 ds->blit_ox = ox; ds->blit_oy = oy;
1221 ds->drag_col = ui->drag_col;
1226 static float game_anim_length(game_state *oldstate, game_state *newstate,
1227 int dir, game_ui *ui)
1232 static float game_flash_length(game_state *oldstate, game_state *newstate,
1233 int dir, game_ui *ui)
1238 static int game_wants_statusbar(void)
1243 static int game_timing_state(game_state *state)
1249 #define thegame guess
1252 const struct game thegame = {
1253 "Guess", "games.guess",
1260 TRUE, game_configure, custom_params,
1269 FALSE, game_text_format,
1280 game_free_drawstate,
1284 game_wants_statusbar,
1285 FALSE, game_timing_state,
1286 0, /* mouse_priorities */
1289 /* vim: set shiftwidth=4 tabstop=8: */