From b4ff6798cdec6c9bd837b4da69e9c7a0b0d9eb24 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Fri, 19 May 2017 11:19:13 +0800 Subject: [PATCH] Seed the pseudo-random generator on initialization. Add example script to solve a maze by flood painting. --- eval.c | 3 +++ floodmaz.cmd | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ maze.cmd | 9 ++++++++ 3 files changed, 75 insertions(+) create mode 100644 floodmaz.cmd diff --git a/eval.c b/eval.c index 379bf0c..842280c 100644 --- a/eval.c +++ b/eval.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "basic.h" #include "bind.h" @@ -305,6 +306,8 @@ void varinit(void) result = malloc( NSTRING) ; ressize = NSTRING ; } + + seed = time( NULL) ; } /* diff --git a/floodmaz.cmd b/floodmaz.cmd new file mode 100644 index 0000000..23f0a5b --- /dev/null +++ b/floodmaz.cmd @@ -0,0 +1,63 @@ +# 6 set $seed +execute-file maze.cmd + +set %thisbuf $cbufname + +10 store-macro # push x y + set %x $curcol + set %y $curline + select-buffer stack + beginning-of-file + insert-string %x + newline + insert-string %y + newline + select-buffer %thisbuf +!endm + +11 store-macro # pop x y + select-buffer stack + beginning-of-file + set %x $line +1 kill-to-end-of-line + set %y $line +1 kill-to-end-of-line + select-buffer %thisbuf + set $curline %y + set $curcol %x +!endm + +set $curline 1 +set $curcol 0 +execute-macro-10 #push stop position +set %x 1 +set $curline 4 +set $curcol %x +set %OC $curchar +set %NC &asc "█" +!while ¬ &equ %x 0 + set $curchar %NC + set %cc $curcol + set %ll $curline + set $curcol &add %cc 1 + !if &equ $curchar %OC + execute-macro-10 + !endif + set $curcol &add %cc -1 + !if &equ $curchar %OC + execute-macro-10 + !endif + set $curline &add %ll 1 + set $curcol %cc + !if &equ $curchar %OC + execute-macro-10 + !endif + set $curline &add %ll -1 + set $curcol %cc + !if &equ $curchar %OC + execute-macro-10 + !endif + execute-macro-11 +!endwhile +set $curline 3 +set $curcol 1 diff --git a/maze.cmd b/maze.cmd index ea02483..8e5f519 100644 --- a/maze.cmd +++ b/maze.cmd @@ -1,4 +1,7 @@ +# record seed # 5 set $seed +set %S $seed + # setup direction offsets set %D1 0 set %D2 1 @@ -83,6 +86,12 @@ set %cnt &tim &sub &div %w 2 1 &sub &div %l 2 1 set $curcol %x !endwhile +# id maze +beginning-of-file +kill-to-end-of-line +insert-string &cat " Maze " &cat %w &cat "x" &cat &sub %l 2 &cat " #" %S +&sub $curwidth $curcol insert-string " " + # draw the entrance set $curline 3 set $curcol 1