The agrippa defense problem (java)


#1

Here is my code… but he keep telling me distance to target is none… i don’t get it…

loop {
var enemy = this.findNearestEnemy();
var distance = this.distanceTo(enemy);
if (distance < 5) {                    
this.isReady("cleave");
this.isReady("cleave");
this.cleave(enemy);           
}
else {
    this.attack(enemy);
}
}

#2

There probably is no enemy at the time.
You need an if (enemy) {}


#3

Oh… so where should i insert it?


#4

enemy =
if (enemy) { rest of stuff }


#5

im not fluently in Java, but isnt in the middle of the code:

if (distance < 5) {                    
this.isReady("cleave");
this.isReady("cleave");
this.cleave(enemy);           
}

shouldnt it be sth like:

if (distance < 5) **and** this.isReady("cleave"){ 
this.cleave(enemy);           
}

for me it seems like there is on “isReady” to much. Nevertheless there is no “if isReady”.
“and” in Java was sth like

if x && y

or?


#6

Makes a lot of sense.


#7

This is what your code should be

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 5 && enemy && this.isReady("cleave")) {                    
        this.cleave(enemy);           
    }
    else {
        this.attack(enemy);
    }
}

(I think)

This is what your code should be with the if (I did several other things)


#8

just as a tip, i dont know whether its important or not. u can save ressources if u first ask “if enemy” and then check the difference, because its the most important that an enemy is generally there…


#9

You actually can’t get the distance without an enemy being there, so @bmmtstb advice is not benefitial, but crucial for your code to work.


#10

I found this on another thread but let’s take a look at it.

    loop {
    var enemy = this.findNearestEnemy();
    if(enemy) {
        var distance = this.distanceTo(enemy);
        if (distance < 5) {
            if (this.isReady("cleave")) {
                this.cleave(enemy);
            }
            else {
                this.attack(enemy);
            }
        }
    }
}

So first we need to establish that we are looking for an enemy. So we set the variable and then set an if statement to look for an enemy.

Now that we’ve found our enemies we need to check the distance so that we cleave at the right time. But we need a distance variable to do that. The reason we can’t create the variable beforehand is because there are no enemies. So now that the if statement looking for enemies is fulfilled we can go ahead and start tracking their distance.

Once the distance between enemies is under 5 we are almost ready to cleave. But we don’t want to continuously try to trigger an ability with a cooldown. So we need to check to see it cleave is ready.

All the conditions are met and we then cleave.

Now we set the else statement.

Now for closing the braces. The end brace before the else statement closes the if (this.isReady) clause. The one right after the else clause closes the else clause. The one after that closes the if (distance) clause. After that the if (enemy) clause. And finally the loop.

Hope that helps!