Birth in predator/prey

You'll find code that creates a new Agent in each class's act() where the instance tries to reproduce. If you compare all three species, you'll find that there are three parts that they have in common:

You'll need to do these same three things whenever you create a new Agent. If you leave out the call to enterGrid(), you'll get a null pointer exception when the new agent tries to act(), because it won't have either a grid or a location in one. If you leave out the call to setAgentAt(), your new agent will never be asked to act.

Note also that if you want one agent to be moved by another agent (as when, for example, a hawk might pick up a rabbit and drop it somewhere else), you'll need to call the moved Agent's enterGrid() to tell it where it has landed.


Cary Gray
Last modified: Tue Mar 15 10:05:06 CDT 2011