-
-
Save ko1nksm/63656db220e17734f2bec1e3a75c34df to your computer and use it in GitHub Desktop.
Conway's Game of Life in POSIX Shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
乱数の生成を xorshift32 に置き換えてみた。
シード値(現在はただのUNIXタイム)をどうするかは検討が必要かもしれない。