# Having trouble with Agrippa defense

This worked for me

``````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 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:
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
@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 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!
(.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]

(.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! i didnt know what to do

1 Like