Sorry for another post about this level. For some reason, my friends won’t listen to commands. The code for commanding them is mostly reused from the previous level. I have the appropriate gear to find, summon, and command friends, so I’m not sure what’s going on.
Here is my current code:
# Practice using modulo to loop over an array
# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer"]
#Meta define
def findStrongest(enemies):
return strongest
#While true define
def summonTroops():
# Use % to wrap around the summonTypes array based on len(hero.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def getCoins():
items = hero.findItems()
item = hero.findNearest(items)
hero.move(item.pos)
def commandTroops(friend):
enemies = friend.findEnemies
enemy = friend.findNearest(enemies)
strongest = None
strongestHealth = 0
for enemy in enemies:
if enemy.health > strongestHealth:
strongest = enemy
strongestHealth = enemy.health
if friend.type == "archer" and strongest:
strongestDistance = friend.distanceTo(strongest)
if strongestDistance < 20:
hero.command(friend, "attack", strongest)
elif friend.type == "soldier" and enemy:
hero.command(friend, "attack", enemy)
#While true
while True:
getCoins()
summonTroops()
friends = hero.findFriends()
for friend in friends:
commandTroops(friend)
Fill the findStrongest() function, remember to do a for-loop, also, you don’t need a parameter for a function which is this simple.
def findStrongest():
bestHealth = 0
bestEnemy = None
for enemy in hero.findEnemies():
if enemy.health > bestHealth:
bestHealth = enemy.health
bestEnemy = enemy
return bestEnemy
I have completed this function for you, also, check if a coin exists for not with a if-statement.
For the commandTroops() part, just to enemy = findStrongest() because the function already returns the best target.
def commandTroops(friend):
strongest = findStrongest() # Strongest will be best target returned to you
if strongest: # Check for the existance of strongest
if friend.type == "archer":
strongestDistance = friend.distanceTo(strongest)
if strongestDistance < 20:
hero.command(friend, "attack", strongest)
elif friend.type == "soldier":
hero.command(friend, "attack", strongest)
Very helpful! As you could tell by the empty function, I was trying to do that before, but failed. Although this was very helpful, the friends still don’t listen. I’m still unsure why though.
Sorry for getting back to you late, I only do CodeCombat when I’m in school. Sadly, I have not beat it, and my friends still refuse to listen. Here’s a screenshot of them standing around.
You don’t need an argument inside the findStrongest() function, as there is no parameter.
Also, friend.findEnemies() is a method, you need to add brackets () after them.
I just reviewed this level and I think there is no need for using the findStrongest function. Just do this:
def commandTroops():
for friend in hero.findFriends():
if friend.type == "soldier" or friend.type == "archer":
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
findStrongest() does need a parameter, it errors without it. Those brackets after friend.findEnemies was the error though, and that solved my issue! I know the findStrongest() isn’t required, I just wanted to go a step above.