X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/1d228b10b0f6bbc1fffb5442d2ad934a5e6aaaed..1cdfcdbdd6bb3f14838be9ff1ef78699bffba9c1:/rect.c diff --git a/rect.c b/rect.c index 5ab296a..c0606dd 100644 --- a/rect.c +++ b/rect.c @@ -386,7 +386,8 @@ static void display_grid(game_params *params, int *grid, int *numbers, int all) } #endif -static char *new_game_seed(game_params *params, random_state *rs) +static char *new_game_seed(game_params *params, random_state *rs, + game_aux_info **aux) { int *grid, *numbers; struct rectlist *list; @@ -898,6 +899,11 @@ static char *new_game_seed(game_params *params, random_state *rs) return seed; } +void game_free_aux_info(game_aux_info *aux) +{ + assert(!"Shouldn't happen"); +} + static char *validate_seed(game_params *params, char *seed) { int area = params->w * params->h; @@ -997,6 +1003,99 @@ static void free_game(game_state *state) sfree(state); } +static char *game_text_format(game_state *state) +{ + char *ret, *p, buf[80]; + int i, x, y, col, maxlen; + + /* + * First determine the number of spaces required to display a + * number. We'll use at least two, because one looks a bit + * silly. + */ + col = 2; + for (i = 0; i < state->w * state->h; i++) { + x = sprintf(buf, "%d", state->grid[i]); + if (col < x) col = x; + } + + /* + * Now we know the exact total size of the grid we're going to + * produce: it's got 2*h+1 rows, each containing w lots of col, + * w+1 boundary characters and a trailing newline. + */ + maxlen = (2*state->h+1) * (state->w * (col+1) + 2); + + ret = snewn(maxlen, char); + p = ret; + + for (y = 0; y <= 2*state->h; y++) { + for (x = 0; x <= 2*state->w; x++) { + if (x & y & 1) { + /* + * Display a number. + */ + int v = grid(state, x/2, y/2); + if (v) + sprintf(buf, "%*d", col, v); + else + sprintf(buf, "%*s", col, ""); + memcpy(p, buf, col); + p += col; + } else if (x & 1) { + /* + * Display a horizontal edge or nothing. + */ + int h = (y==0 || y==2*state->h ? 1 : + HRANGE(state, x/2, y/2) && hedge(state, x/2, y/2)); + int i; + if (h) + h = '-'; + else + h = ' '; + for (i = 0; i < col; i++) + *p++ = h; + } else if (y & 1) { + /* + * Display a vertical edge or nothing. + */ + int v = (x==0 || x==2*state->w ? 1 : + VRANGE(state, x/2, y/2) && vedge(state, x/2, y/2)); + if (v) + *p++ = '|'; + else + *p++ = ' '; + } else { + /* + * Display a corner, or a vertical edge, or a + * horizontal edge, or nothing. + */ + int hl = (y==0 || y==2*state->h ? 1 : + HRANGE(state, (x-1)/2, y/2) && hedge(state, (x-1)/2, y/2)); + int hr = (y==0 || y==2*state->h ? 1 : + HRANGE(state, (x+1)/2, y/2) && hedge(state, (x+1)/2, y/2)); + int vu = (x==0 || x==2*state->w ? 1 : + VRANGE(state, x/2, (y-1)/2) && vedge(state, x/2, (y-1)/2)); + int vd = (x==0 || x==2*state->w ? 1 : + VRANGE(state, x/2, (y+1)/2) && vedge(state, x/2, (y+1)/2)); + if (!hl && !hr && !vu && !vd) + *p++ = ' '; + else if (hl && hr && !vu && !vd) + *p++ = '-'; + else if (!hl && !hr && vu && vd) + *p++ = '|'; + else + *p++ = '+'; + } + } + *p++ = '\n'; + } + + assert(p - ret == maxlen); + *p = '\0'; + return ret; +} + static unsigned char *get_correct(game_state *state) { unsigned char *ret; @@ -1610,10 +1709,12 @@ const struct game thegame = { TRUE, game_configure, custom_params, validate_params, new_game_seed, + game_free_aux_info, validate_seed, new_game, dup_game, free_game, + TRUE, game_text_format, new_ui, free_ui, make_move,