Conway's Game of Life: Difference between revisions

m
→‎version 1: added/changed comments, indentations, and whitespace, made general improvements.
m ((reorder of entry))
m (→‎version 1: added/changed comments, indentations, and whitespace, made general improvements.)
Line 7,533:
=={{header|REXX}}==
===version 1===
This version has been trimmed down from the original REXX program, otherwise the size of the program (with all its options and optional formatting) would
<br>would probably be on the biglarge side for general viewing, &nbsp; and maybe a wee bit complex to demonstrate how to program for this task.
<lang rexx>/*REXX program displays the Conway's game of life, it stops after N repeats. */
signal on halt /*handle a cell growth interruptus. */
parse arg peeps '(' rows cols empty life! clearScreen repeats generations
rows = p(rows 3) /*the maximum number of cell rows. */
cols = p(cols 3) /* " " " " " colscolumns. */
emp = pickChar(empty 'blank') /*an empty cell character (glyph). */
clearScr = p(clearScreen 0) /* "1" indicates to clear the screen.*/
clearscr=1
clearscr=0
life! = pickChar(life! '☼') /*the gylph kinda looks like an amebaamoeba. */
reps = p(repeats 2) /*stop pgm if there are 2two repeats.*/
generations = p(generations 100) /*the number of generations allowed. */
usw=max(linesize()-1,cols) /*usable screen width for the display. */
#reps=0; $.=emp /*the universe is new, ··· and barren.*/
gens=abs(generations) /*use this for convenience. /*used for a programming convenience.*/
x=space(peeps); upper x /*elide superfluous spaces; upperuppercase. */
if x=='' then x='"BLINKER'" /*if nonenothing specified, use BLINKER. */
if x=='BLINKER' then x=' "2,1 2,2 2,3'"
if x=='GLIDER' then x=' "48,11 48,12 48,13 49,13 50,12'"
if x=='OCTAGON' then x=' "1,5 1,6 2,4 2,7 3,3 3,8 4,2 4,9 5,2 5,9 6,3 6,8 7,4 7,7 8,5 8,6'"
call assign. /*assign the initial state of all cells.*/
call showCells /*show the initial state of the cells.*/
/* [↓] cell colony grow/live/diegrows, lives, dies.*/
do life=1 for gens; call assign@ /*construct the next generation. of cells.*/
if generations>0 | life==gens then call showCells /*displayshould itcells be displayed? */
end /*life*/
fin: exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────SHOWCELLS subroutine───────────────────*/
showCells: if clearScr then 'CLS' /* ◄─── change this for your OS. */
call showRows; ?='▒' /*show the rows in the proper order. */
say right(copies('▒'?, usw) life, usw) /*show a fence between the generations.*/
if _=='' then call fin /*if there's no life, then stop the run.*/
if !._ then #reps=#reps+1 /*we detected a repeated cell pattern. */
!._=1 /*existence state &and compare later. */
if reps\==0 & #reps<=reps then return /*so far, so good, regarding repsrepeats.*/
say; say center(' "Life" repeated itself' reps "times, simulation has ended. ", usw,?)
call fin /*stick a fork in it, we're all done. */
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────1─liner subroutines───────────────────────────────────────────────────────────────────────*/
$: parse arg _row,_col; return $._row._col==life!
assign$: do r=1 for rows; do c=1 for cols; $.r.c=@.r.c; end; end; return
assign.: do while x\==''; parse var x r '",'" c x; $.r.c=life!; rows=max(rows,r); cols=max(cols,c); end; life=0; !.=0; return
assign?: ?=$.r.c; n=neighbors(); if ?==emp then do;if n==3 then ?=life!; end; else if n<2 | n>3 then ?=emp; @.r.c=?; return
assign@: @.=emp; do r=1 for rows; do c=1 for cols; call assign?; end; end; call assign$; return
halt: say; say 'REXX program halted.'; say; exit 0
neighbors: rm=r-1; rp=r+1; cm=c-1; cp=c+1; return $(rm,cm)+$(rm,c)+$(rm,cp)+$(r,cm)+$(r,cp)+$(rp,cm)+$(rp,c)+$(rp,cp)
p: return word(arg(1), 1)
pickChar: _=p(arg(1)); arg u .; if u=='BLANK' then _='" '"; L=length(_); if L==3 then _=d2c(_); if L==2 then _=x2c(_); return _
showRows: _=; do r=rows by -1 for rows; z=; do c=1 for cols; z=z||$.r.c; end; z=strip(z,'T',emp); say z; _=_||z; end; return</lang>
This REXX program makes use of &nbsp; '''LINESIZElinesize''' &nbsp; REXX program (or BIF) &nbsp; 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>
 
Line 7,590:
☼☼☼
 
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 0
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 0
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 1
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 1
 
☼☼☼
 
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 2
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 2
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 3
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 3
 
☼☼☼
 
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 4
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 4
 
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ "Life" repeated itself 2 times, simulation has ended. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
</pre>