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.
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:
pugs> my Grid $grid .= new( :diameter(10));
pugs> say $grid.state;
1000000000
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.

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: , ,






<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]