Wireworld: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments, statements, and whitespace, used a template for the output section.
m (→‎{{header|REXX}}: added/changed comments, statements, and whitespace, used a template for the output section.)
Line 4,838:
=={{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" /*should default input file be used? */
Line 4,845 ⟶ 4,844:
rows = p(rows 3 ) /*the number of cell rows. */
cols = p(cols 3 ) /* " " " " columns. */
bare = pickChar(bare bla ) ) /*an empty cell character. used to show an empty cell.*/
clearScreen = p(clearScreen 0 ) /*1 means to clear the screen. */
head = pickChar(head 'H' ) /*pick the character for the head. */
Line 4,852 ⟶ 4,851:
reps = p(reps 2 ) /*stop program if there are 2 repeats.*/
fents=max(cols, linesize() - 1) /*the fence width used after displaying*/
#reps= 0; $.= bare; gens= abs(generations) /*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), 'T'); L=length(q) /*get a line from input file;. get len.*/
L= length(q); cols= max(cols, L) /*calculate maximum number of columns. */
do c=1 for L; $.r.c= substr(q, c, 1); end /*c*/assign the cells /*assignfor the row cells R row. */
end /*rc*/
end /*r*/
rows=r-1 /*adjust the row number (from DO loop).*/
life!.= 0; !.=0; call showCells signal on halt /*display initial state of the cells.; handle halt.*/
rows= r-1; life= 0; call showCells /*display initial state of the cells. */
/*watch cells evolve, 4 possible states*/
do life=1 for gens; @.=bare bare /*perform for the number of generations*/
do r=1 for rows /*process each of the rows. */
do c=1 for cols; ?= $.r.c; ??=? ? /* " " " " columns. */
select /*determine the type of cell. */
when ?==head then ??= tail
when ?==tail then ??= wire
when ?==wire then do; #= hood(); if #==1 | #==2 then ??= head; end
otherwise nop
end /*select*/
signal on halt @.r.c= ?? /*handlepossible assign a any cell growth interruptus.a new state.*/
@.r.c=??
end /*c*/
end /*r*/
Line 4,880 ⟶ 4,879:
end /*life*/
/*stop watching the universe (or life).*/
halt: if life-1\==gens then say 'The ~~~Wireworld~~~───Wireworld─── program was interrupted by user.'
done: exit /*stick a fork in it, we are all done.*/
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
$: parse arg _row,_col; return $._row._col==head
assign$: do r=1 for rows; do c=1 for cols; $.r.c= @.r.c; end; end; return
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)
p: return word(arg(1), 1) /*pick the 1st word in list.*/
pickChar: parse arg _=p( .;arg(1)) U .;L=length(_);if translate(_)U==bla then _=' '; if L==3 then _=d2c(_);if L==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
/*──────────────────────────────────────────────────────────────────────────────────────*/
showCells: if clearScreen then 'CLS' /*◄──change thisCLS for the OS.host*/
call showRows /*show rows in proper order.*/
say right( copies('═', fents)life, fents) /*display a bunchtitle offor cells. */
if _=='' then signal done /*No life? Then stop run. */
if !._ then #reps= #reps +1 1 /*detected repeated pattern.*/
!._= 1 /*it's is now existencean extant state. */
if reps\==0 & #reps<=reps then return /*so far, so good, no reps. */
say '"Wireworld" repeated itself' reps "times, the program is stopping."
signal done /*jump to this pgm's "exit".*/</lang>
Programming note: &nbsp; the &nbsp; '''hood''' &nbsp; subroutine (above) could be optimized for speed by setting some short-circuit values &nbsp; <code>('''r-1''', '''c-1''', '''r+1''', and '''c+1''') </code> &nbsp; and using those values in the subsequent expressions.
 
<br>This REXX program makes use of the &nbsp; '''linesize''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
 
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]]. <br><br>
The &nbsp; '''outputLINESIZE.REX''' &nbsp; whenREXX theprogram defaultis inputincluded filehere is&nbsp; ──► &nbsp; used:[[LINESIZE.REX]]. <br><br>
 
{{out|output|text=&nbsp; when using the default input file:}}
 
(Cycle &nbsp; '''0''' &nbsp; (zero) &nbsp; is essentially a copy of the input file.)
<pre style="height:60ex">
Line 4,990 ⟶ 4,992:
t.tH ......
═══════════════════════════════════════════════════════════════════════════════════════13
"Wireworld" repeated itself 2 times, the program is stopping.
</pre>