Академический Документы
Профессиональный Документы
Культура Документы
true echo ' ' echo '' echo '' ARENA=(o o o o o o o o o o o o o o o o o o o o x o o o o o o o o x o o o o o x x x o o o o o o o o o o o o o o o o o o o o o o o o o o ) TMP_ARENA=${ARENA[*]} ### set debug=true to print debugging info. *** Game Of Life ***
revcopy() { z=0; while [ $z -ne 64 ] do ARENA[$z]=${TMP_ARENA[$z]} z=$[$z + 1] done } copy() { z=0; while [ $z -ne 64 ] do TMP_ARENA[$z]=${ARENA[$z]} z=$[$z + 1] done }
## Procedure for printing the arena. print() { i=0; while [ $i -ne 64 ] do if [ ${ARENA[$i]} = o ] then echo -n ' ' else
## Game logic. while [ 1 = 1 ] do ## Print pemanent arena. print sleep 1 ## Make a temporary copy of the arena. copy iter=0 while [ $iter -ne 64 ] do row=$[$iter / 8] col=$[$iter % 8]
if [ $row -eq 0 ] then prevrow=7 else prevrow=$[$row - 1] fi if [ $row -eq 7 ] then nxtrow=0 else nxtrow=$[$row + 1] fi if [ $col -eq 0 ] then prevcol=7 else prevcol=$[$col - 1]
fi if [ $col -eq 7 ] then nxtcol=0 else nxtcol=$[$col + 1] fi # echo row = $row col = $col # echo # echo -n prevrow = $prevrow nxtrow = $nxtrow prevcol = $prevcol n xtcol = $nxtcol
print ## Count number of neighbors alive -- top bottom left and right live=0 if [ ${ARENA[ 8 * $row + $prevcol]} = x ] then live=$[$live + 1] echo "left is live" fi if [ ${ARENA[8 * $row + $nxtcol]} = x ] then live=$[$live + 1] echo "right is live" fi if [ ${ARENA[8 * $nxtrow + $col]} = x ] then live=$[$live + 1] echo "below is live" fi if [ ${ARENA[8 * $prevrow + $col]} = x ] then live=$[$live + 1] echo "above is live" fi if [ ${ARENA[8 * $prevrow + $prevcol]} = x ] then live=$[$live + 1] echo "left top is live" fi if [ ${ARENA[8 * $prevrow + $nxtcol]} = x ] then live=$[$live + 1] echo "right top is live"
fi if [ ${ARENA[8 * $nxtrow + $prevcol]} = x ] then live=$[$live + 1] fi if [ ${ARENA[8 * $nxtrow + $nxtcol]} = x ] then live=$[$live + 1] fi # echo live=$live ## Update temporary arena. if [ ${ARENA[$iter]} = x ] && [ $live -lt 2 ] ell with less than 2 live neighbors then TMP_ARENA[$iter]=o elif [ ${ARENA[$iter]} = x ] && [ $live -gt 3 ] ell with more than 3 live neighbors then TMP_ARENA[$iter]=o elif [ ${ARENA[$iter]} = o ] && [ $live -eq 3 ] ell with exactly 3 live neighbors then TMP_ARENA[$iter]=x alive fi ##Live c ## DIES! ##Live c ## DIES! ##Dead c ## Gets
iter=$[$iter + 1] done ## Copy updates back to temporary arena. revcopy clear done