Having trouble with Agrippa defense

hey there)
I can’t figure out what’s wrong with my code. Help me, please

1 Like

Why is there a bracket right before the else? Just asking to see if that was your problem. I don’t know Javascript to be honest.

1 Like

Your else statement is associated with the outer if statement (your code says to attack the enemy when the distance is not smaller than 5 meters), when it should be associated with the inner if statement (your hero should attack the enemy when the distance is smaller than 5 meters and cleave is not ready).

1 Like

Thanks for your comment, but I still don’t uderstand what exactly I should do…I tried to fix it, but unsuccessful…

1 Like

Take a good look at the indentation levels, you should be able to spot the problem more easily. Your current code looks like this:

        if (distance < 5) {
            // ... some code here
        } else {
            hero.attack(enemy);
        }

This instructs the hero to attack the enemy when the distance is not smaller than 5, which is incorrect. Instead, the code should look like this:

        if (distance < 5) {
            if (hero.isReady('cleave')) {
                hero.cleave(enemy);
            } else {
                hero.attack(enemy);
            }
        }

Can you see the difference? Both cleave and attack instructions are inside the distance < 5 condition, they may only run when the enemy is close to your hero.

Also, note that although the curly braces ({}) are optional for blocks that contain only a single instruction, I recommend adding the curly brackets to all your if / else / for / do / while blocks in order to make your intention clear and keep the code style consistent. Your inner if statement does not have curly brackets.

1 Like

Thank you very much! Now it works clear.

1 Like

while True:
hero.wait(4)
enemy = hero.findNearestEnemy()
hero.cleave(enemy)
hero.findNearestEnemy()
hero.attack(enemy)
enemy = hero.findNearestEnemy()
hero.cleave(enemy)
enemy = hero.findNearestEnemy()
hero.attack(enemy)

what is wrong with my code my guy keeps dying
please help

1 Like

@Ayden_Johnson have you tried using an if/then/else code structure to arrange the different attacks?

If you just call hero.findNearestEnemy() by itself nothing will occur. The method/function returns a value of the enemy that is nearest. If you don’t capture this enemy in a variable, then you will loose the work done to find out which enemy was closest.

And if you have a wait of 4, or 4 seconds then there will be a lot of time between attacking. I would recommend using an if statement to detect if there was any enemy returned by hero.findNearestEnemy() or not.

1 Like

im haveing trouble with Agrippa defense A here is my code

while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
// Find the distance to the enemy with distanceTo.
var distance = hero.distanceTo(enemy);
// If the distance is less than 5 meters…
if (distace < 5) {
}
// … if “cleave” is ready, cleave!
if (“cleave”) {
} // … else, just attack.
else {
hero.attack(enemy);
}
}
}

1 Like

having trouble heres code

while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
// Find the distance to the enemy with distanceTo.
var distance = hero.distanceTo(enemy);
// If the distance is less than 5 meters…
if (distace < 5) {
}
// … if “cleave” is ready, cleave!
if (“cleave”) {
} // … else, just attack.
else {
hero.attack(enemy);
}
}
}

1 Like

I know this is late but I wanted to throw in my two cents.

The first Agrippa Defense for me is too easy. I could nest the code as you indicated without having to google this. The second Agrippa Defense, regarding refactored code, was not enough as it did not specify, nor hint as much, to code the right way. The third Agrippa Defense was based only on luck as I coded something that did make sense.

Honestly, your first Agrippa Defense is fine. The second one needs more hints to help ease the transition.

1 Like

uhhh well im on java script and mine dosennt work. why? Like he would go to the first group ( I don’t know how to make him stand in the middle) and then he would cleave the first group… but then on the second group. he goes over to them, and they kill him.

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Find the distance to the enemy with distanceTo.
        var distance = hero.distanceTo(enemy);
        // If the distance is less than 5 meters...
        if (distance < 5) {
            // ... if "cleave" is ready, cleave!
            var ready = hero.isReady("cleave");
            hero.cleave(enemy);
            
            // ... else, just attack.
            } else { hero.attack(enemy);
                
            }
    }
}

I see that you made the cleave check into a variable instead of an if statement. Switch that up and match the else attack with the if cleave.

var ready = hero.isReady("cleave") // make this an if statement

ok well how do I do that can u please show

You have to check whether you are ready to cleave. I think making a variable isn’t helpful. You know how to check whether something is ready:
if hero.isR…:
hero.c…
:lion:

k thank you that was really helpful