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 static void game_changed_state(game_ui *ui, game_state *oldstate,
408 game_state *newstate)
412 /* just clear the row-in-progress when we have an undo/redo. */
413 for (i = 0; i < ui->curr_pegs->npegs; i++)
414 ui->curr_pegs->pegs[i] = 0;
415 ui->markable = FALSE;
418 #define PEGSZ (ds->pegsz)
419 #define PEGOFF (ds->pegsz + ds->gapsz)
420 #define HINTSZ (ds->hintsz)
421 #define HINTOFF (ds->hintsz + ds->gapsz)
423 #define GAP (ds->gapsz)
424 #define CGAP (ds->gapsz / 2)
426 #define PEGRAD (ds->pegrad)
427 #define HINTRAD (ds->hintrad)
429 #define COL_OX (ds->colx)
430 #define COL_OY (ds->coly)
431 #define COL_X(c) (COL_OX)
432 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
434 #define COL_H (ds->colours->npegs*PEGOFF)
436 #define GUESS_OX (ds->guessx)
437 #define GUESS_OY (ds->guessy)
438 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
439 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
440 #define GUESS_W (ds->solution->npegs*PEGOFF)
441 #define GUESS_H (ds->nguesses*PEGOFF)
443 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
444 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
445 #define HINT_X(g) HINT_OX
446 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
447 #define HINT_W ((ds->hintw*HINTOFF) - GAP)
448 #define HINT_H GUESS_H
450 #define SOLN_OX GUESS_OX
451 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
452 #define SOLN_W GUESS_W
453 #define SOLN_H PEGOFF
455 struct game_drawstate {
457 pegrow *guesses; /* same size as state->guesses */
458 pegrow solution; /* only displayed if state->solved */
459 pegrow colours; /* length ncolours, not npegs */
461 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
462 int pegrad, hintrad; /* radius of peg, hint */
464 int colx, coly; /* origin of colours vertical bar */
465 int guessx, guessy; /* origin of guesses */
466 int solnx, solny; /* origin of solution */
467 int hintw; /* no. of hint tiles we're wide per row */
468 int w, h, started, solved;
473 int drag_col, blit_ox, blit_oy;
476 static int is_markable(game_params *params, pegrow pegs)
478 int i, nset = 0, nrequired, ret = 0;
479 pegrow colcount = new_pegrow(params->ncolours);
481 nrequired = params->allow_blank ? 1 : params->npegs;
483 for (i = 0; i < params->npegs; i++) {
484 int c = pegs->pegs[i];
486 colcount->pegs[c-1]++;
490 if (nset < nrequired) goto done;
492 if (!params->allow_multiple) {
493 for (i = 0; i < params->ncolours; i++) {
494 if (colcount->pegs[i] > 1) goto done;
499 free_pegrow(colcount);
503 static void set_peg(game_params *params, game_ui *ui, int peg, int col)
505 ui->curr_pegs->pegs[peg] = col;
506 ui->markable = is_markable(params, ui->curr_pegs);
509 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
511 int nc_place = 0, nc_colour = 0, i, j;
513 assert(guess && solution && (guess->npegs == solution->npegs));
515 for (i = 0; i < guess->npegs; i++) {
516 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
519 /* slight bit of cleverness: we have the following formula, from
520 * http://mathworld.wolfram.com/Mastermind.html that gives:
522 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
524 * I think this is due to Knuth.
526 for (i = 1; i <= ncols; i++) {
527 int n_guess = 0, n_solution = 0;
528 for (j = 0; j < guess->npegs; j++) {
529 if (guess->pegs[j] == i) n_guess++;
530 if (solution->pegs[j] == i) n_solution++;
532 nc_colour += min(n_guess, n_solution);
534 nc_colour -= nc_place;
536 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
537 guess->npegs, nc_place, nc_colour));
538 assert((nc_colour + nc_place) <= guess->npegs);
540 memset(guess->feedback, 0, guess->npegs*sizeof(int));
541 for (i = 0, j = 0; i < nc_place; i++)
542 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
543 for (i = 0; i < nc_colour; i++)
544 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
549 static char *encode_move(game_state *from, game_ui *ui)
555 len = ui->curr_pegs->npegs * 20 + 2;
556 buf = snewn(len, char);
560 for (i = 0; i < ui->curr_pegs->npegs; i++) {
561 p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]);
565 assert(p - buf <= len);
566 buf = sresize(buf, len, char);
568 tmppegs = dup_pegrow(ui->curr_pegs);
569 solved = mark_pegs(tmppegs, from->solution, from->params.ncolours);
570 solved = (solved == from->params.ncolours);
571 free_pegrow(tmppegs);
573 for (i = 0; i < from->solution->npegs; i++) {
574 if (!ui->holds[i] || solved) {
575 ui->curr_pegs->pegs[i] = 0;
577 if (solved) ui->holds[i] = 0;
579 ui->markable = is_markable(&from->params, ui->curr_pegs);
580 if (!ui->markable && ui->peg_cur == from->solution->npegs)
586 static char *interpret_move(game_state *from, game_ui *ui, game_drawstate *ds,
587 int x, int y, int button)
589 int over_col = 0; /* one-indexed */
590 int over_guess = -1; /* zero-indexed */
591 int over_past_guess_y = -1; /* zero-indexed */
592 int over_past_guess_x = -1; /* zero-indexed */
593 int over_hint = 0; /* zero or one */
596 int guess_ox = GUESS_X(from->next_go, 0);
597 int guess_oy = GUESS_Y(from->next_go, 0);
599 if (from->solved) return NULL;
601 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
602 y >= COL_OY && y <= (COL_OY + COL_H)) {
603 over_col = ((y - COL_OY) / PEGOFF) + 1;
604 } else if (x >= guess_ox &&
605 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
606 if (x <= (guess_ox + GUESS_W)) {
607 over_guess = (x - guess_ox) / PEGOFF;
611 } else if (x >= guess_ox &&
612 y >= GUESS_OY && y < guess_oy) {
613 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
614 over_past_guess_x = (x - guess_ox) / PEGOFF;
616 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
617 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
618 over_past_guess_x, over_past_guess_y));
620 assert(ds->blit_peg);
623 if (button == LEFT_BUTTON) {
625 ui->drag_col = over_col;
627 debug(("Start dragging from colours"));
628 } else if (over_guess > -1) {
629 int col = ui->curr_pegs->pegs[over_guess];
632 ui->drag_opeg = over_guess;
633 debug(("Start dragging from a guess"));
635 } else if (over_past_guess_y > -1) {
637 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
641 debug(("Start dragging from a past guess"));
647 debug(("Start dragging, col = %d, (%d,%d)",
648 ui->drag_col, ui->drag_x, ui->drag_y));
651 } else if (button == LEFT_DRAG && ui->drag_col) {
654 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
656 } else if (button == LEFT_RELEASE && ui->drag_col) {
657 if (over_guess > -1) {
658 debug(("Dropping colour %d onto guess peg %d",
659 ui->drag_col, over_guess));
660 set_peg(&from->params, ui, over_guess, ui->drag_col);
662 if (ui->drag_opeg > -1) {
663 debug(("Removing colour %d from peg %d",
664 ui->drag_col, ui->drag_opeg));
665 set_peg(&from->params, ui, ui->drag_opeg, 0);
671 debug(("Stop dragging."));
673 } else if (button == RIGHT_BUTTON) {
674 if (over_guess > -1) {
675 /* we use ths feedback in the game_ui to signify
676 * 'carry this peg to the next guess as well'. */
677 ui->holds[over_guess] = 1 - ui->holds[over_guess];
680 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
681 /* NB this won't trigger if on the end of a drag; that's on
682 * purpose, in case you drop by mistake... */
683 ret = encode_move(from, ui);
687 if (button == CURSOR_UP || button == CURSOR_DOWN) {
689 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
691 if (button == CURSOR_UP && ui->colour_cur > 0)
694 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
695 int maxcur = from->params.npegs;
696 if (ui->markable) maxcur++;
699 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
701 if (button == CURSOR_LEFT && ui->peg_cur > 0)
704 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
707 if (ui->peg_cur == from->params.npegs) {
708 ret = encode_move(from, ui);
710 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
713 } else if (button == 'H' || button == 'h') {
715 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
721 static game_state *execute_move(game_state *from, char *move)
727 if (!strcmp(move, "S")) {
728 ret = dup_game(from);
731 } else if (move[0] == 'G') {
734 ret = dup_game(from);
736 for (i = 0; i < from->solution->npegs; i++) {
738 if (val <= 0 || val > from->params.ncolours) {
742 ret->guesses[from->next_go]->pegs[i] = atoi(p);
743 while (*p && isdigit((unsigned char)*p)) p++;
747 nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
749 if (nc_place == ret->solution->npegs) {
750 ret->solved = 1; /* win! */
752 ret->next_go = from->next_go + 1;
753 if (ret->next_go >= ret->params.nguesses)
754 ret->solved = 1; /* 'lose' so we show the pegs. */
762 /* ----------------------------------------------------------------------
766 #define PEG_PREFER_SZ 32
768 /* next three are multipliers for pegsz. It will look much nicer if
769 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
771 #define PEG_HINT 0.35
775 static void game_size(game_params *params, game_drawstate *ds,
776 int *x, int *y, int expand)
778 double hmul, vmul_c, vmul_g, vmul, szx, szy;
779 int sz, colh, guessh;
781 hmul = BORDER * 2.0 + /* border */
782 1.0 * 2.0 + /* vertical colour bar */
783 1.0 * params->npegs + /* guess pegs */
784 PEG_GAP * params->npegs + /* guess gaps */
785 PEG_HINT * ds->hintw + /* hint pegs */
786 PEG_GAP * (ds->hintw - 1); /* hint gaps */
788 vmul_c = BORDER * 2.0 + /* border */
789 1.0 * params->ncolours + /* colour pegs */
790 PEG_GAP * (params->ncolours - 1); /* colour gaps */
792 vmul_g = BORDER * 2.0 + /* border */
793 1.0 * (params->nguesses + 1) + /* guesses plus solution */
794 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
796 vmul = max(vmul_c, vmul_g);
800 sz = max(min((int)szx, (int)szy), 1);
804 ds->pegsz = min(sz, PEG_PREFER_SZ);
806 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
807 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
808 ds->border = (int)((double)ds->pegsz * BORDER);
810 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
811 ds->hintrad = (ds->hintsz-1)/2;
813 *x = (int)ceil((double)ds->pegsz * hmul);
814 *y = (int)ceil((double)ds->pegsz * vmul);
815 ds->w = *x; ds->h = *y;
817 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
818 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
819 guessh += ds->gapsz + ds->pegsz; /* solution */
821 ds->colx = ds->border;
822 ds->coly = (*y - colh) / 2;
824 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
825 ds->guessy = (*y - guessh) / 2;
826 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
829 if (ds->blit_peg) blitter_free(ds->blit_peg);
830 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
834 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
836 float *ret = snewn(3 * NCOLOURS, float), max;
839 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
842 ret[COL_1 * 3 + 0] = 1.0F;
843 ret[COL_1 * 3 + 1] = 0.0F;
844 ret[COL_1 * 3 + 2] = 0.0F;
846 /* yellow (toned down a bit due to pale grey background) */
847 ret[COL_2 * 3 + 0] = 0.7F;
848 ret[COL_2 * 3 + 1] = 0.7F;
849 ret[COL_2 * 3 + 2] = 0.0F;
851 /* green (also toned down) */
852 ret[COL_3 * 3 + 0] = 0.0F;
853 ret[COL_3 * 3 + 1] = 0.5F;
854 ret[COL_3 * 3 + 2] = 0.0F;
857 ret[COL_4 * 3 + 0] = 0.0F;
858 ret[COL_4 * 3 + 1] = 0.0F;
859 ret[COL_4 * 3 + 2] = 1.0F;
862 ret[COL_5 * 3 + 0] = 1.0F;
863 ret[COL_5 * 3 + 1] = 0.5F;
864 ret[COL_5 * 3 + 2] = 0.0F;
867 ret[COL_6 * 3 + 0] = 0.5F;
868 ret[COL_6 * 3 + 1] = 0.0F;
869 ret[COL_6 * 3 + 2] = 0.7F;
872 ret[COL_7 * 3 + 0] = 0.4F;
873 ret[COL_7 * 3 + 1] = 0.2F;
874 ret[COL_7 * 3 + 2] = 0.2F;
877 ret[COL_8 * 3 + 0] = 0.4F;
878 ret[COL_8 * 3 + 1] = 0.7F;
879 ret[COL_8 * 3 + 2] = 1.0F;
882 ret[COL_9 * 3 + 0] = 0.5F;
883 ret[COL_9 * 3 + 1] = 0.8F;
884 ret[COL_9 * 3 + 2] = 0.5F;
887 ret[COL_10 * 3 + 0] = 1.0F;
888 ret[COL_10 * 3 + 1] = 0.6F;
889 ret[COL_10 * 3 + 2] = 1.0F;
891 ret[COL_FRAME * 3 + 0] = 0.0F;
892 ret[COL_FRAME * 3 + 1] = 0.0F;
893 ret[COL_FRAME * 3 + 2] = 0.0F;
895 ret[COL_CURSOR * 3 + 0] = 0.0F;
896 ret[COL_CURSOR * 3 + 1] = 0.0F;
897 ret[COL_CURSOR * 3 + 2] = 0.0F;
899 ret[COL_FLASH * 3 + 0] = 0.5F;
900 ret[COL_FLASH * 3 + 1] = 1.0F;
901 ret[COL_FLASH * 3 + 2] = 1.0F;
903 ret[COL_HOLD * 3 + 0] = 1.0F;
904 ret[COL_HOLD * 3 + 1] = 0.5F;
905 ret[COL_HOLD * 3 + 2] = 0.5F;
907 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
908 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
909 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
911 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
912 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
913 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
915 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
916 * (which we hard-code as white) from COL_BACKGROUND (which
917 * could default to white on some platforms).
918 * Code borrowed from fifteen.c. */
919 max = ret[COL_BACKGROUND*3];
920 for (i = 1; i < 3; i++)
921 if (ret[COL_BACKGROUND*3+i] > max)
922 max = ret[COL_BACKGROUND*3+i];
923 if (max * 1.2F > 1.0F) {
924 for (i = 0; i < 3; i++)
925 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
928 /* We also want to be able to tell the difference between BACKGROUND
929 * and EMPTY, for similar distinguishing-hint reasons. */
930 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
931 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
932 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
934 *ncolours = NCOLOURS;
938 static game_drawstate *game_new_drawstate(game_state *state)
940 struct game_drawstate *ds = snew(struct game_drawstate);
943 memset(ds, 0, sizeof(struct game_drawstate));
945 ds->guesses = snewn(state->params.nguesses, pegrow);
946 ds->nguesses = state->params.nguesses;
947 for (i = 0; i < state->params.nguesses; i++) {
948 ds->guesses[i] = new_pegrow(state->params.npegs);
949 invalidate_pegrow(ds->guesses[i]);
951 ds->solution = new_pegrow(state->params.npegs);
952 invalidate_pegrow(ds->solution);
953 ds->colours = new_pegrow(state->params.ncolours);
954 invalidate_pegrow(ds->colours);
956 ds->hintw = (state->params.npegs+1)/2; /* must round up */
963 static void game_free_drawstate(game_drawstate *ds)
967 if (ds->blit_peg) blitter_free(ds->blit_peg);
968 free_pegrow(ds->colours);
969 free_pegrow(ds->solution);
970 for (i = 0; i < ds->nguesses; i++)
971 free_pegrow(ds->guesses[i]);
976 static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
980 * Some platforms antialias circles, which means we shouldn't
981 * overwrite a circle of one colour with a circle of another
982 * colour without erasing the background first. However, if the
983 * peg is the one being dragged, we don't erase the background
984 * because we _want_ it to alpha-blend nicely into whatever's
988 draw_rect(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
991 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
992 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
994 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
995 draw_update(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
998 static void draw_cursor(frontend *fe, game_drawstate *ds, int x, int y)
1000 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, 0, COL_CURSOR);
1002 draw_update(fe, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1005 static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
1006 pegrow src, int *holds, int cur_col, int force)
1009 int rowx, rowy, i, scol;
1012 dest = ds->solution;
1016 dest = ds->guesses[guess];
1017 rowx = GUESS_X(guess,0);
1018 rowy = GUESS_Y(guess,0);
1020 if (src) assert(src->npegs == dest->npegs);
1022 for (i = 0; i < dest->npegs; i++) {
1023 scol = src ? src->pegs[i] : 0;
1026 if (holds && holds[i])
1028 if ((dest->pegs[i] != scol) || force) {
1029 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000);
1033 draw_rect(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1034 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
1035 draw_update(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1038 draw_cursor(fe, ds, rowx + PEGOFF * i, rowy);
1040 dest->pegs[i] = scol;
1044 static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
1045 pegrow src, int force, int cursor, int markable)
1047 pegrow dest = ds->guesses[guess];
1048 int rowx, rowy, i, scol, col, hintlen;
1050 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
1052 if (src) assert(src->npegs == dest->npegs);
1054 hintlen = (dest->npegs + 1)/2;
1057 * Because of the possible presence of the cursor around this
1058 * entire section, we redraw all or none of it but never part.
1060 need_redraw = FALSE;
1062 for (i = 0; i < dest->npegs; i++) {
1063 scol = src ? src->feedback[i] : 0;
1064 if (i == 0 && cursor)
1066 if (i == 0 && markable)
1068 if ((scol != dest->feedback[i]) || force) {
1071 dest->feedback[i] = scol;
1075 int hinth = HINTSZ + GAP + HINTSZ;
1078 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1079 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1081 /* erase a large background rectangle */
1082 draw_rect(fe, hx, hy, hw, hh, COL_BACKGROUND);
1084 for (i = 0; i < dest->npegs; i++) {
1085 scol = src ? src->feedback[i] : 0;
1086 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1087 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1090 rowx = HINT_X(guess);
1091 rowy = HINT_Y(guess);
1093 rowx += HINTOFF * i;
1095 rowx += HINTOFF * (i - hintlen);
1099 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
1100 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
1102 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
1107 x1 = hx + CGAP; y1 = hy + CGAP;
1108 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1109 draw_line(fe, x1, y1, x2, y1, COL_CURSOR);
1110 draw_line(fe, x2, y1, x2, y2, COL_CURSOR);
1111 draw_line(fe, x2, y2, x1, y2, COL_CURSOR);
1112 draw_line(fe, x1, y2, x1, y1, COL_CURSOR);
1115 draw_update(fe, hx, hy, hw, hh);
1119 static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
1121 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1123 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1124 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1127 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1128 game_state *state, int dir, game_ui *ui,
1129 float animtime, float flashtime)
1131 int i, new_move, last_go;
1133 new_move = (state->next_go != ds->next_go) || !ds->started;
1134 last_go = (state->next_go == state->params.nguesses-1);
1137 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1138 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1139 draw_update(fe, 0, 0, ds->w, ds->h);
1142 if (ds->drag_col != 0) {
1143 debug(("Loading from blitter."));
1144 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1145 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1148 /* draw the colours */
1149 for (i = 0; i < state->params.ncolours; i++) {
1151 if (ui->display_cur && ui->colour_cur == i)
1153 if (ds->colours->pegs[i] != val) {
1154 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
1156 draw_cursor(fe, ds, COL_X(i), COL_Y(i));
1157 ds->colours->pegs[i] = val;
1161 /* draw the guesses (so far) and the hints */
1162 for (i = 0; i < state->params.nguesses; i++) {
1163 if (state->next_go > i || state->solved) {
1164 /* this info is stored in the game_state already */
1165 guess_redraw(fe, ds, i, state->guesses[i], NULL, -1, 0);
1166 hint_redraw(fe, ds, i, state->guesses[i],
1167 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
1168 } else if (state->next_go == i) {
1169 /* this is the one we're on; the (incomplete) guess is
1170 * stored in the game_ui. */
1171 guess_redraw(fe, ds, i, ui->curr_pegs,
1172 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0);
1173 hint_redraw(fe, ds, i, NULL, 1,
1174 ui->display_cur && ui->peg_cur == state->params.npegs,
1177 /* we've not got here yet; it's blank. */
1178 guess_redraw(fe, ds, i, NULL, NULL, -1, 0);
1179 hint_redraw(fe, ds, i, NULL, 0, FALSE, FALSE);
1183 /* draw the 'current move' and 'able to mark' sign. */
1185 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1187 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1189 /* draw the solution (or the big rectangle) */
1190 if ((state->solved != ds->solved) || !ds->started) {
1191 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1192 state->solved ? COL_BACKGROUND : COL_EMPTY);
1193 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1196 guess_redraw(fe, ds, -1, state->solution, NULL, -1, !ds->solved);
1197 ds->solved = state->solved;
1199 ds->next_go = state->next_go;
1201 /* if ui->drag_col != 0, save the screen to the blitter,
1202 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1203 if (ui->drag_col != 0) {
1204 int ox = ui->drag_x - (PEGSZ/2);
1205 int oy = ui->drag_y - (PEGSZ/2);
1206 debug(("Saving to blitter at (%d,%d)", ox, oy));
1207 blitter_save(fe, ds->blit_peg, ox, oy);
1208 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
1210 ds->blit_ox = ox; ds->blit_oy = oy;
1212 ds->drag_col = ui->drag_col;
1217 static float game_anim_length(game_state *oldstate, game_state *newstate,
1218 int dir, game_ui *ui)
1223 static float game_flash_length(game_state *oldstate, game_state *newstate,
1224 int dir, game_ui *ui)
1229 static int game_wants_statusbar(void)
1234 static int game_timing_state(game_state *state)
1240 #define thegame guess
1243 const struct game thegame = {
1244 "Guess", "games.guess",
1251 TRUE, game_configure, custom_params,
1260 FALSE, game_text_format,
1269 game_free_drawstate,
1273 game_wants_statusbar,
1274 FALSE, game_timing_state,
1275 0, /* mouse_priorities */
1278 /* vim: set shiftwidth=4 tabstop=8: */