2 * guess.c: Mastermind clone.
14 #define FLASH_FRAME 0.5F
18 COL_HIGHLIGHT, COL_LOWLIGHT, COL_FRAME, 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;
29 #define FEEDBACK_CORRECTPLACE 1
30 #define FEEDBACK_CORRECTCOLOUR 2
32 typedef struct pegrow {
34 int *pegs; /* 0 is 'empty' */
35 int *feedback; /* may well be unused */
40 pegrow *guesses; /* length params->nguesses */
42 int next_go; /* from 0 to nguesses-1;
43 if next_go == nguesses then they've lost. */
47 static game_params *default_params(void)
49 game_params *ret = snew(game_params);
51 /* AFAIK this is the canonical Mastermind ruleset. */
59 static int game_fetch_preset(int i, char **name, game_params **params)
64 static void free_params(game_params *params)
69 static game_params *dup_params(game_params *params)
71 game_params *ret = snew(game_params);
72 *ret = *params; /* structure copy */
76 static void decode_params(game_params *params, char const *string)
78 char const *p = string;
79 game_params *defs = default_params();
81 *params = *defs; free_params(defs);
86 params->ncolours = atoi(p);
87 while (*p && isdigit((unsigned char)*p)) p++;
91 params->npegs = atoi(p);
92 while (*p && isdigit((unsigned char)*p)) p++;
96 params->nguesses = atoi(p);
97 while (*p && isdigit((unsigned char)*p)) p++;
106 static char *encode_params(game_params *params, int full)
110 sprintf(data, "c%dp%dg%d", params->ncolours, params->npegs, params->nguesses);
115 static config_item *game_configure(game_params *params)
120 ret = snewn(4, config_item);
122 ret[0].name = "No. of colours";
123 ret[0].type = C_STRING;
124 sprintf(buf, "%d", params->ncolours);
125 ret[0].sval = dupstr(buf);
128 ret[1].name = "No. of pegs per row";
129 ret[1].type = C_STRING;
130 sprintf(buf, "%d", params->npegs);
131 ret[1].sval = dupstr(buf);
134 ret[2].name = "No. of guesses";
135 ret[2].type = C_STRING;
136 sprintf(buf, "%d", params->nguesses);
137 ret[2].sval = dupstr(buf);
148 static game_params *custom_params(config_item *cfg)
150 game_params *ret = snew(game_params);
152 ret->ncolours = atoi(cfg[0].sval);
153 ret->npegs = atoi(cfg[1].sval);
154 ret->nguesses = atoi(cfg[2].sval);
159 static char *validate_params(game_params *params)
161 if (params->ncolours < 2 || params->npegs < 2)
162 return "Trivial solutions are uninteresting";
163 /* NB as well as the no. of colours we define, max(ncolours) must
164 * also fit in an unsigned char; see new_game_desc. */
165 if (params->ncolours > 10)
166 return "Too many colours";
167 if (params->nguesses < 1)
168 return "Must have at least one guess";
172 static pegrow new_pegrow(int npegs)
174 pegrow pegs = snew(struct pegrow);
177 pegs->pegs = snewn(pegs->npegs, int);
178 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
179 pegs->feedback = snewn(pegs->npegs, int);
180 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
185 static pegrow dup_pegrow(pegrow pegs)
187 pegrow newpegs = snew(struct pegrow);
189 newpegs->npegs = pegs->npegs;
190 newpegs->pegs = snewn(newpegs->npegs, int);
191 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
192 newpegs->feedback = snewn(newpegs->npegs, int);
193 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
198 static void invalidate_pegrow(pegrow pegs)
200 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
201 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
204 static void free_pegrow(pegrow pegs)
207 sfree(pegs->feedback);
211 static char *new_game_desc(game_params *params, random_state *rs,
212 game_aux_info **aux, int interactive)
214 unsigned char *bmp = snewn(params->npegs, unsigned char);
218 for (i = 0; i < params->npegs; i++)
219 bmp[i] = (unsigned char)(random_upto(rs, params->ncolours)+1);
220 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
222 ret = bin2hex(bmp, params->npegs);
227 static void game_free_aux_info(game_aux_info *aux)
229 assert(!"Shouldn't happen");
232 static char *validate_desc(game_params *params, char *desc)
234 /* desc is just an (obfuscated) bitmap of the solution; all we
235 * care is that it's the correct length. */
236 if (strlen(desc) != params->npegs * 2)
237 return "Game description is wrong length";
241 static game_state *new_game(midend_data *me, game_params *params, char *desc)
243 game_state *state = snew(game_state);
247 state->params = *params;
248 state->guesses = snewn(params->nguesses, pegrow);
249 for (i = 0; i < params->nguesses; i++)
250 state->guesses[i] = new_pegrow(params->npegs);
251 state->solution = new_pegrow(params->npegs);
253 bmp = hex2bin(desc, params->npegs);
254 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
255 for (i = 0; i < params->npegs; i++)
256 state->solution->pegs[i] = (int)bmp[i];
259 state->next_go = state->solved = 0;
264 static game_state *dup_game(game_state *state)
266 game_state *ret = snew(game_state);
270 ret->guesses = snewn(state->params.nguesses, pegrow);
271 for (i = 0; i < state->params.nguesses; i++)
272 ret->guesses[i] = dup_pegrow(state->guesses[i]);
273 ret->solution = dup_pegrow(state->solution);
278 static void free_game(game_state *state)
282 free_pegrow(state->solution);
283 for (i = 0; i < state->params.nguesses; i++)
284 free_pegrow(state->guesses[i]);
285 sfree(state->guesses);
290 static game_state *solve_game(game_state *state, game_state *currstate,
291 game_aux_info *aux, char **error)
293 game_state *ret = dup_game(currstate);
298 static char *game_text_format(game_state *state)
304 pegrow curr_pegs; /* half-finished current move */
306 int colour_cur; /* position of up-down colour picker cursor */
307 int peg_cur; /* position of left-right peg picker cursor */
308 int display_cur, markable;
310 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
313 static game_ui *new_ui(game_state *state)
315 game_ui *ui = snew(struct game_ui);
316 memset(ui, 0, sizeof(struct game_ui));
317 ui->curr_pegs = new_pegrow(state->params.npegs);
318 ui->holds = snewn(state->params.npegs, int);
319 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
323 static void free_ui(game_ui *ui)
325 free_pegrow(ui->curr_pegs);
330 static void game_changed_state(game_ui *ui, game_state *oldstate,
331 game_state *newstate)
335 /* just clear the row-in-progress when we have an undo/redo. */
336 for (i = 0; i < ui->curr_pegs->npegs; i++)
337 ui->curr_pegs->pegs[i] = 0;
340 #define PEGSZ (ds->pegsz)
341 #define PEGOFF (ds->pegsz + ds->gapsz)
342 #define HINTSZ (ds->hintsz)
343 #define HINTOFF (ds->hintsz + ds->gapsz)
345 #define PEGRAD (ds->pegrad)
346 #define HINTRAD (ds->hintrad)
348 #define COL_OX (ds->colx)
349 #define COL_OY (ds->coly)
350 #define COL_X(c) (COL_OX)
351 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
353 #define COL_H (ds->colours->npegs*PEGOFF)
355 #define GUESS_OX (ds->guessx)
356 #define GUESS_OY (ds->guessy)
357 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
358 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
359 #define GUESS_W (ds->solution->npegs*PEGOFF)
360 #define GUESS_H (ds->nguesses*PEGOFF)
362 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
363 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
364 #define HINT_X(g) HINT_OX
365 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
366 #define HINT_W (ds->hintw*HINTOFF)
367 #define HINT_H GUESS_H
369 #define SOLN_OX GUESS_OX
370 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
371 #define SOLN_W GUESS_W
372 #define SOLN_H PEGOFF
374 struct game_drawstate {
376 pegrow *guesses; /* same size as state->guesses */
377 pegrow solution; /* only displayed if state->solved */
378 pegrow colours; /* length ncolours, not npegs */
382 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
383 int pegrad, hintrad; /* radius of peg, hint */
385 int colx, coly; /* origin of colours vertical bar */
386 int guessx, guessy; /* origin of guesses */
387 int solnx, solny; /* origin of solution */
388 int hintw; /* no. of hint tiles we're wide per row */
389 int w, h, started, solved;
391 int colour_cur, peg_cur, display_cur; /* as in game_ui. */
395 int drag_col, blit_ox, blit_oy;
398 static void set_peg(game_ui *ui, int peg, int col)
402 ui->curr_pegs->pegs[peg] = col;
404 /* set to 'markable' if all of our pegs are filled. */
405 for (i = 0; i < ui->curr_pegs->npegs; i++) {
406 if (ui->curr_pegs->pegs[i] == 0) return;
408 debug(("UI is markable."));
412 static int mark_pegs(pegrow guess, pegrow solution, int ncols)
414 int nc_place = 0, nc_colour = 0, i, j;
416 assert(guess && solution && (guess->npegs == solution->npegs));
418 for (i = 0; i < guess->npegs; i++) {
419 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
422 /* slight bit of cleverness: we have the following formula, from
423 * http://mathworld.wolfram.com/Mastermind.html that gives:
425 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
427 * I think this is due to Knuth.
429 for (i = 1; i <= ncols; i++) {
430 int n_guess = 0, n_solution = 0;
431 for (j = 0; j < guess->npegs; j++) {
432 if (guess->pegs[j] == i) n_guess++;
433 if (solution->pegs[j] == i) n_solution++;
435 nc_colour += min(n_guess, n_solution);
437 nc_colour -= nc_place;
439 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
440 guess->npegs, nc_place, nc_colour));
441 assert((nc_colour + nc_place) <= guess->npegs);
443 memset(guess->feedback, 0, guess->npegs*sizeof(int));
444 for (i = 0, j = 0; i < nc_place; i++)
445 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
446 for (i = 0; i < nc_colour; i++)
447 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
452 static game_state *mark_move(game_state *from, game_ui *ui)
454 int i, ncleared = 0, nc_place;
455 game_state *to = dup_game(from);
457 for (i = 0; i < to->solution->npegs; i++) {
458 to->guesses[from->next_go]->pegs[i] = ui->curr_pegs->pegs[i];
460 nc_place = mark_pegs(to->guesses[from->next_go], to->solution, to->params.ncolours);
462 if (nc_place == to->solution->npegs) {
463 to->solved = 1; /* win! */
465 to->next_go = from->next_go + 1;
466 if (to->next_go >= to->params.nguesses)
467 to->solved = 1; /* 'lose' so we show the pegs. */
470 for (i = 0; i < to->solution->npegs; i++) {
471 if (!ui->holds[i] || to->solved) {
472 ui->curr_pegs->pegs[i] = 0;
475 if (to->solved) ui->holds[i] = 0;
479 if (ui->peg_cur == to->solution->npegs)
486 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
487 int x, int y, int button)
489 int over_col = 0; /* one-indexed */
490 int over_guess = -1; /* zero-indexed */
491 int over_hint = 0; /* zero or one */
492 game_state *ret = NULL;
494 int guess_ox = GUESS_X(from->next_go, 0);
495 int guess_oy = GUESS_Y(from->next_go, 0);
497 if (from->solved) return NULL;
499 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
500 y >= COL_OY && y <= (COL_OY + COL_H)) {
501 over_col = ((y - COL_OY) / PEGOFF) + 1;
502 } else if (x >= guess_ox &&
503 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
504 if (x <= (guess_ox + GUESS_W)) {
505 over_guess = (x - guess_ox) / PEGOFF;
510 debug(("make_move: over_col %d, over_guess %d, over_hint %d",
511 over_col, over_guess, over_hint));
513 assert(ds->blit_peg);
516 if (button == LEFT_BUTTON) {
518 ui->drag_col = over_col;
519 debug(("Start dragging from colours"));
520 } else if (over_guess > -1) {
521 int col = ui->curr_pegs->pegs[over_guess];
524 debug(("Start dragging from a guess"));
530 debug(("Start dragging, col = %d, (%d,%d)",
531 ui->drag_col, ui->drag_x, ui->drag_y));
534 } else if (button == LEFT_DRAG && ui->drag_col) {
537 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
539 } else if (button == LEFT_RELEASE && ui->drag_col) {
540 if (over_guess > -1) {
541 debug(("Dropping colour %d onto guess peg %d",
542 ui->drag_col, over_guess));
543 set_peg(ui, over_guess, ui->drag_col);
546 debug(("Stop dragging."));
548 } else if (button == RIGHT_BUTTON) {
549 if (over_guess > -1) {
550 /* we use ths feedback in the game_ui to signify
551 * 'carry this peg to the next guess as well'. */
552 ui->holds[over_guess] = 1 - ui->holds[over_guess];
555 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
556 /* NB this won't trigger if on the end of a drag; that's on
557 * purpose, in case you drop by mistake... */
558 ret = mark_move(from, ui);
562 if (button == CURSOR_UP || button == CURSOR_DOWN) {
564 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
566 if (button == CURSOR_UP && ui->colour_cur > 0)
569 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
570 int maxcur = from->params.npegs;
571 if (ui->markable) maxcur++;
574 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
576 if (button == CURSOR_LEFT && ui->peg_cur > 0)
579 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
581 if (ui->peg_cur == from->params.npegs) {
582 ret = mark_move(from, ui);
584 set_peg(ui, ui->peg_cur, ui->colour_cur+1);
591 /* ----------------------------------------------------------------------
595 #define PEG_PREFER_SZ 32
597 /* next three are multipliers for pegsz. It will look much nicer if
598 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
600 #define PEG_HINT 0.35
604 static void game_size(game_params *params, game_drawstate *ds,
605 int *x, int *y, int expand)
607 double hmul, vmul_c, vmul_g, vmul, szx, szy;
608 int sz, colh, guessh;
610 hmul = BORDER * 2.0 + /* border */
611 1.0 * 2.0 + /* vertical colour bar */
612 1.0 * params->npegs + /* guess pegs */
613 PEG_GAP * params->npegs + /* guess gaps */
614 PEG_HINT * ds->hintw + /* hint pegs */
615 PEG_GAP * (ds->hintw - 1); /* hint gaps */
617 vmul_c = BORDER * 2.0 + /* border */
618 1.0 * params->ncolours + /* colour pegs */
619 PEG_GAP * (params->ncolours - 1); /* colour gaps */
621 vmul_g = BORDER * 2.0 + /* border */
622 1.0 * (params->nguesses + 1) + /* guesses plus solution */
623 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
625 vmul = max(vmul_c, vmul_g);
629 sz = max(min((int)szx, (int)szy), 1);
633 ds->pegsz = min(sz, PEG_PREFER_SZ);
635 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
636 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
637 ds->border = (int)((double)ds->pegsz * BORDER);
639 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
640 ds->hintrad = (ds->hintsz-1)/2;
642 *x = (int)((double)ds->pegsz * hmul);
643 *y = (int)((double)ds->pegsz * vmul);
645 ds->w = *x; ds->h = *y;
647 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
648 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
649 guessh += ds->gapsz + ds->pegsz; /* solution */
651 ds->colx = ds->border;
652 ds->coly = (*y - colh) / 2;
654 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
655 ds->guessy = (*y - guessh) / 2;
656 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
659 if (ds->blit_peg) blitter_free(ds->blit_peg);
660 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
664 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
666 float *ret = snewn(3 * NCOLOURS, float);
668 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
670 ret[COL_1 * 3 + 0] = 0.0F;
671 ret[COL_1 * 3 + 1] = 0.0F;
672 ret[COL_1 * 3 + 2] = 1.0F;
674 ret[COL_2 * 3 + 0] = 0.0F;
675 ret[COL_2 * 3 + 1] = 0.5F;
676 ret[COL_2 * 3 + 2] = 0.0F;
678 ret[COL_3 * 3 + 0] = 1.0F;
679 ret[COL_3 * 3 + 1] = 0.0F;
680 ret[COL_3 * 3 + 2] = 0.0F;
682 ret[COL_4 * 3 + 0] = 1.0F;
683 ret[COL_4 * 3 + 1] = 1.0F;
684 ret[COL_4 * 3 + 2] = 0.0F;
686 ret[COL_5 * 3 + 0] = 1.0F;
687 ret[COL_5 * 3 + 1] = 0.0F;
688 ret[COL_5 * 3 + 2] = 1.0F;
690 ret[COL_6 * 3 + 0] = 0.0F;
691 ret[COL_6 * 3 + 1] = 1.0F;
692 ret[COL_6 * 3 + 2] = 1.0F;
694 ret[COL_7 * 3 + 0] = 0.5F;
695 ret[COL_7 * 3 + 1] = 0.5F;
696 ret[COL_7 * 3 + 2] = 1.0F;
698 ret[COL_8 * 3 + 0] = 0.5F;
699 ret[COL_8 * 3 + 1] = 1.0F;
700 ret[COL_8 * 3 + 2] = 0.5F;
702 ret[COL_9 * 3 + 0] = 1.0F;
703 ret[COL_9 * 3 + 1] = 0.5F;
704 ret[COL_9 * 3 + 2] = 0.5F;
706 ret[COL_10 * 3 + 0] = 1.0F;
707 ret[COL_10 * 3 + 1] = 1.0F;
708 ret[COL_10 * 3 + 2] = 1.0F;
710 ret[COL_FRAME * 3 + 0] = 0.0F;
711 ret[COL_FRAME * 3 + 1] = 0.0F;
712 ret[COL_FRAME * 3 + 2] = 0.0F;
714 ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
715 ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
716 ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
718 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
719 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
720 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
722 ret[COL_FLASH * 3 + 0] = 0.5F;
723 ret[COL_FLASH * 3 + 1] = 1.0F;
724 ret[COL_FLASH * 3 + 2] = 1.0F;
726 ret[COL_HOLD * 3 + 0] = 1.0F;
727 ret[COL_HOLD * 3 + 1] = 0.5F;
728 ret[COL_HOLD * 3 + 2] = 0.5F;
730 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
731 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
732 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
734 ret[COL_CORRECTPLACE*3 + 0] = 1.0F;
735 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
736 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
738 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
739 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
740 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
742 *ncolours = NCOLOURS;
746 static game_drawstate *game_new_drawstate(game_state *state)
748 struct game_drawstate *ds = snew(struct game_drawstate);
751 memset(ds, 0, sizeof(struct game_drawstate));
753 ds->guesses = snewn(state->params.nguesses, pegrow);
754 ds->nguesses = state->params.nguesses;
755 for (i = 0; i < state->params.nguesses; i++) {
756 ds->guesses[i] = new_pegrow(state->params.npegs);
757 invalidate_pegrow(ds->guesses[i]);
759 ds->solution = new_pegrow(state->params.npegs);
760 invalidate_pegrow(ds->solution);
761 ds->colours = new_pegrow(state->params.ncolours);
762 invalidate_pegrow(ds->colours);
764 ds->hintw = (state->params.npegs+1)/2; /* must round up */
766 ds->holds = snewn(state->params.npegs, int);
767 memset(ds->holds, 0, state->params.npegs*sizeof(int));
774 static void game_free_drawstate(game_drawstate *ds)
778 if (ds->blit_peg) blitter_free(ds->blit_peg);
779 free_pegrow(ds->colours);
780 free_pegrow(ds->solution);
781 for (i = 0; i < ds->nguesses; i++)
782 free_pegrow(ds->guesses[i]);
788 static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy, int col)
791 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
793 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
794 draw_update(fe, cx, cy, PEGSZ, PEGSZ);
797 static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
798 pegrow src, int force)
801 int rowx, rowy, i, scol;
808 dest = ds->guesses[guess];
809 rowx = GUESS_X(guess,0);
810 rowy = GUESS_Y(guess,0);
812 if (src) assert(src->npegs == dest->npegs);
814 for (i = 0; i < dest->npegs; i++) {
815 scol = src ? src->pegs[i] : 0;
816 if ((dest->pegs[i] != scol) || force)
817 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, scol);
818 dest->pegs[i] = scol;
822 static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
823 pegrow src, int force, int emptycol)
825 pegrow dest = ds->guesses[guess];
826 int rowx, rowy, i, scol, col, hintlen;
828 if (src) assert(src->npegs == dest->npegs);
830 hintlen = (dest->npegs + 1)/2;
832 for (i = 0; i < dest->npegs; i++) {
833 scol = src ? src->feedback[i] : 0;
834 col = (scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
835 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR : emptycol;
836 if ((scol != dest->feedback[i]) || force) {
837 rowx = HINT_X(guess);
838 rowy = HINT_Y(guess);
842 rowx += HINTOFF * (i - hintlen);
846 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
848 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
849 draw_update(fe, rowx, rowy, HINTSZ, HINTSZ);
851 dest->feedback[i] = scol;
855 static void hold_redraw(frontend *fe, game_drawstate *ds, int guess, int *src, int force)
857 int shold, col, ox, oy, i;
859 if (guess >= ds->nguesses)
862 for (i = 0; i < ds->solution->npegs; i++) {
863 shold = src ? src[i] : 0;
864 col = shold ? COL_HOLD : COL_BACKGROUND;
865 if ((shold != ds->holds[i]) || force) {
866 ox = GUESS_X(guess, i);
867 oy = GUESS_Y(guess, i) + PEGSZ + ds->gapsz/2;
869 draw_rect(fe, ox, oy, PEGSZ, 2, col);
870 draw_update(fe, ox, oy, PEGSZ, 2);
872 if (src) ds->holds[i] = shold;
876 static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
878 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
880 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
881 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
884 static void cur_redraw(frontend *fe, game_drawstate *ds,
885 int x, int y, int erase)
887 int cgap = ds->gapsz / 2;
888 int x1, y1, x2, y2, hi, lo;
890 x1 = x-cgap; x2 = x+PEGSZ+cgap;
891 y1 = y-cgap; y2 = y+PEGSZ+cgap;
892 hi = erase ? COL_BACKGROUND : COL_HIGHLIGHT;
893 lo = erase ? COL_BACKGROUND : COL_LOWLIGHT;
895 draw_line(fe, x1, y1, x2, y1, hi);
896 draw_line(fe, x2, y1, x2, y2, lo);
897 draw_line(fe, x2, y2, x1, y2, lo);
898 draw_line(fe, x1, y2, x1, y1, hi);
900 draw_update(fe, x1, y1, x2, y2);
903 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
904 game_state *state, int dir, game_ui *ui,
905 float animtime, float flashtime)
907 int i, cur_erase = 0, cur_draw = 0, new_move, last_go;
909 new_move = (state->next_go != ds->next_go) || !ds->started;
910 last_go = (state->next_go == state->params.nguesses-1);
913 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
914 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
915 draw_update(fe, 0, 0, ds->w, ds->h);
918 if (ds->drag_col != 0) {
919 debug(("Loading from blitter."));
920 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
921 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
924 /* draw the colours */
925 for (i = 0; i < state->params.ncolours; i++) {
926 if (ds->colours->pegs[i] != i+1) {
927 draw_peg(fe, ds, COL_X(i), COL_Y(i), i+1);
928 ds->colours->pegs[i] = i+1;
932 /* draw the guesses (so far) and the hints */
933 for (i = 0; i < state->params.nguesses; i++) {
934 if (state->next_go > i || state->solved) {
935 /* this info is stored in the game_state already */
936 guess_redraw(fe, ds, i, state->guesses[i], 0);
937 hint_redraw(fe, ds, i, state->guesses[i],
938 i == (state->next_go-1) ? 1 : 0, COL_EMPTY);
939 } else if (state->next_go == i) {
940 /* this is the one we're on; the (incomplete) guess is
941 * stored in the game_ui. */
942 guess_redraw(fe, ds, i, ui->curr_pegs, 0);
943 hint_redraw(fe, ds, i, NULL, 1, ui->markable ? COL_FLASH : COL_EMPTY);
945 /* we've not got here yet; it's blank. */
946 guess_redraw(fe, ds, i, NULL, 0);
947 hint_redraw(fe, ds, i, NULL, 0, COL_EMPTY);
951 /* draw the 'hold' markers */
953 hold_redraw(fe, ds, state->next_go, NULL, 1);
954 } else if (new_move) {
955 hold_redraw(fe, ds, ds->next_go, NULL, 1);
956 hold_redraw(fe, ds, state->next_go, last_go ? NULL : ui->holds, 1);
958 hold_redraw(fe, ds, state->next_go, last_go ? NULL : ui->holds, 0);
960 /* draw the 'current move' and 'able to mark' sign. */
962 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
964 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
966 /* draw the solution (or the big rectangle) */
967 if ((state->solved != ds->solved) || !ds->started) {
968 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
969 state->solved ? COL_BACKGROUND : COL_EMPTY);
970 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
973 guess_redraw(fe, ds, -1, state->solution, !ds->solved);
974 ds->solved = state->solved;
976 if (ui->display_cur && !ds->display_cur)
978 else if (!ui->display_cur && ds->display_cur)
980 else if (ui->display_cur) {
981 if ((state->next_go != ds->next_go) ||
982 (ui->peg_cur != ds->peg_cur) ||
983 (ui->colour_cur != ds->colour_cur)) {
989 cur_redraw(fe, ds, COL_X(ds->colour_cur), COL_Y(ds->colour_cur), 1);
991 GUESS_X(ds->next_go, ds->peg_cur), GUESS_Y(ds->next_go, ds->peg_cur), 1);
994 cur_redraw(fe, ds, COL_X(ui->colour_cur), COL_Y(ui->colour_cur), 0);
996 GUESS_X(state->next_go, ui->peg_cur), GUESS_Y(state->next_go, ui->peg_cur), 0);
998 ds->display_cur = ui->display_cur;
999 ds->peg_cur = ui->peg_cur;
1000 ds->colour_cur = ui->colour_cur;
1001 ds->next_go = state->next_go;
1003 /* if ui->drag_col != 0, save the screen to the blitter,
1004 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1005 if (ui->drag_col != 0) {
1006 int ox = ui->drag_x - (PEGSZ/2);
1007 int oy = ui->drag_y - (PEGSZ/2);
1008 debug(("Saving to blitter at (%d,%d)", ox, oy));
1009 blitter_save(fe, ds->blit_peg, ox, oy);
1010 draw_peg(fe, ds, ox, oy, ui->drag_col);
1012 ds->blit_ox = ox; ds->blit_oy = oy;
1014 ds->drag_col = ui->drag_col;
1019 static float game_anim_length(game_state *oldstate, game_state *newstate,
1020 int dir, game_ui *ui)
1025 static float game_flash_length(game_state *oldstate, game_state *newstate,
1026 int dir, game_ui *ui)
1031 static int game_wants_statusbar(void)
1036 static int game_timing_state(game_state *state)
1042 #define thegame guess
1045 const struct game thegame = {
1046 "Guess", "games.guess",
1053 TRUE, game_configure, custom_params,
1062 FALSE, game_text_format,
1070 game_free_drawstate,
1074 game_wants_statusbar,
1075 FALSE, game_timing_state,
1076 0, /* mouse_priorities */
1079 /* vim: set shiftwidth=4 tabstop=8: */