Archive for February, 2008

Assignment 5

February 18, 2008

I’m using the state pattern to control all the behaviors of my animates: predators and prey.  This requires them to change their behaviors in response to events in their environments or actions performed by other organisms.   The states that will apply to both predators and prey are healthy, pregnant, starved, and dead.  A state that will apply only to prey will be frightened.    A state that will apply only to predators will be enraged.

Healthy will be the default state, where everything runs as normal.  Pregnant will occur when an organism has an excess supply of energy.  After a gestation period, a pregnant organism will reproduce.  Starved will cause an organism to use all of its resources in searching for food.  Dead will provide a way to remove organisms from the simulation by having them perform no actions until they can be deleted.  Frightened will allow a prey organism to move faster for a short period of time at the expense of efficient use of its energy.  Enraged will allow a predator to run faster for a short period of time to catch an evasive prey.

 At the moment, I only have one method and two states for prey: Healthy and Dead.   Predator also has a Starved state.   I’ll add more this coming week – this pattern is great for my simulation.

To implement the State Pattern, you must first create a state interface that contains methods that will map to actions that could happen to the object that will implement the interface.  You need setter methods to set the states of objects in response to some event in the simulation.   In my simulation, the event is declining energy levels.   This is very similar to the Strategy Pattern, but it’s more directed towards specific goals.

Assignment 4

February 10, 2008

I decided to use the decorator pattern to increase the speed of the prey organism every time it is captured.  This is done by using the abstract class “SpeedDecorator”, which is extended by the classes IncreaseSpeed and DecreaseSpeed.  I would like to be able to use these decorators on both predators and prey, but making the class SpeedDecorator extend organism didn’t allow me to apply them to specific instantiations of predators or prey.  For that reason I made SpeedDecorator extend prey.  If there is a way, please let me know.

I think I will end up using the decorator class for many changes of the organisms.  Since I want them to evolve, I will need an effective ways of changing their parameters at runtime.  The current program uses the observer pattern to make the predator chase the prey and the prey run from the predator.  After the predator catches the prey, I use the speed decorator to increase the speed of the next instantiation of the prey.  Eventually the prey becomes too fast for the predator to catch.

To use the decorator pattern, start with an abstract class.  This is the abstract component and the methods are abstract.  Then make a concrete class that extends this component with concrete methods.  This is the class that is going to be decorated, which means we will add new behavior to it with decorators.  The decorator class itself will extend the component and is also an abstract class.  This class must have an instance variable that holds a reference to the component it extends.  Then we must create concrete decorators that extend the abstract decorator.  These must have an instance variable for the class that they decorate.  These decorators are what will extend the state of the component. 

Now we can use these decorators on our concrete components.  Since these concrete components are wrapped by decorators, the decorators can use the instance variables to get all the information from the objects they decorate and use their own methods to decorate it.

In my code, the decorators hold reference variables to prey objects and for that reason have access to the prey’s speed variables.  Then the decorators can add to the speed variable every time they are called.  I think I will eventually make it so that organisms in a healthy state get bonuses to their speed, vision, or defense.

Assignment 3

February 10, 2008

To implement the observer pattern you need to extend the observable class and implement the observer interface (you can use java’s or design your own).  The observers, obviously, observe the observables.  In my program I made organisms (which includes predators and prey) as both observers and observables, as they would be in real life.  Next, you need the observables to contain a method that notifies the observers based on some change.  My organism class notifies its observers based on position changes. 

 Now the observers need an update method that will respond to the information “pushed” by the notify observers method.    In my program the predator and the prey each have different reactions to being notified that their observable has changed position.  The predator chases the prey and the prey runs away.  If the predator is notified that the prey is within a certain distance, the predator calls feedBahavior and the prey’s state is set to dead – this represents the predator initiating the action of killing and eating the prey.

 Once the notifyObservers() and update() methods are correctly set up, all that remains is to add observers.  The easiest way to do this in my program was simply to base it on proximity – when two organisms are within a certain distance of each other (before this they move about randomly), they become mutual observers and observables.  At this point, the chase begins.  If they get far enough away, the chase ends.

At the moment the predator catches the prey over and over again each cycle.  I will have to change the parameters to make the prey faster or it will never be able to reproduce.