| Life Game OperatorThe Life Game demo shows how Elzed handles multi-line expressions with ease. In the two examples below, the first expression sets up a 16 by 16 board and places a 'glider' in the lower left corner. The second expression applies the Life Game rules to the board and produces successive generations.
Any well-formed expression, regardless of the number of lines comprising it, can be processed by Elzed to create an expression-based operator.
// Call this expression to create a 16x16 board
// and place a glider in the lower left corner
global string board[ 16, 20 ];
global number b[ 16, 16 ];
x = 0; y = 0;
while ( y < 16 ) do x = 0;
while ( x < 16 ) do b[ x++, y ] = 0; next
board[ y++ ] = " ";
next
b[ 3, 3 ] = 1;
b[ 3, 5 ] = 1;
b[ 4, 4 ] = 1;
b[ 5, 4 ] = 1;
b[ 4, 5 ] = 1;
// Call this expression repeatedly
// to see successive generations
x = 0; y = 0;
neighbors = 0;
while ( y <= 15 ) do
x = 0;
while ( x <= 15 ) do
neighbors = 0;
if ( (y+1)<=15 ) then if ( (x-1)>=0 ) then if ( b[ x-1, y+1 ] >= 1 ) then neighbors++; end end
if ( b[ x, y+1 ] >= 1 ) then neighbors++; end
if ( (x+1)<=15 ) then if ( b[ x+1, y+1 ] >= 1 ) then neighbors++; end end end
if ( (x-1)>=0 ) then if ( b[ x-1, y ] >= 1 ) then neighbors++; end end
if ( (x+1)<=15 ) then if ( b[ x+1, y ] >= 1 ) then neighbors++; end end
if ( (y-1)>=0 ) then if ( (x-1)>=0 ) then if ( b[ x-1, y-1 ] >= 1 ) then neighbors++; end end
if ( b[ x, y-1 ] >= 1 ) then neighbors++; end
if ( (x+1)<=15 ) then if ( b[ x+1, y-1 ] >= 1 ) then neighbors++; end end end
if ( (b[ x, y ] == 0 ) and (neighbors == 3) ) then b[ x, y ] = -1; end
if ( (b[ x, y ] == 1 ) and ((neighbors < 2) or (neighbors > 3)) ) then b[ x, y ] = 2; end
x++;
next
y++;
next
y = 0;
while ( y <= 15 ) do
x = 0;
while ( x <= 15 ) do
select( b[ x, y ] )
case (-1): b[ x, y ] = 1; replace( board[ y ], "O", x, 1 ); break;
case 0: replace( board[ y ], " ", x, 1 ); break;
case 1: replace( board[ y ], "O", x, 1 ); break;
case 2: b[ x, y ] = 0; replace( board[ y ], " ", x, 1 ); break;
end
x++;
next
y++;
next
|