Move PRNG initialization to simplify cheat.c
authorEric S. Raymond <esr@thyrsus.com>
Sat, 1 Jul 2017 12:59:45 +0000 (08:59 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Sat, 1 Jul 2017 12:59:45 +0000 (08:59 -0400)
advent.h
cheat.c
init.c
main.c

index ac648fecc454317b87847c96b7e4a1ddddfde28b..6f66d1ec4414c3be019c606690fe26911f73bf88 100644 (file)
--- a/advent.h
+++ b/advent.h
@@ -210,7 +210,7 @@ extern int savefile(FILE *, long);
 extern int suspend(void);
 extern int resume(void);
 extern int restore(FILE *);
 extern int suspend(void);
 extern int resume(void);
 extern int restore(FILE *);
-extern void initialise(void);
+extern long initialise(void);
 extern int action(struct command_t *command);
 
 /* Alas, declaring this static confuses the coverage analyzer */
 extern int action(struct command_t *command);
 
 /* Alas, declaring this static confuses the coverage analyzer */
diff --git a/cheat.c b/cheat.c
index 5bca67f3e0f267f3ee07a0cf6362514ef4916793..89b0f72265b544b37cfe755cf9a9f9ac2acc23a4 100644 (file)
--- a/cheat.c
+++ b/cheat.c
@@ -83,13 +83,6 @@ int main(int argc, char *argv[])
 
     FILE *fp = NULL;
 
 
     FILE *fp = NULL;
 
-    game.lcg_a = 1093;
-    game.lcg_c = 221587;
-    game.lcg_m = 1048576;
-    srand(time(NULL));
-    long seedval = (long)rand();
-    set_seed(seedval);
-
     /*  Initialize game variables */
     initialise();
 
     /*  Initialize game variables */
     initialise();
 
diff --git a/init.c b/init.c
index 4d05e0fa03c6b4ff02f2f1541c0c61ab228a3ef9..fdde548f879c5e83db88d0f60ae478a5dfa89c94 100644 (file)
--- a/init.c
+++ b/init.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
+#include <time.h>
 
 #include "advent.h"
 
 
 #include "advent.h"
 
  * Initialisation
  */
 
  * Initialisation
  */
 
-void initialise(void)
+long initialise(void)
 {
     if (oldstyle)
         printf("Initialising...\n");
 
 {
     if (oldstyle)
         printf("Initialising...\n");
 
+    /* Initialize our LCG PRNG with parameters tested against
+     * Knuth vol. 2. by the original authors */
+    game.lcg_a = 1093;
+    game.lcg_c = 221587;
+    game.lcg_m = 1048576;
+    srand(time(NULL));
+    long seedval = (long)rand();
+    set_seed(seedval);
+
     for (int i = 1; i <= NOBJECTS; i++) {
         game.place[i] = LOC_NOWHERE;
     }
     for (int i = 1; i <= NOBJECTS; i++) {
         game.place[i] = LOC_NOWHERE;
     }
@@ -57,4 +67,6 @@ void initialise(void)
         }
     }
     game.conds = setbit(11);
         }
     }
     game.conds = setbit(11);
+
+    return seedval;
 }
 }
diff --git a/main.c b/main.c
index a6027f8136e06637db534a1a3df691a0e8384e3e..077e3a996d200dde709fda3215035b194f5c9c99 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,7 +19,6 @@
 #include <stdbool.h>
 #include <getopt.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <getopt.h>
 #include <signal.h>
-#include <time.h>
 #include <string.h>
 #include "advent.h"
 #include "linenoise/linenoise.h"
 #include <string.h>
 #include "advent.h"
 #include "linenoise/linenoise.h"
@@ -138,17 +137,8 @@ int main(int argc, char *argv[])
 
     linenoiseHistorySetMaxLen(350);
 
 
     linenoiseHistorySetMaxLen(350);
 
-    /* Initialize our LCG PRNG with parameters tested against
-     * Knuth vol. 2. by the original authors */
-    game.lcg_a = 1093;
-    game.lcg_c = 221587;
-    game.lcg_m = 1048576;
-    srand(time(NULL));
-    long seedval = (long)rand();
-    set_seed(seedval);
-
     /*  Initialize game variables */
     /*  Initialize game variables */
-    initialise();
+    long seedval = initialise();
 
     /*  Start-up, dwarf stuff */
     make_zzword(game.zzword);
 
     /*  Start-up, dwarf stuff */
     make_zzword(game.zzword);