Wireworld: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|REXX}}: added/changed comments and whitespace, changed indentations, elided a non-used function.) |
||
Line 4,598: | Line 4,598: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program displays a wire world Cartesian grid of four─state cells. |
<lang rexx>/*REXX program displays a wire world Cartesian grid of four─state cells. */ |
||
signal on halt /*handle any cell growth interruptus. */ |
signal on halt /*handle any cell growth interruptus. */ |
||
parse arg iFID . '(' generations rows cols bare head tail wire clearScreen reps |
parse arg iFID . '(' generations rows cols bare head tail wire clearScreen reps |
||
if iFID=='' then iFID="WIREWORLD.TXT" |
if iFID=='' then iFID= "WIREWORLD.TXT" /*should default input file be used? */ |
||
bla = 'BLANK' /*the "name" for a blank. */ |
|||
generations = p(generations 100 ) /*number generations that are allowed. */ |
generations = p(generations 100 ) /*number generations that are allowed. */ |
||
rows = p(rows 3 ) /*the number of cell rows. */ |
rows = p(rows 3 ) /*the number of cell rows. */ |
||
cols = p(cols 3 ) /* " " " " columns. */ |
cols = p(cols 3 ) /* " " " " columns. */ |
||
bare = pickChar(bare |
bare = pickChar(bare bla ) /*an empty cell character. */ |
||
clearScreen = p(clearScreen 0 ) /*1 means to clear the screen. */ |
clearScreen = p(clearScreen 0 ) /*1 means to clear the screen. */ |
||
head = pickChar(head 'H' ) /*pick the character for the head. */ |
head = pickChar(head 'H' ) /*pick the character for the head. */ |
||
tail = pickChar( |
tail = pickChar(tail 't' ) /* " " " " " tail. */ |
||
wire = pickChar(wire . ) /* " " " " " wire. */ |
|||
reps = p(reps 2 ) /*stop program if there are 2 repeats.*/ |
reps = p(reps 2 ) /*stop program if there are 2 repeats.*/ |
||
fents=max(linesize()-1 |
fents=max(cols, linesize() - 1) /*the fence width used after displaying*/ |
||
#reps=0; $.=bare /*at start, universe is new and barren.*/ |
#reps=0; $.=bare /*at start, universe is new and barren.*/ |
||
gens=abs(generations) /*use for convenience (and short name).*/ |
gens=abs(generations) /*use for convenience (and short name).*/ |
||
/* [↓] read the input file. */ |
/* [↓] read the input file. */ |
||
do r=1 while lines(iFID)\==0 /*keep reading until the End─Of─File. */ |
do r=1 while lines(iFID)\==0 /*keep reading until the End─Of─File. */ |
||
q=strip(linein(iFID), |
q=strip(linein(iFID),'T'); L=length(q) /*get a line from input file; get len.*/ |
||
cols=max(cols, L) /*calculate maximum number of columns. */ |
|||
do c=1 for L; $.r.c=substr(q, c, 1); end /*c*/ /*assign the row cells.*/ |
|||
⚫ | |||
end /*r*/ |
end /*r*/ |
||
rows=r-1 /*adjust the row number (from DO loop).*/ |
rows=r-1 /*adjust the row number (from DO loop).*/ |
||
life=0; !.=0; |
life=0; !.=0; call showCells /*display initial state of the cells. */ |
||
/*watch cells evolve, 4 possible states*/ |
/*watch cells evolve, 4 possible states*/ |
||
do life=1 for gens; |
do life=1 for gens; @.=bare /*perform for the number of generations*/ |
||
do r=1 for rows /*process each of the rows. */ |
do r=1 for rows /*process each of the rows. */ |
||
do c=1 for cols; ?=$.r.c; ??=? /* " " " " columns. */ |
do c=1 for cols; ?=$.r.c; ??=? /* " " " " columns. */ |
||
Line 4,631: | Line 4,630: | ||
when ?==head then ??=tail |
when ?==head then ??=tail |
||
when ?==tail then ??=wire |
when ?==tail then ??=wire |
||
when ?==wire then do; |
when ?==wire then do; #=hood(); if #==1 | #==2 then ??=head; end |
||
otherwise nop |
otherwise nop |
||
end /*select*/ |
end /*select*/ |
||
Line 4,644: | Line 4,643: | ||
halt: if life-1\==gens then say 'The ~~~Wireworld~~~ program was interrupted.' |
halt: if life-1\==gens then say 'The ~~~Wireworld~~~ program was interrupted.' |
||
done: exit /*stick a fork in it, we are all done.*/ |
done: exit /*stick a fork in it, we are all done.*/ |
||
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
p: return word(arg(1), 1) /*pick the 1st word in list.*/ |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
showCells: if clearScreen then 'CLS' /*◄──change this for the OS.*/ |
showCells: if clearScreen then 'CLS' /*◄──change this for the OS.*/ |
||
call showRows /*show rows in proper order.*/ |
call showRows /*show rows in proper order.*/ |
||
say right(copies('═',fents)life,fents) |
say right( copies('═', fents)life, fents) /*display a bunch of cells. */ |
||
if _=='' |
if _=='' then signal done /*No life? Then stop run. */ |
||
if !._ |
if !._ then #reps=#reps+1 /*detected repeated pattern.*/ |
||
!._=1 /*it |
!._=1 /*it's now existence state. */ |
||
if reps\==0 & #reps<=reps then return /*so far, so good, no reps. */ |
if reps\==0 & #reps<=reps then return /*so far, so good, no reps. */ |
||
say '"Wireworld" repeated itself' reps "times, program is stopping." |
say '"Wireworld" repeated itself' reps "times, program is stopping." |
||
signal done /* |
signal done /*jump to this pgm's "exit".*/</lang> |
||
/*─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
assign$: do r=1 for rows; do c=1 for cols; $.r.c=@.r.c; end; end; return |
|||
err: say; say center(' error! ',max(40,linesize()%2),"*"); say; do j=1 for arg(); say arg(j); say; end; say; exit 13 |
|||
⚫ | |||
p: return word(arg(1), 1) |
|||
⚫ | |||
⚫ | |||
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. |
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. |
||