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 = snew(struct pegrow);
226 newpegs->npegs = pegs->npegs;
227 newpegs->pegs = snewn(newpegs->npegs, int);
228 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
229 newpegs->feedback = snewn(newpegs->npegs, int);
230 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
235 static void invalidate_pegrow(pegrow pegs)
237 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
238 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
241 static void free_pegrow(pegrow pegs)
244 sfree(pegs->feedback);
248 static char *new_game_desc(game_params *params, random_state *rs,
249 game_aux_info **aux, int interactive)
251 unsigned char *bmp = snewn(params->npegs, unsigned char);
254 pegrow colcount = new_pegrow(params->ncolours);
256 for (i = 0; i < params->npegs; i++) {
258 c = random_upto(rs, params->ncolours);
259 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
261 bmp[i] = (unsigned char)(c+1);
263 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
265 ret = bin2hex(bmp, params->npegs);
267 free_pegrow(colcount);
271 static void game_free_aux_info(game_aux_info *aux)
273 assert(!"Shouldn't happen");
276 static char *validate_desc(game_params *params, char *desc)
281 /* desc is just an (obfuscated) bitmap of the solution; check that
282 * it's the correct length and (when unobfuscated) contains only
283 * sensible colours. */
284 if (strlen(desc) != params->npegs * 2)
285 return "Game description is wrong length";
286 bmp = hex2bin(desc, params->npegs);
287 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
288 for (i = 0; i < params->npegs; i++) {
289 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
291 return "Game description is corrupted";
299 static game_state *new_game(midend_data *me, game_params *params, char *desc)
301 game_state *state = snew(game_state);
305 state->params = *params;
306 state->guesses = snewn(params->nguesses, pegrow);
307 for (i = 0; i < params->nguesses; i++)
308 state->guesses[i] = new_pegrow(params->npegs);
309 state->solution = new_pegrow(params->npegs);
311 bmp = hex2bin(desc, params->npegs);
312 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
313 for (i = 0; i < params->npegs; i++)
314 state->solution->pegs[i] = (int)bmp[i];
317 state->next_go = state->solved = 0;
322 static game_state *dup_game(game_state *state)
324 game_state *ret = snew(game_state);
328 ret->guesses = snewn(state->params.nguesses, pegrow);
329 for (i = 0; i < state->params.nguesses; i++)
330 ret->guesses[i] = dup_pegrow(state->guesses[i]);
331 ret->solution = dup_pegrow(state->solution);
336 static void free_game(game_state *state)
340 free_pegrow(state->solution);
341 for (i = 0; i < state->params.nguesses; i++)
342 free_pegrow(state->guesses[i]);
343 sfree(state->guesses);
348 static game_state *solve_game(game_state *state, game_state *currstate,
349 game_aux_info *aux, char **error)
351 game_state *ret = dup_game(currstate);
356 static char *game_text_format(game_state *state)
362 pegrow curr_pegs; /* half-finished current move */
364 int colour_cur; /* position of up-down colour picker cursor */
365 int peg_cur; /* position of left-right peg picker cursor */
366 int display_cur, markable;
368 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
369 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
372 static game_ui *new_ui(game_state *state)
374 game_ui *ui = snew(struct game_ui);
375 memset(ui, 0, sizeof(struct game_ui));
376 ui->curr_pegs = new_pegrow(state->params.npegs);
377 ui->holds = snewn(state->params.npegs, int);
378 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
383 static void free_ui(game_ui *ui)
385 free_pegrow(ui->curr_pegs);
390 static void game_changed_state(game_ui *ui, game_state *oldstate,
391 game_state *newstate)
395 /* just clear the row-in-progress when we have an undo/redo. */
396 for (i = 0; i < ui->curr_pegs->npegs; i++)
397 ui->curr_pegs->pegs[i] = 0;
400 #define PEGSZ (ds->pegsz)
401 #define PEGOFF (ds->pegsz + ds->gapsz)
402 #define HINTSZ (ds->hintsz)
403 #define HINTOFF (ds->hintsz + ds->gapsz)
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 */
442 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
443 int pegrad, hintrad; /* radius of peg, hint */
445 int colx, coly; /* origin of colours vertical bar */
446 int guessx, guessy; /* origin of guesses */
447 int solnx, solny; /* origin of solution */
448 int hintw; /* no. of hint tiles we're wide per row */
449 int w, h, started, solved;
451 int colour_cur, peg_cur, display_cur; /* as in game_ui. */
455 int drag_col, blit_ox, blit_oy;
458 static int is_markable(game_params *params, pegrow pegs)
460 int i, nset = 0, nrequired, ret = 0;
461 pegrow colcount = new_pegrow(params->ncolours);
463 nrequired = params->allow_blank ? 1 : params->npegs;
465 for (i = 0; i < params->npegs; i++) {
466 if (pegs->pegs[i] > 0) {
467 colcount->pegs[pegs->pegs[i]]++;
471 if (nset < nrequired) goto done;
473 if (!params->allow_multiple) {
474 for (i = 0; i < params->ncolours; i++) {
475 if (colcount->pegs[i] > 1) goto done;
480 free_pegrow(colcount);
484 static void set_peg(game_params *params, game_ui *ui, int peg, int col)
486 ui->curr_pegs->pegs[peg] = col;
487 ui->markable = is_markable(params, ui->curr_pegs);
490 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
492 int nc_place = 0, nc_colour = 0, i, j;
494 assert(guess && solution && (guess->npegs == solution->npegs));
496 for (i = 0; i < guess->npegs; i++) {
497 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
500 /* slight bit of cleverness: we have the following formula, from
501 * http://mathworld.wolfram.com/Mastermind.html that gives:
503 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
505 * I think this is due to Knuth.
507 for (i = 1; i <= ncols; i++) {
508 int n_guess = 0, n_solution = 0;
509 for (j = 0; j < guess->npegs; j++) {
510 if (guess->pegs[j] == i) n_guess++;
511 if (solution->pegs[j] == i) n_solution++;
513 nc_colour += min(n_guess, n_solution);
515 nc_colour -= nc_place;
517 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
518 guess->npegs, nc_place, nc_colour));
519 assert((nc_colour + nc_place) <= guess->npegs);
521 memset(guess->feedback, 0, guess->npegs*sizeof(int));
522 for (i = 0, j = 0; i < nc_place; i++)
523 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
524 for (i = 0; i < nc_colour; i++)
525 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
530 static game_state *mark_move(game_state *from, game_ui *ui)
532 int i, ncleared = 0, nc_place;
533 game_state *to = dup_game(from);
535 for (i = 0; i < to->solution->npegs; i++) {
536 to->guesses[from->next_go]->pegs[i] = ui->curr_pegs->pegs[i];
538 nc_place = mark_pegs(to->guesses[from->next_go], to->solution, to->params.ncolours);
540 if (nc_place == to->solution->npegs) {
541 to->solved = 1; /* win! */
543 to->next_go = from->next_go + 1;
544 if (to->next_go >= to->params.nguesses)
545 to->solved = 1; /* 'lose' so we show the pegs. */
548 for (i = 0; i < to->solution->npegs; i++) {
549 if (!ui->holds[i] || to->solved) {
550 ui->curr_pegs->pegs[i] = 0;
553 if (to->solved) ui->holds[i] = 0;
555 ui->markable = is_markable(&from->params, ui->curr_pegs);
556 if (!ui->markable && ui->peg_cur == to->solution->npegs)
562 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
563 int x, int y, int button)
565 int over_col = 0; /* one-indexed */
566 int over_guess = -1; /* zero-indexed */
567 int over_past_guess_y = -1; /* zero-indexed */
568 int over_past_guess_x = -1; /* zero-indexed */
569 int over_hint = 0; /* zero or one */
570 game_state *ret = NULL;
572 int guess_ox = GUESS_X(from->next_go, 0);
573 int guess_oy = GUESS_Y(from->next_go, 0);
575 if (from->solved) return NULL;
577 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
578 y >= COL_OY && y <= (COL_OY + COL_H)) {
579 over_col = ((y - COL_OY) / PEGOFF) + 1;
580 } else if (x >= guess_ox &&
581 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
582 if (x <= (guess_ox + GUESS_W)) {
583 over_guess = (x - guess_ox) / PEGOFF;
587 } else if (x >= guess_ox &&
588 y >= GUESS_OY && y < guess_oy) {
589 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
590 over_past_guess_x = (x - guess_ox) / PEGOFF;
592 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
593 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
594 over_past_guess_x, over_past_guess_y));
596 assert(ds->blit_peg);
599 if (button == LEFT_BUTTON) {
601 ui->drag_col = over_col;
603 debug(("Start dragging from colours"));
604 } else if (over_guess > -1) {
605 int col = ui->curr_pegs->pegs[over_guess];
608 ui->drag_opeg = over_guess;
609 debug(("Start dragging from a guess"));
611 } else if (over_past_guess_y > -1) {
613 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
617 debug(("Start dragging from a past guess"));
623 debug(("Start dragging, col = %d, (%d,%d)",
624 ui->drag_col, ui->drag_x, ui->drag_y));
627 } else if (button == LEFT_DRAG && ui->drag_col) {
630 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
632 } else if (button == LEFT_RELEASE && ui->drag_col) {
633 if (over_guess > -1) {
634 debug(("Dropping colour %d onto guess peg %d",
635 ui->drag_col, over_guess));
636 set_peg(&from->params, ui, over_guess, ui->drag_col);
638 if (ui->drag_opeg > -1) {
639 debug(("Removing colour %d from peg %d",
640 ui->drag_col, ui->drag_opeg));
641 set_peg(&from->params, ui, ui->drag_opeg, 0);
646 debug(("Stop dragging."));
648 } else if (button == RIGHT_BUTTON) {
649 if (over_guess > -1) {
650 /* we use ths feedback in the game_ui to signify
651 * 'carry this peg to the next guess as well'. */
652 ui->holds[over_guess] = 1 - ui->holds[over_guess];
655 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
656 /* NB this won't trigger if on the end of a drag; that's on
657 * purpose, in case you drop by mistake... */
658 ret = mark_move(from, ui);
662 if (button == CURSOR_UP || button == CURSOR_DOWN) {
664 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
666 if (button == CURSOR_UP && ui->colour_cur > 0)
669 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
670 int maxcur = from->params.npegs;
671 if (ui->markable) maxcur++;
674 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
676 if (button == CURSOR_LEFT && ui->peg_cur > 0)
679 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
681 if (ui->peg_cur == from->params.npegs) {
682 ret = mark_move(from, ui);
684 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
691 /* ----------------------------------------------------------------------
695 #define PEG_PREFER_SZ 32
697 /* next three are multipliers for pegsz. It will look much nicer if
698 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
700 #define PEG_HINT 0.35
704 static void game_size(game_params *params, game_drawstate *ds,
705 int *x, int *y, int expand)
707 double hmul, vmul_c, vmul_g, vmul, szx, szy;
708 int sz, colh, guessh;
710 hmul = BORDER * 2.0 + /* border */
711 1.0 * 2.0 + /* vertical colour bar */
712 1.0 * params->npegs + /* guess pegs */
713 PEG_GAP * params->npegs + /* guess gaps */
714 PEG_HINT * ds->hintw + /* hint pegs */
715 PEG_GAP * (ds->hintw - 1); /* hint gaps */
717 vmul_c = BORDER * 2.0 + /* border */
718 1.0 * params->ncolours + /* colour pegs */
719 PEG_GAP * (params->ncolours - 1); /* colour gaps */
721 vmul_g = BORDER * 2.0 + /* border */
722 1.0 * (params->nguesses + 1) + /* guesses plus solution */
723 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
725 vmul = max(vmul_c, vmul_g);
729 sz = max(min((int)szx, (int)szy), 1);
733 ds->pegsz = min(sz, PEG_PREFER_SZ);
735 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
736 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
737 ds->border = (int)((double)ds->pegsz * BORDER);
739 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
740 ds->hintrad = (ds->hintsz-1)/2;
742 *x = (int)ceil((double)ds->pegsz * hmul);
743 *y = (int)ceil((double)ds->pegsz * vmul);
744 ds->w = *x; ds->h = *y;
746 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
747 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
748 guessh += ds->gapsz + ds->pegsz; /* solution */
750 ds->colx = ds->border;
751 ds->coly = (*y - colh) / 2;
753 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
754 ds->guessy = (*y - guessh) / 2;
755 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
758 if (ds->blit_peg) blitter_free(ds->blit_peg);
759 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
763 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
765 float *ret = snewn(3 * NCOLOURS, float);
767 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
770 ret[COL_1 * 3 + 0] = 1.0F;
771 ret[COL_1 * 3 + 1] = 0.0F;
772 ret[COL_1 * 3 + 2] = 0.0F;
774 /* yellow (toned down a bit due to pale grey background) */
775 ret[COL_2 * 3 + 0] = 0.7F;
776 ret[COL_2 * 3 + 1] = 0.7F;
777 ret[COL_2 * 3 + 2] = 0.0F;
779 /* green (also toned down) */
780 ret[COL_3 * 3 + 0] = 0.0F;
781 ret[COL_3 * 3 + 1] = 0.5F;
782 ret[COL_3 * 3 + 2] = 0.0F;
785 ret[COL_4 * 3 + 0] = 0.0F;
786 ret[COL_4 * 3 + 1] = 0.0F;
787 ret[COL_4 * 3 + 2] = 1.0F;
790 ret[COL_5 * 3 + 0] = 1.0F;
791 ret[COL_5 * 3 + 1] = 0.5F;
792 ret[COL_5 * 3 + 2] = 0.0F;
795 ret[COL_6 * 3 + 0] = 0.5F;
796 ret[COL_6 * 3 + 1] = 0.0F;
797 ret[COL_6 * 3 + 2] = 0.7F;
800 ret[COL_7 * 3 + 0] = 0.4F;
801 ret[COL_7 * 3 + 1] = 0.2F;
802 ret[COL_7 * 3 + 2] = 0.2F;
805 ret[COL_8 * 3 + 0] = 0.4F;
806 ret[COL_8 * 3 + 1] = 0.7F;
807 ret[COL_8 * 3 + 2] = 1.0F;
810 ret[COL_9 * 3 + 0] = 0.5F;
811 ret[COL_9 * 3 + 1] = 0.8F;
812 ret[COL_9 * 3 + 2] = 0.5F;
815 ret[COL_10 * 3 + 0] = 1.0F;
816 ret[COL_10 * 3 + 1] = 0.6F;
817 ret[COL_10 * 3 + 2] = 1.0F;
819 ret[COL_FRAME * 3 + 0] = 0.0F;
820 ret[COL_FRAME * 3 + 1] = 0.0F;
821 ret[COL_FRAME * 3 + 2] = 0.0F;
823 ret[COL_CURSOR * 3 + 0] = 0.0F;
824 ret[COL_CURSOR * 3 + 1] = 0.0F;
825 ret[COL_CURSOR * 3 + 2] = 0.0F;
827 ret[COL_FLASH * 3 + 0] = 0.5F;
828 ret[COL_FLASH * 3 + 1] = 1.0F;
829 ret[COL_FLASH * 3 + 2] = 1.0F;
831 ret[COL_HOLD * 3 + 0] = 1.0F;
832 ret[COL_HOLD * 3 + 1] = 0.5F;
833 ret[COL_HOLD * 3 + 2] = 0.5F;
835 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
836 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
837 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
839 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
840 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
841 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
843 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
844 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
845 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
847 *ncolours = NCOLOURS;
851 static game_drawstate *game_new_drawstate(game_state *state)
853 struct game_drawstate *ds = snew(struct game_drawstate);
856 memset(ds, 0, sizeof(struct game_drawstate));
858 ds->guesses = snewn(state->params.nguesses, pegrow);
859 ds->nguesses = state->params.nguesses;
860 for (i = 0; i < state->params.nguesses; i++) {
861 ds->guesses[i] = new_pegrow(state->params.npegs);
862 invalidate_pegrow(ds->guesses[i]);
864 ds->solution = new_pegrow(state->params.npegs);
865 invalidate_pegrow(ds->solution);
866 ds->colours = new_pegrow(state->params.ncolours);
867 invalidate_pegrow(ds->colours);
869 ds->hintw = (state->params.npegs+1)/2; /* must round up */
871 ds->holds = snewn(state->params.npegs, int);
872 memset(ds->holds, 0, state->params.npegs*sizeof(int));
879 static void game_free_drawstate(game_drawstate *ds)
883 if (ds->blit_peg) blitter_free(ds->blit_peg);
884 free_pegrow(ds->colours);
885 free_pegrow(ds->solution);
886 for (i = 0; i < ds->nguesses; i++)
887 free_pegrow(ds->guesses[i]);
893 static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
897 * Some platforms antialias circles, which means we shouldn't
898 * overwrite a circle of one colour with a circle of another
899 * colour without erasing the background first. However, if the
900 * peg is the one being dragged, we don't erase the background
901 * because we _want_ it to alpha-blend nicely into whatever's
905 draw_rect(fe, cx-1, cy-1, PEGSZ+2, PEGSZ+2, COL_BACKGROUND);
907 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
908 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
910 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
911 draw_update(fe, cx, cy, PEGSZ, PEGSZ);
914 static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
915 pegrow src, int force)
918 int rowx, rowy, i, scol;
925 dest = ds->guesses[guess];
926 rowx = GUESS_X(guess,0);
927 rowy = GUESS_Y(guess,0);
929 if (src) assert(src->npegs == dest->npegs);
931 for (i = 0; i < dest->npegs; i++) {
932 scol = src ? src->pegs[i] : 0;
933 if ((dest->pegs[i] != scol) || force)
934 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol);
935 dest->pegs[i] = scol;
939 static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
940 pegrow src, int force, int emptycol)
942 pegrow dest = ds->guesses[guess];
943 int rowx, rowy, i, scol, col, hintlen;
945 if (src) assert(src->npegs == dest->npegs);
947 hintlen = (dest->npegs + 1)/2;
949 for (i = 0; i < dest->npegs; i++) {
950 scol = src ? src->feedback[i] : 0;
951 col = (scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
952 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR : emptycol;
953 if ((scol != dest->feedback[i]) || force) {
954 rowx = HINT_X(guess);
955 rowy = HINT_Y(guess);
959 rowx += HINTOFF * (i - hintlen);
962 /* erase background for antialiasing platforms */
963 draw_rect(fe, rowx-1, rowy-1, HINTSZ+2, HINTSZ+2, COL_BACKGROUND);
965 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
966 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
968 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
970 draw_update(fe, rowx, rowy, HINTSZ, HINTSZ);
972 dest->feedback[i] = scol;
976 static void hold_redraw(frontend *fe, game_drawstate *ds, int guess, int *src, int force)
978 int shold, col, ox, oy, i;
980 if (guess >= ds->nguesses)
983 for (i = 0; i < ds->solution->npegs; i++) {
984 shold = src ? src[i] : 0;
985 col = shold ? COL_HOLD : COL_BACKGROUND;
986 if ((shold != ds->holds[i]) || force) {
987 ox = GUESS_X(guess, i);
988 oy = GUESS_Y(guess, i) + PEGSZ + ds->gapsz/2;
990 draw_rect(fe, ox, oy, PEGSZ, 2, col);
991 draw_update(fe, ox, oy, PEGSZ, 2);
993 if (src) ds->holds[i] = shold;
997 static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
999 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1001 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1002 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1005 static void cur_redraw(frontend *fe, game_drawstate *ds,
1006 int x, int y, int erase)
1008 int cgap = ds->gapsz / 2;
1010 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+cgap, 0,
1011 erase ? COL_BACKGROUND : COL_CURSOR);
1013 draw_update(fe, x-cgap, y-cgap, x+PEGSZ+cgap, y+PEGSZ+cgap);
1016 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1017 game_state *state, int dir, game_ui *ui,
1018 float animtime, float flashtime)
1020 int i, cur_erase = 0, cur_draw = 0, new_move, last_go;
1022 new_move = (state->next_go != ds->next_go) || !ds->started;
1023 last_go = (state->next_go == state->params.nguesses-1);
1026 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1027 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1028 draw_update(fe, 0, 0, ds->w, ds->h);
1031 if (ds->drag_col != 0) {
1032 debug(("Loading from blitter."));
1033 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1034 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1037 /* draw the colours */
1038 for (i = 0; i < state->params.ncolours; i++) {
1039 if (ds->colours->pegs[i] != i+1) {
1040 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
1041 ds->colours->pegs[i] = i+1;
1045 /* draw the guesses (so far) and the hints */
1046 for (i = 0; i < state->params.nguesses; i++) {
1047 if (state->next_go > i || state->solved) {
1048 /* this info is stored in the game_state already */
1049 guess_redraw(fe, ds, i, state->guesses[i], 0);
1050 hint_redraw(fe, ds, i, state->guesses[i],
1051 i == (state->next_go-1) ? 1 : 0, COL_EMPTY);
1052 } else if (state->next_go == i) {
1053 /* this is the one we're on; the (incomplete) guess is
1054 * stored in the game_ui. */
1055 guess_redraw(fe, ds, i, ui->curr_pegs, 0);
1056 hint_redraw(fe, ds, i, NULL, 1, ui->markable ? COL_FLASH : COL_EMPTY);
1058 /* we've not got here yet; it's blank. */
1059 guess_redraw(fe, ds, i, NULL, 0);
1060 hint_redraw(fe, ds, i, NULL, 0, COL_EMPTY);
1064 /* draw the 'hold' markers */
1065 if (state->solved) {
1066 hold_redraw(fe, ds, state->next_go, NULL, 1);
1067 } else if (new_move) {
1068 hold_redraw(fe, ds, ds->next_go, NULL, 1);
1069 hold_redraw(fe, ds, state->next_go, last_go ? NULL : ui->holds, 1);
1071 hold_redraw(fe, ds, state->next_go, last_go ? NULL : ui->holds, 0);
1073 /* draw the 'current move' and 'able to mark' sign. */
1075 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1077 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1079 /* draw the solution (or the big rectangle) */
1080 if ((state->solved != ds->solved) || !ds->started) {
1081 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1082 state->solved ? COL_BACKGROUND : COL_EMPTY);
1083 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1086 guess_redraw(fe, ds, -1, state->solution, !ds->solved);
1087 ds->solved = state->solved;
1089 if (ui->display_cur && !ds->display_cur)
1091 else if (!ui->display_cur && ds->display_cur)
1093 else if (ui->display_cur) {
1094 if ((state->next_go != ds->next_go) ||
1095 (ui->peg_cur != ds->peg_cur) ||
1096 (ui->colour_cur != ds->colour_cur)) {
1102 cur_redraw(fe, ds, COL_X(ds->colour_cur), COL_Y(ds->colour_cur), 1);
1104 GUESS_X(ds->next_go, ds->peg_cur), GUESS_Y(ds->next_go, ds->peg_cur), 1);
1107 cur_redraw(fe, ds, COL_X(ui->colour_cur), COL_Y(ui->colour_cur), 0);
1109 GUESS_X(state->next_go, ui->peg_cur), GUESS_Y(state->next_go, ui->peg_cur), 0);
1111 ds->display_cur = ui->display_cur;
1112 ds->peg_cur = ui->peg_cur;
1113 ds->colour_cur = ui->colour_cur;
1114 ds->next_go = state->next_go;
1116 /* if ui->drag_col != 0, save the screen to the blitter,
1117 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1118 if (ui->drag_col != 0) {
1119 int ox = ui->drag_x - (PEGSZ/2);
1120 int oy = ui->drag_y - (PEGSZ/2);
1121 debug(("Saving to blitter at (%d,%d)", ox, oy));
1122 blitter_save(fe, ds->blit_peg, ox, oy);
1123 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
1125 ds->blit_ox = ox; ds->blit_oy = oy;
1127 ds->drag_col = ui->drag_col;
1132 static float game_anim_length(game_state *oldstate, game_state *newstate,
1133 int dir, game_ui *ui)
1138 static float game_flash_length(game_state *oldstate, game_state *newstate,
1139 int dir, game_ui *ui)
1144 static int game_wants_statusbar(void)
1149 static int game_timing_state(game_state *state)
1155 #define thegame guess
1158 const struct game thegame = {
1159 "Guess", "games.guess",
1166 TRUE, game_configure, custom_params,
1175 FALSE, game_text_format,
1183 game_free_drawstate,
1187 game_wants_statusbar,
1188 FALSE, game_timing_state,
1189 0, /* mouse_priorities */
1192 /* vim: set shiftwidth=4 tabstop=8: */