new option to set seed
authorLaurent Mazet <mazet@softndesign.org>
Mon, 5 Aug 2024 22:04:34 +0000 (00:04 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 5 Aug 2024 22:04:34 +0000 (00:04 +0200)
oneplayer.c
tetris.c
time.c
time.h
twoplayers.c

index 3270377a23d833b5e15bef0221b560f9d765bcfe..3cb9d6c00f5cdd9c2971e35b57a1ba53e42a258e 100644 (file)
@@ -61,9 +61,6 @@ int oneplayer (int width, int height, int scale, int chrono, char *filename)
                                     maxblockheight (blocks, nb_blocks) + 2);
     setscale (nextblock, scale);
 
-    /* init seed */
-    newseed ();
-
     /* get first bloc */
     int xblock, yblock;
     block_t *cblock = drawblock (board, blocks, nb_blocks, NULL, &current, &xblock, &yblock, &next);
index 4b1c1b631e6ea41f5addaa9058ed5af5482be560..e43c173f1ab841a7fc0a86fbeed276506ca297b4 100644 (file)
--- a/tetris.c
+++ b/tetris.c
@@ -11,6 +11,7 @@
 #include "constant.h"
 #include "debug.h"
 #include "oneplayer.h"
+#include "time.h"
 #include "twoplayers.h"
 
 /* static variables */
@@ -21,15 +22,17 @@ int chrono = 0;
 char *filename = NULL;
 int multi = 0;
 int scale = 1;
+unsigned int seed = 0;
 
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-c] [-f file] [-h] [-s int] [-v int] [-w int]\n", progname);
+    fprintf (fd, "usage: %s [-c] [-f file] [-h] [-r int] [-s int] [-v int] [-w int]\n", progname);
     fprintf (fd, " -c: time penalty (%s)\n", (chrono) ? "yes" : "no" );
     fprintf (fd, " -f: file name (%s)\n", (filename) ? filename : "none");
     fprintf (fd, " -h: help message\n");
-    fprintf (fd, " -m: multi players (%s)", (multi) ? "yes" : "no" );
+    fprintf (fd, " -m: multi players (%s)\n", (multi) ? "yes" : "no" );
+    fprintf (fd, " -r: random seed (%u)\n", seed);
     fprintf (fd, " -s: scale [0..3] (%d)\n", scale);
     fprintf (fd, " -v: verbose level (%d)\n", verbose);
     fprintf (fd, " -w: board width  [%d, %d] (%d)\n", minwidth, maxwidth, width);
@@ -78,6 +81,14 @@ int main (int argc, char *argv[])
             filename = NULL;
             chrono = 0;
             break;
+        case 'r':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no seed specified\n", progname));
+                return usage (1);
+            }
+            seed = atoi (arg);
+            break;
         case 's':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -118,6 +129,9 @@ int main (int argc, char *argv[])
         return 1;
     }
 
+    /* init seed */
+    seed = newseed (seed);
+
     switch (multi) {
     case 0:
         return oneplayer (width, height, scale, chrono, filename);
@@ -134,6 +148,7 @@ int main (int argc, char *argv[])
 /* test: tetris.exe -f nofile.ttr 2>&1 | grep "can't read file" */
 /* test: tetris.exe -f bogus.ttr 2>&1 | grep 'incorrect file' */
 /* test: tetris.exe -h | grep usage */
+/* test: tetris.exe -r 2>&1 | grep 'no seed' */
 /* test: tetris.exe -s 2>&1 | grep 'no scale' */
 /* test: tetris.exe -s 4 2>&1 | grep incorrect */
 /* test: tetris.exe -v 2>&1 | grep missing */
@@ -154,8 +169,9 @@ int main (int argc, char *argv[])
 /* test: { echo -n s; sleep 1; echo -n jjuuui; sleep 1; echo -n jjoi; sleep 1; echo q; } | tetris.exe -c -f lines.ttr */
 /* test: { echo -n siiiiiii; sleep 50; echo q; } | tetris.exe -c */
 
-/* test: { echo -n s; sleep 1; echo -n ooollllkieeeddddsz; sleep 1; echo -n uuukjjjjiaaasqqqqz; sleep 1; echo -n ooollllkieeeddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n ooollllkieeedddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n ooollllkieeeedddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n izizizizzi; sleep 1; echo -ne '\e'; } | tetris.exe -m -w 9 */
+/* test: { echo -n s; sleep 1; echo -n ooollllkieeeddddszp; sleep 1; echo -n puuukjjjjiaaasqqqqz; sleep 1; echo -n ooollllkieeeddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n ooollllkieeedddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n ooollllkieeeedddddsz; sleep 1; echo -n uuukjjjjiaaaasqqqqz; sleep 1; echo -n izizizizzi; sleep 1; echo -ne '\e'; } | tetris.exe -m -w 9 */
 /* test: { echo -n s; sleep 1; echo -n lllololoidddededez; sleep 1; echo -n jjjujujuiqqqaqaqaz; sleep 1; echo -n lllololoidddededez; sleep 1; echo -n jjjujujuiqqqaqaqaz; sleep 1; echo -ne '\e'; } | tetris.exe -m -w 9 */
 /* test: { echo -n s; sleep 1; echo -n iziziziziziziziziziziziziz; sleep 1; echo -ne '\e'; } | tetris.exe -m */
+/* test: { echo -n sddddzdzdzqz; sleep 1; echo -n oollliooiujijjjji; sleep 1; echo -n aaqqqqzojjji; sleep 1; echo -ne '\e'; } | tetris.exe -m -r 1 */
 
 /* vim: set ts=4 sw=4 et: */
diff --git a/time.c b/time.c
index 4381e484084ee15f4d8b04ced826f50ed718b1ab..a342181814b1e75afcc4f2951c9db8cde7a625aa 100644 (file)
--- a/time.c
+++ b/time.c
@@ -5,9 +5,13 @@
 
 #include "time.h"
 
-void newseed ()
+int newseed (int seed)
 {
-    srand (time (NULL));
+    if (seed == 0) {
+        seed = time (NULL);
+    }
+    srand (seed);
+    return seed;
 }
 
 void setendtime (timeval_t *t, int s)
diff --git a/time.h b/time.h
index 75c2307add39c7dbb042c38bae6e858775b30251..e032e5cc3baa00af8d9d4b436f1285689e93a00a 100644 (file)
--- a/time.h
+++ b/time.h
@@ -5,7 +5,7 @@
 
 typedef struct timeval timeval_t;
 
-void newseed ();
+int newseed (int seed);
 
 void setendtime (timeval_t *t, int s);
 
index a387835324dd9c103aa63613fca1119c3a6399a6..36bc306c4694d437645dbc50dcc972d98568fa52 100644 (file)
@@ -44,9 +44,6 @@ int twoplayers (int width, int height, int scale)
                                     maxblockheight (blocks, nb_blocks) + 2);
     setscale (nextblock, scale);
 
-    /* init seed */
-    newseed ();
-
     /* get first bloc */
     int xblock_left, yblock_left;
     block_t *cblock_left = drawblock (board_left, blocks, nb_blocks, NULL, &current_left, &xblock_left, &yblock_left, &next);