So I have a rather simple piece of code:
Soldier Horseman = new Soldier("Horseman",Archer, 20);
Soldier Spearman = new Soldier("Spearman",Horseman, 10);
Soldier Archer = new Soldier("Archer",Spearman, 10);
Where the constructor for soldier takes the arguments
Soldier(String name, Soldier target, double range)
The target is then used to calculate distance between the two in a method.
public double DistanceCalculation() {
distanceToTarget = ((Math.pow((soldierPosition[0] - soldierTarget.soldierPosition[0]), 2)) + Math.pow((soldierPosition[1] - soldierTarget.soldierPosition[1]), 2));
return distanceToTarget;
However, when I try to create this code, the top most soldier cannot be created because its target doesnt exist yet. I tried using String instead of Soldier in the constructor, but then I cannot figure out how to convert string into Soldier so that the SoldierTarget.soldierPosition works. Any ideas?
SoldierlikesetTarget(Soldier target)that targets a particular Soldier object and removetargetfrom the constructor? Seems your design might need a little revision.targeta different type then the soldier itself.