Friday, April 20, 2007
Deus Ex Automatis, Part V(b)
We've already got code for rules from last post. Now we need a grid.
Fairly simple, takes one argument: diameter. Building a new grid is done like so:
Now that we've got a rule, and got a grid, we can put them together into a cellular automaton, and discover some more syntactic (and semantic) sugar from Perl 6.
class Grid {
has Int $.diameter is rw;
has Bool @.state is rw;
submethod BUILD (:$.diameter) {
@.state = 1,;
@.state.push( 0 xx ($.diameter-1) );
}
}
Fairly simple, takes one argument: diameter. Building a new grid is done like so:
Once again, we've taken some shortcuts. The grid code assumes a single dimension, with a second, emergent dimension (often called "time") that happens when you apply the rule.pugs> my Grid $grid .= new( :diameter(10));
pugs> say $grid.state;
1000000000
Now that we've got a rule, and got a grid, we can put them together into a cellular automaton, and discover some more syntactic (and semantic) sugar from Perl 6.
Labels: cellular automata, deusexautomatis, etech
Subscribe to Posts [Atom]