Online File
Rick Aster: Professional SAS Programming Shortcuts: Contents
filename lifeinit catalog 'work.life.init.source'; data _null_; array l{1:16} $ 32; array new{1:16} $ 32 _temporary_; window Life rows=22 columns=35 irow=2 icolumn=10 #1 l1 $char32. protect=yes #2 l2 $char32. protect=yes #3 l3 $char32. protect=yes #4 l4 $char32. protect=yes #5 l5 $char32. protect=yes #6 l6 $char32. protect=yes #7 l7 $char32. protect=yes #8 l8 $char32. protect=yes #9 l9 $char32. protect=yes #10 l10 $char32. protect=yes #11 l11 $char32. protect=yes #12 l12 $char32. protect=yes #13 l13 $char32. protect=yes #14 l14 $char32. protect=yes #15 l15 $char32. protect=yes #16 l16 $char32. protect=yes #17 'Life' color=blue +5 'Time' color=cyan +1 time f4. color=green protect=yes +5 'Live' color=cyan +1 live f4. color=green protect=yes ; * Read initial grid from file, converting any nonblank character to X; infile lifeinit truncover; input (l1-l16) ($char32. /); do y = 1 to 16; do x = 1 to 32; if substr(l{y}, x, 1) ne ' ' then substr(l{y}, x, 1) = 'X'; end; end; do time = 0 to 1000; * Count live cells.; live = 0; do y = 1 to 16; do x = 1 to 32; live + substr(l{y}, x, 1) = 'X'; end; end; * Display grid and check for end of game. ; _msg_ = 'Press Enter to continue.'; if ststate then _msg_ = 'Steady state. Press Enter to finish.'; if live = 0 THEN _MSG_ = 'Game over. Press Enter to finish.'; display life; if ststate or live = 0 then stop; * Recalculate grid. ; do y = 1 to 16; new{y} = l{y}; do x = 1 to 32; livecell = substr(l{y}, x, 1) = 'X'; * Count surrounding cells. ; count = 0; do dx = -1 to 1; do dy = -1 to 1; if dx = 0 and dy = 0 then continue; if 1 <= x + dx <= 32 and 1 <= y + dy <= 16 then count = count + (substr(l{y + dy}, x + dx, 1) = 'X'); end; end; * Change state of cell. ; if not livecell and count = 3 then substr(new{y}, x, 1) = 'X'; * New live cell; else if livecell and count notin (2, 3) then substr(new{y}, x, 1) = ' '; * New dead cell; end; end; * Activate new grid and check for steady state.; ststate = 1; do y = 1 to 16; if new{y} ne l{y} then ststate = 0; l{y} = new{y}; end; end; stop; run;