X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/28094c81f193d9c964f3334483027341f5250553..61072f7be9d1ab83c459ec1eabc0f9503b57fac6:/guess.c diff --git a/guess.c b/guess.c index d2bbd0a..3e5d120 100644 --- a/guess.c +++ b/guess.c @@ -15,7 +15,7 @@ enum { COL_BACKGROUND, - COL_HIGHLIGHT, COL_LOWLIGHT, COL_FRAME, COL_FLASH, COL_HOLD, + COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD, COL_EMPTY, /* must be COL_1 - 1 */ COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10, COL_CORRECTPLACE, COL_CORRECTCOLOUR, @@ -24,6 +24,7 @@ enum { struct game_params { int ncolours, npegs, nguesses; + int allow_blank, allow_multiple; }; #define FEEDBACK_CORRECTPLACE 1 @@ -53,6 +54,9 @@ static game_params *default_params(void) ret->npegs = 4; ret->nguesses = 10; + ret->allow_blank = 0; + ret->allow_multiple = 1; + return ret; } @@ -97,6 +101,22 @@ static void decode_params(game_params *params, char const *string) while (*p && isdigit((unsigned char)*p)) p++; break; + case 'b': + params->allow_blank = 1; + break; + + case 'B': + params->allow_blank = 0; + break; + + case 'm': + params->allow_multiple = 1; + break; + + case 'M': + params->allow_multiple = 0; + break; + default: ; } @@ -107,7 +127,9 @@ static char *encode_params(game_params *params, int full) { char data[256]; - sprintf(data, "c%dp%dg%d", params->ncolours, params->npegs, params->nguesses); + sprintf(data, "c%dp%dg%d%s%s", + params->ncolours, params->npegs, params->nguesses, + params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M"); return dupstr(data); } @@ -117,30 +139,40 @@ static config_item *game_configure(game_params *params) config_item *ret; char buf[80]; - ret = snewn(4, config_item); + ret = snewn(6, config_item); - ret[0].name = "No. of colours"; + ret[0].name = "Colours"; ret[0].type = C_STRING; sprintf(buf, "%d", params->ncolours); ret[0].sval = dupstr(buf); ret[0].ival = 0; - ret[1].name = "No. of pegs per row"; + ret[1].name = "Pegs per guess"; ret[1].type = C_STRING; sprintf(buf, "%d", params->npegs); ret[1].sval = dupstr(buf); ret[1].ival = 0; - ret[2].name = "No. of guesses"; + ret[2].name = "Guesses"; ret[2].type = C_STRING; sprintf(buf, "%d", params->nguesses); ret[2].sval = dupstr(buf); ret[2].ival = 0; - ret[3].name = NULL; - ret[3].type = C_END; + ret[3].name = "Allow blanks"; + ret[3].type = C_BOOLEAN; ret[3].sval = NULL; - ret[3].ival = 0; + ret[3].ival = params->allow_blank; + + ret[4].name = "Allow duplicates"; + ret[4].type = C_BOOLEAN; + ret[4].sval = NULL; + ret[4].ival = params->allow_multiple; + + ret[5].name = NULL; + ret[5].type = C_END; + ret[5].sval = NULL; + ret[5].ival = 0; return ret; } @@ -153,6 +185,9 @@ static game_params *custom_params(config_item *cfg) ret->npegs = atoi(cfg[1].sval); ret->nguesses = atoi(cfg[2].sval); + ret->allow_blank = cfg[3].ival; + ret->allow_multiple = cfg[4].ival; + return ret; } @@ -166,6 +201,8 @@ static char *validate_params(game_params *params) return "Too many colours"; if (params->nguesses < 1) return "Must have at least one guess"; + if (!params->allow_multiple && params->ncolours < params->npegs) + return "Disallowing multiple colours requires at least as many colours as pegs"; return NULL; } @@ -213,14 +250,21 @@ static char *new_game_desc(game_params *params, random_state *rs, { unsigned char *bmp = snewn(params->npegs, unsigned char); char *ret; - int i; - - for (i = 0; i < params->npegs; i++) - bmp[i] = (unsigned char)(random_upto(rs, params->ncolours)+1); + int i, c; + pegrow colcount = new_pegrow(params->ncolours); + + for (i = 0; i < params->npegs; i++) { +newcol: + c = random_upto(rs, params->ncolours); + if (!params->allow_multiple && colcount->pegs[c]) goto newcol; + colcount->pegs[c]++; + bmp[i] = (unsigned char)(c+1); + } obfuscate_bitmap(bmp, params->npegs*8, FALSE); ret = bin2hex(bmp, params->npegs); sfree(bmp); + free_pegrow(colcount); return ret; } @@ -231,10 +275,24 @@ static void game_free_aux_info(game_aux_info *aux) static char *validate_desc(game_params *params, char *desc) { - /* desc is just an (obfuscated) bitmap of the solution; all we - * care is that it's the correct length. */ + unsigned char *bmp; + int i; + + /* desc is just an (obfuscated) bitmap of the solution; check that + * it's the correct length and (when unobfuscated) contains only + * sensible colours. */ if (strlen(desc) != params->npegs * 2) return "Game description is wrong length"; + bmp = hex2bin(desc, params->npegs); + obfuscate_bitmap(bmp, params->npegs*8, TRUE); + for (i = 0; i < params->npegs; i++) { + if (bmp[i] < 1 || bmp[i] > params->ncolours) { + sfree(bmp); + return "Game description is corrupted"; + } + } + sfree(bmp); + return NULL; } @@ -308,6 +366,7 @@ struct game_ui { int display_cur, markable; int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */ + int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */ }; static game_ui *new_ui(game_state *state) @@ -317,6 +376,7 @@ static game_ui *new_ui(game_state *state) ui->curr_pegs = new_pegrow(state->params.npegs); ui->holds = snewn(state->params.npegs, int); memset(ui->holds, 0, sizeof(int)*state->params.npegs); + ui->drag_opeg = -1; return ui; } @@ -395,18 +455,36 @@ struct game_drawstate { int drag_col, blit_ox, blit_oy; }; -static void set_peg(game_ui *ui, int peg, int col) +static int is_markable(game_params *params, pegrow pegs) { - int i; + int i, nset = 0, nrequired, ret = 0; + pegrow colcount = new_pegrow(params->ncolours); - ui->curr_pegs->pegs[peg] = col; + nrequired = params->allow_blank ? 1 : params->npegs; - /* set to 'markable' if all of our pegs are filled. */ - for (i = 0; i < ui->curr_pegs->npegs; i++) { - if (ui->curr_pegs->pegs[i] == 0) return; + for (i = 0; i < params->npegs; i++) { + if (pegs->pegs[i] > 0) { + colcount->pegs[pegs->pegs[i]]++; + nset++; + } } - debug(("UI is markable.")); - ui->markable = 1; + if (nset < nrequired) goto done; + + if (!params->allow_multiple) { + for (i = 0; i < params->ncolours; i++) { + if (colcount->pegs[i] > 1) goto done; + } + } + ret = 1; +done: + free_pegrow(colcount); + return ret; +} + +static void set_peg(game_params *params, game_ui *ui, int peg, int col) +{ + ui->curr_pegs->pegs[peg] = col; + ui->markable = is_markable(params, ui->curr_pegs); } static int mark_pegs(pegrow guess, pegrow solution, int ncols) @@ -474,11 +552,9 @@ static game_state *mark_move(game_state *from, game_ui *ui) } if (to->solved) ui->holds[i] = 0; } - if (ncleared) { - ui->markable = 0; - if (ui->peg_cur == to->solution->npegs) - ui->peg_cur--; - } + ui->markable = is_markable(&from->params, ui->curr_pegs); + if (!ui->markable && ui->peg_cur == to->solution->npegs) + ui->peg_cur--; return to; } @@ -488,6 +564,8 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, { int over_col = 0; /* one-indexed */ int over_guess = -1; /* zero-indexed */ + int over_past_guess_y = -1; /* zero-indexed */ + int over_past_guess_x = -1; /* zero-indexed */ int over_hint = 0; /* zero or one */ game_state *ret = NULL; @@ -506,9 +584,14 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, } else { over_hint = 1; } + } else if (x >= guess_ox && + y >= GUESS_OY && y < guess_oy) { + over_past_guess_y = (y - GUESS_OY) / PEGOFF; + over_past_guess_x = (x - guess_ox) / PEGOFF; } - debug(("make_move: over_col %d, over_guess %d, over_hint %d", - over_col, over_guess, over_hint)); + debug(("make_move: over_col %d, over_guess %d, over_hint %d," + " over_past_guess (%d,%d)", over_col, over_guess, over_hint, + over_past_guess_x, over_past_guess_y)); assert(ds->blit_peg); @@ -516,13 +599,23 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, if (button == LEFT_BUTTON) { if (over_col > 0) { ui->drag_col = over_col; + ui->drag_opeg = -1; debug(("Start dragging from colours")); } else if (over_guess > -1) { int col = ui->curr_pegs->pegs[over_guess]; if (col) { ui->drag_col = col; + ui->drag_opeg = over_guess; debug(("Start dragging from a guess")); } + } else if (over_past_guess_y > -1) { + int col = + from->guesses[over_past_guess_y]->pegs[over_past_guess_x]; + if (col) { + ui->drag_col = col; + ui->drag_opeg = -1; + debug(("Start dragging from a past guess")); + } } if (ui->drag_col) { ui->drag_x = x; @@ -540,9 +633,16 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, if (over_guess > -1) { debug(("Dropping colour %d onto guess peg %d", ui->drag_col, over_guess)); - set_peg(ui, over_guess, ui->drag_col); + set_peg(&from->params, ui, over_guess, ui->drag_col); + } else { + if (ui->drag_opeg > -1) { + debug(("Removing colour %d from peg %d", + ui->drag_col, ui->drag_opeg)); + set_peg(&from->params, ui, ui->drag_opeg, 0); + } } ui->drag_col = 0; + ui->drag_opeg = -1; debug(("Stop dragging.")); ret = from; } else if (button == RIGHT_BUTTON) { @@ -581,7 +681,7 @@ static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, if (ui->peg_cur == from->params.npegs) { ret = mark_move(from, ui); } else { - set_peg(ui, ui->peg_cur, ui->colour_cur+1); + set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1); ret = from; } } @@ -666,57 +766,63 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours) frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); - ret[COL_1 * 3 + 0] = 0.0F; + /* red */ + ret[COL_1 * 3 + 0] = 1.0F; ret[COL_1 * 3 + 1] = 0.0F; - ret[COL_1 * 3 + 2] = 1.0F; + ret[COL_1 * 3 + 2] = 0.0F; - ret[COL_2 * 3 + 0] = 0.0F; - ret[COL_2 * 3 + 1] = 0.5F; + /* yellow (toned down a bit due to pale grey background) */ + ret[COL_2 * 3 + 0] = 0.7F; + ret[COL_2 * 3 + 1] = 0.7F; ret[COL_2 * 3 + 2] = 0.0F; - ret[COL_3 * 3 + 0] = 1.0F; - ret[COL_3 * 3 + 1] = 0.0F; + /* green (also toned down) */ + ret[COL_3 * 3 + 0] = 0.0F; + ret[COL_3 * 3 + 1] = 0.5F; ret[COL_3 * 3 + 2] = 0.0F; - ret[COL_4 * 3 + 0] = 1.0F; - ret[COL_4 * 3 + 1] = 1.0F; - ret[COL_4 * 3 + 2] = 0.0F; + /* blue */ + ret[COL_4 * 3 + 0] = 0.0F; + ret[COL_4 * 3 + 1] = 0.0F; + ret[COL_4 * 3 + 2] = 1.0F; + /* orange */ ret[COL_5 * 3 + 0] = 1.0F; - ret[COL_5 * 3 + 1] = 0.0F; - ret[COL_5 * 3 + 2] = 1.0F; - - ret[COL_6 * 3 + 0] = 0.0F; - ret[COL_6 * 3 + 1] = 1.0F; - ret[COL_6 * 3 + 2] = 1.0F; - - ret[COL_7 * 3 + 0] = 0.5F; - ret[COL_7 * 3 + 1] = 0.5F; - ret[COL_7 * 3 + 2] = 1.0F; - - ret[COL_8 * 3 + 0] = 0.5F; - ret[COL_8 * 3 + 1] = 1.0F; - ret[COL_8 * 3 + 2] = 0.5F; - - ret[COL_9 * 3 + 0] = 1.0F; - ret[COL_9 * 3 + 1] = 0.5F; + ret[COL_5 * 3 + 1] = 0.5F; + ret[COL_5 * 3 + 2] = 0.0F; + + /* purple */ + ret[COL_6 * 3 + 0] = 0.5F; + ret[COL_6 * 3 + 1] = 0.0F; + ret[COL_6 * 3 + 2] = 0.7F; + + /* brown */ + ret[COL_7 * 3 + 0] = 0.4F; + ret[COL_7 * 3 + 1] = 0.2F; + ret[COL_7 * 3 + 2] = 0.2F; + + /* light blue */ + ret[COL_8 * 3 + 0] = 0.4F; + ret[COL_8 * 3 + 1] = 0.7F; + ret[COL_8 * 3 + 2] = 1.0F; + + /* light green */ + ret[COL_9 * 3 + 0] = 0.5F; + ret[COL_9 * 3 + 1] = 0.8F; ret[COL_9 * 3 + 2] = 0.5F; + /* pink */ ret[COL_10 * 3 + 0] = 1.0F; - ret[COL_10 * 3 + 1] = 1.0F; + ret[COL_10 * 3 + 1] = 0.6F; ret[COL_10 * 3 + 2] = 1.0F; ret[COL_FRAME * 3 + 0] = 0.0F; ret[COL_FRAME * 3 + 1] = 0.0F; ret[COL_FRAME * 3 + 2] = 0.0F; - ret[COL_HIGHLIGHT * 3 + 0] = 1.0F; - ret[COL_HIGHLIGHT * 3 + 1] = 1.0F; - ret[COL_HIGHLIGHT * 3 + 2] = 1.0F; - - ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0; - ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0; - ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0; + ret[COL_CURSOR * 3 + 0] = 0.0F; + ret[COL_CURSOR * 3 + 1] = 0.0F; + ret[COL_CURSOR * 3 + 2] = 0.0F; ret[COL_FLASH * 3 + 0] = 0.5F; ret[COL_FLASH * 3 + 1] = 1.0F; @@ -730,7 +836,7 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours) ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0; ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0; - ret[COL_CORRECTPLACE*3 + 0] = 1.0F; + ret[COL_CORRECTPLACE*3 + 0] = 0.0F; ret[COL_CORRECTPLACE*3 + 1] = 0.0F; ret[COL_CORRECTPLACE*3 + 2] = 0.0F; @@ -784,8 +890,19 @@ static void game_free_drawstate(game_drawstate *ds) sfree(ds); } -static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy, int col) +static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy, + int moving, int col) { + /* + * Some platforms antialias circles, which means we shouldn't + * overwrite a circle of one colour with a circle of another + * colour without erasing the background first. However, if the + * peg is the one being dragged, we don't erase the background + * because we _want_ it to alpha-blend nicely into whatever's + * behind it. + */ + if (!moving) + draw_rect(fe, cx-1, cy-1, PEGSZ+2, PEGSZ+2, COL_BACKGROUND); if (PEGRAD > 0) { draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col); draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col); @@ -814,7 +931,7 @@ static void guess_redraw(frontend *fe, game_drawstate *ds, int guess, for (i = 0; i < dest->npegs; i++) { scol = src ? src->pegs[i] : 0; if ((dest->pegs[i] != scol) || force) - draw_peg(fe, ds, rowx + PEGOFF * i, rowy, scol); + draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol); dest->pegs[i] = scol; } } @@ -842,6 +959,8 @@ static void hint_redraw(frontend *fe, game_drawstate *ds, int guess, rowx += HINTOFF * (i - hintlen); rowy += HINTOFF; } + /* erase background for antialiasing platforms */ + draw_rect(fe, rowx-1, rowy-1, HINTSZ+2, HINTSZ+2, COL_BACKGROUND); if (HINTRAD > 0) { draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col); draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col); @@ -887,19 +1006,11 @@ static void cur_redraw(frontend *fe, game_drawstate *ds, int x, int y, int erase) { int cgap = ds->gapsz / 2; - int x1, y1, x2, y2, hi, lo; - - x1 = x-cgap; x2 = x+PEGSZ+cgap; - y1 = y-cgap; y2 = y+PEGSZ+cgap; - hi = erase ? COL_BACKGROUND : COL_HIGHLIGHT; - lo = erase ? COL_BACKGROUND : COL_LOWLIGHT; - draw_line(fe, x1, y1, x2, y1, hi); - draw_line(fe, x2, y1, x2, y2, lo); - draw_line(fe, x2, y2, x1, y2, lo); - draw_line(fe, x1, y2, x1, y1, hi); + draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+cgap, 0, + erase ? COL_BACKGROUND : COL_CURSOR); - draw_update(fe, x1, y1, x2, y2); + draw_update(fe, x-cgap, y-cgap, x+PEGSZ+cgap, y+PEGSZ+cgap); } static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, @@ -926,7 +1037,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, /* draw the colours */ for (i = 0; i < state->params.ncolours; i++) { if (ds->colours->pegs[i] != i+1) { - draw_peg(fe, ds, COL_X(i), COL_Y(i), i+1); + draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1); ds->colours->pegs[i] = i+1; } } @@ -1009,7 +1120,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, int oy = ui->drag_y - (PEGSZ/2); debug(("Saving to blitter at (%d,%d)", ox, oy)); blitter_save(fe, ds->blit_peg, ox, oy); - draw_peg(fe, ds, ox, oy, ui->drag_col); + draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col); ds->blit_ox = ox; ds->blit_oy = oy; } @@ -1045,7 +1156,7 @@ static int game_timing_state(game_state *state) #endif const struct game thegame = { - "Guess", NULL, + "Guess", "games.guess", default_params, game_fetch_preset, decode_params,