Anonymous user
Wireworld: Difference between revisions
→{{header|REXX}}: added/changed comments and whitespace, changed indentations, elided a non-used function.
No edit summary |
(→{{header|REXX}}: added/changed comments and whitespace, changed indentations, elided a non-used function.) |
||
Line 4,598:
=={{header|REXX}}==
<lang rexx>/*REXX program displays a wire world Cartesian grid of four─state cells.
signal on halt /*handle any cell growth interruptus. */
parse arg iFID . '(' generations rows cols bare head tail wire clearScreen reps
if iFID=='' then iFID= "WIREWORLD.TXT"
generations = p(generations 100 ) /*number generations that are allowed. */
rows = p(rows 3 ) /*the number of cell rows. */
cols = p(cols 3 ) /* " " " " columns. */
bare = pickChar(bare
clearScreen = p(clearScreen 0 ) /*1 means to clear the screen. */
head = pickChar(head 'H' ) /*pick the character for the head. */
tail = pickChar(
wire = pickChar(wire . ) /* " " " " " wire. */
reps = p(reps 2 ) /*stop program if there are 2 repeats.*/
fents=max(cols, linesize() - 1
#reps=0; $.=bare /*at start, universe is new and barren.*/
gens=abs(generations) /*use for convenience (and short name).*/
/* [↓] read the input file. */
do r=1 while lines(iFID)\==0 /*keep reading until the End─Of─File. */
q=strip(linein(iFID),
do c=1 for _; $.r.c=substr(q, c, 1); end /*assign the row cells.*/▼
end /*r*/
rows=r-1 /*adjust the row number (from DO loop).*/
life=0; !.=0;
/*watch cells evolve, 4 possible states*/
do life=1 for gens;
do r=1 for rows /*process each of the rows. */
do c=1 for cols; ?=$.r.c; ??=? /* " " " " columns. */
Line 4,631 ⟶ 4,630:
when ?==head then ??=tail
when ?==tail then ??=wire
when ?==wire then do;
otherwise nop
end /*select*/
Line 4,644 ⟶ 4,643:
halt: if life-1\==gens then say 'The ~~~Wireworld~~~ program was interrupted.'
done: exit /*stick a fork in it, we are all done.*/
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
▲assign$: do r=1 for
hood: return $(r-1,c-1) +
p: return word(arg(1), 1) /*pick the 1st word in list.*/
pickChar: _=p(arg(1));L=length(_);if translate(_)==
showRows: _=; do r=1 for rows; z=; do c=1 for cols; z=z
/*──────────────────────────────────────────────────────────────────────────────────────*/
showCells: if clearScreen then 'CLS' /*◄──change this for the OS.*/
call showRows /*show rows in proper order.*/
say right( copies('═', fents)life, fents)
if _==''
if !._
!._=1 /*it
if reps\==0 & #reps<=reps then return /*so far, so good, no reps. */
say '"Wireworld" repeated itself' reps "times, program is stopping."
signal done /*
▲$: parse arg _row,_col; return ($._row._col==head)
▲hood: return $(r-1,c-1) + $(r-1,c) + $(r-1,c+1) + $(r,c-1) + $(r,c+1) + $(r+1,c-1) + $(r+1,c) + $(r+1,c+1)
▲pickChar: _=p(arg(1));if translate(_)==blank then _=' ';if length(_)==3 then _=d2c(_);if length(_)==2 then _=x2c(_);return _
▲showRows: _=; do r=1 for rows; z=; do c=1 for cols; z=z || $.r.c; end; z=strip(z,'T'); say z; _=_ || z; end; return</lang>
Programming note: the '''hood''' subroutine (above) could be optimized for speed by setting some short-circuit values <code>('''r-1''', '''c-1''', '''r+1''', and '''c+1''') </code> and using those values in the subsequent expressions.
|