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 int game_fetch_preset(int i, char **name, game_params **params)
68 static void free_params(game_params *params)
73 static game_params *dup_params(game_params *params)
75 game_params *ret = snew(game_params);
76 *ret = *params; /* structure copy */
80 static void decode_params(game_params *params, char const *string)
82 char const *p = string;
83 game_params *defs = default_params();
85 *params = *defs; free_params(defs);
90 params->ncolours = atoi(p);
91 while (*p && isdigit((unsigned char)*p)) p++;
95 params->npegs = atoi(p);
96 while (*p && isdigit((unsigned char)*p)) p++;
100 params->nguesses = atoi(p);
101 while (*p && isdigit((unsigned char)*p)) p++;
105 params->allow_blank = 1;
109 params->allow_blank = 0;
113 params->allow_multiple = 1;
117 params->allow_multiple = 0;
126 static char *encode_params(game_params *params, int full)
130 sprintf(data, "c%dp%dg%d%s%s",
131 params->ncolours, params->npegs, params->nguesses,
132 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
137 static config_item *game_configure(game_params *params)
142 ret = snewn(6, config_item);
144 ret[0].name = "Colours";
145 ret[0].type = C_STRING;
146 sprintf(buf, "%d", params->ncolours);
147 ret[0].sval = dupstr(buf);
150 ret[1].name = "Pegs per guess";
151 ret[1].type = C_STRING;
152 sprintf(buf, "%d", params->npegs);
153 ret[1].sval = dupstr(buf);
156 ret[2].name = "Guesses";
157 ret[2].type = C_STRING;
158 sprintf(buf, "%d", params->nguesses);
159 ret[2].sval = dupstr(buf);
162 ret[3].name = "Allow blanks";
163 ret[3].type = C_BOOLEAN;
165 ret[3].ival = params->allow_blank;
167 ret[4].name = "Allow duplicates";
168 ret[4].type = C_BOOLEAN;
170 ret[4].ival = params->allow_multiple;
180 static game_params *custom_params(config_item *cfg)
182 game_params *ret = snew(game_params);
184 ret->ncolours = atoi(cfg[0].sval);
185 ret->npegs = atoi(cfg[1].sval);
186 ret->nguesses = atoi(cfg[2].sval);
188 ret->allow_blank = cfg[3].ival;
189 ret->allow_multiple = cfg[4].ival;
194 static char *validate_params(game_params *params)
196 if (params->ncolours < 2 || params->npegs < 2)
197 return "Trivial solutions are uninteresting";
198 /* NB as well as the no. of colours we define, max(ncolours) must
199 * also fit in an unsigned char; see new_game_desc. */
200 if (params->ncolours > 10)
201 return "Too many colours";
202 if (params->nguesses < 1)
203 return "Must have at least one guess";
204 if (!params->allow_multiple && params->ncolours < params->npegs)
205 return "Disallowing multiple colours requires at least as many colours as pegs";
209 static pegrow new_pegrow(int npegs)
211 pegrow pegs = snew(struct pegrow);
214 pegs->pegs = snewn(pegs->npegs, int);
215 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
216 pegs->feedback = snewn(pegs->npegs, int);
217 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
222 static pegrow dup_pegrow(pegrow pegs)
224 pegrow newpegs = new_pegrow(pegs->npegs);
226 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
227 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
232 static void invalidate_pegrow(pegrow pegs)
234 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
235 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
238 static void free_pegrow(pegrow pegs)
241 sfree(pegs->feedback);
245 static char *new_game_desc(game_params *params, random_state *rs,
246 game_aux_info **aux, int interactive)
248 unsigned char *bmp = snewn(params->npegs, unsigned char);
251 pegrow colcount = new_pegrow(params->ncolours);
253 for (i = 0; i < params->npegs; i++) {
255 c = random_upto(rs, params->ncolours);
256 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
258 bmp[i] = (unsigned char)(c+1);
260 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
262 ret = bin2hex(bmp, params->npegs);
264 free_pegrow(colcount);
268 static void game_free_aux_info(game_aux_info *aux)
270 assert(!"Shouldn't happen");
273 static char *validate_desc(game_params *params, char *desc)
278 /* desc is just an (obfuscated) bitmap of the solution; check that
279 * it's the correct length and (when unobfuscated) contains only
280 * sensible colours. */
281 if (strlen(desc) != params->npegs * 2)
282 return "Game description is wrong length";
283 bmp = hex2bin(desc, params->npegs);
284 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
285 for (i = 0; i < params->npegs; i++) {
286 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
288 return "Game description is corrupted";
296 static game_state *new_game(midend_data *me, game_params *params, char *desc)
298 game_state *state = snew(game_state);
302 state->params = *params;
303 state->guesses = snewn(params->nguesses, pegrow);
304 for (i = 0; i < params->nguesses; i++)
305 state->guesses[i] = new_pegrow(params->npegs);
306 state->solution = new_pegrow(params->npegs);
308 bmp = hex2bin(desc, params->npegs);
309 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
310 for (i = 0; i < params->npegs; i++)
311 state->solution->pegs[i] = (int)bmp[i];
314 state->next_go = state->solved = 0;
319 static game_state *dup_game(game_state *state)
321 game_state *ret = snew(game_state);
326 ret->guesses = snewn(state->params.nguesses, pegrow);
327 for (i = 0; i < state->params.nguesses; i++)
328 ret->guesses[i] = dup_pegrow(state->guesses[i]);
329 ret->solution = dup_pegrow(state->solution);
334 static void free_game(game_state *state)
338 free_pegrow(state->solution);
339 for (i = 0; i < state->params.nguesses; i++)
340 free_pegrow(state->guesses[i]);
341 sfree(state->guesses);
346 static game_state *solve_game(game_state *state, game_state *currstate,
347 game_aux_info *aux, char **error)
349 game_state *ret = dup_game(currstate);
354 static char *game_text_format(game_state *state)
360 pegrow curr_pegs; /* half-finished current move */
362 int colour_cur; /* position of up-down colour picker cursor */
363 int peg_cur; /* position of left-right peg picker cursor */
364 int display_cur, markable;
366 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
367 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
370 static game_ui *new_ui(game_state *state)
372 game_ui *ui = snew(struct game_ui);
373 memset(ui, 0, sizeof(struct game_ui));
374 ui->curr_pegs = new_pegrow(state->params.npegs);
375 ui->holds = snewn(state->params.npegs, int);
376 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
381 static void free_ui(game_ui *ui)
383 free_pegrow(ui->curr_pegs);
388 static void game_changed_state(game_ui *ui, game_state *oldstate,
389 game_state *newstate)
393 /* just clear the row-in-progress when we have an undo/redo. */
394 for (i = 0; i < ui->curr_pegs->npegs; i++)
395 ui->curr_pegs->pegs[i] = 0;
398 #define PEGSZ (ds->pegsz)
399 #define PEGOFF (ds->pegsz + ds->gapsz)
400 #define HINTSZ (ds->hintsz)
401 #define HINTOFF (ds->hintsz + ds->gapsz)
403 #define CGAP (ds->gapsz / 2)
405 #define PEGRAD (ds->pegrad)
406 #define HINTRAD (ds->hintrad)
408 #define COL_OX (ds->colx)
409 #define COL_OY (ds->coly)
410 #define COL_X(c) (COL_OX)
411 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
413 #define COL_H (ds->colours->npegs*PEGOFF)
415 #define GUESS_OX (ds->guessx)
416 #define GUESS_OY (ds->guessy)
417 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
418 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
419 #define GUESS_W (ds->solution->npegs*PEGOFF)
420 #define GUESS_H (ds->nguesses*PEGOFF)
422 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
423 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
424 #define HINT_X(g) HINT_OX
425 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
426 #define HINT_W (ds->hintw*HINTOFF)
427 #define HINT_H GUESS_H
429 #define SOLN_OX GUESS_OX
430 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
431 #define SOLN_W GUESS_W
432 #define SOLN_H PEGOFF
434 struct game_drawstate {
436 pegrow *guesses; /* same size as state->guesses */
437 pegrow solution; /* only displayed if state->solved */
438 pegrow colours; /* length ncolours, not npegs */
440 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
441 int pegrad, hintrad; /* radius of peg, hint */
443 int colx, coly; /* origin of colours vertical bar */
444 int guessx, guessy; /* origin of guesses */
445 int solnx, solny; /* origin of solution */
446 int hintw; /* no. of hint tiles we're wide per row */
447 int w, h, started, solved;
452 int drag_col, blit_ox, blit_oy;
455 static int is_markable(game_params *params, pegrow pegs)
457 int i, nset = 0, nrequired, ret = 0;
458 pegrow colcount = new_pegrow(params->ncolours);
460 nrequired = params->allow_blank ? 1 : params->npegs;
462 for (i = 0; i < params->npegs; i++) {
463 int c = pegs->pegs[i];
465 colcount->pegs[c-1]++;
469 if (nset < nrequired) goto done;
471 if (!params->allow_multiple) {
472 for (i = 0; i < params->ncolours; i++) {
473 if (colcount->pegs[i] > 1) goto done;
478 free_pegrow(colcount);
482 static void set_peg(game_params *params, game_ui *ui, int peg, int col)
484 ui->curr_pegs->pegs[peg] = col;
485 ui->markable = is_markable(params, ui->curr_pegs);
488 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
490 int nc_place = 0, nc_colour = 0, i, j;
492 assert(guess && solution && (guess->npegs == solution->npegs));
494 for (i = 0; i < guess->npegs; i++) {
495 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
498 /* slight bit of cleverness: we have the following formula, from
499 * http://mathworld.wolfram.com/Mastermind.html that gives:
501 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
503 * I think this is due to Knuth.
505 for (i = 1; i <= ncols; i++) {
506 int n_guess = 0, n_solution = 0;
507 for (j = 0; j < guess->npegs; j++) {
508 if (guess->pegs[j] == i) n_guess++;
509 if (solution->pegs[j] == i) n_solution++;
511 nc_colour += min(n_guess, n_solution);
513 nc_colour -= nc_place;
515 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
516 guess->npegs, nc_place, nc_colour));
517 assert((nc_colour + nc_place) <= guess->npegs);
519 memset(guess->feedback, 0, guess->npegs*sizeof(int));
520 for (i = 0, j = 0; i < nc_place; i++)
521 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
522 for (i = 0; i < nc_colour; i++)
523 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
528 static game_state *mark_move(game_state *from, game_ui *ui)
530 int i, ncleared = 0, nc_place;
531 game_state *to = dup_game(from);
533 for (i = 0; i < to->solution->npegs; i++) {
534 to->guesses[from->next_go]->pegs[i] = ui->curr_pegs->pegs[i];
536 nc_place = mark_pegs(to->guesses[from->next_go], to->solution, to->params.ncolours);
538 if (nc_place == to->solution->npegs) {
539 to->solved = 1; /* win! */
541 to->next_go = from->next_go + 1;
542 if (to->next_go >= to->params.nguesses)
543 to->solved = 1; /* 'lose' so we show the pegs. */
546 for (i = 0; i < to->solution->npegs; i++) {
547 if (!ui->holds[i] || to->solved) {
548 ui->curr_pegs->pegs[i] = 0;
551 if (to->solved) ui->holds[i] = 0;
553 ui->markable = is_markable(&from->params, ui->curr_pegs);
554 if (!ui->markable && ui->peg_cur == to->solution->npegs)
560 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
561 int x, int y, int button)
563 int over_col = 0; /* one-indexed */
564 int over_guess = -1; /* zero-indexed */
565 int over_past_guess_y = -1; /* zero-indexed */
566 int over_past_guess_x = -1; /* zero-indexed */
567 int over_hint = 0; /* zero or one */
568 game_state *ret = NULL;
570 int guess_ox = GUESS_X(from->next_go, 0);
571 int guess_oy = GUESS_Y(from->next_go, 0);
573 if (from->solved) return NULL;
575 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
576 y >= COL_OY && y <= (COL_OY + COL_H)) {
577 over_col = ((y - COL_OY) / PEGOFF) + 1;
578 } else if (x >= guess_ox &&
579 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
580 if (x <= (guess_ox + GUESS_W)) {
581 over_guess = (x - guess_ox) / PEGOFF;
585 } else if (x >= guess_ox &&
586 y >= GUESS_OY && y < guess_oy) {
587 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
588 over_past_guess_x = (x - guess_ox) / PEGOFF;
590 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
591 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
592 over_past_guess_x, over_past_guess_y));
594 assert(ds->blit_peg);
597 if (button == LEFT_BUTTON) {
599 ui->drag_col = over_col;
601 debug(("Start dragging from colours"));
602 } else if (over_guess > -1) {
603 int col = ui->curr_pegs->pegs[over_guess];
606 ui->drag_opeg = over_guess;
607 debug(("Start dragging from a guess"));
609 } else if (over_past_guess_y > -1) {
611 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
615 debug(("Start dragging from a past guess"));
621 debug(("Start dragging, col = %d, (%d,%d)",
622 ui->drag_col, ui->drag_x, ui->drag_y));
625 } else if (button == LEFT_DRAG && ui->drag_col) {
628 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
630 } else if (button == LEFT_RELEASE && ui->drag_col) {
631 if (over_guess > -1) {
632 debug(("Dropping colour %d onto guess peg %d",
633 ui->drag_col, over_guess));
634 set_peg(&from->params, ui, over_guess, ui->drag_col);
636 if (ui->drag_opeg > -1) {
637 debug(("Removing colour %d from peg %d",
638 ui->drag_col, ui->drag_opeg));
639 set_peg(&from->params, ui, ui->drag_opeg, 0);
645 debug(("Stop dragging."));
647 } else if (button == RIGHT_BUTTON) {
648 if (over_guess > -1) {
649 /* we use ths feedback in the game_ui to signify
650 * 'carry this peg to the next guess as well'. */
651 ui->holds[over_guess] = 1 - ui->holds[over_guess];
654 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
655 /* NB this won't trigger if on the end of a drag; that's on
656 * purpose, in case you drop by mistake... */
657 ret = mark_move(from, ui);
661 if (button == CURSOR_UP || button == CURSOR_DOWN) {
663 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
665 if (button == CURSOR_UP && ui->colour_cur > 0)
668 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
669 int maxcur = from->params.npegs;
670 if (ui->markable) maxcur++;
673 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
675 if (button == CURSOR_LEFT && ui->peg_cur > 0)
678 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
680 if (ui->peg_cur == from->params.npegs) {
681 ret = mark_move(from, ui);
683 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
686 } else if (button == 'H' || button == 'h') {
687 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
693 /* ----------------------------------------------------------------------
697 #define PEG_PREFER_SZ 32
699 /* next three are multipliers for pegsz. It will look much nicer if
700 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
702 #define PEG_HINT 0.35
706 static void game_size(game_params *params, game_drawstate *ds,
707 int *x, int *y, int expand)
709 double hmul, vmul_c, vmul_g, vmul, szx, szy;
710 int sz, colh, guessh;
712 hmul = BORDER * 2.0 + /* border */
713 1.0 * 2.0 + /* vertical colour bar */
714 1.0 * params->npegs + /* guess pegs */
715 PEG_GAP * params->npegs + /* guess gaps */
716 PEG_HINT * ds->hintw + /* hint pegs */
717 PEG_GAP * (ds->hintw - 1); /* hint gaps */
719 vmul_c = BORDER * 2.0 + /* border */
720 1.0 * params->ncolours + /* colour pegs */
721 PEG_GAP * (params->ncolours - 1); /* colour gaps */
723 vmul_g = BORDER * 2.0 + /* border */
724 1.0 * (params->nguesses + 1) + /* guesses plus solution */
725 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
727 vmul = max(vmul_c, vmul_g);
731 sz = max(min((int)szx, (int)szy), 1);
735 ds->pegsz = min(sz, PEG_PREFER_SZ);
737 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
738 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
739 ds->border = (int)((double)ds->pegsz * BORDER);
741 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
742 ds->hintrad = (ds->hintsz-1)/2;
744 *x = (int)ceil((double)ds->pegsz * hmul);
745 *y = (int)ceil((double)ds->pegsz * vmul);
746 ds->w = *x; ds->h = *y;
748 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
749 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
750 guessh += ds->gapsz + ds->pegsz; /* solution */
752 ds->colx = ds->border;
753 ds->coly = (*y - colh) / 2;
755 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
756 ds->guessy = (*y - guessh) / 2;
757 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
760 if (ds->blit_peg) blitter_free(ds->blit_peg);
761 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
765 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
767 float *ret = snewn(3 * NCOLOURS, float);
769 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
772 ret[COL_1 * 3 + 0] = 1.0F;
773 ret[COL_1 * 3 + 1] = 0.0F;
774 ret[COL_1 * 3 + 2] = 0.0F;
776 /* yellow (toned down a bit due to pale grey background) */
777 ret[COL_2 * 3 + 0] = 0.7F;
778 ret[COL_2 * 3 + 1] = 0.7F;
779 ret[COL_2 * 3 + 2] = 0.0F;
781 /* green (also toned down) */
782 ret[COL_3 * 3 + 0] = 0.0F;
783 ret[COL_3 * 3 + 1] = 0.5F;
784 ret[COL_3 * 3 + 2] = 0.0F;
787 ret[COL_4 * 3 + 0] = 0.0F;
788 ret[COL_4 * 3 + 1] = 0.0F;
789 ret[COL_4 * 3 + 2] = 1.0F;
792 ret[COL_5 * 3 + 0] = 1.0F;
793 ret[COL_5 * 3 + 1] = 0.5F;
794 ret[COL_5 * 3 + 2] = 0.0F;
797 ret[COL_6 * 3 + 0] = 0.5F;
798 ret[COL_6 * 3 + 1] = 0.0F;
799 ret[COL_6 * 3 + 2] = 0.7F;
802 ret[COL_7 * 3 + 0] = 0.4F;
803 ret[COL_7 * 3 + 1] = 0.2F;
804 ret[COL_7 * 3 + 2] = 0.2F;
807 ret[COL_8 * 3 + 0] = 0.4F;
808 ret[COL_8 * 3 + 1] = 0.7F;
809 ret[COL_8 * 3 + 2] = 1.0F;
812 ret[COL_9 * 3 + 0] = 0.5F;
813 ret[COL_9 * 3 + 1] = 0.8F;
814 ret[COL_9 * 3 + 2] = 0.5F;
817 ret[COL_10 * 3 + 0] = 1.0F;
818 ret[COL_10 * 3 + 1] = 0.6F;
819 ret[COL_10 * 3 + 2] = 1.0F;
821 ret[COL_FRAME * 3 + 0] = 0.0F;
822 ret[COL_FRAME * 3 + 1] = 0.0F;
823 ret[COL_FRAME * 3 + 2] = 0.0F;
825 ret[COL_CURSOR * 3 + 0] = 0.0F;
826 ret[COL_CURSOR * 3 + 1] = 0.0F;
827 ret[COL_CURSOR * 3 + 2] = 0.0F;
829 ret[COL_FLASH * 3 + 0] = 0.5F;
830 ret[COL_FLASH * 3 + 1] = 1.0F;
831 ret[COL_FLASH * 3 + 2] = 1.0F;
833 ret[COL_HOLD * 3 + 0] = 1.0F;
834 ret[COL_HOLD * 3 + 1] = 0.5F;
835 ret[COL_HOLD * 3 + 2] = 0.5F;
837 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
838 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
839 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
841 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
842 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
843 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
845 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
846 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
847 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
849 *ncolours = NCOLOURS;
853 static game_drawstate *game_new_drawstate(game_state *state)
855 struct game_drawstate *ds = snew(struct game_drawstate);
858 memset(ds, 0, sizeof(struct game_drawstate));
860 ds->guesses = snewn(state->params.nguesses, pegrow);
861 ds->nguesses = state->params.nguesses;
862 for (i = 0; i < state->params.nguesses; i++) {
863 ds->guesses[i] = new_pegrow(state->params.npegs);
864 invalidate_pegrow(ds->guesses[i]);
866 ds->solution = new_pegrow(state->params.npegs);
867 invalidate_pegrow(ds->solution);
868 ds->colours = new_pegrow(state->params.ncolours);
869 invalidate_pegrow(ds->colours);
871 ds->hintw = (state->params.npegs+1)/2; /* must round up */
878 static void game_free_drawstate(game_drawstate *ds)
882 if (ds->blit_peg) blitter_free(ds->blit_peg);
883 free_pegrow(ds->colours);
884 free_pegrow(ds->solution);
885 for (i = 0; i < ds->nguesses; i++)
886 free_pegrow(ds->guesses[i]);
891 static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
895 * Some platforms antialias circles, which means we shouldn't
896 * overwrite a circle of one colour with a circle of another
897 * colour without erasing the background first. However, if the
898 * peg is the one being dragged, we don't erase the background
899 * because we _want_ it to alpha-blend nicely into whatever's
903 draw_rect(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
906 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
907 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
909 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
910 draw_update(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
913 static void draw_cursor(frontend *fe, game_drawstate *ds, int x, int y)
915 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, 0, COL_CURSOR);
917 draw_update(fe, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
920 static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
921 pegrow src, int *holds, int cur_col, int force)
924 int rowx, rowy, i, scol;
931 dest = ds->guesses[guess];
932 rowx = GUESS_X(guess,0);
933 rowy = GUESS_Y(guess,0);
935 if (src) assert(src->npegs == dest->npegs);
937 for (i = 0; i < dest->npegs; i++) {
938 scol = src ? src->pegs[i] : 0;
941 if (holds && holds[i])
943 if ((dest->pegs[i] != scol) || force) {
944 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000);
948 draw_rect(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
949 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
950 draw_update(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
953 draw_cursor(fe, ds, rowx + PEGOFF * i, rowy);
955 dest->pegs[i] = scol;
959 static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
960 pegrow src, int force, int cursor, int emptycol)
962 pegrow dest = ds->guesses[guess];
963 int rowx, rowy, i, scol, col, hintlen;
966 if (src) assert(src->npegs == dest->npegs);
968 hintlen = (dest->npegs + 1)/2;
971 * Because of the possible presence of the cursor around this
972 * entire section, we redraw all or none of it but never part.
976 for (i = 0; i < dest->npegs; i++) {
977 scol = src ? src->feedback[i] : 0;
978 if (i == 0 && cursor)
980 if ((scol != dest->feedback[i]) || force) {
983 dest->feedback[i] = scol;
987 /* erase a large background rectangle */
988 draw_rect(fe, GUESS_X(guess, dest->npegs)-CGAP,
989 GUESS_Y(guess, dest->npegs)-CGAP,
990 PEGSZ+CGAP*2, PEGSZ+CGAP*2, COL_BACKGROUND);
992 for (i = 0; i < dest->npegs; i++) {
993 scol = src ? src->feedback[i] : 0;
994 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
995 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
998 rowx = HINT_X(guess);
999 rowy = HINT_Y(guess);
1001 rowx += HINTOFF * i;
1003 rowx += HINTOFF * (i - hintlen);
1007 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
1008 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
1010 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
1014 draw_cursor(fe, ds, GUESS_X(guess, dest->npegs),
1015 GUESS_Y(guess, dest->npegs));
1017 draw_update(fe, GUESS_X(guess, dest->npegs)-CGAP,
1018 GUESS_Y(guess, dest->npegs)-CGAP,
1019 PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1023 static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
1025 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1027 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1028 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1031 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1032 game_state *state, int dir, game_ui *ui,
1033 float animtime, float flashtime)
1035 int i, new_move, last_go;
1037 new_move = (state->next_go != ds->next_go) || !ds->started;
1038 last_go = (state->next_go == state->params.nguesses-1);
1041 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1042 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1043 draw_update(fe, 0, 0, ds->w, ds->h);
1046 if (ds->drag_col != 0) {
1047 debug(("Loading from blitter."));
1048 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1049 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1052 /* draw the colours */
1053 for (i = 0; i < state->params.ncolours; i++) {
1055 if (ui->display_cur && ui->colour_cur == i)
1057 if (ds->colours->pegs[i] != val) {
1058 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
1060 draw_cursor(fe, ds, COL_X(i), COL_Y(i));
1061 ds->colours->pegs[i] = val;
1065 /* draw the guesses (so far) and the hints */
1066 for (i = 0; i < state->params.nguesses; i++) {
1067 if (state->next_go > i || state->solved) {
1068 /* this info is stored in the game_state already */
1069 guess_redraw(fe, ds, i, state->guesses[i], NULL, -1, 0);
1070 hint_redraw(fe, ds, i, state->guesses[i],
1071 i == (state->next_go-1) ? 1 : 0, FALSE, COL_EMPTY);
1072 } else if (state->next_go == i) {
1073 /* this is the one we're on; the (incomplete) guess is
1074 * stored in the game_ui. */
1075 guess_redraw(fe, ds, i, ui->curr_pegs,
1076 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0);
1077 hint_redraw(fe, ds, i, NULL, 1,
1078 ui->display_cur && ui->peg_cur == state->params.npegs,
1079 ui->markable ? COL_FLASH : COL_EMPTY);
1081 /* we've not got here yet; it's blank. */
1082 guess_redraw(fe, ds, i, NULL, NULL, -1, 0);
1083 hint_redraw(fe, ds, i, NULL, 0, FALSE, COL_EMPTY);
1087 /* draw the 'current move' and 'able to mark' sign. */
1089 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1091 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1093 /* draw the solution (or the big rectangle) */
1094 if ((state->solved != ds->solved) || !ds->started) {
1095 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1096 state->solved ? COL_BACKGROUND : COL_EMPTY);
1097 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1100 guess_redraw(fe, ds, -1, state->solution, NULL, -1, !ds->solved);
1101 ds->solved = state->solved;
1103 ds->next_go = state->next_go;
1105 /* if ui->drag_col != 0, save the screen to the blitter,
1106 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1107 if (ui->drag_col != 0) {
1108 int ox = ui->drag_x - (PEGSZ/2);
1109 int oy = ui->drag_y - (PEGSZ/2);
1110 debug(("Saving to blitter at (%d,%d)", ox, oy));
1111 blitter_save(fe, ds->blit_peg, ox, oy);
1112 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
1114 ds->blit_ox = ox; ds->blit_oy = oy;
1116 ds->drag_col = ui->drag_col;
1121 static float game_anim_length(game_state *oldstate, game_state *newstate,
1122 int dir, game_ui *ui)
1127 static float game_flash_length(game_state *oldstate, game_state *newstate,
1128 int dir, game_ui *ui)
1133 static int game_wants_statusbar(void)
1138 static int game_timing_state(game_state *state)
1144 #define thegame guess
1147 const struct game thegame = {
1148 "Guess", "games.guess",
1155 TRUE, game_configure, custom_params,
1164 FALSE, game_text_format,
1172 game_free_drawstate,
1176 game_wants_statusbar,
1177 FALSE, game_timing_state,
1178 0, /* mouse_priorities */
1181 /* vim: set shiftwidth=4 tabstop=8: */