Hi guys,
I'm new here so please be kind to me

To start off, I'd like to submit some not useful but funny piece of awk code, the Game of Life.
Usage:
life.sh < seed1or:
genlife.sh | life.sh (
for random seed)
or:
genlife.sh 15 15 | life.sh 20 (
for random 15x15 seed and 20 itterations)
or:
genlife.sh 20 20 | life.sh 50 0.1 (
for 20x20, quick 50 itterations)
or:
life.sh 100 0 < seed3 (
for a very quick 100 itterations of seed3)
Code:
#!/bin/sh
## life.sh -- Jun 25 2010, by Patsie
# awk implementation of the Game of Life. A simple automata
# Usage: life.sh [<nr of runs> [<sleep interval>] ]
#
# On a rectangular grid, let each cell be either living or dead.
# Designate a living cell with a dot and a dead one with a blank space.
# Begin with an arbitrarily drawn dot-and-blank grid, and let this be
# the starting generation. Determine each successive generation by the
# following rules:
# 1) Each cell has 8 neighbors, the adjoining cells.
# 2) A living cell with either 2 or 3 living neighbors remains alive.
# 3) A dead cell with 3 living neighbors comes alive (a birth).
# 4) All other cases result in a dead cell for the next generation.
awk -v runs=${1:-10} -v sleep=${2:-0.5} '
BEGIN { DEAD = " "; ALIVE = "o"; }
# return if a neighbor is alive
function isAlive(cell, neighbor) {
if ( (neighbor < 1) || (neighbor > size) ) return 0; # above or below screen
if ( ((cell%cols) == 1) && ((neighbor%cols) == 0) ) return 0; # left off screen
if ( ((cell%cols) == 0) && ((neighbor%cols) == 1) ) return 0; # right off screen
return (substr(data, neighbor, 1) == ALIVE)?1:0;
}
## count number of alive neighbors
function countAlive(cell) {
cnt = isAlive(cell, cell-cols-1) + isAlive(cell, cell-cols) + isAlive(cell, cell-cols+1);
cnt += isAlive(cell, cell-1) + isAlive(cell, cell+1);
cnt += isAlive(cell, cell+cols-1) + isAlive(cell, cell+cols) + isAlive(cell, cell+cols+1);
return cnt;
}
# display playfield
function display(field) {
for (i=0; i<lines; i++)
printf("%s\033[K\n", substr(field, i*cols+1, cols));
}
{
# get generation 0 from stdin
lines++; # count all lines
data=data""$0; # add read data
} END {
## clear screen
printf("\033[2J");
## calc number of columns
cols=int(length(data)/lines);
if ( (length(data)/lines) != cols) { printf("Incorrect data\n"); exit(1); }
size = cols*lines;
## do number of runs
for (run=0; run<runs; run++) {
printf("\033[HItteration %d:\033[K\n", run);
display(data);
## generate new data
newdata="";
for (cell=1; cell<=size; cell++) {
oldstate = substr(data, cell, 1); # get old cell state
newstate = DEAD; # default state is dead
cnt = countAlive(cell); # number of 'alives' around cell
if (cnt == 3) newstate = ALIVE; # 3 is always alive
if (cnt == 2) newstate = oldstate; # no change
newdata=newdata""newstate;
}
## put newdata back and take a break
data = newdata;
if (sleep > 0) system(sprintf("sleep %.1f", sleep));
}
# display final itteration
printf("\033[HItteration %d:\033[K\n", run);
display(data);
}'
And here's a seed generator for life.sh:
Code:
#!/bin/sh
## genlife.sh -- Jun 25 2010, by Patsie
# generates random data for a first generation of Life
# usage: genlife.sh [<width> [<height> [<fill percentage>] ] ]
# defaults to a grid of 15x10 with 25% filling
awk -v width=${1:-15} -v height=${2:-10} -v perc=${3:-25} '
BEGIN { DEAD=" "; ALIVE="o"; srand(); }
END {
for (y=0; y<height; y++) {
for (x=0; x<width; x++)
printf("%c", (rand()*100<perc)?ALIVE:DEAD);
printf("\n");
}
}' </dev/null
and finally 3 basic, static seed files.
(I tried putting the seed files between code tags, but they were still mangled up, so you'll have to do with the seed generator)
Any feedback on the scripts is kindly appreciated.
Regards,
Patsie