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_past_guess_y
= -1; /* zero-indexed */
492 int over_past_guess_x
= -1; /* zero-indexed */
493 int over_hint
= 0; /* zero or one */
494 game_state
*ret
= NULL
;
496 int guess_ox
= GUESS_X(from
->next_go
, 0);
497 int guess_oy
= GUESS_Y(from
->next_go
, 0);
499 if (from
->solved
) return NULL
;
501 if (x
>= COL_OX
&& x
<= (COL_OX
+ COL_W
) &&
502 y
>= COL_OY
&& y
<= (COL_OY
+ COL_H
)) {
503 over_col
= ((y
- COL_OY
) / PEGOFF
) + 1;
504 } else if (x
>= guess_ox
&&
505 y
>= guess_oy
&& y
<= (guess_oy
+ GUESS_H
)) {
506 if (x
<= (guess_ox
+ GUESS_W
)) {
507 over_guess
= (x
- guess_ox
) / PEGOFF
;
511 } else if (x
>= guess_ox
&&
512 y
>= GUESS_OY
&& y
< guess_oy
) {
513 over_past_guess_y
= (y
- GUESS_OY
) / PEGOFF
;
514 over_past_guess_x
= (x
- guess_ox
) / PEGOFF
;
516 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
517 " over_past_guess %d", over_col
, over_guess
, over_hint
,
520 assert(ds
->blit_peg
);
523 if (button
== LEFT_BUTTON
) {
525 ui
->drag_col
= over_col
;
526 debug(("Start dragging from colours"));
527 } else if (over_guess
> -1) {
528 int col
= ui
->curr_pegs
->pegs
[over_guess
];
531 debug(("Start dragging from a guess"));
533 } else if (over_past_guess_y
> -1) {
535 from
->guesses
[over_past_guess_y
]->pegs
[over_past_guess_x
];
538 debug(("Start dragging from a past guess"));
544 debug(("Start dragging, col = %d, (%d,%d)",
545 ui
->drag_col
, ui
->drag_x
, ui
->drag_y
));
548 } else if (button
== LEFT_DRAG
&& ui
->drag_col
) {
551 debug(("Keep dragging, (%d,%d)", ui
->drag_x
, ui
->drag_y
));
553 } else if (button
== LEFT_RELEASE
&& ui
->drag_col
) {
554 if (over_guess
> -1) {
555 debug(("Dropping colour %d onto guess peg %d",
556 ui
->drag_col
, over_guess
));
557 set_peg(ui
, over_guess
, ui
->drag_col
);
560 debug(("Stop dragging."));
562 } else if (button
== RIGHT_BUTTON
) {
563 if (over_guess
> -1) {
564 /* we use ths feedback in the game_ui to signify
565 * 'carry this peg to the next guess as well'. */
566 ui
->holds
[over_guess
] = 1 - ui
->holds
[over_guess
];
569 } else if (button
== LEFT_RELEASE
&& over_hint
&& ui
->markable
) {
570 /* NB this won't trigger if on the end of a drag; that's on
571 * purpose, in case you drop by mistake... */
572 ret
= mark_move(from
, ui
);
576 if (button
== CURSOR_UP
|| button
== CURSOR_DOWN
) {
578 if (button
== CURSOR_DOWN
&& (ui
->colour_cur
+1) < from
->params
.ncolours
)
580 if (button
== CURSOR_UP
&& ui
->colour_cur
> 0)
583 } else if (button
== CURSOR_LEFT
|| button
== CURSOR_RIGHT
) {
584 int maxcur
= from
->params
.npegs
;
585 if (ui
->markable
) maxcur
++;
588 if (button
== CURSOR_RIGHT
&& (ui
->peg_cur
+1) < maxcur
)
590 if (button
== CURSOR_LEFT
&& ui
->peg_cur
> 0)
593 } else if (button
== CURSOR_SELECT
|| button
== ' ' || button
== '\r' ||
595 if (ui
->peg_cur
== from
->params
.npegs
) {
596 ret
= mark_move(from
, ui
);
598 set_peg(ui
, ui
->peg_cur
, ui
->colour_cur
+1);
605 /* ----------------------------------------------------------------------
609 #define PEG_PREFER_SZ 32
611 /* next three are multipliers for pegsz. It will look much nicer if
612 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
614 #define PEG_HINT 0.35
618 static void game_size(game_params
*params
, game_drawstate
*ds
,
619 int *x
, int *y
, int expand
)
621 double hmul
, vmul_c
, vmul_g
, vmul
, szx
, szy
;
622 int sz
, colh
, guessh
;
624 hmul
= BORDER
* 2.0 + /* border */
625 1.0 * 2.0 + /* vertical colour bar */
626 1.0 * params
->npegs
+ /* guess pegs */
627 PEG_GAP
* params
->npegs
+ /* guess gaps */
628 PEG_HINT
* ds
->hintw
+ /* hint pegs */
629 PEG_GAP
* (ds
->hintw
- 1); /* hint gaps */
631 vmul_c
= BORDER
* 2.0 + /* border */
632 1.0 * params
->ncolours
+ /* colour pegs */
633 PEG_GAP
* (params
->ncolours
- 1); /* colour gaps */
635 vmul_g
= BORDER
* 2.0 + /* border */
636 1.0 * (params
->nguesses
+ 1) + /* guesses plus solution */
637 PEG_GAP
* (params
->nguesses
+ 1); /* gaps plus gap above soln */
639 vmul
= max(vmul_c
, vmul_g
);
643 sz
= max(min((int)szx
, (int)szy
), 1);
647 ds
->pegsz
= min(sz
, PEG_PREFER_SZ
);
649 ds
->hintsz
= (int)((double)ds
->pegsz
* PEG_HINT
);
650 ds
->gapsz
= (int)((double)ds
->pegsz
* PEG_GAP
);
651 ds
->border
= (int)((double)ds
->pegsz
* BORDER
);
653 ds
->pegrad
= (ds
->pegsz
-1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
654 ds
->hintrad
= (ds
->hintsz
-1)/2;
656 *x
= (int)ceil((double)ds
->pegsz
* hmul
);
657 *y
= (int)ceil((double)ds
->pegsz
* vmul
);
658 ds
->w
= *x
; ds
->h
= *y
;
660 colh
= ((ds
->pegsz
+ ds
->gapsz
) * params
->ncolours
) - ds
->gapsz
;
661 guessh
= ((ds
->pegsz
+ ds
->gapsz
) * params
->nguesses
); /* guesses */
662 guessh
+= ds
->gapsz
+ ds
->pegsz
; /* solution */
664 ds
->colx
= ds
->border
;
665 ds
->coly
= (*y
- colh
) / 2;
667 ds
->guessx
= ds
->solnx
= ds
->border
+ ds
->pegsz
* 2; /* border + colours */
668 ds
->guessy
= (*y
- guessh
) / 2;
669 ds
->solny
= ds
->guessy
+ ((ds
->pegsz
+ ds
->gapsz
) * params
->nguesses
) + ds
->gapsz
;
672 if (ds
->blit_peg
) blitter_free(ds
->blit_peg
);
673 ds
->blit_peg
= blitter_new(ds
->pegsz
, ds
->pegsz
);
677 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
679 float *ret
= snewn(3 * NCOLOURS
, float);
681 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
684 ret
[COL_1
* 3 + 0] = 1.0F
;
685 ret
[COL_1
* 3 + 1] = 0.0F
;
686 ret
[COL_1
* 3 + 2] = 0.0F
;
688 /* yellow (toned down a bit due to pale grey background) */
689 ret
[COL_2
* 3 + 0] = 0.7F
;
690 ret
[COL_2
* 3 + 1] = 0.7F
;
691 ret
[COL_2
* 3 + 2] = 0.0F
;
693 /* green (also toned down) */
694 ret
[COL_3
* 3 + 0] = 0.0F
;
695 ret
[COL_3
* 3 + 1] = 0.5F
;
696 ret
[COL_3
* 3 + 2] = 0.0F
;
699 ret
[COL_4
* 3 + 0] = 0.0F
;
700 ret
[COL_4
* 3 + 1] = 0.0F
;
701 ret
[COL_4
* 3 + 2] = 1.0F
;
704 ret
[COL_5
* 3 + 0] = 1.0F
;
705 ret
[COL_5
* 3 + 1] = 0.5F
;
706 ret
[COL_5
* 3 + 2] = 0.0F
;
709 ret
[COL_6
* 3 + 0] = 0.5F
;
710 ret
[COL_6
* 3 + 1] = 0.0F
;
711 ret
[COL_6
* 3 + 2] = 0.7F
;
714 ret
[COL_7
* 3 + 0] = 0.4F
;
715 ret
[COL_7
* 3 + 1] = 0.2F
;
716 ret
[COL_7
* 3 + 2] = 0.2F
;
719 ret
[COL_8
* 3 + 0] = 0.4F
;
720 ret
[COL_8
* 3 + 1] = 0.7F
;
721 ret
[COL_8
* 3 + 2] = 1.0F
;
724 ret
[COL_9
* 3 + 0] = 0.5F
;
725 ret
[COL_9
* 3 + 1] = 0.8F
;
726 ret
[COL_9
* 3 + 2] = 0.5F
;
729 ret
[COL_10
* 3 + 0] = 1.0F
;
730 ret
[COL_10
* 3 + 1] = 0.6F
;
731 ret
[COL_10
* 3 + 2] = 1.0F
;
733 ret
[COL_FRAME
* 3 + 0] = 0.0F
;
734 ret
[COL_FRAME
* 3 + 1] = 0.0F
;
735 ret
[COL_FRAME
* 3 + 2] = 0.0F
;
737 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
738 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
739 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
741 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 2.0 / 3.0;
742 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 2.0 / 3.0;
743 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 2.0 / 3.0;
745 ret
[COL_FLASH
* 3 + 0] = 0.5F
;
746 ret
[COL_FLASH
* 3 + 1] = 1.0F
;
747 ret
[COL_FLASH
* 3 + 2] = 1.0F
;
749 ret
[COL_HOLD
* 3 + 0] = 1.0F
;
750 ret
[COL_HOLD
* 3 + 1] = 0.5F
;
751 ret
[COL_HOLD
* 3 + 2] = 0.5F
;
753 ret
[COL_EMPTY
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 2.0 / 3.0;
754 ret
[COL_EMPTY
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 2.0 / 3.0;
755 ret
[COL_EMPTY
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 2.0 / 3.0;
757 ret
[COL_CORRECTPLACE
*3 + 0] = 0.0F
;
758 ret
[COL_CORRECTPLACE
*3 + 1] = 0.0F
;
759 ret
[COL_CORRECTPLACE
*3 + 2] = 0.0F
;
761 ret
[COL_CORRECTCOLOUR
*3 + 0] = 1.0F
;
762 ret
[COL_CORRECTCOLOUR
*3 + 1] = 1.0F
;
763 ret
[COL_CORRECTCOLOUR
*3 + 2] = 1.0F
;
765 *ncolours
= NCOLOURS
;
769 static game_drawstate
*game_new_drawstate(game_state
*state
)
771 struct game_drawstate
*ds
= snew(struct game_drawstate
);
774 memset(ds
, 0, sizeof(struct game_drawstate
));
776 ds
->guesses
= snewn(state
->params
.nguesses
, pegrow
);
777 ds
->nguesses
= state
->params
.nguesses
;
778 for (i
= 0; i
< state
->params
.nguesses
; i
++) {
779 ds
->guesses
[i
] = new_pegrow(state
->params
.npegs
);
780 invalidate_pegrow(ds
->guesses
[i
]);
782 ds
->solution
= new_pegrow(state
->params
.npegs
);
783 invalidate_pegrow(ds
->solution
);
784 ds
->colours
= new_pegrow(state
->params
.ncolours
);
785 invalidate_pegrow(ds
->colours
);
787 ds
->hintw
= (state
->params
.npegs
+1)/2; /* must round up */
789 ds
->holds
= snewn(state
->params
.npegs
, int);
790 memset(ds
->holds
, 0, state
->params
.npegs
*sizeof(int));
797 static void game_free_drawstate(game_drawstate
*ds
)
801 if (ds
->blit_peg
) blitter_free(ds
->blit_peg
);
802 free_pegrow(ds
->colours
);
803 free_pegrow(ds
->solution
);
804 for (i
= 0; i
< ds
->nguesses
; i
++)
805 free_pegrow(ds
->guesses
[i
]);
811 static void draw_peg(frontend
*fe
, game_drawstate
*ds
, int cx
, int cy
,
815 * Some platforms antialias circles, which means we shouldn't
816 * overwrite a circle of one colour with a circle of another
817 * colour without erasing the background first. However, if the
818 * peg is the one being dragged, we don't erase the background
819 * because we _want_ it to alpha-blend nicely into whatever's
823 draw_rect(fe
, cx
-1, cy
-1, PEGSZ
+2, PEGSZ
+2, COL_BACKGROUND
);
825 draw_circle(fe
, cx
+PEGRAD
, cy
+PEGRAD
, PEGRAD
, 1, COL_EMPTY
+ col
);
826 draw_circle(fe
, cx
+PEGRAD
, cy
+PEGRAD
, PEGRAD
, 0, COL_EMPTY
+ col
);
828 draw_rect(fe
, cx
, cy
, PEGSZ
, PEGSZ
, COL_EMPTY
+ col
);
829 draw_update(fe
, cx
, cy
, PEGSZ
, PEGSZ
);
832 static void guess_redraw(frontend
*fe
, game_drawstate
*ds
, int guess
,
833 pegrow src
, int force
)
836 int rowx
, rowy
, i
, scol
;
843 dest
= ds
->guesses
[guess
];
844 rowx
= GUESS_X(guess
,0);
845 rowy
= GUESS_Y(guess
,0);
847 if (src
) assert(src
->npegs
== dest
->npegs
);
849 for (i
= 0; i
< dest
->npegs
; i
++) {
850 scol
= src ? src
->pegs
[i
] : 0;
851 if ((dest
->pegs
[i
] != scol
) || force
)
852 draw_peg(fe
, ds
, rowx
+ PEGOFF
* i
, rowy
, FALSE
, scol
);
853 dest
->pegs
[i
] = scol
;
857 static void hint_redraw(frontend
*fe
, game_drawstate
*ds
, int guess
,
858 pegrow src
, int force
, int emptycol
)
860 pegrow dest
= ds
->guesses
[guess
];
861 int rowx
, rowy
, i
, scol
, col
, hintlen
;
863 if (src
) assert(src
->npegs
== dest
->npegs
);
865 hintlen
= (dest
->npegs
+ 1)/2;
867 for (i
= 0; i
< dest
->npegs
; i
++) {
868 scol
= src ? src
->feedback
[i
] : 0;
869 col
= (scol
== FEEDBACK_CORRECTPLACE
) ? COL_CORRECTPLACE
:
870 (scol
== FEEDBACK_CORRECTCOLOUR
) ? COL_CORRECTCOLOUR
: emptycol
;
871 if ((scol
!= dest
->feedback
[i
]) || force
) {
872 rowx
= HINT_X(guess
);
873 rowy
= HINT_Y(guess
);
877 rowx
+= HINTOFF
* (i
- hintlen
);
880 /* erase background for antialiasing platforms */
881 draw_rect(fe
, rowx
-1, rowy
-1, HINTSZ
+2, HINTSZ
+2, COL_BACKGROUND
);
883 draw_circle(fe
, rowx
+HINTRAD
, rowy
+HINTRAD
, HINTRAD
, 1, col
);
884 draw_circle(fe
, rowx
+HINTRAD
, rowy
+HINTRAD
, HINTRAD
, 0, col
);
886 draw_rect(fe
, rowx
, rowy
, HINTSZ
, HINTSZ
, col
);
888 draw_update(fe
, rowx
, rowy
, HINTSZ
, HINTSZ
);
890 dest
->feedback
[i
] = scol
;
894 static void hold_redraw(frontend
*fe
, game_drawstate
*ds
, int guess
, int *src
, int force
)
896 int shold
, col
, ox
, oy
, i
;
898 if (guess
>= ds
->nguesses
)
901 for (i
= 0; i
< ds
->solution
->npegs
; i
++) {
902 shold
= src ? src
[i
] : 0;
903 col
= shold ? COL_HOLD
: COL_BACKGROUND
;
904 if ((shold
!= ds
->holds
[i
]) || force
) {
905 ox
= GUESS_X(guess
, i
);
906 oy
= GUESS_Y(guess
, i
) + PEGSZ
+ ds
->gapsz
/2;
908 draw_rect(fe
, ox
, oy
, PEGSZ
, 2, col
);
909 draw_update(fe
, ox
, oy
, PEGSZ
, 2);
911 if (src
) ds
->holds
[i
] = shold
;
915 static void currmove_redraw(frontend
*fe
, game_drawstate
*ds
, int guess
, int col
)
917 int ox
= GUESS_X(guess
, 0), oy
= GUESS_Y(guess
, 0), off
= PEGSZ
/4;
919 draw_rect(fe
, ox
-off
-1, oy
, 2, PEGSZ
, col
);
920 draw_update(fe
, ox
-off
-1, oy
, 2, PEGSZ
);
923 static void cur_redraw(frontend
*fe
, game_drawstate
*ds
,
924 int x
, int y
, int erase
)
926 int cgap
= ds
->gapsz
/ 2;
927 int x1
, y1
, x2
, y2
, hi
, lo
;
929 x1
= x
-cgap
; x2
= x
+PEGSZ
+cgap
;
930 y1
= y
-cgap
; y2
= y
+PEGSZ
+cgap
;
931 hi
= erase ? COL_BACKGROUND
: COL_HIGHLIGHT
;
932 lo
= erase ? COL_BACKGROUND
: COL_LOWLIGHT
;
934 draw_line(fe
, x1
, y1
, x2
, y1
, hi
);
935 draw_line(fe
, x2
, y1
, x2
, y2
, lo
);
936 draw_line(fe
, x2
, y2
, x1
, y2
, lo
);
937 draw_line(fe
, x1
, y2
, x1
, y1
, hi
);
939 draw_update(fe
, x1
, y1
, x2
, y2
);
942 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
943 game_state
*state
, int dir
, game_ui
*ui
,
944 float animtime
, float flashtime
)
946 int i
, cur_erase
= 0, cur_draw
= 0, new_move
, last_go
;
948 new_move
= (state
->next_go
!= ds
->next_go
) || !ds
->started
;
949 last_go
= (state
->next_go
== state
->params
.nguesses
-1);
952 draw_rect(fe
, 0, 0, ds
->w
, ds
->h
, COL_BACKGROUND
);
953 draw_rect(fe
, SOLN_OX
, SOLN_OY
- ds
->gapsz
- 1, SOLN_W
, 2, COL_FRAME
);
954 draw_update(fe
, 0, 0, ds
->w
, ds
->h
);
957 if (ds
->drag_col
!= 0) {
958 debug(("Loading from blitter."));
959 blitter_load(fe
, ds
->blit_peg
, ds
->blit_ox
, ds
->blit_oy
);
960 draw_update(fe
, ds
->blit_ox
, ds
->blit_oy
, PEGSZ
, PEGSZ
);
963 /* draw the colours */
964 for (i
= 0; i
< state
->params
.ncolours
; i
++) {
965 if (ds
->colours
->pegs
[i
] != i
+1) {
966 draw_peg(fe
, ds
, COL_X(i
), COL_Y(i
), FALSE
, i
+1);
967 ds
->colours
->pegs
[i
] = i
+1;
971 /* draw the guesses (so far) and the hints */
972 for (i
= 0; i
< state
->params
.nguesses
; i
++) {
973 if (state
->next_go
> i
|| state
->solved
) {
974 /* this info is stored in the game_state already */
975 guess_redraw(fe
, ds
, i
, state
->guesses
[i
], 0);
976 hint_redraw(fe
, ds
, i
, state
->guesses
[i
],
977 i
== (state
->next_go
-1) ?
1 : 0, COL_EMPTY
);
978 } else if (state
->next_go
== i
) {
979 /* this is the one we're on; the (incomplete) guess is
980 * stored in the game_ui. */
981 guess_redraw(fe
, ds
, i
, ui
->curr_pegs
, 0);
982 hint_redraw(fe
, ds
, i
, NULL
, 1, ui
->markable ? COL_FLASH
: COL_EMPTY
);
984 /* we've not got here yet; it's blank. */
985 guess_redraw(fe
, ds
, i
, NULL
, 0);
986 hint_redraw(fe
, ds
, i
, NULL
, 0, COL_EMPTY
);
990 /* draw the 'hold' markers */
992 hold_redraw(fe
, ds
, state
->next_go
, NULL
, 1);
993 } else if (new_move
) {
994 hold_redraw(fe
, ds
, ds
->next_go
, NULL
, 1);
995 hold_redraw(fe
, ds
, state
->next_go
, last_go ? NULL
: ui
->holds
, 1);
997 hold_redraw(fe
, ds
, state
->next_go
, last_go ? NULL
: ui
->holds
, 0);
999 /* draw the 'current move' and 'able to mark' sign. */
1001 currmove_redraw(fe
, ds
, ds
->next_go
, COL_BACKGROUND
);
1003 currmove_redraw(fe
, ds
, state
->next_go
, COL_HOLD
);
1005 /* draw the solution (or the big rectangle) */
1006 if ((state
->solved
!= ds
->solved
) || !ds
->started
) {
1007 draw_rect(fe
, SOLN_OX
, SOLN_OY
, SOLN_W
, SOLN_H
,
1008 state
->solved ? COL_BACKGROUND
: COL_EMPTY
);
1009 draw_update(fe
, SOLN_OX
, SOLN_OY
, SOLN_W
, SOLN_H
);
1012 guess_redraw(fe
, ds
, -1, state
->solution
, !ds
->solved
);
1013 ds
->solved
= state
->solved
;
1015 if (ui
->display_cur
&& !ds
->display_cur
)
1017 else if (!ui
->display_cur
&& ds
->display_cur
)
1019 else if (ui
->display_cur
) {
1020 if ((state
->next_go
!= ds
->next_go
) ||
1021 (ui
->peg_cur
!= ds
->peg_cur
) ||
1022 (ui
->colour_cur
!= ds
->colour_cur
)) {
1028 cur_redraw(fe
, ds
, COL_X(ds
->colour_cur
), COL_Y(ds
->colour_cur
), 1);
1030 GUESS_X(ds
->next_go
, ds
->peg_cur
), GUESS_Y(ds
->next_go
, ds
->peg_cur
), 1);
1033 cur_redraw(fe
, ds
, COL_X(ui
->colour_cur
), COL_Y(ui
->colour_cur
), 0);
1035 GUESS_X(state
->next_go
, ui
->peg_cur
), GUESS_Y(state
->next_go
, ui
->peg_cur
), 0);
1037 ds
->display_cur
= ui
->display_cur
;
1038 ds
->peg_cur
= ui
->peg_cur
;
1039 ds
->colour_cur
= ui
->colour_cur
;
1040 ds
->next_go
= state
->next_go
;
1042 /* if ui->drag_col != 0, save the screen to the blitter,
1043 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1044 if (ui
->drag_col
!= 0) {
1045 int ox
= ui
->drag_x
- (PEGSZ
/2);
1046 int oy
= ui
->drag_y
- (PEGSZ
/2);
1047 debug(("Saving to blitter at (%d,%d)", ox
, oy
));
1048 blitter_save(fe
, ds
->blit_peg
, ox
, oy
);
1049 draw_peg(fe
, ds
, ox
, oy
, TRUE
, ui
->drag_col
);
1051 ds
->blit_ox
= ox
; ds
->blit_oy
= oy
;
1053 ds
->drag_col
= ui
->drag_col
;
1058 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1059 int dir
, game_ui
*ui
)
1064 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1065 int dir
, game_ui
*ui
)
1070 static int game_wants_statusbar(void)
1075 static int game_timing_state(game_state
*state
)
1081 #define thegame guess
1084 const struct game thegame
= {
1092 TRUE
, game_configure
, custom_params
,
1101 FALSE
, game_text_format
,
1109 game_free_drawstate
,
1113 game_wants_statusbar
,
1114 FALSE
, game_timing_state
,
1115 0, /* mouse_priorities */
1118 /* vim: set shiftwidth=4 tabstop=8: */