Skip to content

Instantly share code, notes, and snippets.

@polm
Forked from anonymous/life.sh
Created February 25, 2014 13:54
Show Gist options
  • Save polm/9209179 to your computer and use it in GitHub Desktop.
Save polm/9209179 to your computer and use it in GitHub Desktop.
#!/bin/bash
GENS=50
WIDTH=12
HEIGHT=12
declare -a OLD
declare -a NEW
OLD=( \
0 0 1 0 0 0 0 0 0 0 0 0 \
1 0 1 0 0 0 0 0 0 0 0 0 \
0 1 1 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 0 0 0 \
0 0 0 0 0 0 0 0 0 1 1 0 \
0 0 0 0 0 0 0 0 0 1 0 1 \
0 0 0 0 0 0 0 0 0 1 0 0 \
)
GENCOUNT=0
while (( GENCOUNT < GENS ))
do
#evolve
COUNT=0
while (( COUNT < ( WIDTH * HEIGHT ) ))
do
NBR=0
(( X = COUNT % WIDTH ))
(( Y = COUNT / WIDTH ))
if (( X > 0 ))
then
if (( Y > 0 )) && [ ${OLD[ COUNT - 1 - WIDTH ]} -eq 1 ]
then (( NBR++ ))
fi
if [ ${OLD[ COUNT - 1 ]} -eq 1 ]
then (( NBR++ ))
fi
if (( Y < HEIGHT - 1 )) && [ ${OLD[ COUNT - 1 + WIDTH ]} -eq 1 ]
then (( NBR++ ))
fi
fi
if (( X < WIDTH - 1 ))
then
if (( Y > 0 )) && [ ${OLD[ COUNT + 1 - WIDTH ]} -eq 1 ]
then (( NBR++ ))
fi
if [ ${OLD[ COUNT + 1 ]} -eq 1 ]
then (( NBR++ ))
fi
if (( Y < HEIGHT - 1 )) && [ ${OLD[ COUNT + 1 + WIDTH ]} -eq 1 ]
then (( NBR++ ))
fi
fi
if (( Y > 0 )) && [ ${OLD[ COUNT - WIDTH]} -eq 1 ]
then (( NBR++ ))
fi
if (( Y < HEIGHT - 1 )) && [ ${OLD[ COUNT + WIDTH]} -eq 1 ]
then (( NBR++ ))
fi
if [ $NBR = 3 ] || ([ $NBR = 2 ] && [ ${OLD[COUNT]} = 1 ])
then NEW[COUNT]=1
else NEW[COUNT]=0
fi
(( COUNT++ ))
done
#echo "${OLD[@]}"
#echo "${NEW[@]}"
OLD=""
OLD=( "${NEW[@]}" )
#echo ""
#sleep 1
#print
clear
COUNT=0
while (( COUNT < (WIDTH * HEIGHT) ))
do
echo -e -n \
"$( echo -n "${NEW[COUNT]} " | \
sed \
-e 's/1 /\\e[41m \\e[m/' \
-e 's/0 /\\e[46m \\e[m/')"
if !(( (COUNT + 1) % WIDTH ))
then echo ""
fi
(( COUNT++ ))
done
#echo ""
#echo "${NEW[@]}"
sleep 1
(( GENCOUNT++ ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment