mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
"Hold" function in Guess was completely broken.
Fix it, add holds to the undo history (by analogy with Net), and save the current holds in saved games. Also fix a couple of unrelated minor issues with string encoding. [originally from svn r6590]
This commit is contained in:
73
guess.c
73
guess.c
@ -39,6 +39,7 @@ typedef struct pegrow {
|
|||||||
struct game_state {
|
struct game_state {
|
||||||
game_params params;
|
game_params params;
|
||||||
pegrow *guesses; /* length params->nguesses */
|
pegrow *guesses; /* length params->nguesses */
|
||||||
|
int *holds;
|
||||||
pegrow solution;
|
pegrow solution;
|
||||||
int next_go; /* from 0 to nguesses-1;
|
int next_go; /* from 0 to nguesses-1;
|
||||||
if next_go == nguesses then they've lost. */
|
if next_go == nguesses then they've lost. */
|
||||||
@ -319,6 +320,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
|
|||||||
state->guesses = snewn(params->nguesses, pegrow);
|
state->guesses = snewn(params->nguesses, pegrow);
|
||||||
for (i = 0; i < params->nguesses; i++)
|
for (i = 0; i < params->nguesses; i++)
|
||||||
state->guesses[i] = new_pegrow(params->npegs);
|
state->guesses[i] = new_pegrow(params->npegs);
|
||||||
|
state->holds = snewn(params->npegs, int);
|
||||||
state->solution = new_pegrow(params->npegs);
|
state->solution = new_pegrow(params->npegs);
|
||||||
|
|
||||||
bmp = hex2bin(desc, params->npegs);
|
bmp = hex2bin(desc, params->npegs);
|
||||||
@ -327,6 +329,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
|
|||||||
state->solution->pegs[i] = (int)bmp[i];
|
state->solution->pegs[i] = (int)bmp[i];
|
||||||
sfree(bmp);
|
sfree(bmp);
|
||||||
|
|
||||||
|
memset(state->holds, 0, sizeof(int) * params->npegs);
|
||||||
state->next_go = state->solved = 0;
|
state->next_go = state->solved = 0;
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
@ -342,6 +345,8 @@ static game_state *dup_game(game_state *state)
|
|||||||
ret->guesses = snewn(state->params.nguesses, pegrow);
|
ret->guesses = snewn(state->params.nguesses, pegrow);
|
||||||
for (i = 0; i < state->params.nguesses; i++)
|
for (i = 0; i < state->params.nguesses; i++)
|
||||||
ret->guesses[i] = dup_pegrow(state->guesses[i]);
|
ret->guesses[i] = dup_pegrow(state->guesses[i]);
|
||||||
|
ret->holds = snewn(state->params.npegs, int);
|
||||||
|
memcpy(ret->holds, state->holds, sizeof(int) * state->params.npegs);
|
||||||
ret->solution = dup_pegrow(state->solution);
|
ret->solution = dup_pegrow(state->solution);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -354,6 +359,7 @@ static void free_game(game_state *state)
|
|||||||
free_pegrow(state->solution);
|
free_pegrow(state->solution);
|
||||||
for (i = 0; i < state->params.nguesses; i++)
|
for (i = 0; i < state->params.nguesses; i++)
|
||||||
free_pegrow(state->guesses[i]);
|
free_pegrow(state->guesses[i]);
|
||||||
|
sfree(state->holds);
|
||||||
sfree(state->guesses);
|
sfree(state->guesses);
|
||||||
|
|
||||||
sfree(state);
|
sfree(state);
|
||||||
@ -435,17 +441,19 @@ static char *encode_ui(game_ui *ui)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* For this game it's worth storing the contents of the current
|
* For this game it's worth storing the contents of the current
|
||||||
* guess.
|
* guess, and the current set of holds.
|
||||||
*/
|
*/
|
||||||
ret = snewn(40 * ui->curr_pegs->npegs, char);
|
ret = snewn(40 * ui->curr_pegs->npegs, char);
|
||||||
p = ret;
|
p = ret;
|
||||||
sep = "";
|
sep = "";
|
||||||
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
||||||
p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]);
|
p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
|
||||||
|
ui->holds[i] ? "_" : "");
|
||||||
sep = ",";
|
sep = ",";
|
||||||
}
|
}
|
||||||
|
*p++ = '\0';
|
||||||
assert(p - ret < 40 * ui->curr_pegs->npegs);
|
assert(p - ret < 40 * ui->curr_pegs->npegs);
|
||||||
return sresize(ret, p - ret + 1, char);
|
return sresize(ret, p - ret, char);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void decode_ui(game_ui *ui, char *encoding)
|
static void decode_ui(game_ui *ui, char *encoding)
|
||||||
@ -455,6 +463,12 @@ static void decode_ui(game_ui *ui, char *encoding)
|
|||||||
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
||||||
ui->curr_pegs->pegs[i] = atoi(p);
|
ui->curr_pegs->pegs[i] = atoi(p);
|
||||||
while (*p && isdigit((unsigned char)*p)) p++;
|
while (*p && isdigit((unsigned char)*p)) p++;
|
||||||
|
if (*p == '_') {
|
||||||
|
/* NB: old versions didn't store holds */
|
||||||
|
ui->holds[i] = 1;
|
||||||
|
p++;
|
||||||
|
} else
|
||||||
|
ui->holds[i] = 0;
|
||||||
if (*p == ',') p++;
|
if (*p == ',') p++;
|
||||||
}
|
}
|
||||||
ui->markable = is_markable(&ui->params, ui->curr_pegs);
|
ui->markable = is_markable(&ui->params, ui->curr_pegs);
|
||||||
@ -465,10 +479,24 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* just clear the row-in-progress when we have an undo/redo. */
|
/* Implement holds, clear other pegs.
|
||||||
for (i = 0; i < ui->curr_pegs->npegs; i++)
|
* This does something that is arguably the Right Thing even
|
||||||
ui->curr_pegs->pegs[i] = 0;
|
* for undo. */
|
||||||
ui->markable = FALSE;
|
for (i = 0; i < newstate->solution->npegs; i++) {
|
||||||
|
if (newstate->solved)
|
||||||
|
ui->holds[i] = 0;
|
||||||
|
else
|
||||||
|
ui->holds[i] = newstate->holds[i];
|
||||||
|
if (newstate->solved || (newstate->next_go == 0) || !ui->holds[i]) {
|
||||||
|
ui->curr_pegs->pegs[i] = 0;
|
||||||
|
} else
|
||||||
|
ui->curr_pegs->pegs[i] =
|
||||||
|
newstate->guesses[newstate->next_go-1]->pegs[i];
|
||||||
|
}
|
||||||
|
ui->markable = is_markable(&newstate->params, ui->curr_pegs);
|
||||||
|
/* Clean up cursor position */
|
||||||
|
if (!ui->markable && ui->peg_cur == newstate->solution->npegs)
|
||||||
|
ui->peg_cur--;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define PEGSZ (ds->pegsz)
|
#define PEGSZ (ds->pegsz)
|
||||||
@ -578,8 +606,7 @@ static int mark_pegs(pegrow guess, pegrow solution, int ncols)
|
|||||||
static char *encode_move(game_state *from, game_ui *ui)
|
static char *encode_move(game_state *from, game_ui *ui)
|
||||||
{
|
{
|
||||||
char *buf, *p, *sep;
|
char *buf, *p, *sep;
|
||||||
int len, i, solved;
|
int len, i;
|
||||||
pegrow tmppegs;
|
|
||||||
|
|
||||||
len = ui->curr_pegs->npegs * 20 + 2;
|
len = ui->curr_pegs->npegs * 20 + 2;
|
||||||
buf = snewn(len, char);
|
buf = snewn(len, char);
|
||||||
@ -587,28 +614,14 @@ static char *encode_move(game_state *from, game_ui *ui)
|
|||||||
*p++ = 'G';
|
*p++ = 'G';
|
||||||
sep = "";
|
sep = "";
|
||||||
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
for (i = 0; i < ui->curr_pegs->npegs; i++) {
|
||||||
p += sprintf(p, "%s%d", sep, ui->curr_pegs->pegs[i]);
|
p += sprintf(p, "%s%d%s", sep, ui->curr_pegs->pegs[i],
|
||||||
|
ui->holds[i] ? "_" : "");
|
||||||
sep = ",";
|
sep = ",";
|
||||||
}
|
}
|
||||||
*p++ = '\0';
|
*p++ = '\0';
|
||||||
assert(p - buf <= len);
|
assert(p - buf <= len);
|
||||||
buf = sresize(buf, len, char);
|
buf = sresize(buf, len, char);
|
||||||
|
|
||||||
tmppegs = dup_pegrow(ui->curr_pegs);
|
|
||||||
solved = mark_pegs(tmppegs, from->solution, from->params.ncolours);
|
|
||||||
solved = (solved == from->params.ncolours);
|
|
||||||
free_pegrow(tmppegs);
|
|
||||||
|
|
||||||
for (i = 0; i < from->solution->npegs; i++) {
|
|
||||||
if (!ui->holds[i] || solved) {
|
|
||||||
ui->curr_pegs->pegs[i] = 0;
|
|
||||||
}
|
|
||||||
if (solved) ui->holds[i] = 0;
|
|
||||||
}
|
|
||||||
ui->markable = is_markable(&from->params, ui->curr_pegs);
|
|
||||||
if (!ui->markable && ui->peg_cur == from->solution->npegs)
|
|
||||||
ui->peg_cur--;
|
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -775,6 +788,11 @@ static game_state *execute_move(game_state *from, char *move)
|
|||||||
}
|
}
|
||||||
ret->guesses[from->next_go]->pegs[i] = atoi(p);
|
ret->guesses[from->next_go]->pegs[i] = atoi(p);
|
||||||
while (*p && isdigit((unsigned char)*p)) p++;
|
while (*p && isdigit((unsigned char)*p)) p++;
|
||||||
|
if (*p == '_') {
|
||||||
|
ret->holds[i] = 1;
|
||||||
|
p++;
|
||||||
|
} else
|
||||||
|
ret->holds[i] = 0;
|
||||||
if (*p == ',') p++;
|
if (*p == ',') p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1191,8 +1209,9 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* draw the guesses (so far) and the hints */
|
/* draw the guesses (so far) and the hints
|
||||||
for (i = 0; i < state->params.nguesses; i++) {
|
* (in reverse order to avoid trampling holds) */
|
||||||
|
for (i = state->params.nguesses - 1; i >= 0; i--) {
|
||||||
if (state->next_go > i || state->solved) {
|
if (state->next_go > i || state->solved) {
|
||||||
/* this info is stored in the game_state already */
|
/* this info is stored in the game_state already */
|
||||||
guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0);
|
guess_redraw(dr, ds, i, state->guesses[i], NULL, -1, 0);
|
||||||
|
Reference in New Issue
Block a user