Hello Smart People,
I'm implementing an Ant Simulator (swarm search stuff) and I'd like to be able to load different "species" of Ants (.java classes that implement an Ant interface). I've been having trouble creating "num" of a certain species and initializing their initial position.
If num = x, then all of the x ants created have the xth Ant's cooridinates. It's as if I add the same object x times as opposed to creating x new objects and adding them to my vector.
I've tried three seperate things that should work, however none of them do. In the second attempt, I directly create and add a certain species (my "Explorer Ant"), and even this doesn't work.
I get the feeling that it's a very basic problem, but I'm not seeing anything clearly right now. What do you guys think?
while (i<num)
{
try
{
// Used for all attempts
Dimension d = new Dimension(i, 100);
// Attempt Eins
Class c = Class.forName(species); //species is a string
Class[] intArgsClass = new Class[]{Dimension.class};
Object[] antArgs = {d};
Constructor antConstructor =c.getConstructor(intArgsClass);
Object object = null;
object = antConstructor.newInstance(antArgs);
Ant ant = (Ant) object;
newAnts.addElement(ant);
// Attempt Zwei
newAnts2.addElement(new Explorer(d));
// Attempt Drei
Ant o = (Ant) c.newInstance();
o.setLocation(d);
newAnts3.addElement(o);
}
catch (All Kinds of Crap e)
{
System.out.println(e);
}
i++;
}
much love.
g.