chiark / gitweb /
The Windows RNG turns out to only give about 16 bits at a time. This
[sgt-puzzles.git] / fifteen.c
index 338bdfb77480a95734d12603ff2d5428735d150a..3965844da94baf8909d90101d12a44cd84a2d7c5 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -11,6 +11,7 @@
 #include "puzzles.h"
 
 const char *const game_name = "Fifteen";
+const int game_can_configure = TRUE;
 
 #define TILE_SIZE 48
 #define BORDER    (TILE_SIZE / 2)
@@ -71,6 +72,51 @@ game_params *dup_params(game_params *params)
     return ret;
 }
 
+config_item *game_configure(game_params *params)
+{
+    config_item *ret;
+    char buf[80];
+
+    ret = snewn(3, config_item);
+
+    ret[0].name = "Width";
+    ret[0].type = C_STRING;
+    sprintf(buf, "%d", params->w);
+    ret[0].sval = dupstr(buf);
+    ret[0].ival = 0;
+
+    ret[1].name = "Height";
+    ret[1].type = C_STRING;
+    sprintf(buf, "%d", params->h);
+    ret[1].sval = dupstr(buf);
+    ret[1].ival = 0;
+
+    ret[2].name = NULL;
+    ret[2].type = C_END;
+    ret[2].sval = NULL;
+    ret[2].ival = 0;
+
+    return ret;
+}
+
+game_params *custom_params(config_item *cfg)
+{
+    game_params *ret = snew(game_params);
+
+    ret->w = atoi(cfg[0].sval);
+    ret->h = atoi(cfg[1].sval);
+
+    return ret;
+}
+
+char *validate_params(game_params *params)
+{
+    if (params->w < 2 && params->h < 2)
+       return "Width and height must both be at least two";
+
+    return NULL;
+}
+
 int perm_parity(int *perm, int n)
 {
     int i, j, ret;
@@ -85,7 +131,7 @@ int perm_parity(int *perm, int n)
     return ret;
 }
 
-char *new_game_seed(game_params *params)
+char *new_game_seed(game_params *params, random_state *rs)
 {
     int gap, n, i, x;
     int x1, x2, p1, p2, parity;
@@ -103,7 +149,7 @@ char *new_game_seed(game_params *params)
         used[i] = FALSE;
     }
 
-    gap = rand_upto(n);
+    gap = random_upto(rs, n);
     tiles[gap] = 0;
     used[0] = TRUE;
 
@@ -111,7 +157,7 @@ char *new_game_seed(game_params *params)
      * Place everything else except the last two tiles.
      */
     for (x = 0, i = n-1; i > 2; i--) {
-        int k = rand_upto(i);
+        int k = random_upto(rs, i);
         int j;
 
         for (j = 0; j < n; j++)
@@ -153,15 +199,16 @@ char *new_game_seed(game_params *params)
      * Determine the required parity of the overall permutation.
      * This is the XOR of:
      * 
-     *  - The chessboard parity ((x^y)&1) of the gap square. The
-     *    bottom right, and therefore also the top left, count as
-     *    even.
+     *         - The chessboard parity ((x^y)&1) of the gap square. The
+     *           bottom right counts as even.
      * 
      *  - The parity of n. (The target permutation is 1,...,n-1,0
      *    rather than 0,...,n-1; this is a cyclic permutation of
      *    the starting point and hence is odd iff n is even.)
      */
-    parity = (X(params, gap) ^ Y(params, gap) ^ (n+1)) & 1;
+    parity = ((X(params, gap) - (params->w-1)) ^
+             (Y(params, gap) - (params->h-1)) ^
+             (n+1)) & 1;
 
     /*
      * Try the last two tiles one way round. If that fails, swap
@@ -199,6 +246,57 @@ char *new_game_seed(game_params *params)
     return ret;
 }
 
+char *validate_seed(game_params *params, char *seed)
+{
+    char *p, *err;
+    int i, area;
+    int *used;
+
+    area = params->w * params->h;
+    p = seed;
+    err = NULL;
+
+    used = snewn(area, int);
+    for (i = 0; i < area; i++)
+       used[i] = FALSE;
+
+    for (i = 0; i < area; i++) {
+       char *q = p;
+       int n;
+
+       if (*p < '0' || *p > '9') {
+           err = "Not enough numbers in string";
+           goto leave;
+       }
+       while (*p >= '0' && *p <= '9')
+           p++;
+       if (i < area-1 && *p != ',') {
+           err = "Expected comma after number";
+           goto leave;
+       }
+       else if (i == area-1 && *p) {
+           err = "Excess junk at end of string";
+           goto leave;
+       }
+       n = atoi(q);
+       if (n < 0 || n >= area) {
+           err = "Number out of range";
+           goto leave;
+       }
+       if (used[n]) {
+           err = "Number used twice";
+           goto leave;
+       }
+       used[n] = TRUE;
+
+       if (*p) p++;                   /* eat comma */
+    }
+
+    leave:
+    sfree(used);
+    return err;
+}
+
 game_state *new_game(game_params *params, char *seed)
 {
     game_state *state = snew(game_state);