72 Replies - 3093 Views - Last Post: 13 June 2012 - 10:52 AM
#31
Re: Ant farm Program
Posted 07 June 2012 - 01:28 PM
#32
Re: Ant farm Program
Posted 07 June 2012 - 01:30 PM
#34
Re: Ant farm Program
Posted 07 June 2012 - 01:38 PM
ferguson32, on 07 June 2012 - 03:26 PM, said:
class AntBase implements IAntBase
{
private int strength;
private AntBase ant;
Random number = new Random();
public AntBase(AntBase ant)
{
this.ant = ant;
strength = number.nextInt(10);
}
If the ants are marching in a line, maybe.
What you have here is an ant that points to another ant somewhere. Each ant has an ant that it refers to. There's a very useful data structure that works this way, called a linked list. In a linked list, each element points to the next element, allowing you to scan a list one piece at a time - think of a daisy chain, where each link is tied to a next link. This is a very useful structure, as I say, but I don't see how it relates to this particular case.
As Ryan says,
Quote
What use would this serve? Each ant has certain characteristics. One of them, it seems, is "strength". What purpose would this.ant serve in your program?
This post has been edited by jon.kiparsky: 07 June 2012 - 01:38 PM
#35
Re: Ant farm Program
Posted 07 June 2012 - 01:42 PM
#36
Re: Ant farm Program
Posted 07 June 2012 - 01:43 PM
#37
Re: Ant farm Program
Posted 07 June 2012 - 01:45 PM
#38
Re: Ant farm Program
Posted 07 June 2012 - 01:46 PM
You assign a random number to the field (which you have already done), and that's it for now. There isn't any other fancy shenanigans that you need to include.
public AntBase()
{
strength = randomNumberGenerator.nextInt(10);
}
P.S - I think you should rename your AntBase class to just Ant (as it's meant to be). I reckon that may clear things up slightly.
This post has been edited by Ryano121: 07 June 2012 - 01:47 PM
#39
Re: Ant farm Program
Posted 07 June 2012 - 01:48 PM
Quote
...
Quote
*sigh* No.. it says to create an ant which is an object... and the object itself is created by it's own constructor.... nothing there says "the object's constructor should create another object instance inside the scope of the constructor".
Nothing.
#40
Re: Ant farm Program
Posted 07 June 2012 - 01:58 PM
#41
Re: Ant farm Program
Posted 07 June 2012 - 02:05 PM
public IAntBase breed(IAntBase mate)
So basically what want to do is get the strength of the mate, and the strength of the current ant, find the largest one and create a new Ant with that strength.
int mateStrength = mate.getStrength(); // get the strength of the mate int myStrength = this.getStrength(); // get my strength // now work out the largest number or average the two
Once we have the new strength that the baby ant will have, we can create a new instance an return it.
If you don't feel like making a couple of if statements to get the greatest number, you can use the Math.max(int a, int b ) method, which will return the greater of the two parameters.
As for the fight method, the concept is much the same
boolean fight(IAntBase competitor)
We get the strength of the competitor and strength of the current ant -
int competitorStrength = competitor.getStrength(); // get the strength of the competitor int myStrength = this.getStrength(); // get my strength
Then if the competitors strength is greater than my strength, he has killed me so we return false. Otherwise if we have a greater strength than the competitor then we kill that other puny ant - so we return true.
Have a go at them and ask back here with your problems.
This post has been edited by Ryano121: 07 June 2012 - 02:12 PM
#42
Re: Ant farm Program
Posted 07 June 2012 - 02:06 PM
#43
Re: Ant farm Program
Posted 07 June 2012 - 07:17 PM
Laser spider goes pew pew pew!
This post has been edited by atraub: 08 June 2012 - 08:02 AM
#44
Re: Ant farm Program
Posted 08 June 2012 - 05:10 PM
class AntBase implements IAntBase
{
int Strength
public AntBase()
{
Strength = Random.nextInt(10);
}
public IAntBase breed(IAntBase mate)
{
}
public boolean fight(IAntBase enemy)
{
}
public int getStrength()
{
return Strength;
}
}
as you can see before the constructor i have added a variable (int Strength) and in the constructor i have put "Strength = Random.nextInt(10)" this will assign the variable Strength with a random number from 0-10 you can change the 10 to whatever number you want and that will become the maximum number when using random.
Sothatsit, on 08 June 2012 - 05:09 PM, said:
class AntBase implements IAntBase
{
int Strength
public AntBase()
{
Strength = Random.nextInt(10);
}
public IAntBase breed(IAntBase mate)
{
}
public boolean fight(IAntBase enemy)
{
}
public int getStrength()
{
return Strength;
}
}
as you can see before the constructor i have added a variable (int Strength) and in the constructor i have put "Strength = Random.nextInt(10)" this will assign the variable Strength with a random number from 0-10 you can change the 10 to whatever number you want and that will become the maximum number when using random.
sorry im new didn't know there where extra pages
|
|

New Topic/Question
Reply




MultiQuote








|