Prepare to release sgt-puzzles (20170606.272beef-1).
[sgt-puzzles.git] / guess.c
blob8f058638daa68d521b2d9aada412484b82aa493d
1 /*
2 * guess.c: Mastermind clone.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <ctype.h>
10 #include <math.h>
12 #include "puzzles.h"
14 #define FLASH_FRAME 0.5F
16 enum {
17 COL_BACKGROUND,
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,
22 NCOLOURS
25 struct game_params {
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 {
34 int npegs;
35 int *pegs; /* 0 is 'empty' */
36 int *feedback; /* may well be unused */
37 } *pegrow;
39 struct game_state {
40 game_params params;
41 pegrow *guesses; /* length params->nguesses */
42 int *holds;
43 pegrow solution;
44 int next_go; /* from 0 to nguesses-1;
45 if next_go == nguesses then they've lost. */
46 int solved; /* +1 = win, -1 = lose, 0 = still playing */
49 static game_params *default_params(void)
51 game_params *ret = snew(game_params);
53 /* AFAIK this is the canonical Mastermind ruleset. */
54 ret->ncolours = 6;
55 ret->npegs = 4;
56 ret->nguesses = 10;
58 ret->allow_blank = 0;
59 ret->allow_multiple = 1;
61 return ret;
64 static void free_params(game_params *params)
66 sfree(params);
69 static game_params *dup_params(const game_params *params)
71 game_params *ret = snew(game_params);
72 *ret = *params; /* structure copy */
73 return ret;
76 static const struct {
77 char *name;
78 game_params params;
79 } guess_presets[] = {
80 {"Standard", {6, 4, 10, FALSE, TRUE}},
81 {"Super", {8, 5, 12, FALSE, TRUE}},
85 static int game_fetch_preset(int i, char **name, game_params **params)
87 if (i < 0 || i >= lenof(guess_presets))
88 return FALSE;
90 *name = dupstr(guess_presets[i].name);
92 * get round annoying const issues
95 game_params tmp = guess_presets[i].params;
96 *params = dup_params(&tmp);
99 return TRUE;
102 static void decode_params(game_params *params, char const *string)
104 char const *p = string;
105 game_params *defs = default_params();
107 *params = *defs; free_params(defs);
109 while (*p) {
110 switch (*p++) {
111 case 'c':
112 params->ncolours = atoi(p);
113 while (*p && isdigit((unsigned char)*p)) p++;
114 break;
116 case 'p':
117 params->npegs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
119 break;
121 case 'g':
122 params->nguesses = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
124 break;
126 case 'b':
127 params->allow_blank = 1;
128 break;
130 case 'B':
131 params->allow_blank = 0;
132 break;
134 case 'm':
135 params->allow_multiple = 1;
136 break;
138 case 'M':
139 params->allow_multiple = 0;
140 break;
142 default:
148 static char *encode_params(const game_params *params, int full)
150 char data[256];
152 sprintf(data, "c%dp%dg%d%s%s",
153 params->ncolours, params->npegs, params->nguesses,
154 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
156 return dupstr(data);
159 static config_item *game_configure(const game_params *params)
161 config_item *ret;
162 char buf[80];
164 ret = snewn(6, config_item);
166 ret[0].name = "Colours";
167 ret[0].type = C_STRING;
168 sprintf(buf, "%d", params->ncolours);
169 ret[0].sval = dupstr(buf);
170 ret[0].ival = 0;
172 ret[1].name = "Pegs per guess";
173 ret[1].type = C_STRING;
174 sprintf(buf, "%d", params->npegs);
175 ret[1].sval = dupstr(buf);
176 ret[1].ival = 0;
178 ret[2].name = "Guesses";
179 ret[2].type = C_STRING;
180 sprintf(buf, "%d", params->nguesses);
181 ret[2].sval = dupstr(buf);
182 ret[2].ival = 0;
184 ret[3].name = "Allow blanks";
185 ret[3].type = C_BOOLEAN;
186 ret[3].sval = NULL;
187 ret[3].ival = params->allow_blank;
189 ret[4].name = "Allow duplicates";
190 ret[4].type = C_BOOLEAN;
191 ret[4].sval = NULL;
192 ret[4].ival = params->allow_multiple;
194 ret[5].name = NULL;
195 ret[5].type = C_END;
196 ret[5].sval = NULL;
197 ret[5].ival = 0;
199 return ret;
202 static game_params *custom_params(const config_item *cfg)
204 game_params *ret = snew(game_params);
206 ret->ncolours = atoi(cfg[0].sval);
207 ret->npegs = atoi(cfg[1].sval);
208 ret->nguesses = atoi(cfg[2].sval);
210 ret->allow_blank = cfg[3].ival;
211 ret->allow_multiple = cfg[4].ival;
213 return ret;
216 static char *validate_params(const game_params *params, int full)
218 if (params->ncolours < 2 || params->npegs < 2)
219 return "Trivial solutions are uninteresting";
220 /* NB as well as the no. of colours we define, max(ncolours) must
221 * also fit in an unsigned char; see new_game_desc. */
222 if (params->ncolours > 10)
223 return "Too many colours";
224 if (params->nguesses < 1)
225 return "Must have at least one guess";
226 if (!params->allow_multiple && params->ncolours < params->npegs)
227 return "Disallowing multiple colours requires at least as many colours as pegs";
228 return NULL;
231 static pegrow new_pegrow(int npegs)
233 pegrow pegs = snew(struct pegrow);
235 pegs->npegs = npegs;
236 pegs->pegs = snewn(pegs->npegs, int);
237 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
238 pegs->feedback = snewn(pegs->npegs, int);
239 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
241 return pegs;
244 static pegrow dup_pegrow(pegrow pegs)
246 pegrow newpegs = new_pegrow(pegs->npegs);
248 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
249 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
251 return newpegs;
254 static void invalidate_pegrow(pegrow pegs)
256 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
257 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
260 static void free_pegrow(pegrow pegs)
262 sfree(pegs->pegs);
263 sfree(pegs->feedback);
264 sfree(pegs);
267 static char *new_game_desc(const game_params *params, random_state *rs,
268 char **aux, int interactive)
270 unsigned char *bmp = snewn(params->npegs, unsigned char);
271 char *ret;
272 int i, c;
273 pegrow colcount = new_pegrow(params->ncolours);
275 for (i = 0; i < params->npegs; i++) {
276 newcol:
277 c = random_upto(rs, params->ncolours);
278 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
279 colcount->pegs[c]++;
280 bmp[i] = (unsigned char)(c+1);
282 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
284 ret = bin2hex(bmp, params->npegs);
285 sfree(bmp);
286 free_pegrow(colcount);
287 return ret;
290 static char *validate_desc(const game_params *params, const char *desc)
292 unsigned char *bmp;
293 int i;
295 /* desc is just an (obfuscated) bitmap of the solution; check that
296 * it's the correct length and (when unobfuscated) contains only
297 * sensible colours. */
298 if (strlen(desc) != params->npegs * 2)
299 return "Game description is wrong length";
300 bmp = hex2bin(desc, params->npegs);
301 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
302 for (i = 0; i < params->npegs; i++) {
303 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
304 sfree(bmp);
305 return "Game description is corrupted";
308 sfree(bmp);
310 return NULL;
313 static game_state *new_game(midend *me, const game_params *params,
314 const char *desc)
316 game_state *state = snew(game_state);
317 unsigned char *bmp;
318 int i;
320 state->params = *params;
321 state->guesses = snewn(params->nguesses, pegrow);
322 for (i = 0; i < params->nguesses; i++)
323 state->guesses[i] = new_pegrow(params->npegs);
324 state->holds = snewn(params->npegs, int);
325 state->solution = new_pegrow(params->npegs);
327 bmp = hex2bin(desc, params->npegs);
328 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
329 for (i = 0; i < params->npegs; i++)
330 state->solution->pegs[i] = (int)bmp[i];
331 sfree(bmp);
333 memset(state->holds, 0, sizeof(int) * params->npegs);
334 state->next_go = state->solved = 0;
336 return state;
339 static game_state *dup_game(const game_state *state)
341 game_state *ret = snew(game_state);
342 int i;
344 *ret = *state;
346 ret->guesses = snewn(state->params.nguesses, pegrow);
347 for (i = 0; i < state->params.nguesses; i++)
348 ret->guesses[i] = dup_pegrow(state->guesses[i]);
349 ret->holds = snewn(state->params.npegs, int);
350 memcpy(ret->holds, state->holds, sizeof(int) * state->params.npegs);
351 ret->solution = dup_pegrow(state->solution);
353 return ret;
356 static void free_game(game_state *state)
358 int i;
360 free_pegrow(state->solution);
361 for (i = 0; i < state->params.nguesses; i++)
362 free_pegrow(state->guesses[i]);
363 sfree(state->holds);
364 sfree(state->guesses);
366 sfree(state);
369 static char *solve_game(const game_state *state, const game_state *currstate,
370 const char *aux, char **error)
372 return dupstr("S");
375 static int game_can_format_as_text_now(const game_params *params)
377 return TRUE;
380 static char *game_text_format(const game_state *state)
382 return NULL;
385 static int is_markable(const game_params *params, pegrow pegs)
387 int i, nset = 0, nrequired, ret = 0;
388 pegrow colcount = new_pegrow(params->ncolours);
390 nrequired = params->allow_blank ? 1 : params->npegs;
392 for (i = 0; i < params->npegs; i++) {
393 int c = pegs->pegs[i];
394 if (c > 0) {
395 colcount->pegs[c-1]++;
396 nset++;
399 if (nset < nrequired) goto done;
401 if (!params->allow_multiple) {
402 for (i = 0; i < params->ncolours; i++) {
403 if (colcount->pegs[i] > 1) goto done;
406 ret = 1;
407 done:
408 free_pegrow(colcount);
409 return ret;
412 struct game_ui {
413 game_params params;
414 pegrow curr_pegs; /* half-finished current move */
415 int *holds;
416 int colour_cur; /* position of up-down colour picker cursor */
417 int peg_cur; /* position of left-right peg picker cursor */
418 int display_cur, markable;
420 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
421 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
423 int show_labels; /* label the colours with letters */
424 pegrow hint;
427 static game_ui *new_ui(const game_state *state)
429 game_ui *ui = snew(game_ui);
430 memset(ui, 0, sizeof(game_ui));
431 ui->params = state->params; /* structure copy */
432 ui->curr_pegs = new_pegrow(state->params.npegs);
433 ui->holds = snewn(state->params.npegs, int);
434 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
435 ui->drag_opeg = -1;
436 return ui;
439 static void free_ui(game_ui *ui)
441 if (ui->hint)
442 free_pegrow(ui->hint);
443 free_pegrow(ui->curr_pegs);
444 sfree(ui->holds);
445 sfree(ui);
448 static char *encode_ui(const game_ui *ui)
450 char *ret, *p, *sep;
451 int i;
454 * For this game it's worth storing the contents of the current
455 * guess, and the current set of holds.
457 ret = snewn(40 * ui->curr_pegs->npegs, char);
458 p = ret;
459 sep = "";
460 for (i = 0; i < ui->curr_pegs->npegs; i++) {
461 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
462 ui->holds[i] ? "_" : "");
463 sep = ",";
465 *p++ = '\0';
466 assert(p - ret < 40 * ui->curr_pegs->npegs);
467 return sresize(ret, p - ret, char);
470 static void decode_ui(game_ui *ui, const char *encoding)
472 int i;
473 const char *p = encoding;
474 for (i = 0; i < ui->curr_pegs->npegs; i++) {
475 ui->curr_pegs->pegs[i] = atoi(p);
476 while (*p && isdigit((unsigned char)*p)) p++;
477 if (*p == '_') {
478 /* NB: old versions didn't store holds */
479 ui->holds[i] = 1;
480 p++;
481 } else
482 ui->holds[i] = 0;
483 if (*p == ',') p++;
485 ui->markable = is_markable(&ui->params, ui->curr_pegs);
488 static void game_changed_state(game_ui *ui, const game_state *oldstate,
489 const game_state *newstate)
491 int i;
493 if (newstate->next_go < oldstate->next_go) {
494 sfree(ui->hint);
495 ui->hint = NULL;
498 /* Implement holds, clear other pegs.
499 * This does something that is arguably the Right Thing even
500 * for undo. */
501 for (i = 0; i < newstate->solution->npegs; i++) {
502 if (newstate->solved)
503 ui->holds[i] = 0;
504 else
505 ui->holds[i] = newstate->holds[i];
506 if (newstate->solved || (newstate->next_go == 0) || !ui->holds[i]) {
507 ui->curr_pegs->pegs[i] = 0;
508 } else
509 ui->curr_pegs->pegs[i] =
510 newstate->guesses[newstate->next_go-1]->pegs[i];
512 ui->markable = is_markable(&newstate->params, ui->curr_pegs);
513 /* Clean up cursor position */
514 if (!ui->markable && ui->peg_cur == newstate->solution->npegs)
515 ui->peg_cur--;
518 #define PEGSZ (ds->pegsz)
519 #define PEGOFF (ds->pegsz + ds->gapsz)
520 #define HINTSZ (ds->hintsz)
521 #define HINTOFF (ds->hintsz + ds->gapsz)
523 #define GAP (ds->gapsz)
524 #define CGAP (ds->gapsz / 2)
526 #define PEGRAD (ds->pegrad)
527 #define HINTRAD (ds->hintrad)
529 #define COL_OX (ds->colx)
530 #define COL_OY (ds->coly)
531 #define COL_X(c) (COL_OX)
532 #define COL_Y(c) (COL_OY + (c)*PEGOFF)
533 #define COL_W PEGOFF
534 #define COL_H (ds->colours->npegs*PEGOFF)
536 #define GUESS_OX (ds->guessx)
537 #define GUESS_OY (ds->guessy)
538 #define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
539 #define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
540 #define GUESS_W (ds->solution->npegs*PEGOFF)
541 #define GUESS_H (ds->nguesses*PEGOFF)
543 #define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
544 #define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
545 #define HINT_X(g) HINT_OX
546 #define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
547 #define HINT_W ((ds->hintw*HINTOFF) - GAP)
548 #define HINT_H GUESS_H
550 #define SOLN_OX GUESS_OX
551 #define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
552 #define SOLN_W GUESS_W
553 #define SOLN_H PEGOFF
555 struct game_drawstate {
556 int nguesses;
557 pegrow *guesses; /* same size as state->guesses */
558 pegrow solution; /* only displayed if state->solved */
559 pegrow colours; /* length ncolours, not npegs */
561 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
562 int pegrad, hintrad; /* radius of peg, hint */
563 int border;
564 int colx, coly; /* origin of colours vertical bar */
565 int guessx, guessy; /* origin of guesses */
566 int solnx, solny; /* origin of solution */
567 int hintw; /* no. of hint tiles we're wide per row */
568 int w, h, started, solved;
570 int next_go;
572 blitter *blit_peg;
573 int drag_col, blit_ox, blit_oy;
576 static void set_peg(const game_params *params, game_ui *ui, int peg, int col)
578 ui->curr_pegs->pegs[peg] = col;
579 ui->markable = is_markable(params, ui->curr_pegs);
582 static int mark_pegs(pegrow guess, const pegrow solution, int ncols)
584 int nc_place = 0, nc_colour = 0, i, j;
586 assert(guess && solution && (guess->npegs == solution->npegs));
588 for (i = 0; i < guess->npegs; i++) {
589 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
592 /* slight bit of cleverness: we have the following formula, from
593 * http://mathworld.wolfram.com/Mastermind.html that gives:
595 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
597 * I think this is due to Knuth.
599 for (i = 1; i <= ncols; i++) {
600 int n_guess = 0, n_solution = 0;
601 for (j = 0; j < guess->npegs; j++) {
602 if (guess->pegs[j] == i) n_guess++;
603 if (solution->pegs[j] == i) n_solution++;
605 nc_colour += min(n_guess, n_solution);
607 nc_colour -= nc_place;
609 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
610 guess->npegs, nc_place, nc_colour));
611 assert((nc_colour + nc_place) <= guess->npegs);
613 memset(guess->feedback, 0, guess->npegs*sizeof(int));
614 for (i = 0, j = 0; i < nc_place; i++)
615 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
616 for (i = 0; i < nc_colour; i++)
617 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
619 return nc_place;
622 static char *encode_move(const game_state *from, game_ui *ui)
624 char *buf, *p, *sep;
625 int len, i;
627 len = ui->curr_pegs->npegs * 20 + 2;
628 buf = snewn(len, char);
629 p = buf;
630 *p++ = 'G';
631 sep = "";
632 for (i = 0; i < ui->curr_pegs->npegs; i++) {
633 p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
634 ui->holds[i] ? "_" : "");
635 sep = ",";
637 *p++ = '\0';
638 assert(p - buf <= len);
639 buf = sresize(buf, len, char);
641 return buf;
644 static void compute_hint(const game_state *state, game_ui *ui)
646 /* Suggest the lexicographically first row consistent with all
647 * previous feedback. This is not only a useful hint, but also
648 * a reasonable strategy if applied consistently. If the user
649 * uses hints in every turn, they may be able to intuit this
650 * strategy, or one similar to it. I (Jonas Kölker) came up
651 * with something close to it without seeing it in action. */
653 /* Some performance characteristics: I want to ask for each n,
654 * how many solutions are guessed in exactly n guesses if you
655 * use the hint in each turn.
657 * With 4 pegs and 6 colours you get the following histogram:
659 * 1 guesses: 1 solution
660 * 2 guesses: 4 solutions
661 * 3 guesses: 25 solutions
662 * 4 guesses: 108 solutions
663 * 5 guesses: 305 solutions
664 * 6 guesses: 602 solutions
665 * 7 guesses: 196 solutions
666 * 8 guesses: 49 solutions
667 * 9 guesses: 6 solutions
668 * (note: the tenth guess is never necessary.)
670 * With 5 pegs and 8 colours you get the following histogram:
672 * 1 guesses: 1 solution
673 * 2 guesses: 5 solutions
674 * 3 guesses: 43 solutions
675 * 4 guesses: 278 solutions
676 * 5 guesses: 1240 solutions
677 * 6 guesses: 3515 solutions
678 * 7 guesses: 7564 solutions
679 * 8 guesses: 14086 solutions
680 * 9 guesses: 4614 solutions
681 * 10 guesses: 1239 solutions
682 * 11 guesses: 175 solutions
683 * 12 guesses: 7 solutions
684 * 13 guesses: 1 solution
686 * The solution which takes too many guesses is {8, 8, 5, 6, 7}.
687 * The game ID is c8p5g12Bm:4991e5e41a. */
689 int mincolour = 1, maxcolour = 0, i, j;
691 /* For large values of npegs and ncolours, the lexicographically
692 * next guess make take a while to find. Finding upper and
693 * lower limits on which colours we have to consider will speed
694 * this up, as will caching our progress from one invocation to
695 * the next. The latter strategy works, since if we have ruled
696 * out a candidate we will never reverse this judgment in the
697 * light of new information. Removing information, i.e. undo,
698 * will require us to backtrack somehow. We backtrack by fully
699 * forgetting our progress (and recomputing it if required). */
701 for (i = 0; i < state->next_go; ++i)
702 for (j = 0; j < state->params.npegs; ++j)
703 if (state->guesses[i]->pegs[j] > maxcolour)
704 maxcolour = state->guesses[i]->pegs[j];
705 maxcolour = min(maxcolour + 1, state->params.ncolours);
707 increase_mincolour:
708 for (i = 0; i < state->next_go; ++i) {
709 if (state->guesses[i]->feedback[0])
710 goto next_iteration;
711 for (j = 0; j < state->params.npegs; ++j)
712 if (state->guesses[i]->pegs[j] != mincolour)
713 goto next_iteration;
714 ++mincolour;
715 goto increase_mincolour;
716 next_iteration:
720 if (!ui->hint) {
721 ui->hint = new_pegrow(state->params.npegs);
722 for (i = 0; i < state->params.npegs; ++i)
723 ui->hint->pegs[i] = 1;
726 while (ui->hint->pegs[0] <= state->params.ncolours) {
727 for (i = 0; i < state->next_go; ++i) {
728 mark_pegs(ui->hint, state->guesses[i], maxcolour);
729 for (j = 0; j < state->params.npegs; ++j)
730 if (ui->hint->feedback[j] != state->guesses[i]->feedback[j])
731 goto increment_pegrow;
733 /* a valid guess was found; install it and return */
734 for (i = 0; i < state->params.npegs; ++i)
735 ui->curr_pegs->pegs[i] = ui->hint->pegs[i];
737 ui->markable = TRUE;
738 ui->peg_cur = state->params.npegs;
739 ui->display_cur = 1;
740 return;
742 increment_pegrow:
743 for (i = ui->hint->npegs;
744 ++ui->hint->pegs[--i], i && ui->hint->pegs[i] > maxcolour;
745 ui->hint->pegs[i] = mincolour);
747 /* No solution is compatible with the given hints. Impossible! */
748 /* (hack new_game_desc to create invalid solutions to get here) */
750 /* For some values of npegs and ncolours, the hinting function takes a
751 * long time to complete. To visually indicate completion with failure,
752 * should it ever happen, update the ui in some trivial way. This gives
753 * the user a sense of broken(ish)ness and futility. */
754 if (!ui->display_cur) {
755 ui->display_cur = 1;
756 } else if (state->params.npegs == 1) {
757 ui->display_cur = 0;
758 } else {
759 ui->peg_cur = (ui->peg_cur + 1) % state->params.npegs;
763 static char *interpret_move(const game_state *from, game_ui *ui,
764 const game_drawstate *ds,
765 int x, int y, int button)
767 int over_col = 0; /* one-indexed */
768 int over_guess = -1; /* zero-indexed */
769 int over_past_guess_y = -1; /* zero-indexed */
770 int over_past_guess_x = -1; /* zero-indexed */
771 int over_hint = 0; /* zero or one */
772 char *ret = NULL;
774 int guess_ox = GUESS_X(from->next_go, 0);
775 int guess_oy = GUESS_Y(from->next_go, 0);
778 * Enable or disable labels on colours.
780 if (button == 'l' || button == 'L') {
781 ui->show_labels = !ui->show_labels;
782 return "";
785 if (from->solved) return NULL;
787 if (x >= COL_OX && x < (COL_OX + COL_W) &&
788 y >= COL_OY && y < (COL_OY + COL_H)) {
789 over_col = ((y - COL_OY) / PEGOFF) + 1;
790 assert(over_col >= 1 && over_col <= ds->colours->npegs);
791 } else if (x >= guess_ox &&
792 y >= guess_oy && y < (guess_oy + GUESS_H)) {
793 if (x < (guess_ox + GUESS_W)) {
794 over_guess = (x - guess_ox) / PEGOFF;
795 assert(over_guess >= 0 && over_guess < ds->solution->npegs);
796 } else {
797 over_hint = 1;
799 } else if (x >= guess_ox && x < (guess_ox + GUESS_W) &&
800 y >= GUESS_OY && y < guess_oy) {
801 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
802 over_past_guess_x = (x - guess_ox) / PEGOFF;
803 assert(over_past_guess_y >= 0 && over_past_guess_y < from->next_go);
804 assert(over_past_guess_x >= 0 && over_past_guess_x < ds->solution->npegs);
806 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
807 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
808 over_past_guess_x, over_past_guess_y));
810 assert(ds->blit_peg);
812 /* mouse input */
813 if (button == LEFT_BUTTON) {
814 if (over_col > 0) {
815 ui->drag_col = over_col;
816 ui->drag_opeg = -1;
817 debug(("Start dragging from colours"));
818 } else if (over_guess > -1) {
819 int col = ui->curr_pegs->pegs[over_guess];
820 if (col) {
821 ui->drag_col = col;
822 ui->drag_opeg = over_guess;
823 debug(("Start dragging from a guess"));
825 } else if (over_past_guess_y > -1) {
826 int col =
827 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
828 if (col) {
829 ui->drag_col = col;
830 ui->drag_opeg = -1;
831 debug(("Start dragging from a past guess"));
834 if (ui->drag_col) {
835 ui->drag_x = x;
836 ui->drag_y = y;
837 debug(("Start dragging, col = %d, (%d,%d)",
838 ui->drag_col, ui->drag_x, ui->drag_y));
839 ret = "";
841 } else if (button == LEFT_DRAG && ui->drag_col) {
842 ui->drag_x = x;
843 ui->drag_y = y;
844 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
845 ret = "";
846 } else if (button == LEFT_RELEASE && ui->drag_col) {
847 if (over_guess > -1) {
848 debug(("Dropping colour %d onto guess peg %d",
849 ui->drag_col, over_guess));
850 set_peg(&from->params, ui, over_guess, ui->drag_col);
851 } else {
852 if (ui->drag_opeg > -1) {
853 debug(("Removing colour %d from peg %d",
854 ui->drag_col, ui->drag_opeg));
855 set_peg(&from->params, ui, ui->drag_opeg, 0);
858 ui->drag_col = 0;
859 ui->drag_opeg = -1;
860 ui->display_cur = 0;
861 debug(("Stop dragging."));
862 ret = "";
863 } else if (button == RIGHT_BUTTON) {
864 if (over_guess > -1) {
865 /* we use ths feedback in the game_ui to signify
866 * 'carry this peg to the next guess as well'. */
867 ui->holds[over_guess] = 1 - ui->holds[over_guess];
868 ret = "";
870 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
871 /* NB this won't trigger if on the end of a drag; that's on
872 * purpose, in case you drop by mistake... */
873 ret = encode_move(from, ui);
876 /* keyboard input */
877 if (button == CURSOR_UP || button == CURSOR_DOWN) {
878 ui->display_cur = 1;
879 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
880 ui->colour_cur++;
881 if (button == CURSOR_UP && ui->colour_cur > 0)
882 ui->colour_cur--;
883 ret = "";
884 } else if (button == 'h' || button == 'H' || button == '?') {
885 compute_hint(from, ui);
886 ret = "";
887 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
888 int maxcur = from->params.npegs;
889 if (ui->markable) maxcur++;
891 ui->display_cur = 1;
892 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
893 ui->peg_cur++;
894 if (button == CURSOR_LEFT && ui->peg_cur > 0)
895 ui->peg_cur--;
896 ret = "";
897 } else if (IS_CURSOR_SELECT(button)) {
898 ui->display_cur = 1;
899 if (ui->peg_cur == from->params.npegs) {
900 ret = encode_move(from, ui);
901 } else {
902 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
903 ret = "";
905 } else if (button == 'D' || button == 'd' || button == '\b') {
906 ui->display_cur = 1;
907 set_peg(&from->params, ui, ui->peg_cur, 0);
908 ret = "";
909 } else if (button == CURSOR_SELECT2) {
910 if (ui->peg_cur == from->params.npegs)
911 return NULL;
912 ui->display_cur = 1;
913 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
914 ret = "";
916 return ret;
919 static game_state *execute_move(const game_state *from, const char *move)
921 int i, nc_place;
922 game_state *ret;
923 const char *p;
925 if (!strcmp(move, "S")) {
926 ret = dup_game(from);
927 ret->solved = -1;
928 return ret;
929 } else if (move[0] == 'G') {
930 p = move+1;
932 ret = dup_game(from);
934 for (i = 0; i < from->solution->npegs; i++) {
935 int val = atoi(p);
936 int min_colour = from->params.allow_blank? 0 : 1;
937 if (val < min_colour || val > from->params.ncolours) {
938 free_game(ret);
939 return NULL;
941 ret->guesses[from->next_go]->pegs[i] = atoi(p);
942 while (*p && isdigit((unsigned char)*p)) p++;
943 if (*p == '_') {
944 ret->holds[i] = 1;
945 p++;
946 } else
947 ret->holds[i] = 0;
948 if (*p == ',') p++;
951 nc_place = mark_pegs(ret->guesses[from->next_go], ret->solution, ret->params.ncolours);
953 if (nc_place == ret->solution->npegs) {
954 ret->solved = +1; /* win! */
955 } else {
956 ret->next_go = from->next_go + 1;
957 if (ret->next_go >= ret->params.nguesses)
958 ret->solved = -1; /* lose, meaning we show the pegs. */
961 return ret;
962 } else
963 return NULL;
966 /* ----------------------------------------------------------------------
967 * Drawing routines.
970 #define PEG_PREFER_SZ 32
972 /* next three are multipliers for pegsz. It will look much nicer if
973 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
974 #define PEG_GAP 0.10
975 #define PEG_HINT 0.35
977 #define BORDER 0.5
979 static void game_compute_size(const game_params *params, int tilesize,
980 int *x, int *y)
982 double hmul, vmul_c, vmul_g, vmul;
983 int hintw = (params->npegs+1)/2;
985 hmul = BORDER * 2.0 + /* border */
986 1.0 * 2.0 + /* vertical colour bar */
987 1.0 * params->npegs + /* guess pegs */
988 PEG_GAP * params->npegs + /* guess gaps */
989 PEG_HINT * hintw + /* hint pegs */
990 PEG_GAP * (hintw - 1); /* hint gaps */
992 vmul_c = BORDER * 2.0 + /* border */
993 1.0 * params->ncolours + /* colour pegs */
994 PEG_GAP * (params->ncolours - 1); /* colour gaps */
996 vmul_g = BORDER * 2.0 + /* border */
997 1.0 * (params->nguesses + 1) + /* guesses plus solution */
998 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
1000 vmul = max(vmul_c, vmul_g);
1002 *x = (int)ceil((double)tilesize * hmul);
1003 *y = (int)ceil((double)tilesize * vmul);
1006 static void game_set_size(drawing *dr, game_drawstate *ds,
1007 const game_params *params, int tilesize)
1009 int colh, guessh;
1011 ds->pegsz = tilesize;
1013 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
1014 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
1015 ds->border = (int)((double)ds->pegsz * BORDER);
1017 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
1018 ds->hintrad = (ds->hintsz-1)/2;
1020 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
1021 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
1022 guessh += ds->gapsz + ds->pegsz; /* solution */
1024 game_compute_size(params, tilesize, &ds->w, &ds->h);
1025 ds->colx = ds->border;
1026 ds->coly = (ds->h - colh) / 2;
1028 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
1029 ds->guessy = (ds->h - guessh) / 2;
1030 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
1032 assert(ds->pegsz > 0);
1033 assert(!ds->blit_peg); /* set_size is never called twice */
1034 ds->blit_peg = blitter_new(dr, ds->pegsz+2, ds->pegsz+2);
1037 static float *game_colours(frontend *fe, int *ncolours)
1039 float *ret = snewn(3 * NCOLOURS, float), max;
1040 int i;
1042 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1044 /* red */
1045 ret[COL_1 * 3 + 0] = 1.0F;
1046 ret[COL_1 * 3 + 1] = 0.0F;
1047 ret[COL_1 * 3 + 2] = 0.0F;
1049 /* yellow */
1050 ret[COL_2 * 3 + 0] = 1.0F;
1051 ret[COL_2 * 3 + 1] = 1.0F;
1052 ret[COL_2 * 3 + 2] = 0.0F;
1054 /* green */
1055 ret[COL_3 * 3 + 0] = 0.0F;
1056 ret[COL_3 * 3 + 1] = 1.0F;
1057 ret[COL_3 * 3 + 2] = 0.0F;
1059 /* blue */
1060 ret[COL_4 * 3 + 0] = 0.2F;
1061 ret[COL_4 * 3 + 1] = 0.3F;
1062 ret[COL_4 * 3 + 2] = 1.0F;
1064 /* orange */
1065 ret[COL_5 * 3 + 0] = 1.0F;
1066 ret[COL_5 * 3 + 1] = 0.5F;
1067 ret[COL_5 * 3 + 2] = 0.0F;
1069 /* purple */
1070 ret[COL_6 * 3 + 0] = 0.5F;
1071 ret[COL_6 * 3 + 1] = 0.0F;
1072 ret[COL_6 * 3 + 2] = 0.7F;
1074 /* brown */
1075 ret[COL_7 * 3 + 0] = 0.5F;
1076 ret[COL_7 * 3 + 1] = 0.3F;
1077 ret[COL_7 * 3 + 2] = 0.3F;
1079 /* light blue */
1080 ret[COL_8 * 3 + 0] = 0.4F;
1081 ret[COL_8 * 3 + 1] = 0.8F;
1082 ret[COL_8 * 3 + 2] = 1.0F;
1084 /* light green */
1085 ret[COL_9 * 3 + 0] = 0.7F;
1086 ret[COL_9 * 3 + 1] = 1.0F;
1087 ret[COL_9 * 3 + 2] = 0.7F;
1089 /* pink */
1090 ret[COL_10 * 3 + 0] = 1.0F;
1091 ret[COL_10 * 3 + 1] = 0.6F;
1092 ret[COL_10 * 3 + 2] = 1.0F;
1094 ret[COL_FRAME * 3 + 0] = 0.0F;
1095 ret[COL_FRAME * 3 + 1] = 0.0F;
1096 ret[COL_FRAME * 3 + 2] = 0.0F;
1098 ret[COL_CURSOR * 3 + 0] = 0.0F;
1099 ret[COL_CURSOR * 3 + 1] = 0.0F;
1100 ret[COL_CURSOR * 3 + 2] = 0.0F;
1102 ret[COL_FLASH * 3 + 0] = 0.5F;
1103 ret[COL_FLASH * 3 + 1] = 1.0F;
1104 ret[COL_FLASH * 3 + 2] = 1.0F;
1106 ret[COL_HOLD * 3 + 0] = 1.0F;
1107 ret[COL_HOLD * 3 + 1] = 0.5F;
1108 ret[COL_HOLD * 3 + 2] = 0.5F;
1110 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
1111 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
1112 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
1114 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
1115 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
1116 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
1118 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
1119 * (which we hard-code as white) from COL_BACKGROUND (which
1120 * could default to white on some platforms).
1121 * Code borrowed from fifteen.c. */
1122 max = ret[COL_BACKGROUND*3];
1123 for (i = 1; i < 3; i++)
1124 if (ret[COL_BACKGROUND*3+i] > max)
1125 max = ret[COL_BACKGROUND*3+i];
1126 if (max * 1.2F > 1.0F) {
1127 for (i = 0; i < 3; i++)
1128 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
1131 /* We also want to be able to tell the difference between BACKGROUND
1132 * and EMPTY, for similar distinguishing-hint reasons. */
1133 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0F / 3.0F;
1134 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0F / 3.0F;
1135 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0F / 3.0F;
1137 *ncolours = NCOLOURS;
1138 return ret;
1141 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1143 struct game_drawstate *ds = snew(struct game_drawstate);
1144 int i;
1146 memset(ds, 0, sizeof(struct game_drawstate));
1148 ds->guesses = snewn(state->params.nguesses, pegrow);
1149 ds->nguesses = state->params.nguesses;
1150 for (i = 0; i < state->params.nguesses; i++) {
1151 ds->guesses[i] = new_pegrow(state->params.npegs);
1152 invalidate_pegrow(ds->guesses[i]);
1154 ds->solution = new_pegrow(state->params.npegs);
1155 invalidate_pegrow(ds->solution);
1156 ds->colours = new_pegrow(state->params.ncolours);
1157 invalidate_pegrow(ds->colours);
1159 ds->hintw = (state->params.npegs+1)/2; /* must round up */
1161 ds->blit_peg = NULL;
1163 return ds;
1166 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1168 int i;
1170 if (ds->blit_peg) blitter_free(dr, ds->blit_peg);
1171 free_pegrow(ds->colours);
1172 free_pegrow(ds->solution);
1173 for (i = 0; i < ds->nguesses; i++)
1174 free_pegrow(ds->guesses[i]);
1175 sfree(ds->guesses);
1176 sfree(ds);
1179 static void draw_peg(drawing *dr, game_drawstate *ds, int cx, int cy,
1180 int moving, int labelled, int col)
1183 * Some platforms antialias circles, which means we shouldn't
1184 * overwrite a circle of one colour with a circle of another
1185 * colour without erasing the background first. However, if the
1186 * peg is the one being dragged, we don't erase the background
1187 * because we _want_ it to alpha-blend nicely into whatever's
1188 * behind it.
1190 if (!moving)
1191 draw_rect(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
1192 COL_BACKGROUND);
1193 if (PEGRAD > 0) {
1194 draw_circle(dr, cx+PEGRAD, cy+PEGRAD, PEGRAD,
1195 COL_EMPTY + col, (col ? COL_FRAME : COL_EMPTY));
1196 } else
1197 draw_rect(dr, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
1199 if (labelled && col) {
1200 char buf[2];
1201 buf[0] = 'a'-1 + col;
1202 buf[1] = '\0';
1203 draw_text(dr, cx+PEGRAD, cy+PEGRAD, FONT_VARIABLE, PEGRAD,
1204 ALIGN_HCENTRE|ALIGN_VCENTRE, COL_FRAME, buf);
1207 draw_update(dr, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1210 static void draw_cursor(drawing *dr, game_drawstate *ds, int x, int y)
1212 draw_circle(dr, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, -1, COL_CURSOR);
1214 draw_update(dr, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
1217 static void guess_redraw(drawing *dr, game_drawstate *ds, int guess,
1218 pegrow src, int *holds, int cur_col, int force,
1219 int labelled)
1221 pegrow dest;
1222 int rowx, rowy, i, scol;
1224 if (guess == -1) {
1225 dest = ds->solution;
1226 rowx = SOLN_OX;
1227 rowy = SOLN_OY;
1228 } else {
1229 dest = ds->guesses[guess];
1230 rowx = GUESS_X(guess,0);
1231 rowy = GUESS_Y(guess,0);
1233 if (src) assert(src->npegs == dest->npegs);
1235 for (i = 0; i < dest->npegs; i++) {
1236 scol = src ? src->pegs[i] : 0;
1237 if (i == cur_col)
1238 scol |= 0x1000;
1239 if (holds && holds[i])
1240 scol |= 0x2000;
1241 if (labelled)
1242 scol |= 0x4000;
1243 if ((dest->pegs[i] != scol) || force) {
1244 draw_peg(dr, ds, rowx + PEGOFF * i, rowy, FALSE, labelled,
1245 scol &~ 0x7000);
1247 * Hold marker.
1249 draw_rect(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1250 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
1251 draw_update(dr, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
1252 PEGSZ, 2);
1253 if (scol & 0x1000)
1254 draw_cursor(dr, ds, rowx + PEGOFF * i, rowy);
1256 dest->pegs[i] = scol;
1260 static void hint_redraw(drawing *dr, game_drawstate *ds, int guess,
1261 pegrow src, int force, int cursor, int markable)
1263 pegrow dest = ds->guesses[guess];
1264 int rowx, rowy, i, scol, col, hintlen;
1265 int need_redraw;
1266 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
1268 if (src) assert(src->npegs == dest->npegs);
1270 hintlen = (dest->npegs + 1)/2;
1273 * Because of the possible presence of the cursor around this
1274 * entire section, we redraw all or none of it but never part.
1276 need_redraw = FALSE;
1278 for (i = 0; i < dest->npegs; i++) {
1279 scol = src ? src->feedback[i] : 0;
1280 if (i == 0 && cursor)
1281 scol |= 0x1000;
1282 if (i == 0 && markable)
1283 scol |= 0x2000;
1284 if ((scol != dest->feedback[i]) || force) {
1285 need_redraw = TRUE;
1287 dest->feedback[i] = scol;
1290 if (need_redraw) {
1291 int hinth = HINTSZ + GAP + HINTSZ;
1292 int hx,hy,hw,hh;
1294 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1295 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1297 /* erase a large background rectangle */
1298 draw_rect(dr, hx, hy, hw, hh, COL_BACKGROUND);
1300 for (i = 0; i < dest->npegs; i++) {
1301 scol = src ? src->feedback[i] : 0;
1302 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1303 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1304 emptycol);
1306 rowx = HINT_X(guess);
1307 rowy = HINT_Y(guess);
1308 if (i < hintlen) {
1309 rowx += HINTOFF * i;
1310 } else {
1311 rowx += HINTOFF * (i - hintlen);
1312 rowy += HINTOFF;
1314 if (HINTRAD > 0) {
1315 draw_circle(dr, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, col,
1316 (col == emptycol ? emptycol : COL_FRAME));
1317 } else {
1318 draw_rect(dr, rowx, rowy, HINTSZ, HINTSZ, col);
1321 if (cursor) {
1322 int x1,y1,x2,y2;
1323 x1 = hx + CGAP; y1 = hy + CGAP;
1324 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1325 draw_line(dr, x1, y1, x2, y1, COL_CURSOR);
1326 draw_line(dr, x2, y1, x2, y2, COL_CURSOR);
1327 draw_line(dr, x2, y2, x1, y2, COL_CURSOR);
1328 draw_line(dr, x1, y2, x1, y1, COL_CURSOR);
1331 draw_update(dr, hx, hy, hw, hh);
1335 static void currmove_redraw(drawing *dr, game_drawstate *ds, int guess, int col)
1337 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1339 draw_rect(dr, ox-off-1, oy, 2, PEGSZ, col);
1340 draw_update(dr, ox-off-1, oy, 2, PEGSZ);
1343 static void game_redraw(drawing *dr, game_drawstate *ds,
1344 const game_state *oldstate, const game_state *state,
1345 int dir, const game_ui *ui,
1346 float animtime, float flashtime)
1348 int i, new_move;
1350 new_move = (state->next_go != ds->next_go) || !ds->started;
1352 if (!ds->started) {
1353 draw_rect(dr, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1354 draw_rect(dr, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1355 draw_update(dr, 0, 0, ds->w, ds->h);
1358 if (ds->drag_col != 0) {
1359 debug(("Loading from blitter."));
1360 blitter_load(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1361 draw_update(dr, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1364 /* draw the colours */
1365 for (i = 0; i < state->params.ncolours; i++) {
1366 int val = i+1;
1367 if (ui->display_cur && ui->colour_cur == i)
1368 val |= 0x1000;
1369 if (ui->show_labels)
1370 val |= 0x2000;
1371 if (ds->colours->pegs[i] != val) {
1372 draw_peg(dr, ds, COL_X(i), COL_Y(i), FALSE, ui->show_labels, i+1);
1373 if (val & 0x1000)
1374 draw_cursor(dr, ds, COL_X(i), COL_Y(i));
1375 ds->colours->pegs[i] = val;
1379 /* draw the guesses (so far) and the hints
1380 * (in reverse order to avoid trampling holds, and postponing the
1381 * next_go'th to not overrender the top of the circular cursor) */
1382 for (i = state->params.nguesses - 1; i >= 0; i--) {
1383 if (i < state->next_go || state->solved) {
1384 /* this info is stored in the game_state already */
1385 guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0,
1386 ui->show_labels);
1387 hint_redraw(dr, ds, i, state->guesses[i],
1388 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
1389 } else if (i > state->next_go) {
1390 /* we've not got here yet; it's blank. */
1391 guess_redraw(dr, ds, i, NULL, NULL, -1, 0, ui->show_labels);
1392 hint_redraw(dr, ds, i, NULL, 0, FALSE, FALSE);
1395 if (!state->solved) {
1396 /* this is the one we're on; the (incomplete) guess is stored in
1397 * the game_ui. */
1398 guess_redraw(dr, ds, state->next_go, ui->curr_pegs,
1399 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0,
1400 ui->show_labels);
1401 hint_redraw(dr, ds, state->next_go, NULL, 1,
1402 ui->display_cur && ui->peg_cur == state->params.npegs,
1403 ui->markable);
1406 /* draw the 'current move' and 'able to mark' sign. */
1407 if (new_move)
1408 currmove_redraw(dr, ds, ds->next_go, COL_BACKGROUND);
1409 if (!state->solved)
1410 currmove_redraw(dr, ds, state->next_go, COL_HOLD);
1412 /* draw the solution (or the big rectangle) */
1413 if ((!state->solved ^ !ds->solved) || !ds->started) {
1414 draw_rect(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1415 state->solved ? COL_BACKGROUND : COL_EMPTY);
1416 draw_update(dr, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1418 if (state->solved)
1419 guess_redraw(dr, ds, -1, state->solution, NULL, -1, !ds->solved,
1420 ui->show_labels);
1421 ds->solved = state->solved;
1423 ds->next_go = state->next_go;
1425 /* if ui->drag_col != 0, save the screen to the blitter,
1426 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1427 if (ui->drag_col != 0) {
1428 int ox = ui->drag_x - (PEGSZ/2);
1429 int oy = ui->drag_y - (PEGSZ/2);
1430 ds->blit_ox = ox - 1; ds->blit_oy = oy - 1;
1431 debug(("Saving to blitter at (%d,%d)", ds->blit_ox, ds->blit_oy));
1432 blitter_save(dr, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1433 draw_peg(dr, ds, ox, oy, TRUE, ui->show_labels, ui->drag_col);
1435 ds->drag_col = ui->drag_col;
1437 ds->started = 1;
1440 static float game_anim_length(const game_state *oldstate,
1441 const game_state *newstate, int dir, game_ui *ui)
1443 return 0.0F;
1446 static float game_flash_length(const game_state *oldstate,
1447 const game_state *newstate, int dir, game_ui *ui)
1449 return 0.0F;
1452 static int game_status(const game_state *state)
1455 * We return nonzero whenever the solution has been revealed, even
1456 * (on spoiler grounds) if it wasn't guessed correctly. The
1457 * correct return value from this function is already in
1458 * state->solved.
1460 return state->solved;
1463 static int game_timing_state(const game_state *state, game_ui *ui)
1465 return TRUE;
1468 static void game_print_size(const game_params *params, float *x, float *y)
1472 static void game_print(drawing *dr, const game_state *state, int tilesize)
1476 #ifdef COMBINED
1477 #define thegame guess
1478 #endif
1480 const struct game thegame = {
1481 "Guess", "games.guess", "guess",
1482 default_params,
1483 game_fetch_preset, NULL,
1484 decode_params,
1485 encode_params,
1486 free_params,
1487 dup_params,
1488 TRUE, game_configure, custom_params,
1489 validate_params,
1490 new_game_desc,
1491 validate_desc,
1492 new_game,
1493 dup_game,
1494 free_game,
1495 TRUE, solve_game,
1496 FALSE, game_can_format_as_text_now, game_text_format,
1497 new_ui,
1498 free_ui,
1499 encode_ui,
1500 decode_ui,
1501 game_changed_state,
1502 interpret_move,
1503 execute_move,
1504 PEG_PREFER_SZ, game_compute_size, game_set_size,
1505 game_colours,
1506 game_new_drawstate,
1507 game_free_drawstate,
1508 game_redraw,
1509 game_anim_length,
1510 game_flash_length,
1511 game_status,
1512 FALSE, FALSE, game_print_size, game_print,
1513 FALSE, /* wants_statusbar */
1514 FALSE, game_timing_state,
1515 0, /* flags */
1518 /* vim: set shiftwidth=4 tabstop=8: */