View Single Post
Old 03-20-2012, 12:58 PM   #23
kaminkatze
Member
kaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with others
 
Posts: 17
Karma: 2600
Join Date: Mar 2012
Device: Kindle 3
Pixel based, kindle 3 game of life.

Spoiler:

Code:
#!/bin/sh

#===========================
# initvar - init global vars
# usage: initvar w h
#---------------------------
initvar() {
  DZ=/dev/zero DN=/dev/null DF=/dev/fb0 BF=/tmp/fb0
  FW=$1 FH=$2 MX=$(($1-1)) MY=$(($2-1))
  LL=300 D='0' L='F'
}

#===========================
# usage: load x y name
#---------------------------
load() {
  local x=$1 y=$2 name=$3
  case "$name" in
  glider)
    setpix $DF $((x+1)) $y $L
    setpix $DF $((x+2)) $((y+1)) $L
    setpix $DF $x $((y+2)) $L
    setpix $DF $((x+1)) $((y+2)) $L
    setpix $DF $((x+2)) $((y+2)) $L ;;
  esac; eips ''
}

#===================================
# usage: setpix f x y v
#-----------------------------------
setpix() {
  local x=$((($2+$FW)%$FW)) y=$((($3+$FH)%$FH))
  local b=$(hexdump -s $(($y*LL+$x/2)) -n 1 -e '"%02X"' $1)
  { [[ $(($x%2)) == 0 ]] && echo -en "\x$4${b#?}" || echo -en "\x${b%?}$4"; } \
  | dd of=$1 bs=1 count=1 seek=$(($y*LL+$x/2)) 2>$DN
}

#===================================
# usage: getpix f x y
#-----------------------------------
getpix() {
  local x=$((($2+$FW)%$FW)) y=$((($3+$FH)%$FH))
  local b=$(hexdump -s $(($y*LL+$x/2)) -n 1 -e '"%02X"' $1)
  [[ $(($x%2)) == 0 ]] && echo ${b%?} || echo ${b#?}
}

#===================================
# update - update field
#-----------------------------------
update() {
  local bl=0 bm=0 br=0 cl=0 cm=0 cr=0 tl=0 tm=0 tr=0 c
  cat $DF > $BF
  for x in $(seq 0 $MX); do
  for y in -1 0; do
    tl=$cl tm=$cm tr=$cr cl=$bl cm=$bm cr=$br
    bl=$(getpix $BF $((x-1)) $y | tr 'F' '1')
    bm=$(getpix $BF $x $y | tr 'F' '1')
    br=$(getpix $BF $((x+1)) $y | tr 'F' '1')
  done
  for y in $(seq 0 $MY); do
    tl=$cl tm=$cm tr=$cr cl=$bl cm=$bm cr=$br
    bl=$(getpix $BF $((x-1)) $((y+1)) | tr 'F' '1')
    bm=$(getpix $BF $x $((y+1)) | tr 'F' '1')
    br=$(getpix $BF $((x+1)) $((y+1)) | tr 'F' '1')
    c=$((bl+bm+br+cl+cr+tl+tm+tr))
    v=$(getpix $BF $x $y)
    [[ $c -lt 2 -o $c -gt 3 ]] && v=$D
    [[ $c -eq 3 ]] && v=$L
    setpix $DF $x $y $v 
  done; done; eips ''
}

#===================================
# loop - update loop
# usage: loop steps
#-----------------------------------
loop() {
  local c=$1 p=0
  while [[ $p -lt $c ]]; do
    update; p=$((p+1))
  done
}

eips -c
initvar 8 8
load 0 0 glider
loop 1

(Hope I haven't violated to many of your style constrains.)

Last edited by kaminkatze; 03-20-2012 at 04:55 PM.
kaminkatze is offline   Reply With Quote