Skip to content

Instantly share code, notes, and snippets.

@ko1nksm
Forked from ytaki0801/CGoL.sh
Last active February 3, 2023 12:15
Show Gist options
  • Save ko1nksm/63656db220e17734f2bec1e3a75c34df to your computer and use it in GitHub Desktop.
Save ko1nksm/63656db220e17734f2bec1e3a75c34df to your computer and use it in GitHub Desktop.
Conway's Game of Life in POSIX Shell
#!/bin/sh
xs=80 ys=35
ORIGIN=$(clear) LF='
'
xorshift32=$(date +%s)
xorshift32() {
set -- "$1" "$xorshift32"
set -- "$1" $(( $2 ^ (($2 << 13) & 0xFFFFFFFF) ))
set -- "$1" $(( $2 ^ ($2 >> 17) ))
set -- "$1" $(( $2 ^ (($2 << 5) & 0xFFFFFFFF) ))
export $1=$(( (xorshift32 = $2) > 0 ? $2 : -1 * $2))
}
j=$ys; while [ $j != 0 ]; do
i=$xs; while [ $i != 0 ]; do
xorshift32 r
export c$i$j=$((r%2))
i=$((i-1))
done; j=$((j-1))
done
clear
while true; do
buf=$ORIGIN
j=$ys; while [ $j != 0 ]; do
i=$xs; while [ $i != 0 ]; do
case $((c$i$j)) in 1) buf="$buf*" ;; *) buf="$buf " ;; esac
i=$((i-1))
done; buf="$buf$LF" j=$((j-1))
done
echo "${buf%$LF}"
j=$ys; while [ $j != 0 ]; do
i=$xs; while [ $i != 0 ]; do
t=0
for l in -1 0 1; do
for k in -1 0 1; do
tx=$(((i+k)%xs)); case $tx in -1) tx=$((tx+xs)) ;; esac
ty=$(((j+l)%ys)); case $ty in -1) ty=$((ty+ys)) ;; esac
t0=$((c$tx$ty)); t=$((t+t0))
case $k in 0) case $l in 0) t=$((t-t0)) ;; esac ;; esac
done
done
case $((c$i$j)) in
1) case $t in 2|3) export cn$i$j=1 ;; *) export cn$i$j=0 ;; esac ;;
*) case $t in 3) export cn$i$j=1 ;; *) export cn$i$j=0 ;; esac ;;
esac
i=$((i-1))
done; j=$((j-1))
done
j=$ys; while [ $j != 0 ]; do
i=$xs; while [ $i != 0 ]; do
export c$i$j=$((cn$i$j)) i=$((i-1))
done; j=$((j-1))
done
done
@ko1nksm
Copy link
Author

ko1nksm commented Feb 2, 2023

よくよく考えたら dd 使うよりも head -c の方が良さそうだから書き換えたら、 /dev/random からの読み込みでも問題なかった。

追記 head -c は POSIX Issue 7 では標準化されていませんでした。Issue 8 では標準化されます。

@ko1nksm
Copy link
Author

ko1nksm commented Feb 3, 2023

@ko1nksm
Copy link
Author

ko1nksm commented Feb 3, 2023

乱数の生成を xorshift32 に置き換えてみた。
シード値(現在はただのUNIXタイム)をどうするかは検討が必要かもしれない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment