From 7e84ad0d2a5ae8d59eb81c77061f862f232f32f9 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 6 Aug 2024 00:04:34 +0200 Subject: [PATCH] new option to set seed --- oneplayer.c | 3 --- tetris.c | 22 +++++++++++++++++++--- time.c | 8 ++++++-- time.h | 2 +- twoplayers.c | 3 --- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/oneplayer.c b/oneplayer.c index 3270377..3cb9d6c 100644 --- a/oneplayer.c +++ b/oneplayer.c @@ -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, ¤t, &xblock, &yblock, &next); diff --git a/tetris.c b/tetris.c index 4b1c1b6..e43c173 100644 --- 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 4381e48..a342181 100644 --- 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 75c2307..e032e5c 100644 --- 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); diff --git a/twoplayers.c b/twoplayers.c index a387835..36bc306 100644 --- a/twoplayers.c +++ b/twoplayers.c @@ -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, ¤t_left, &xblock_left, &yblock_left, &next); -- 2.30.2