June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
137
Task/Wireworld/Elena/wireworld.elena
Normal file
137
Task/Wireworld/Elena/wireworld.elena
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import extensions.
|
||||
import cellular.
|
||||
import system'routines.
|
||||
import system'threading.
|
||||
import system'text.
|
||||
|
||||
const int maxX = 48.
|
||||
const int maxY = 28.
|
||||
|
||||
type listener_func = ListenerFunc.
|
||||
|
||||
limited class ListenerFunc
|
||||
{
|
||||
action eval space:space []
|
||||
}
|
||||
|
||||
sealed class Model
|
||||
{
|
||||
space theSpace.
|
||||
ruleset theRuleSet.
|
||||
bool started.
|
||||
|
||||
listener_func event onUpdate :: theListener.
|
||||
|
||||
constructor newRandomset ruleset:transformSet
|
||||
[
|
||||
theSpace := IntMatrixSpace new int:maxY int:maxX ruleset:randomSet.
|
||||
|
||||
theRuleSet := transformSet.
|
||||
|
||||
started := false.
|
||||
]
|
||||
|
||||
constructor newLoaded ruleset:initSet ruleset:transformSet
|
||||
[
|
||||
theSpace := IntMatrixSpace new int:maxY int:maxX ruleset:initSet.
|
||||
|
||||
theRuleSet := transformSet.
|
||||
|
||||
started := false.
|
||||
]
|
||||
|
||||
$onUpdate
|
||||
[
|
||||
if ($nil != theListener)
|
||||
[ 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
|
||||
[
|
||||
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.
|
||||
].
|
||||
|
||||
i := i + 1.
|
||||
console writeLine.
|
||||
].
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
console clear.
|
||||
|
||||
var model := Model newRandomset ruleset:gameOfLifeRuleSet.
|
||||
|
||||
model onUpdate listener_func(&space:sp)[ sp print ].
|
||||
|
||||
until (console isKeyAvailable)
|
||||
[
|
||||
model run.
|
||||
|
||||
thread sleep:DELAY.
|
||||
].
|
||||
|
||||
console readChar.
|
||||
].
|
||||
|
|
@ -1,44 +1,81 @@
|
|||
class Wireworld {
|
||||
has @.line;
|
||||
method height () { @!line.elems }
|
||||
has int $.width;
|
||||
|
||||
multi method new(@line) { self.new: :@line }
|
||||
multi method new($str ) { self.new: $str.lines }
|
||||
multi method new(@line) { samewith :@line, :width(max @line».chars) }
|
||||
multi method new($str ) { samewith $str.lines }
|
||||
|
||||
method gist { join "\n", @.line }
|
||||
method postcircumfix:<[ ]>($i) { @.line[$i].comb }
|
||||
|
||||
method neighbors($i where ^@.line, $j where ^$.line.pick.chars)
|
||||
method !neighbors($i where ^$.height, $j where ^$.width)
|
||||
{
|
||||
my @i = grep any(^@.line), $i «+« (-1, 0, 1);
|
||||
my @j = grep any(^@.line.pick.chars), $j «+« (-1, 0, 1);
|
||||
gather for @i X @j -> \i, \j {
|
||||
my @i = grep ^$.height, $i «+« (-1, 0, 1);
|
||||
my @j = grep ^$.width, $j «+« (-1, 0, 1);
|
||||
gather for @i X @j -> (\i, \j) {
|
||||
next if [ i, j ] ~~ [ $i, $j ];
|
||||
take self[i][j];
|
||||
take @!line[i].comb[j];
|
||||
}
|
||||
}
|
||||
method succ {
|
||||
my $succ = self.new: '' xx @.line;
|
||||
for ^@.line X ^@.line.pick.chars -> $i, $j {
|
||||
$succ.line[$i] ~=
|
||||
do given self[$i][$j] {
|
||||
my @succ;
|
||||
for ^$.height X ^$.width -> ($i, $j) {
|
||||
@succ[$i] ~=
|
||||
do given @!line[$i].comb[$j] {
|
||||
when 'H' { 't' }
|
||||
when 't' { '.' }
|
||||
when '.' {
|
||||
grep('H', self.neighbors($i, $j)) == 1|2 ?? 'H' !! '.'
|
||||
grep('H', self!neighbors($i, $j)) == 1|2 ?? 'H' !! '.'
|
||||
}
|
||||
default { ' ' }
|
||||
}
|
||||
}
|
||||
return $succ;
|
||||
return self.new: @succ;
|
||||
}
|
||||
}
|
||||
|
||||
my $str =
|
||||
"tH.........
|
||||
. .
|
||||
...
|
||||
. .
|
||||
Ht.. ......";
|
||||
my %*SUB-MAIN-OPTS;
|
||||
%*SUB-MAIN-OPTS<named-anywhere> = True;
|
||||
|
||||
my Wireworld $world .= new: $str;
|
||||
say $world++ for ^3;
|
||||
multi sub MAIN (
|
||||
IO() $filename,
|
||||
Numeric:D :$interval = 1/4,
|
||||
Bool :$stop-on-repeat,
|
||||
) {
|
||||
run-loop :$interval, :$stop-on-repeat, Wireworld.new: $filename.slurp;
|
||||
}
|
||||
|
||||
#| run a built-in example
|
||||
multi sub MAIN (
|
||||
Numeric:D :$interval = 1/4,
|
||||
Bool :$stop-on-repeat,
|
||||
) {
|
||||