# Hushbaum has been ambushed by ogres!
# She is busy healing her soldiers, you should command them to fight!
# The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
# Soldiers spread out in a circle and defend.
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
hero.command(soldier, "defend", defendPos);
# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
# Only focus archers' fire if there is a big ogre.
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
while True:
# If archerTarget is dead or doesn't exist, find a new one.
if not archerTarget or archerTarget.health <= 0:
# Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
# Create a variable containing your archers.
archers = hero.findByType("archers")
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
I always have one soldier that dies. Is there something not right with my code that I could fix to prevent that.
@kiwiapple87 Yes I remember this challenge. You have done well with the code so far, as I did and ran into the same challenge.
I believe that we have properly identified the issue:
- Only one soldier dies each time.
So now the question is āhow do I protect my soldiers so that if they are weak they donāt get killedā ?
I will give you a hint. commandSoldier
is where we tell each soldier what to do, perhaps we can leverage this function and update it so that the injured soldiers protect themselves.
Right before the While True loop it is set to āNoneā
You need to set it before that.
Why do I need to set it before that? I have no problem with my archers shooting.
Oh ok. I have to do some developing things at the moment so I canāt help. Just wait a moment.
I have tried a bunch of different things to try and protect that one soldier but I canāt seem to get him to survive. I have been stuck on this level for over 2 weeks now.
Sometimes the best defense is a good retreat.
I barely remember this level but i will try to help.
I canāt get the retreat to work. I get the error saying that my code is taking to long to run or there is an infinite loop.
Also, I think I have figured out my issue but I have been unable to solve it. My archers donāt seem to be focusing on the ogreās. I have changed the commandArcher function to:
def findStrongestTarget():
bestTarget = None
enemies = hero.findEnemies()
# Figure out which enemy has the most health, and set bestTarget to be that enemy. or type ogre
for enemy in enemies:
if enemy.type == "ogre":
bestTarget = enemy
# Only focus archers' fire if there is a big ogre.
if bestTarget and bestTarget.health > 15:
return bestTarget
But even with changing it to go by type instead of health they still donāt focus on the ogreās. After watching this about 40 times I know if I could solve that I would get past this level. Any help would be greatly appreciated.
@kiwiapple87 the original code you had posted for the āfindStrongestTargetā looked ok, you might try reverting to it.
For the retreat code you can update the ācommandSoldierā code to check for the individual soldiers health. After you validate that the soldiers health is less than a certain number (near death) then command that soldier to retreat towards Hushbaum.
I think the original archer code detected the enemy with the greatest health and had the archers attack that target until it was vanquishedā¦ That should turn out to focus the attacks on the ogres.
Using either Health or Type in the findStrongestTarget function results in the same problem. The archers arenāt focusing on the ogreās until they are dead. Sometimes with will all focus on the ogre and then when they are almost dead switch to the nearest munchkin. Sometimes when a new ogre appears they wonāt all start shooting him right away. They will be shooting at the nearest munchkinās first then switch to him. And this is happening when I use either health detection or type detection. I am very confident that if I could get the archers to focus on the ogreās till death and focus on the ogreās immediately when they show up I would be able to pass this level. I just donāt understand why they arenāt focusing on them.
@kiwiapple87 Ah! you are right. The reason that your archers are not attacking the right target is because there is a bug in your code. It was one I glanced over myself.
Itās an easy fix that is the good news!
Have you used " console.log() " before to debug the code? or even hero.say() ?
Hint: start with ensuring all the functions are being called by including a console.log() or hero.say() statement in the beginning of each.
.
if you have to use hero.say() then only have one hero.say() statement active in the code per test run as it can cause delays in the āhero.command()ā statements. console.log() is more efficient in showing what is going on, but you will have to know how and where to read the web browser log output.
One freaking letter . Thank you for the help .
Those are the kinds of bugs that keep people up at night.
1 Like