Dahlia
<< MazeBug | APQuestionsTrailIndex | Bee >>
Dahlia
If you don't have this question handy, you can download the pdf document DahliaQuestion.pdf
I made some 48 x 48 gif files which can be downloaded and placed in the same folder as your class files, and you can see the flowers and seed as they grow!


import java.awt.Color;
import info.gridworld.grid.*;
import info.gridworld.actor.*;
import java.util.ArrayList;
class Dahlia extends Flower
{
int age;
Color color;
public Dahlia()
{
super();
age=0;
color=Color.PINK;
}
public Dahlia(Color c)
{
super(c);
age=0;
color=c;
}
public void act()
{
age++;
if (age < 4)
{
super.act();
}
else
{
Grid<Actor> gr = getGrid();
ArrayList<Location> locs =
gr.getValidAdjacentLocations(getLocation());
int n = locs.size();
if (n > 0)
{
for (int k = 1; k <= 3; k++)
{
int r = (int)(Math.random() * n);
Location loc = locs.get(r);
if (gr.get(loc) == null)
{
DahliaSeed seed = new DahliaSeed(color);
seed.putSelfInGrid(gr, loc);
}
}
}
removeSelfFromGrid();
}
}
}
DahliaSeed
import java.awt.Color;
import info.gridworld.grid.*;
import info.gridworld.actor.*;
class DahliaSeed extends Flower
{
int age;
Color color;
public DahliaSeed(Color c)
{
super(c);
age=0;
color=c;
}
public void act()
{
age++;
if(age>2){
Grid<Actor> gr = getGrid();
Dahlia baby=new Dahlia(color);
Location loc=getLocation();
removeSelfFromGrid();
baby.putSelfInGrid(gr, loc);
}
}
}
DahliaRunner
import info.gridworld.grid.*;
import info.gridworld.actor.*;
import java.awt.Color;
public class DahliaRunner
{
// instance variables - replace the example below with your own
public static void main (String[] args)
{
ActorWorld world = new ActorWorld(new BoundedGrid(8,8) );
Dahlia d1=new Dahlia();
Dahlia d2=new Dahlia(Color.BLUE);
world.add(new Location (2,2), d1);
world.add(new Location (5,3), d2);
world.show();
}
}
