# Heal allied soldiers to survive the siege.
while True:
if hero.canCast("regen"):
bernardDistance = hero.distanceTo("Bernard")
if bernardDistance < 10:
# "Bernard" needs regeneration!
hero.cast("regen", "Bernard")
# Use "if" and "distanceTo" to regenerate "Chandra"
# if she is closer than 10 meters away.
if hero.canCast("regen"):
chandraDistance = hero.distanceTo("Chandra")
if chandraDistance < 10:
hero.cast("regen","Chandra")
else:
# If you aren't casting "regen", use "if" and "distanceTo"
# to attack enemies that are closer than hero.attackRange.
enemy = hero.findNearestEnemy()
enemyDistance = hero.distanceTo(enemy)
if enemy:
if enemyDistance <10:
hero.attack(enemy)
passste code here
Hello and welcome back to codecombat discourse @BlackRabbit! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
@cheddarcheese is correct that if you remove those lines your code will work, but the actual error lies in the fact that you’re defining enemyDistance, without knowing whether the enemy exists. You simply move that line to another location where it will only run when you know an enemy exists.
Instead of 10 as the distance you can use: hero.attackRange(), that’s what’s recommended in the hints and it will find the range of your wand.
Danny
@Deadpool198 : I despair when I see a code where “Strings” and all its derivatives are attacked , cast, taken distance to…
The above code will fail if Bernard or Chandra are killed. There is very good post how to find a minion by id (Name): Easiest way to select the enemy hero
You can use this method without advanced glasses or books. Simply remember the pattern. You can find this way id, type, team of the unit(s).
So for me the beginning of code will look like this:
while True:
friends = hero.findFriends()
Bernard = [friend for friend in friends if friend.id in ["Bernard"]][0]
Chandra = [friend for friend in friends if friend.id in ["Chandra"]][0]
if hero.canCast("regen"):
if Bernard:
bernardDistance = hero.distanceTo(Bernard)
if bernardDistance < 10:
hero.cast("regen", Bernard)
if Chandra:
chandradDistance = hero.distanceTo(Chandra)
if hero.distanceTo(Chandra) < 10:
hero.cast("regen", Chandra)
p.s.
the level is designed to pass even you completely comment the else clause…
but it’s really important to use properly if enemy, if friend clause and learn hero.attackRange()
Thanks for the offer to help, you are right it is bonemender but I don’t think it explains it well enough about hero.attackRange before you go into this puzzel. It will be the first time encountering hero.attackrange and I really think they should have used it as an example somewhere in some interactive code rather than making it so you have to guess what would work. Which is difficult if you have not seen it working context before.
Yep that would be in the Comments not the Hints… but if you never used hero.attackRange or seen how its used how would you know how it works. How do you know if you need to define it? I know where it gives a value for it. I don’t want to give any spoliers unless that is part of the learning process. But from the comments and the Hints how would you know the value , I’m not sure if im explaining it well enough but it is a variable that can change so should not be referred to as a given value when learning and not having used it before. I’m just trying to help for other people learning in that maybe it can be described better in the hints or comments. I have done the puzzel now so hopefully if someone does get stuck these pointers may help them get ALOT closer than I first did.
I’ve seen awful examples of 200 lines of code in the Clash of Clones where the hero attacks “Enemy 1”, “Enemy 2”…, “Enemy N”. Even in the Glacier this error is common. This way of coding is so perplexing…
Yes, you’re right. It’s probably important to stop that habit now, before you move on to harder levels. It really isn’t a good tactic.
Yes @BlackRabbit you’re also right, it hasn’t explained hero.attackRange() that much.