Prevent starting in a solved state in Fifteen & Flood

(cherry picked from Android port, commit
cb38abdc71780bd9b393b90514396c338306fa69)
This commit is contained in:
Chris Boyle
2016-12-21 20:01:25 +00:00
committed by Ben Harris
parent 0d43753ff2
commit f967bfa87b
2 changed files with 78 additions and 69 deletions

View File

@ -156,6 +156,14 @@ static int perm_parity(int *perm, int n)
return ret;
}
static int is_completed(int *tiles, int n) {
int p;
for (p = 0; p < n; p++)
if (tiles[p] != (p < n-1 ? p+1 : 0))
return 0;
return 1;
}
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, bool interactive)
{
@ -171,6 +179,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
tiles = snewn(n, int);
used = snewn(n, bool);
do {
for (i = 0; i < n; i++) {
tiles[i] = -1;
used[i] = false;
@ -183,7 +192,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
/*
* Place everything else except the last two tiles.
*/
for (x = 0, i = n-1; i > 2; i--) {
for (x = 0, i = n - 1; i > 2; i--) {
int k = random_upto(rs, i);
int j;
@ -217,7 +226,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
if (!used[i])
break;
p1 = i;
for (i = p1+1; i < n; i++)
for (i = p1 + 1; i < n; i++)
if (!used[i])
break;
p2 = i;
@ -246,6 +255,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
tiles[x2] = p1;
assert(perm_parity(tiles, n) == parity);
}
} while (is_completed(tiles, n));
/*
* Now construct the game description, by describing the tile
@ -786,11 +796,8 @@ static game_state *execute_move(const game_state *from, const char *move)
/*
* See if the game has been completed.
*/
if (!ret->completed) {
if (!ret->completed && is_completed(ret->tiles, ret->n)) {
ret->completed = ret->movecount;
for (p = 0; p < ret->n; p++)
if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
ret->completed = 0;
}
return ret;

View File

@ -552,8 +552,10 @@ static char *new_game_desc(const game_params *params, random_state *rs,
/*
* Invent a random grid.
*/
do {
for (i = 0; i < wh; i++)
scratch->grid[i] = random_upto(rs, params->colours);
} while (completed(w, h, scratch->grid));
/*
* Run the solver, and count how many moves it uses.