# 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!
console.log()
# 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:
hero.findFriends()
wizard = hero.findNearest(hero.findByType("librarian"))
# 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")
enemy = hero.findNearestEnemy()
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers))
# use commandArcher() to command your archers
friends = hero.findFriends()
soldiers = hero.findByType("archer")
wizard = hero.findNearest(hero.findByType("librarian"))
for i in enumerate(archers):
archerTarget = FindStrongestTarget
archer = archers[i]
commandArcher(archer, i, len(archers))
You donāt need this.(After the for i in range(archers):)
You need the for i in range type of for loop. Or else, it return a different enemy for every archer.(I think)
Right after the for loop, you need something that gets one of the enemies from the enemies.
Hint
You already have some in your code. Look at the for loop for the soldiers and what comes after it.
You can del the
at the top of the while loop.
Your defining wizard twice. But I donāt think your gonna be using that(might as well del that).
Like i said before,
Your code:
at that line, commandArcher(archer) is just fine.(Since you have archer = archers[i] it already returns one out of all of the archers(And then signals the others to do the same. Pretty much like a repeat for the others))
Im pretty sure thereās a difference in those two. I tried the enumerate method and it giveās an error on the other hand. I switched it back to range method and it worked fine.
# 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!
console.log()
# 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
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers))
# 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:
wizard = hero.findNearest(hero.findByType("librarian"))
# 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')
enemy = hero.findNearestEnemy()
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers))
# use commandArcher() to command your archers
friends = hero.findFriends()
soldiers = hero.findByType("archer")
wizard = hero.findNearest(hero.findByType("librarian"))
for i in enumerate(archers):
archer = archers[i]
commandArcher(archer, i, len(archers))
Ok, umm I see that some of the things I told you to do are not done. OFC Iām lazy (I should really change that personality of mine huh ?) and donāt want to point them out. @Lukyme you really need me to point them out I can. Just ask .
P.S : Can anyone explain the difference between enumerate and range?
I would like you to point them out for me if you can, I would greatly appreciate it if you do.
P.S Not lazy, I just donāt know. I may be smart, but obviously not as smart as you
Umm, if you followed all my instructions on this post it should be fine . Check that post and if you have any other problems post the updated code (Your code doesnāt contain changes )
Iām not that smart. If you code everyday and practice a lot you should be able to surpass me in coding .
Umm, then there is a slight problem with the archers they are not supposed to die i think. Iāll review the code in some time. Just try to fix the defined thing with archers. I think I mentioned somewhere above.
I tried that and it didnāt work, instead what happened was I made it longer, but I ended up dying either way. I donāt know why It is happening. It also gave me a code error on line 14 when I put what you told me to put.
It isnāt the archers who are dying, its the soldiers who are dying. I donāt know why but I tried @herokolbert technique and it didnāt seem to work.