ive seen so many people not being able to solve this. let me explain my madness before I show you my code. im currently trying to make sure the soldier retreats before he dies a second time. ( have an undying ring ). my code has an infinate loop even though it is all spaced out, to make sure it isnt slow.

``````decider = 1
# 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):
if decider and decider == 1:
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:
archerTarget = bestTarget
else:
return None
def decide():
if soldier.pos.x == 43 or soldier.pos.y == 38:
decider = 1

def getSafety():
for soldier in hero.findByType("soldier"):
if soldier and soldier.health <= soldier.maxHealth / 4 + 16:
while soldier.pos.x != 43 or soldier.pos.y != 38:
decider = 0
hero.command(soldier, "move", {"x" : 43, "y" : 38})
decide()
# 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
def castUndyingCharm():

set = hero.findFriends()
for good in set:
if good.health < good.maxHealth / 5:
utoh = good
if utoh and hero.canCast("undying-charm") == True:
hero.cast("undying-charm", utoh)
while True:
getSafety()
castUndyingCharm()
# If archerTarget is defeated 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()
archers = hero.findByType("archer")
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
# Create a variable containing your archers.
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers
for archer in archers:
commandArcher(archer)

``````

Sorry, heres my code without all the # stuff

``````decider = 1
def commandSoldier(soldier, soldierIndex, numSoldiers):
if decider and decider == 1:
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);

def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
archerTarget = bestTarget
else:
return None
def decide():
if soldier.pos.x == 43 or soldier.pos.y == 38:
decider = 1

def getSafety():
for soldier in hero.findByType("soldier"):
if soldier and soldier.health <= soldier.maxHealth / 4 + 16:
while soldier.pos.x != 43 or soldier.pos.y != 38:
decider = 0
hero.command(soldier, "move", {"x" : 43, "y" : 38})
decide()
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)

archerTarget = None
def castUndyingCharm():

set = hero.findFriends()
for good in set:
if good.health < good.maxHealth / 5:
utoh = good
if utoh and hero.canCast("undying-charm") == True:
hero.cast("undying-charm", utoh)
while True:
getSafety()
castUndyingCharm()
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
archers = hero.findByType("archer")
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
for archer in archers:
commandArcher(archer)

``````

Hello @R_Ross, just noticed a few things here - first try to complete the level with the functions provided instead of going on a tangent, unless the level asks that you make your own. This is the easiest way for a beginner to make a mistake; use what you know to begin with.

ya, I did that at first

I did that. You can literally reset your code ( would copy it first if you already got to this level and have something that works) and do everything it tells you to and BOOM no errors one soldier just dies. you can even see he does everything heâ€™s supposed to and still dies.

I have been stuck here for two weeks and this is what I know. Ive added more functions on top of what the level already says to do after the level not working as itâ€™s supposed to, like trying to make the soldier retreat, having it recieve an undying effect from the ring, etc. but nope

thank you guys for all the help its been 6 weeks no replies i cant move past this level and i may have to quit cc

5 weeks*

ji;oigl;kh;io

This level has a relatively simple solution compared to the code you have at the moment. Restart the level and fill in only what the yellow arrows say to fill in. Have a crack at this and then post your code if you are still struggling.

ive done this seven times so you know what im gonna give you a step by step of what im doing

code right after reset:

``````# 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.

# 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 defeated 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.

for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers

``````

first thing to do:
find the enemy with the strongest health and set bestTarget to that enemy.

``````    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
#im typing here
# Only focus archers' fire if there is a big ogre.
``````
``````    # 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.
``````

done. Next:
create a variable containing your archers.

``````    # Create a variable containing your archers.
# Im typing here

``````
``````    # Create a variable containing your archers.
archers = hero.findByType("archer")
``````

done. now im assuming commandArcher(archer) needs to use that variable, so i guess i can create a loop going over archers[ ] to command them all (this should work even if there is an easier way to do it.)

``````    # use commandArcher() to command your archers
# Im typing here
``````

first I add a variable equaling one somewhere before the while loop:
z = 1

``````    # use commandArcher() to command your archers
while z <= 4:
commandArcher(archers[z])
z += 1
``````

first error: something the level set FOR me:

``````    nearest = archer.findNearestEnemy()
``````

archer is defined in the function itself
commandArcher(archer)
whatever I put in the ()s should also be defined as archer in the code but no worries I can fix that
updated while loop:

``````    while z <= 4:
archer = archers[z]
commandArcher(archers[z])
z += 1
``````

still the same error even thought ive defined archer

1 Like

@staff

``````

``````
``````
``````
``````
``````

i need to have an active coding class (im homeschooled). if no one helps me with this i will no longer be using codecombat and will not reccomend it to anyone else. if im making a stupid mistake just frikin tell me what im doing wrong.

First of all, donâ€™t be rude
Donâ€™t ping staff as they are busy
Just say Can someone help me with this level?

2 Likes

well, i bought this course. no one is giving me a solution or helping at all, only saying that I am doing something wrong when really I followed the instructions clearly given by the game and if I am doing something wrong #1 make it more clear what I am supposed to do or #2 just tell me what I am doing wrong. I understand that the staff team is busy but either it should take two minutes to tell me what to do or this is a bug which they then have to deal with.

Also I CLEARLY titled this thread â€śplease help meâ€ť and NO ONE has really helped other than to tell me Im doing something wrong, and again, what they suggest never works.

So dont say â€śJust say Can someone help me with this level?â€ť because Ive already done that.

First of all, donâ€™t be rude.
Second we have lives as well, the world doesnâ€™t revolve around you.
And third if you want help post your code.

1 Like

1: im not trying to be rude, sorry if i am. ive been stuck here for months and am getting a teensy tiny bit frustrated at the lack of help

There should be

``````for enemy in enemies:
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy

[quote="R_Ross, post:11, topic:29843"]
``````

else:
return Non

``````[/quote]

delete that

[quote="R_Ross, post:11, topic:29843"]

# use commandArcher() to command your archers

[/quote]