sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ return NULL;
+}
+
static char *game_text_format(game_state *state)
{
return NULL;
new_game,
dup_game,
free_game,
+ FALSE, solve_game,
NULL, game_text_format,
new_ui,
free_ui,
int *tiles;
int gap_pos;
int completed;
+ int just_used_solve; /* used to suppress undo animation */
+ int used_solve; /* used to suppress completion flash */
int movecount;
};
return ret;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
assert(state->tiles[state->gap_pos] == 0);
state->completed = state->movecount = 0;
+ state->used_solve = state->just_used_solve = FALSE;
return state;
}
ret->gap_pos = state->gap_pos;
ret->completed = state->completed;
ret->movecount = state->movecount;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
return ret;
}
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret = dup_game(state);
+ int i;
+
+ /*
+ * Simply replace the grid with a solved one. For this game,
+ * this isn't a useful operation for actually telling the user
+ * what they should have done, but it is useful for
+ * conveniently being able to get hold of a clean state from
+ * which to practise manoeuvres.
+ */
+ for (i = 0; i < ret->n; i++)
+ ret->tiles[i] = (i+1) % ret->n;
+ ret->gap_pos = ret->n-1;
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = ret->movecount;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
char *ret, *p, buf[80];
up = C(from, ux, uy);
ret = dup_game(from);
+ ret->just_used_solve = FALSE; /* zero this in a hurry */
ret->gap_pos = C(from, dx, dy);
assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
if (oldstate)
state = oldstate;
- sprintf(statusbuf, "%sMoves: %d",
- (state->completed ? "COMPLETED! " : ""),
- (state->completed ? state->completed : state->movecount));
+ if (state->used_solve)
+ sprintf(statusbuf, "Moves since auto-solve: %d",
+ state->movecount - state->completed);
+ else
+ sprintf(statusbuf, "%sMoves: %d",
+ (state->completed ? "COMPLETED! " : ""),
+ (state->completed ? state->completed : state->movecount));
status_bar(fe, statusbuf);
}
static float game_anim_length(game_state *oldstate,
game_state *newstate, int dir)
{
- return ANIM_TIME;
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+ else
+ return ANIM_TIME;
}
static float game_flash_length(game_state *oldstate,
game_state *newstate, int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve)
return 2 * FLASH_FRAME;
else
return 0.0F;
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
}
}
+static void menu_solve_event(GtkMenuItem *menuitem, gpointer data)
+{
+ frontend *fe = (frontend *)data;
+ char *msg;
+
+ msg = midend_solve(fe->me);
+
+ if (msg)
+ error_box(fe->window, msg);
+}
+
static void menu_config_event(GtkMenuItem *menuitem, gpointer data)
{
frontend *fe = (frontend *)data;
GTK_SIGNAL_FUNC(menu_copy_event), fe);
gtk_widget_show(menuitem);
}
+ if (thegame.can_solve) {
+ add_menu_separator(GTK_CONTAINER(menu));
+ menuitem = gtk_menu_item_new_with_label("Solve");
+ gtk_container_add(GTK_CONTAINER(menu), menuitem);
+ gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
+ GTK_SIGNAL_FUNC(menu_solve_event), fe);
+ gtk_widget_show(menuitem);
+ }
add_menu_separator(GTK_CONTAINER(menu));
add_menu_item_with_key(fe, GTK_CONTAINER(menu), "Exit", 'q');
else
return NULL;
}
+
+char *midend_solve(midend_data *me)
+{
+ game_state *s;
+ char *msg;
+
+ if (!me->ourgame->can_solve)
+ return "This game does not support the Solve operation";
+
+ if (me->statepos < 1)
+ return "No game set up to solve"; /* _shouldn't_ happen! */
+
+ msg = "Solve operation failed"; /* game _should_ overwrite on error */
+ s = me->ourgame->solve(me->states[0], me->aux_info, &msg);
+ if (!s)
+ return msg;
+
+ /*
+ * Now enter the solved state as the next move.~|~
+ */
+ midend_stop_anim(me);
+ while (me->nstates > me->statepos)
+ me->ourgame->free_game(me->states[--me->nstates]);
+ ensure(me);
+ me->states[me->nstates] = s;
+ me->statepos = ++me->nstates;
+ me->anim_time = 0.0;
+ midend_finish_move(me);
+ midend_redraw(me);
+ activate_timer(me->frontend);
+ return NULL;
+}
float barrier_probability;
};
+struct solved_game_state {
+ int width, height;
+ int refcount;
+ unsigned char *tiles;
+};
+
struct game_state {
int width, height, cx, cy, wrapping, completed, last_rotate_dir;
+ int used_solve, just_used_solve;
unsigned char *tiles;
unsigned char *barriers;
+ struct solved_game_state *solution;
};
#define OFFSET(x2,y2,x1,y1,dir,state) \
return dupstr(buf);
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
state->cy = state->height / 2;
state->wrapping = params->wrapping;
state->last_rotate_dir = 0;
- state->completed = FALSE;
+ state->completed = state->used_solve = state->just_used_solve = FALSE;
state->tiles = snewn(state->width * state->height, unsigned char);
memset(state->tiles, 0, state->width * state->height);
state->barriers = snewn(state->width * state->height, unsigned char);
}
}
+ /*
+ * Save the unshuffled grid. We do this using a separate
+ * reference-counted structure since it's a large chunk of
+ * memory which we don't want to have to replicate in every
+ * game state while playing.
+ */
+ {
+ struct solved_game_state *solution;
+
+ solution = snew(struct solved_game_state);
+ solution->width = state->width;
+ solution->height = state->height;
+ solution->refcount = 1;
+ solution->tiles = snewn(state->width * state->height, unsigned char);
+ memcpy(solution->tiles, state->tiles, state->width * state->height);
+
+ state->solution = solution;
+ }
+
/*
* Now shuffle the grid.
*/
ret->cy = state->cy;
ret->wrapping = state->wrapping;
ret->completed = state->completed;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
ret->last_rotate_dir = state->last_rotate_dir;
ret->tiles = snewn(state->width * state->height, unsigned char);
memcpy(ret->tiles, state->tiles, state->width * state->height);
ret->barriers = snewn(state->width * state->height, unsigned char);
memcpy(ret->barriers, state->barriers, state->width * state->height);
+ ret->solution = state->solution;
+ if (ret->solution)
+ ret->solution->refcount++;
return ret;
}
static void free_game(game_state *state)
{
+ if (state->solution && --state->solution->refcount <= 0) {
+ sfree(state->solution->tiles);
+ sfree(state->solution);
+ }
sfree(state->tiles);
sfree(state->barriers);
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret;
+
+ if (!state->solution) {
+ /*
+ * 2005-05-02: This shouldn't happen, at the time of
+ * writing, because Net is incapable of receiving a puzzle
+ * description from outside. If in future it becomes so,
+ * then we will have puzzles for which we don't know the
+ * solution.
+ */
+ *error = "Solution not known for this puzzle";
+ return NULL;
+ }
+
+ assert(state->solution->width == state->width);
+ assert(state->solution->height == state->height);
+ ret = dup_game(state);
+ memcpy(ret->tiles, state->solution->tiles, ret->width * ret->height);
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = TRUE;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
return NULL;
if (button == MIDDLE_BUTTON) {
ret = dup_game(state);
+ ret->just_used_solve = FALSE;
tile(ret, tx, ty) ^= LOCKED;
ret->last_rotate_dir = 0;
return ret;
* turns anticlockwise; right button turns clockwise.
*/
ret = dup_game(state);
+ ret->just_used_solve = FALSE;
orig = tile(ret, tx, ty);
if (button == LEFT_BUTTON) {
tile(ret, tx, ty) = A(orig);
*/
int jx, jy;
ret = dup_game(state);
+ ret->just_used_solve = FALSE;
for (jy = 0; jy < ret->height; jy++) {
for (jx = 0; jx < ret->width; jx++) {
if (!(tile(ret, jx, jy) & LOCKED)) {
a++;
sprintf(statusbuf, "%sActive: %d/%d",
- (state->completed ? "COMPLETED! " : ""), a, n);
+ (state->used_solve ? "Auto-solved. " :
+ state->completed ? "COMPLETED! " : ""), a, n);
status_bar(fe, statusbuf);
}
{
int x, y, last_rotate_dir;
+ /*
+ * Don't animate an auto-solve move.
+ */
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+
/*
* Don't animate if last_rotate_dir is zero.
*/
* If the game has just been completed, we display a completion
* flash.
*/
- if (!oldstate->completed && newstate->completed) {
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve) {
int size;
size = 0;
if (size < newstate->cx+1)
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
FALSE, game_text_format,
new_ui,
free_ui,
float barrier_probability;
};
+struct solved_game_state {
+ int width, height;
+ int refcount;
+ unsigned char *tiles;
+};
+
struct game_state {
int width, height, cx, cy, wrapping, completed;
+ int used_solve, just_used_solve;
int move_count;
/* position (row or col number, starting at 0) of last move. */
unsigned char *tiles;
unsigned char *barriers;
+ struct solved_game_state *solution;
};
#define OFFSET(x2,y2,x1,y1,dir,state) \
return dupstr(buf);
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
state->cy = state->height / 2;
state->wrapping = params->wrapping;
state->completed = 0;
+ state->used_solve = state->just_used_solve = FALSE;
state->move_count = 0;
state->last_move_row = -1;
state->last_move_col = -1;
}
}
+ /*
+ * Save the unshuffled grid. We do this using a separate
+ * reference-counted structure since it's a large chunk of
+ * memory which we don't want to have to replicate in every
+ * game state while playing.
+ */
+ {
+ struct solved_game_state *solution;
+
+ solution = snew(struct solved_game_state);
+ solution->width = state->width;
+ solution->height = state->height;
+ solution->refcount = 1;
+ solution->tiles = snewn(state->width * state->height, unsigned char);
+ memcpy(solution->tiles, state->tiles, state->width * state->height);
+
+ state->solution = solution;
+ }
+
/*
* Now shuffle the grid.
* FIXME - this simply does a set of random moves to shuffle the pieces.
ret->cy = state->cy;
ret->wrapping = state->wrapping;
ret->completed = state->completed;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
ret->move_count = state->move_count;
ret->last_move_row = state->last_move_row;
ret->last_move_col = state->last_move_col;
memcpy(ret->tiles, state->tiles, state->width * state->height);
ret->barriers = snewn(state->width * state->height, unsigned char);
memcpy(ret->barriers, state->barriers, state->width * state->height);
+ ret->solution = state->solution;
+ if (ret->solution)
+ ret->solution->refcount++;
return ret;
}
static void free_game(game_state *state)
{
+ if (state->solution && --state->solution->refcount <= 0) {
+ sfree(state->solution->tiles);
+ sfree(state->solution);
+ }
sfree(state->tiles);
sfree(state->barriers);
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret;
+
+ if (!state->solution) {
+ /*
+ * 2005-05-02: This shouldn't happen, at the time of
+ * writing, because Net is incapable of receiving a puzzle
+ * description from outside. If in future it becomes so,
+ * then we will have puzzles for which we don't know the
+ * solution.
+ */
+ *error = "Solution not known for this puzzle";
+ return NULL;
+ }
+
+ assert(state->solution->width == state->width);
+ assert(state->solution->height == state->height);
+ ret = dup_game(state);
+ memcpy(ret->tiles, state->solution->tiles, ret->width * ret->height);
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = ret->move_count;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
return NULL;
}
ret = dup_game(state);
+ ret->just_used_solve = FALSE;
if (dx == 0) slide_col(ret, dy, cx);
else slide_row(ret, dx, cy);
if (active[i])
a++;
- sprintf(statusbuf, "%sMoves: %d Active: %d/%d",
- (state->completed ? "COMPLETED! " : ""),
- (state->completed ? state->completed : state->move_count),
- a, n);
+ if (state->used_solve)
+ sprintf(statusbuf, "Moves since auto-solve: %d",
+ state->move_count - state->completed);
+ else
+ sprintf(statusbuf, "%sMoves: %d",
+ (state->completed ? "COMPLETED! " : ""),
+ (state->completed ? state->completed : state->move_count));
+
+ sprintf(statusbuf + strlen(statusbuf), " Active: %d/%d", a, n);
status_bar(fe, statusbuf);
}
static float game_anim_length(game_state *oldstate,
game_state *newstate, int dir)
{
+ /*
+ * Don't animate an auto-solve move.
+ */
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+
return ANIM_TIME;
}
* If the game has just been completed, we display a completion
* flash.
*/
- if (!oldstate->completed && newstate->completed) {
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve) {
int size;
size = 0;
if (size < newstate->cx+1)
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
FALSE, game_text_format,
new_ui,
free_ui,
return dupstr("FIXME");
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ return NULL;
+}
+
static char *game_text_format(game_state *state)
{
return NULL;
new_game,
dup_game,
free_game,
+ FALSE, solve_game,
FALSE, game_text_format,
new_ui,
free_ui,
NSBeep();
}
+- (void)solveGame:(id)sender
+{
+ char *msg;
+ NSAlert *alert;
+
+ msg = midend_solve(me);
+
+ if (msg) {
+ alert = [[[NSAlert alloc] init] autorelease];
+ [alert addButtonWithTitle:@"Bah"];
+ [alert setInformativeText:[NSString stringWithCString:msg]];
+ [alert beginSheetModalForWindow:self modalDelegate:nil
+ didEndSelector:nil contextInfo:nil];
+ }
+}
+
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
if ([item action] == @selector(copy:))
return (ourgame->can_format_as_text ? YES : NO);
+ else if ([item action] == @selector(solveGame:))
+ return (ourgame->can_solve ? YES : NO);
else
return [super validateMenuItem:item];
}
item = newitem(menu, "Cut", "x", NULL, @selector(cut:));
item = newitem(menu, "Copy", "c", NULL, @selector(copy:));
item = newitem(menu, "Paste", "v", NULL, @selector(paste:));
+ [menu addItem:[NSMenuItem separatorItem]];
+ item = newitem(menu, "Solve", "S-s", NULL, @selector(solveGame:));
menu = newsubmenu([NSApp mainMenu], "Type");
typemenu = menu;
/*
* pattern.c: the pattern-reconstruction game known as `nonograms'.
- *
- * TODO before checkin:
- *
- * - make some sort of stab at number-of-numbers judgment
*/
#include <stdio.h>
unsigned char *grid;
int rowsize;
int *rowdata, *rowlen;
- int completed;
+ int completed, cheated;
};
#define FLASH_TIME 0.13F
return seed;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
state->rowdata = snewn(state->rowsize * (state->w + state->h), int);
state->rowlen = snewn(state->w + state->h, int);
- state->completed = FALSE;
+ state->completed = state->cheated = FALSE;
for (i = 0; i < params->w + params->h; i++) {
state->rowlen[i] = 0;
(ret->w + ret->h) * sizeof(int));
ret->completed = state->completed;
+ ret->cheated = state->cheated;
return ret;
}
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret;
+
+ /*
+ * I could have stored the grid I invented in the game_aux_info
+ * and extracted it here where available, but it seems easier
+ * just to run my internal solver in all cases.
+ */
+
+ ret = dup_game(state);
+ ret->completed = ret->cheated = TRUE;
+
+ {
+ int w = state->w, h = state->h, i, j, done_any, max;
+ unsigned char *matrix, *workspace;
+ int *rowdata;
+
+ matrix = snewn(w*h, unsigned char);
+ max = max(w, h);
+ workspace = snewn(max*3, unsigned char);
+ rowdata = snewn(max+1, int);
+
+ memset(matrix, 0, w*h);
+
+ do {
+ done_any = 0;
+ for (i=0; i<h; i++) {
+ memcpy(rowdata, state->rowdata + state->rowsize*(w+i),
+ max*sizeof(int));
+ rowdata[state->rowlen[w+i]] = 0;
+ done_any |= do_row(workspace, workspace+max, workspace+2*max,
+ matrix+i*w, w, 1, rowdata);
+ }
+ for (i=0; i<w; i++) {
+ memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int));
+ rowdata[state->rowlen[i]] = 0;
+ done_any |= do_row(workspace, workspace+max, workspace+2*max,
+ matrix+i, h, w, rowdata);
+ }
+ } while (done_any);
+
+ for (i = 0; i < h; i++) {
+ for (j = 0; j < w; j++) {
+ int c = (matrix[i*w+j] == BLOCK ? GRID_FULL :
+ matrix[i*w+j] == DOT ? GRID_EMPTY : GRID_UNKNOWN);
+ ret->grid[i*w+j] = c;
+ if (c == GRID_UNKNOWN)
+ ret->completed = FALSE;
+ }
+ }
+
+ if (!ret->completed) {
+ free_game(ret);
+ *error = "Solving algorithm cannot complete this puzzle";
+ return NULL;
+ }
+ }
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
return NULL;
static float game_flash_length(game_state *oldstate,
game_state *newstate, int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->cheated && !newstate->cheated)
return FLASH_TIME;
return 0.0F;
}
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
FALSE, game_text_format,
new_ui,
free_ui,
web message board if you're discussing the game with someone else.
(Not all games support this feature.)
+\dt \ii\e{Solve}
+
+\dd Transforms the puzzle instantly into its solved state. For some
+games (Cube) this feature is not supported at all because it is of
+no particular use. For other games (such as Pattern), the solved
+state can be used to give you information, if you can't see how a
+solution can exist at all or you want to know where you made a
+mistake. For still other games (such as Sixteen), automatic solution
+tells you nothing about how to \e{get} to the solution, but it does
+provide a useful way to get there quickly so that you can experiment
+with set-piece moves and transformations.
+
+\lcont{
+
+Some games (such as Solo) are capable of solving a game ID you have
+typed in from elsewhere. Other games (such as Rectangles) cannot
+solve a game ID they didn't invent themself, but when they did
+invent the game ID they know what the solution is already. Still
+other games (Pattern) can solve \e{some} external game IDs, but only
+if they aren't too difficult.
+
+}
+
\dt \I{exit}\ii\e{Quit} (\q{Q}, Ctrl+\q{Q})
\dd Closes the application entirely.
char *midend_set_config(midend_data *me, int which, config_item *cfg);
char *midend_game_id(midend_data *me, char *id, int def_seed);
char *midend_text_format(midend_data *me);
+char *midend_solve(midend_data *me);
/*
* malloc.c
game_state *(*new_game)(game_params *params, char *seed);
game_state *(*dup_game)(game_state *state);
void (*free_game)(game_state *state);
+ int can_solve;
+ game_state *(*solve)(game_state *state, game_aux_info *aux, char **error);
int can_format_as_text;
char *(*text_format)(game_state *state);
game_ui *(*new_ui)(game_state *state);
int *grid; /* contains the numbers */
unsigned char *vedge; /* (w+1) x h */
unsigned char *hedge; /* w x (h+1) */
- int completed;
+ int completed, cheated;
};
static game_params *default_params(void)
}
#endif
+struct game_aux_info {
+ int w, h;
+ unsigned char *vedge; /* (w+1) x h */
+ unsigned char *hedge; /* w x (h+1) */
+};
+
static char *new_game_seed(game_params *params, random_state *rs,
game_aux_info **aux)
{
#endif
}
+ /*
+ * Store the rectangle data in the game_aux_info.
+ */
+ {
+ game_aux_info *ai = snew(game_aux_info);
+
+ ai->w = params->w;
+ ai->h = params->h;
+ ai->vedge = snewn(ai->w * ai->h, unsigned char);
+ ai->hedge = snewn(ai->w * ai->h, unsigned char);
+
+ for (y = 0; y < params->h; y++)
+ for (x = 1; x < params->w; x++) {
+ vedge(ai, x, y) =
+ index(params, grid, x, y) != index(params, grid, x-1, y);
+ }
+ for (y = 1; y < params->h; y++)
+ for (x = 0; x < params->w; x++) {
+ hedge(ai, x, y) =
+ index(params, grid, x, y) != index(params, grid, x, y-1);
+ }
+
+ *aux = ai;
+ }
+
/*
* Place numbers.
*/
return seed;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *ai)
{
- assert(!"Shouldn't happen");
+ sfree(ai->vedge);
+ sfree(ai->hedge);
+ sfree(ai);
}
static char *validate_seed(game_params *params, char *seed)
state->grid = snewn(area, int);
state->vedge = snewn(area, unsigned char);
state->hedge = snewn(area, unsigned char);
- state->completed = FALSE;
+ state->completed = state->cheated = FALSE;
i = 0;
while (*seed) {
ret->grid = snewn(state->w * state->h, int);
ret->completed = state->completed;
+ ret->cheated = state->cheated;
memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *ai,
+ char **error)
+{
+ game_state *ret;
+
+ if (!ai) {
+ *error = "Solution not known for this puzzle";
+ return NULL;
+ }
+
+ assert(state->w == ai->w);
+ assert(state->h == ai->h);
+
+ ret = dup_game(state);
+ memcpy(ret->vedge, ai->vedge, ai->w * ai->h * sizeof(unsigned char));
+ memcpy(ret->hedge, ai->hedge, ai->w * ai->h * sizeof(unsigned char));
+ ret->cheated = TRUE;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
char *ret, *p, buf[80];
static float game_flash_length(game_state *oldstate,
game_state *newstate, int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->cheated && !newstate->cheated)
return FLASH_TIME;
return 0.0F;
}
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
int w, h, n;
int *tiles;
int completed;
+ int just_used_solve; /* used to suppress undo animation */
+ int used_solve; /* used to suppress completion flash */
int movecount;
int last_movement_sense;
};
return ret;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
assert(!*p);
state->completed = state->movecount = 0;
+ state->used_solve = state->just_used_solve = FALSE;
state->last_movement_sense = 0;
return state;
memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
ret->completed = state->completed;
ret->movecount = state->movecount;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
ret->last_movement_sense = state->last_movement_sense;
return ret;
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret = dup_game(state);
+ int i;
+
+ /*
+ * Simply replace the grid with a solved one. For this game,
+ * this isn't a useful operation for actually telling the user
+ * what they should have done, but it is useful for
+ * conveniently being able to get hold of a clean state from
+ * which to practise manoeuvres.
+ */
+ for (i = 0; i < ret->n; i++)
+ ret->tiles[i] = i+1;
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = ret->movecount;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
char *ret, *p, buf[80];
}
ret = dup_game(from);
+ ret->just_used_solve = FALSE; /* zero this in a hurry */
do {
cx += dx;
if (oldstate)
state = oldstate;
- sprintf(statusbuf, "%sMoves: %d",
- (state->completed ? "COMPLETED! " : ""),
- (state->completed ? state->completed : state->movecount));
+ if (state->used_solve)
+ sprintf(statusbuf, "Moves since auto-solve: %d",
+ state->movecount - state->completed);
+ else
+ sprintf(statusbuf, "%sMoves: %d",
+ (state->completed ? "COMPLETED! " : ""),
+ (state->completed ? state->completed : state->movecount));
status_bar(fe, statusbuf);
}
static float game_anim_length(game_state *oldstate,
game_state *newstate, int dir)
{
- return ANIM_TIME;
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+ else
+ return ANIM_TIME;
}
static float game_flash_length(game_state *oldstate,
game_state *newstate, int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve)
return 2 * FLASH_FRAME;
else
return 0.0F;
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
int c, r;
digit *grid;
unsigned char *immutable; /* marks which digits are clues */
- int completed;
+ int completed, cheated;
};
static game_params *default_params(void)
return seed;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
state->immutable = snewn(area, unsigned char);
memset(state->immutable, FALSE, area);
- state->completed = FALSE;
+ state->completed = state->cheated = FALSE;
i = 0;
while (*seed) {
memcpy(ret->immutable, state->immutable, area);
ret->completed = state->completed;
+ ret->cheated = state->cheated;
return ret;
}
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret;
+ int c = state->c, r = state->r;
+ int rsolve_ret;
+
+ /*
+ * I could have stored the grid I invented in the game_aux_info
+ * and extracted it here where available, but it seems easier
+ * just to run my internal solver in all cases.
+ */
+
+ ret = dup_game(state);
+ ret->completed = ret->cheated = TRUE;
+
+ rsolve_ret = rsolve(c, r, ret->grid, NULL, 2);
+
+ if (rsolve_ret != 1) {
+ free_game(ret);
+ if (rsolve_ret == 0)
+ *error = "No solution exists for this puzzle";
+ else
+ *error = "Multiple solutions exist for this puzzle";
+ return NULL;
+ }
+
+ return ret;
+}
+
static char *grid_text_format(int c, int r, digit *grid)
{
int cr = c*r;
static float game_flash_length(game_state *oldstate, game_state *newstate,
int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->cheated && !newstate->cheated)
return FLASH_TIME;
return 0.0F;
}
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
int orientable;
int *grid;
int completed;
+ int just_used_solve; /* used to suppress undo animation */
+ int used_solve; /* used to suppress completion flash */
int movecount;
int lastx, lasty, lastr; /* coordinates of last rotation */
};
return ret;
}
-void game_free_aux_info(game_aux_info *aux)
+static void game_free_aux_info(game_aux_info *aux)
{
assert(!"Shouldn't happen");
}
state->n = n;
state->orientable = params->orientable;
state->completed = 0;
+ state->used_solve = state->just_used_solve = FALSE;
state->movecount = 0;
state->lastx = state->lasty = state->lastr = -1;
ret->lastx = state->lastx;
ret->lasty = state->lasty;
ret->lastr = state->lastr;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
ret->grid = snewn(ret->w * ret->h, int);
memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
sfree(state);
}
+static int compare_int(const void *av, const void *bv)
+{
+ const int *a = (const int *)av;
+ const int *b = (const int *)bv;
+ if (*a < *b)
+ return -1;
+ else if (*a > *b)
+ return +1;
+ else
+ return 0;
+}
+
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret = dup_game(state);
+
+ /*
+ * Simply replace the grid with a solved one. For this game,
+ * this isn't a useful operation for actually telling the user
+ * what they should have done, but it is useful for
+ * conveniently being able to get hold of a clean state from
+ * which to practise manoeuvres.
+ */
+ qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = ret->movecount;
+
+ return ret;
+}
+
static char *game_text_format(game_state *state)
{
char *ret, *p, buf[80];
* This is a valid move. Make it.
*/
ret = dup_game(from);
+ ret->just_used_solve = FALSE; /* zero this in a hurry */
ret->movecount++;
dir = (button == LEFT_BUTTON ? 1 : -1);
do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
static float game_anim_length(game_state *oldstate, game_state *newstate,
int dir)
{
- return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+ else
+ return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
}
static float game_flash_length(game_state *oldstate, game_state *newstate,
int dir)
{
- if (!oldstate->completed && newstate->completed)
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve)
return 2 * FLASH_FRAME;
else
return 0.0F;
if (oldstate)
state = oldstate;
- sprintf(statusbuf, "%sMoves: %d",
- (state->completed ? "COMPLETED! " : ""),
- (state->completed ? state->completed : state->movecount));
+ if (state->used_solve)
+ sprintf(statusbuf, "Moves since auto-solve: %d",
+ state->movecount - state->completed);
+ else
+ sprintf(statusbuf, "%sMoves: %d",
+ (state->completed ? "COMPLETED! " : ""),
+ (state->completed ? state->completed : state->movecount));
status_bar(fe, statusbuf);
}
new_game,
dup_game,
free_game,
+ TRUE, solve_game,
TRUE, game_text_format,
new_ui,
free_ui,
#define IDM_UNDO 0x0030
#define IDM_REDO 0x0040
#define IDM_COPY 0x0050
-#define IDM_QUIT 0x0060
-#define IDM_CONFIG 0x0070
-#define IDM_SEED 0x0080
-#define IDM_HELPC 0x0090
-#define IDM_GAMEHELP 0x00A0
+#define IDM_SOLVE 0x0060
+#define IDM_QUIT 0x0070
+#define IDM_CONFIG 0x0080
+#define IDM_SEED 0x0090
+#define IDM_HELPC 0x00A0
+#define IDM_GAMEHELP 0x00B0
#define IDM_PRESETS 0x0100
#define HELP_FILE_NAME "puzzles.hlp"
AppendMenu(menu, MF_SEPARATOR, 0, 0);
AppendMenu(menu, MF_ENABLED, IDM_COPY, "Copy");
}
+ if (thegame.can_solve) {
+ AppendMenu(menu, MF_SEPARATOR, 0, 0);
+ AppendMenu(menu, MF_ENABLED, IDM_SOLVE, "Solve");
+ }
AppendMenu(menu, MF_SEPARATOR, 0, 0);
AppendMenu(menu, MF_ENABLED, IDM_QUIT, "Exit");
if (fe->help_path) {
MessageBeep(MB_ICONWARNING);
}
break;
+ case IDM_SOLVE:
+ {
+ char *msg = midend_solve(fe->me);
+ if (msg)
+ MessageBox(hwnd, msg, "Unable to solve",
+ MB_ICONERROR | MB_OK);
+ }
+ break;
case IDM_QUIT:
if (!midend_process_key(fe->me, 0, 0, 'q'))
PostQuitMessage(0);