PreQuiz 4 - Spring 2006
Below is the code for act() in the class PersonAgent, as part of the DiseaseSimulation.
/**
* How a Person acts
**/
public void act()
{
// Is there a person within infection range of me?
PersonAgent closePerson = (PersonAgent) getClosest(20,
simulation.getAgents());
if (closePerson != null) {
// If this person is infected, and I'm not infected
if (closePerson.infection && !this.infection) {
// I become infected
this.infect();
}
}
// Run the normal act() -- wander aimlessly
super.act();
}
Imagine that we want to change how people behave such that:
- If you become infected by someone within 10 steps of you, you become VERY sick and your speed drops by 1/2.
- If you become infected by someone within 20 steps of you, you become sick like normal.
Rewrite act() to implement these new rules.
Questions? Comments? Answers? Comments on Answers?
Well, it sorta depends on how we can access the person's speed, doesn't it?
I mean if we don't know that... I'm assuming that a person's speed is called 'speed' (as it is in PersonAgent).
/**
* How a Person acts
**/
public void act()
{
// Is there a person within infection range of me?
PersonAgent closePerson = (PersonAgent) getClosest(20,
simulation.getAgents());
if (closePerson != null) {
// If this person is infected, and I'm not infected
if (closePerson.infection && !this.infection) {
// I become infected
this.infect();
// If the closest person within 10 returns the same person, Extra Infect!
if( ((PersonAgent) getClosest(10,simulation.getAgents())).equals(closePerson)){
/****** if( (PersonAgent) getClosest(10,simulation.getAgents())==closePerson){ *********/
this.getSpeed()=this.setSpeed()/2;
}
}
}
// Run the normal act() -- wander aimlessly
super.act();
}
Yes? No?
~Jim?
I was thinking of something a lot simpler, without .equals. You can use getSpeed and setSpeed. Mark Guzdial |
Oh, ok. Well, I can do it with 2 closest person checks...
/**
* How a Person acts
**/
public void act()
{
// Is there a person within infection range of me?
PersonAgent closePerson = (PersonAgent) getClosest(10,simulation.getAgents());
if (closePerson != null) {
// If this person is infected, and I'm not infected
if (closePerson.infection && !this.infection) {
// I slow down and become infected...
this.setSpeed(this.getSpeed()/2);
this.infect();
}
}
else{
closePerson = (PersonAgent) getClosest(20,simulation.getAgents());
if (closePerson != null) {
// If this person is infected, and I'm not infected
if (closePerson.infection && !this.infection) {
// I become infected
this.infect();
}
}
}
// Run the normal act() -- wander aimlessly
super.act();
}
I suppose that would work too? Not sure what else you'd be looking for.
~Jim
The .equals looks a little easier to me....and shorter. It cuts out the need to redefine closePerson twice.
I agree. Actually, I'm wondering if you could use == for that one, since you want the same object. The 'content' isn't important (whatever it is). I think getting the memory location would work here.
Can I get a yes/no on that?
~Jim
Link to this Page