More life forms
The dramatic highlight of our world, if you watch for a bit, is when two
critters bounce off each other. Can you think of another interesting form
of behavior?
The one I came up with is a critter that moves along walls. Conceptually,
the critter keeps its left hand (paw, tentacle, whatever) to the wall
and follows along. This turns out to be not entirely trivial to implement.
We need to be able to “compute” with compass directions. Since directions
are modeled by a set of strings, we need to define our own operation
(dirPlus) to calculate relative directions. So dirPlus("n", 1) means one 45-
degree turn clockwise from north, giving "ne". Similarly, dirPlus("s", -2)
means 90 degrees counterclockwise from south, which is east.
f u n c t i o n d i r P l u s ( dir , n ) {
var index = d i r e c t i o n N a m e s . i n d e x O f ( dir ) ;
r e t u r n d i r e c t i o n N a m e s [( index + n + 8) % 8];
}
f u n c t i o n W a l l F o l l o w e r () {
this . dir = " s ";
}
W a l l F o l l o w e r . p r o t o t y p e . act = f u n c t i o n ( view ) {
var start = this . dir ;
if ( view . look ( d i r P l u s ( this . dir , -3) ) != " ")
start = this . dir = d i r P l u s ( this . dir , -2) ;
while ( view . look ( this . dir ) != " ") {
this . dir = d i r P l u s ( this . dir , 1) ;
if ( this . dir == start ) break ;
}
r e t u r n { type : " move " , d i r e c t i o n : this . dir };
};
The act method only has to “scan” the critter’s surroundings, starting
from its left side and going clockwise until it finds an empty square. It
then moves in the direction of that empty square.
What complicates things is that a critter may end up in the middle of
empty space, either as its start position or as a result of walking around
another critter. If we apply the approach I just described in empty space,