Conway's Game of Life: Difference between revisions

Content added Content deleted
(→‎{{header|Lambdatalk}}: adding lambdatalk task)
Line 7,128: Line 7,128:
{ „tcol” }
{ „tcol” }
{ „x” } { „y” } { „n” }
{ „x” } { „y” } { „n” }

</syntaxhighlight>

=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude args.uh
###sysinclude str.uh
###sysinclude system.uh
// Life simulator (game).
// It is a 'cellular automaton', and was invented by Cambridge mathematician John Conway.

// The Rules

// For a space that is 'populated':
// Each cell with one or no neighbors dies, as if by solitude.
// Each cell with four or more neighbors dies, as if by overpopulation.
// Each cell with two or three neighbors survives.
// For a space that is 'empty' or 'unpopulated'
// Each cell with three neighbors becomes populated.
// --------------------------------------------------------------------------------------------
#g
// Get the terminal-resolution:
terminallines -- sto tlin
terminalcolumns sto tcol
// .............................
// Verify the commandline parameters:
argc 3 < { #s ."Usage: " 0 argv print SPACE 1 argv print ." lifeshape-file.txt\n" end }
2 argv 'f inv istrue { #s ."The given file ( " 2 argv print ." ) doesn't exist!\n" end }
2 argv filetolist sto startingshape // read the file into the list
[[startingshape]]~~~ sto maxlinlen
@tlin @tcol [[mem]] sto neighbour // Generate the stringarray for the neighbour-calculations
@tlin @tcol [[mem]] sto livingspace // Generate the stringarray for the actual generations
@tlin @tcol [[mem]]! sto cellscreen // Generate the stringarray for the visible livingspace
// Calculate offset for the shape ( it must be put to the centre):
@tlin startingshape~ - 2 / sto originlin
@tcol @maxlinlen - 2 / sto origincol

startingshape~ shaperead: {{ {{}} [[startingshape]]~ {{ {{}}§shaperead {{}} [[startingshape]][]
32 > { 1 }{ 0 } sto emblem
@originlin {{}}§shaperead + @origincol {{}} + @emblem inv [[livingspace]][]
}}
}}
cursoroff
// ==================================================================
{.. // infinite loop starts
sbr §renderingsbr
topleft @cellscreen ![[print]]
."Generation: " {..} print fflush // print the number of the generations.
0 [[neighbour]][^] // fill up the neighbour list with zero value
// Calculate neighbourhoods
@tlin linloop: {{ // loop for every lines
@tcol {{ // loop for every columns
{{}}§linloop {{}} sbr §neighbors
{{}}§linloop {{}} @nn inv [[neighbour]][] // store the neighbournumber
}} }}
// Now, kill everybody if the neighbors are less than 2 or more than 3:
@tlin linloop2: {{ // loop for every lines
@tcol {{ // loop for every columns
{{}}§linloop2 {{}} [[neighbour]][] 2 < then §kill
{{}}§linloop2 {{}} [[neighbour]][] 3 > then §kill
{{<}} // Continue the inner loop
kill: {{}}§linloop2 {{}} 0 inv [[livingspace]][]
}} }}
@tlin linloop3: {{ // loop for every lines
@tcol {{ // loop for every columns
// Generate the newborn cells:
{{}}§linloop3 {{}} [[neighbour]][] 3 == { {{}}§linloop3 {{}} 1 inv [[livingspace]][] }
}} }}
50000 inv sleep
..} // infinite loop ends
// ==================================================================
end
// ==========================================================
neighbors: // This subroutine calculates the quantity of neighborhood
sto xx sto yy zero nn
@yy ? @tlin -- @xx ? @tcol -- [[livingspace]][] sum nn // upleft corner
@yy @xx ? @tcol -- [[livingspace]][] sum nn // upmid corner
@yy ++? tlin @xx ? @tcol -- [[livingspace]][] sum nn // upright corner
@yy ? @tlin -- @xx [[livingspace]][] sum nn // midleft corner
@yy ++? tlin @xx [[livingspace]][] sum nn // midright corner
@yy ? @tlin -- @xx ++? tcol [[livingspace]][] sum nn // downleft corner
@yy @xx ++? tcol [[livingspace]][] sum nn // downmid corner
@yy ++? tlin @xx ++? tcol [[livingspace]][] sum nn // downright corner
rts
// ==========================================================
renderingsbr:
@tlin livingspaceloop: {{ @tcol {{
{{}}§livingspaceloop {{}}
{{}}§livingspaceloop {{}} [[livingspace]][] { '* }{ 32 } inv [[cellscreen]][]
}}
}}
rts

{ „tlin” }
{ „tcol” }
{ „startingshape” }
{ „maxlinlen” }
{ „livingspace” }
{ „neighbour” }
{ „originlin” }
{ „origincol” }
{ „emblem” }
{ „cellscreen” }
{ „xx” } { „yy” } { „nn” }


</syntaxhighlight>
</syntaxhighlight>