Conway's Game of Life: Difference between revisions

Line 3,330:
 
=={{header|Elena}}==
ELENA 3.x2, using cellular library
<lang elena>#import system'routinesextensions.
#import system'threadingcellular.
#import extensionssystem'routines.
#import cellularsystem'threading.
import system'text.
 
const int maxX = 48.
const int maxY = 28.
 
#subjecttype listener_func = ListenerFunc.
 
limited dispatchable(listener_func) class ListenerFunc :: dispatchable<listener_func>
{
action eval &space:space []
}
 
Line 3,352 ⟶ 3,353:
bool started.
event(listener_func, event onUpdate) :: theListener.
constructor newRandomset &ruleset:transformSet
[
theSpace := IntMatrixSpace new &int:maxY &int:maxX &ruleset:randomSet.
theRuleSet := transformSet.
Line 3,363 ⟶ 3,364:
]
constructor newLoaded &ruleset:initSet &ruleset:transformSet
[
theSpace := IntMatrixSpace new &int:maxY &int:maxX &ruleset:initSet.
theRuleSet := transformSet.
Line 3,375 ⟶ 3,376:
[
if ($nil != theListener)
[ theListener eval &space:theSpace ].
]
Line 3,386 ⟶ 3,387:
[
if (started)
[ theSpace update &ruleset:theRuleSet. ];
[ started := true. ].
Line 3,395 ⟶ 3,396:
const int DELAY = 50.
 
symbol testSet = ((0,1,0),
class gameOfLifeRuleSet = BaseRuleSet
(0,1,0),
(0,1,0)).
 
class gameOfLifeRuleSet = BaseRuleSet::
{
validate &space:s &int:x &int:y
[
int cell := s getAt &int:x &int:y.
int number := s getLiveCell &int:x &int:y. // NOTE : number of living cells around the self includes the cell itself
if ((cell == 0) && (number == 3))
Line 3,411 ⟶ 3,416:
]
}.
 
extension space presenterOp
{
print
[
console setCursorPosition int:0 int:0.
int columns := self columns.
int rows := self rows.
int i := 0.
int j := 0.
while (i < rows)
[
j := 0.
while (j < columns)
[
int cell := self getAt int:i int:j.
console write((cell == 0)iif(" ","o")).
j := j + 1.
(&space:sp) [ sp print ].
i := i + 1.
console writeLine.
].
]
}
 
program =
Line 3,416 ⟶ 3,451:
console clear.
var model := Model newRandomset &ruleset:gameOfLifeRuleSet.
 
model onUpdate &listener_func(&space:sp)[ sp print ].
(&space:sp) [ sp print ].
 
until (console is &keyAvailableisKeyAvailable)
[
model run.
Anonymous user