diff --git a/sedtris.internal-prng.sh b/sedtris.internal-prng.sh new file mode 100755 index 0000000..1f91557 --- /dev/null +++ b/sedtris.internal-prng.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# Bash script to make playing Sedtris more comfortable. +# It presses "enter" automatically once in a second and after +# every player's move. +# +# It also creates a random seed for the internal PRNG to improve gameplay. +# +# Based on playsed.sh by Aurelio Marinho Jargas http://sed.sf.net/grabbag/scripts/playsed.sh.txt + +mytime="`date +%s`" + +IFS='' +CMD='' +TMP='' + +cat /dev/urandom | tr -dc "01" | fold -w 18 | head -1 > seed.txt + +export LC_ALL="C" +(while true; +do + read -s -t 1 -n 1 TMP + RES=$? + CMD="$CMD$TMP" + if [ "$CMD" == '' ] + then + read -s -n 1 TMP; CMD="$CMD$TMP"; + if [ "$CMD" == '[' ] + then + read -s -n 1 TMP; CMD="$CMD$TMP"; + fi + fi + if [ $RES -eq 0 ] + then + echo "$CMD" + fi + CMD='' + TMP='' + mytimenew="`date +%s`" + if [ "$mytimenew" != "$mytime" ] + then + echo + mytime=$mytimenew + fi +done) | cat seed.txt - | sed -nf `dirname $0`/sedtris.sed diff --git a/sedtris.sed b/sedtris.sed old mode 100644 new mode 100755 index 90bb910..b786561 --- a/sedtris.sed +++ b/sedtris.sed @@ -2,7 +2,17 @@ # sedtris.sed - sed tetris # 26th of May, 2008 # Julia Jomantaite + +# Usage examples: +# ./sedtris.sed +# ./sedtris.sh +# ./sedtris.internal-prng.sh +# cat seed.txt - | ./sedtris.sed +# where seed.txt contains a string of 0 and 1, +# e.g., echo 00000110000 > seed.txt +:again 1{ +/./! s/$/0001000/; h # Backup the seed. s/.*/ 2a2a2a2a|3a3a3a3a3a3a3a3a3a3a|2a2a2a2a~/ s/[^~]*~$/&&&/ s/.*/& 2a2a2a2a|0a0a0a0a0a0a0a0a0a0a|2a2a2a2a~/ @@ -18,13 +28,17 @@ s/$/#AAAA EDCA GPJA IMBA~/ s/$/#ACIA EGME BDJB ACIA~/ s/$/#AAAA IKEA FPFA BKCA~/ s/$/#AAAA CGBA MPDA EJIA~/ +G; s/\ +\([01]*\)$/#\1~/ # Place the seed among other "variables." s/$/#NEXTAAAA ANAA LPOA AHAA 5~/ s/$/#SCORE0~/ h b display } -/1/{s/1//;x;s/#\.\.*/&./;s/#\.\{8\}/#./;x;} +brandom; :return; s/0// # Call the PRNG code. + +/1/{s/1//g;x;s/#\.\.*/&./;s/#\.\{8\}/#./;x;} /^w/b rotate /^a/b left @@ -342,3 +356,22 @@ i\  p /GAME OVER!/q + +n; bagain + +:random # Rule 30 automaton (see github.com/Circiter/elementary-ca-in-sed). + g; s/^.*#\([01]*\)~.*$/\1/ # Get the old generation. + s/^\(.\)\(.*\)\(.\)$/>\3\1\2\3\1/ + :update # Evolve according to rule 30. + s/$/\n000=0;001=1;010=1;011=1;100=1;101=0;110=0;111=0/ + s/^\([^>]*\)>\(...\)\([^\n]*\n\).*\2=\(.\)/\1\4>\2\3/ + s/\n.*$//; s/>./>/ + />$/! bupdate + s/>// + # Save the new generation. + x; s/$/@/; G; s/#[01]*~\(.*\)@[^01]*\([01]*\)$/#\2~\1/; x + :center # Remove all but the central cell. + s/^.\(.\)/\1/; + s/\(.\).$/\1/ + /../bcenter + breturn diff --git a/sedtris.sh b/sedtris.sh old mode 100644 new mode 100755 index 361c0f2..32875ca --- a/sedtris.sh +++ b/sedtris.sh @@ -39,7 +39,8 @@ do let "temp=($RANDOM/10)%7" if [ $temp == 1 ] then - echo $temp + #echo $temp + echo else echo fi