0

I am building a model with anylogic. Here is the code:

victim = null;
for (People p : main.people){

    //když je dostatečně daleko
    if((distanceTo(p, METER)) < fightDistance){
            if( randomTrue( fightProbability ) && !p.equals(victim) ) { 
            victim = p; //set the victim
            break; //stop scan
            }
    }
}

The fighter is supposed to choose his victim with the probability of 20%. Two fighters can't share the same victim, which is not working. I need to make sure that the choosen people is not the victim already, though && !p.equals(victim) doesn't help...

T.Nosek
  • 65
  • 11
  • Have you overridden the equals(Object o) and hashCode methods in your `People` class? – MaxPower Jul 18 '17 at 19:56
  • `p.equals(victim)` will always return `false` since `victim` is null. You should use another approach. E.g. create a collection of victims and add some peopel to it – Ivan Pronin Jul 18 '17 at 19:56

2 Answers2

0

You need to override equals() method of People class. Inside equals() compare a unique field of People.

taner
  • 83
  • 5
0

I have solved the issue by creating a variable isChased in People class which is set as false. Then I implemented this into the chasing method of Fighter class:

victim = null;
for (People p : main.people){

    //když je dostatečně daleko
    if((distanceTo(p, METER)) < fightDistance){
            if( randomTrue( fightProbability ) && p.isChased != true ) { //random decision
            victim = p; //set the victim
            p.isChased = true;
            break; //stop scan
            }
    }
}`
T.Nosek
  • 65
  • 11