diff options
| author | Simon Tatham <anakin@pobox.com> | 2004-05-19 11:57:09 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2004-05-19 11:57:09 +0000 |
| commit | 350683b25371ec6a7548b2e83b2be15eb629815f (patch) | |
| tree | 99e1103fd09c1e6ed73df97d33c0ae8c101564f0 /cube.c | |
| parent | ba076fbdb26149d39e076e161ace330149e71b30 (diff) | |
| download | puzzles-350683b25371ec6a7548b2e83b2be15eb629815f.zip puzzles-350683b25371ec6a7548b2e83b2be15eb629815f.tar.gz puzzles-350683b25371ec6a7548b2e83b2be15eb629815f.tar.bz2 puzzles-350683b25371ec6a7548b2e83b2be15eb629815f.tar.xz | |
Introduce routines in each game module to encode a set of game
parameters as a string, and decode it again. This is used in
midend.c to prepend the game parameters to the game seed, so that
copying out of the Specific box is sufficient to completely specify
the game you were playing.
Throughout development of these games I have referred to `seed'
internally, and `game ID' externally. Now there's a measurable
difference between them! :-)
[originally from svn r4231]
Diffstat (limited to 'cube.c')
| -rw-r--r-- | cube.c | 42 |
1 files changed, 37 insertions, 5 deletions
@@ -6,6 +6,7 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <ctype.h> #include <math.h> #include "puzzles.h" @@ -276,6 +277,37 @@ game_params *dup_params(game_params *params) return ret; } +game_params *decode_params(char const *string) +{ + game_params *ret = default_params(); + + switch (*string) { + case 't': ret->solid = TETRAHEDRON; string++; break; + case 'c': ret->solid = CUBE; string++; break; + case 'o': ret->solid = OCTAHEDRON; string++; break; + case 'i': ret->solid = ICOSAHEDRON; string++; break; + default: break; + } + ret->d1 = ret->d2 = atoi(string); + while (*string && isdigit(*string)) string++; + if (*string == 'x') { + string++; + ret->d2 = atoi(string); + } + + return ret; +} + +char *encode_params(game_params *params) +{ + char data[256]; + + assert(params->solid >= 0 && params->solid < 4); + sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2); + + return dupstr(data); +} + static void enum_grid_squares(game_params *params, void (*callback)(void *, struct grid_square *), void *ctx) @@ -653,7 +685,7 @@ char *new_game_seed(game_params *params, random_state *rs) /* * Choose a non-blue square for the polyhedron. */ - sprintf(p, ":%d", data.gridptrs[0][random_upto(rs, m)]); + sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]); sfree(data.gridptrs[0]); sfree(flags); @@ -824,13 +856,13 @@ char *validate_seed(game_params *params, char *seed) /* NB if seed[j]=='\0' that will also be caught here, so we're safe */ } - if (seed[i] != ':') - return "Expected ':' after hex digits"; + if (seed[i] != ',') + return "Expected ',' after hex digits"; i++; do { if (seed[i] < '0' || seed[i] > '9') - return "Expected decimal integer after ':'"; + return "Expected decimal integer after ','"; i++; } while (seed[i]); @@ -883,7 +915,7 @@ game_state *new_game(game_params *params, char *seed) j = 8; } - if (*p == ':') + if (*p == ',') p++; state->current = atoi(p); |