Agrippa Defense (Javascript)


#1

Anya just sits there and doesn’t do anything…

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);
}
}

Help would be appreciated!


#2

Help us, Help you. (read the FAQ and allow your code to display properly (use the triple backtick))

What I can tell from what you have there is that there are more open curly braces than close… 5 { but only 3 } other than that it looks good as far as I can tell. I’ll have to go play the level to be sure. – Yup, works for me with the +2 }'s. My hero cleaves the first group, whacks the second, and cleaves the third. (I copied and pasted what is above (so no pretty formatting) and just added the two '}'s to the end.)


#3

Im also having issues with my code. Could someone help me? It says that the target is null.

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

please I want to understand the problem :frowning:


#4

First of all Beaute : use proper indentation to provide code for us to help you. See FAQ Section 1 paragraph 2. On the post editor, you can use the </> button also in order to change the code you posted into (the same but more readable).

The main problem to your code is that you don’t have a standard way to write statement. It’s important for readability, but also it helps A LOT to find errors in your code if it has a proper indentation and braces.

A standard pattern to write if statement is the following :

if ( condition1 ) {
    statement1 ; 
    statement2 ;
} else {
    statement 3 ;
    statement 4 ;
}

Please, consider reading the box above aloud like this : if space parenthesis space condition1 space closedparenthesis space openbrace newline tab statement1 space semicolon newline tab statement2 space semicolon etc… And try to apply this exact pattern to your code. This is important in debugging a bugged code.

Specific issues other than formating :
1)

if (this.isReady("cleave"));

There is a ; instead of a {

end;

isn’t JS. Replace with }

  1. The else part refers to the wrong if. You want to attack if cleave isn’t ready, not if distance >=5.

Your code worked for me after that. Good luck !


#5

I’m having an issue with my code on this level… help would be appreciated!

loop {
var enemy = this.findNearestEnemy();
if(enemy) {
var distance = this.distanceTo(enemy);
if (distance < 5) {
if (this.isReady(“cleave”)) {
this.cleave(enemy);
}

    }
else {
        this.moveXY(38, 21);
        this.attack(enemy);
        this.attack(enemy);
    }

}
}

Thank you!


#6

Hello, Pevensie, and welcome. Please format your code as per the FAQ.

Unfortunately, it is impossible to conduct a full examination of your code unless it is formatted properly, but you do not appear to attack an enemy unless the distance is greater than 5.


#7

Hello!

So I also faced this issue multiple times with some of my coding (I have to finish all the levels for a college course, seems easy, right? Not quite!
What I always seem to be seems to be what your’e doing as well, and I’m posting this for future users as well.
After you finish your “if” variable, DON’T close the loop (I’ve done this on 2 levels and had to beat my head against the wall to see the error).
There should only be ONE bracket closing your “if” variable, so it leaves the “else” variable open to continue the loop.

@Eric_Conklin

This is what I kept doing, look at your bracket placements and try to adjust from there.
Hope this helps some!


#8

why?


#9

//Made by sanlega, i’ve made it with Java
while(true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
var distance = hero.distanceTo(enemy);
var ready = hero.isReady(“cleave”);
if (distance < 5) {
if (ready) {
hero.cleave(enemy);
}
else {
hero.attack(enemy);
}
}
}
}


#10

Please format your code, you can highlight it and press the “< / >” button in the comment section. Or add two backticks before and after your code. It helps a lot