Wireworld: Difference between revisions

Content added Content deleted
No edit summary
Line 1,282: Line 1,282:
.. </pre>
.. </pre>
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.2.1, using cellular library
ELENA 3.4, using cellular library
<lang elena>import extensions.
<lang elena>import system'routines.
import extensions.
import cellular.
import cellular.
import system'routines.
import system'threading.
import system'text.


const int maxX = 48.
const literal sample =
" tH......
const int maxY = 28.
. ......
...Ht... .
....
. .....
....
......tH .
. ......
...Ht...".


const literal conductorLabel = ".".
type listener_func = ListenerFunc.
const literal headLabel = "H".
const literal tailLabel = "t".
const literal emptyLabel = " ".


const int empty = 0.
limited class ListenerFunc
const int conductor = 1.
{
const int electronHead = 2.
action eval space:space []
const int electronTail = 3.
}


wireWorldRuleSet = RuleSet::
sealed class Model
{
{
proceed(Space s, int x, int y, ref<int> retVal)
space theSpace.
ruleset theRuleSet.
bool started.
listener_func event onUpdate :: theListener.
constructor newRandomset ruleset:transformSet
[
[
int cell := s getAt(x, y).
theSpace := IntMatrixSpace new int:maxY int:maxX ruleset:randomSet.
theRuleSet := transformSet.
started := false.
cell =>
conductor
[
int number := s getLiveCell(x, y, electronHead).
if ((number == 1)||(number == 2))
[
retVal value := electronHead
];
[
retVal value := conductor
]
];
electronHead
[
retVal value := electronTail
];
electronTail
[
retVal value := conductor
];
![
retVal value := cell
].
]
]
}.

sealed class Model
{
T<Space> theSpace.
constructor newLoaded ruleset:initSet ruleset:transformSet
constructor load(LiteralValue stateString,int maxX, int maxY)
[
[
var strings := stateString split(newLine); selectBy(:s)(s toArray); toArray.
theSpace := IntMatrixSpace new int:maxY int:maxX ruleset:initSet.
theRuleSet := transformSet.
theSpace := IntMatrixSpace new(maxX, maxY, RuleSet::
started := false.
{
proceed(Space s, int x, int y, ref<int> retVal)
[
if (x < strings length)
[
var l := strings[x].
if (y < l length)
[
(l[y]) =>
conductorLabel [ retVal value := conductor ];
headLabel [ retVal value := electronHead ];
tailLabel [ retVal value := electronTail ];
emptyLabel [ retVal value := empty ].
];
[
retVal value := empty
]
];
[
retVal value := empty
]
]
}).
]
]
$onUpdate
run
[
[
if ($nil != theListener)
theSpace update(wireWorldRuleSet).
]
[ theListener eval space:theSpace ].
]
init
[
$self $onUpdate.
]
run
[
if (started)
[ theSpace update ruleset:theRuleSet. ];
[ started := true. ].
$self $onUpdate.
]
}

const int DELAY = 50.

symbol testSet = ((0,1,0),
(0,1,0),
(0,1,0)).

class gameOfLifeRuleSet = BaseRuleSet::
{
proceed space:s int:x int:y vint:retVal
[
int cell := s getAt int:x int:y.
int number := s getLiveCell int:x int:y int:1. // NOTE : number of living cells around the self includes the cell itself
if ((cell == 0) && (number == 3))
[
retVal int := 1
];
if ((cell == 1) && ((number == 4) || (number == 3)))
[
retVal int := 1
];
[
retVal int := 0
]
]
}.

extension space presenterOp
{
print
print
[
[
console setCursorPosition int:0 int:0.
int columns := theSpace columns.
int rows := theSpace rows.
int columns := self columns.
int rows := self rows.
int i := 0.
int i := 0.
Line 1,390: Line 1,393:
while (j < columns)
while (j < columns)
[
[
int cell := self getAt int:i int:j.
var label := emptyLabel.
int cell := theSpace getAt(i, j).
console write((cell == 0)iif(" ","o")).
cell =>
conductor [ label := conductorLabel ];
electronHead [ label := headLabel ];
electronTail [ label := tailLabel ].
console write(label).
j := j + 1.
j := j + 1.
Line 1,403: Line 1,412:
}
}


public program
program =
[
[
T<Model> model := Model load(sample,10,30).
console clear.
0 to:10 do(:i)
var model := Model newRandomset ruleset:gameOfLifeRuleSet.

model onUpdate listener_func(&space:sp)[ sp print ].

until (console isKeyAvailable)
[
[
console printLineFormatted("Iteration {0}",i).
model run.
model print; run.
thread sleep:DELAY.
]
]</lang>
].
console readChar.
].</lang>
{{out}}
{{out}}
<pre>
<pre>