Having trouble with Agrippa defense

This worked for me :smile:

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        distance = self.distanceTo(enemy)
        if distance < 5:
            self.attack(enemy)
            self.attack(enemy)
            self.attack(enemy)
            self.cleave(enemy)
        else:
            self.attack(enemy)
            self.attack(enemy)
            self.attack(enemy)
            self.attack(enemy)
            self.attack(enemy)
            self.attack(enemy)
1 Like

Remember all the spaces

loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if self.isReady(“cleave”):
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)

1 Like

loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if distance < 5 and self.isReady(“cleave”):
self.cleave(enemy)
else:
self.attack(enemy)

This code worked best for me. I can’t figure out how to indent it…
If there is an enemy… they are less than 5 from you… and you are ready to cleave… do so
Or else just attack the enemy. Each time your hero attacks the code will loop and check if the cleave is ready again. I hope that helps.

1 Like

For anyone saying “I can’t figure out how to indent it”:

Read the FAQ! It’s explained there. Put three backticks (```) before and after your code.

1 Like

Thank You for this JS answer. And don’t say your a beginner, more like a novice because I am a beginner and could not figure this one out :). I think the problem is that this is a “brick wall” in ratio to the other levels leading up to this one. The distanceTo mixed with the already known isReady can be too much at once. It would be nice to have a simpler distanceTo test and/or more clarification that the isReady(cleave) has to be used again also(!).

Thanks again for this post. at least I know something works.

2 Likes

Look i have been making this for a long time and cant seem to get it working. The only thing he does is stand there and shield

loop:
if enemy:
enemy = self.findNearestEnemy()
distance = self.distanceTo(enemy)
if self.distanceTo(enemy) < 10:
if self.isReady(“cleave”):
self.cleave(enemy)
else:
self.attack(enemy)
else:
self.shield()

1 Like

nevermind figured it out

1 Like

Hello, LumpyImp, and welcome. For future reference, please read the FAQ on how to format your code.

Please do not post correct code on the forums, as it gives players the answer without them learning the required material.

1 Like

ChronistGilver sorry i didnt know

1 Like

Hi Guys, i have another problem:
Syntax error on line 1, column 1: unexpected ‘d’ (\u0064)
1 : plan = () ->
^ : ^
2 :
3 : loop:
4 : enemy = self.findNearestEnemy()

loop:
    enemys = @findNearestEnemy()
    if enemys
        if @distanceTo(enemys) < 15
            if @isReady "cleave"
                @cleave enemys
            else
                @attack enemys

1 Like

Try deleting the colon after the loop on line 1; Python uses colons after keywords like that, but CoffeeScript doesn’t.

1 Like

True :wink: Tnx, this a little confusing, because I chose coffee on the beginning.

1 Like

I also think that this level is broken with clojure.

Here is my code:

(dotimes [n 1000]
    (def enemy (.findNearestEnemy this))
    (if enemy
        (do
            ;; Find the distance to the enemy with distanceTo.
            (def distance (.distanceTo this enemy))
            ;; If the distance is less than 5 meters...
            (if (< distance 5)
                ;; ... if "cleave" is ready, cleave!
                (if (.isReady this "cleave")
                    (.cleave this enemy)
                    ;; ... else, just attack.
                    (.attack this enemy)
                )
            )
        )
    )
)

I really think that this should work but my character just stands there.

1 Like

Two issues, neither are really “yours”…

  1. “do” is broken, so we have to use “(when” to get multi-line clauses…
;; if we need both the "true" and "false" clauses then we use "when true" instead of "do"
(if enemy
  (when true
    ;; multi-line clause
  )
  (single line clause)  ;; or "(when true" again for another multiline clause

or in your case, since you have no else-clause use “when” instead of “if do”

(when enemy

    ;; Find the ........

  1. coco-closure has no fancy protected “loop” command like has been added to the other languages. So “doTimes [n infinity]” runs without breaks and is over before the mobs even get on the screen…

To slow it down you have to perform some action every time around.
In this case the simpliest way is to add a moveXY as the last command of the doTimes

(doTimes [n 1000]

    ;; all your code here

    (.moveXY this 39 32)
)

“39 32” is your hero’s start position, this causes you to move to the spot you are already at (unless you moved to “attack”), stopping the code from racing off into infinity (and beyond).

Less simply, but slightly better would be to add the moveXY to every clause that doesn’t contain an action (in this case that would be as the false-clause of every if that only has a true-clause… (the simple way always moveXYs even if it is not needed since another action (cleave/attack/etc) was performed.)

or for later levels, where you want to move away from your start spot:

(.moveXY this (.x (.pos this)) (.y (.pos this)))

will move you to where you currently are.

2 Likes

loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)

This is my code and I dont understand why its not working I have everything indented but she doesnt cleave the second group

1 Like

As you can read in the FAQ, you can format your code correctly in posts by placing three backticks (`) before and after your code. Or, you can highlight it, then click the “</>” button.

You can’t cleave every enemy because the “cleave” command has a specific cooldown of ten seconds. Instead of attacking if the distance is more than five meters, try checking to see whether you are ready to cleave; if you aren’t, then attack, as it says in the comments in the code when the level first starts:

    # Find the distance to the enemy with distanceTo.
    
    # If the distance is less than 5 meters...
    
        # ... if "cleave" is ready, cleave!
        
        # ... else, just attack.
3 Likes

This is my code and it works, my hero has 53 left after 3 groups of people.
[redacted, we don’t post correct code]

1 Like

For Anyone who is having trouble defeating the Second and third group of munchkins/ogres (blue people) follow this

[redacted, we don’t post correct code, especially non-formatted correct code]

1 Like

Wow, I have really convoluted code for this particular level.
I haven’t looked at it in a long time.
I have a habit with my cleaves of making a variable storing how many enemies are within 5m and not launching my cleave if it is less than 3 or 4.

I did not do that on this level.
I also avoided declaring variables it looks like.

Example:

else if (!this.isReady("cleave") && (this.findNearestEnemy()) && this.distanceTo(this.findNearestEnemy()) < 5 ){
        this.attack(this.findNearestEnemy());
    }

How strange, also I forgot that I could use the not (!) and have it parse correctly to find a false condition.

1 Like

thanks! :smile: i didnt know what to do :blush:

1 Like