Please need help my code is correct but an archer dies in the end.
Here is my code:
defcommandSoldier(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)
self.command(soldier, “defend”, defendPos);
def findStrongestTarget():
mostHealth = 0
enemies = self.findEnemies()
enemyIndex = 0
for enemy in hero.findEnemies():
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
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 self.gold > self.costOf("soldier"):
self.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
archers = self.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Howdy and welcome to the forum!
I must first ask, why are you using ‘self’ in your code? This was replaced back around 2017 and is no longer used. Please provide your own, original code, so that we can help you.
Also, formatting of the code is critical. Please review this topic to learn how:
[Essentials] How To Post/Format Your Code Correctly
Ooops sorry I will change it right away.
Thank You!
Here is my code:
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);
def findStrongestTarget():
mostHealth = 0
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in hero.findEnemies():
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
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 hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Ok, but you still need to format it properly. Please read this topic:
[Essentials] How To Post/Format Your Code Correctly
Sorry I will do that right away
Here is my code:
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);
def findStrongestTarget():
mostHealth = 0
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in hero.findEnemies():
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
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 hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Indentations are critical in Python…for example, your first function definition should look like this:
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);
When you copy and past from another web source, most likely github, you can lose this formatting. All of your code needs to be fixed…needs to be properly indented before we can proceed.
Otherwise, if you copy directly from you own code editor, your just need to use the three backticks (```) as explained in the topic I provided.
I still don’t get it how do I fix the code?
Regardless, it is important to learn the techniques required to post code on this forum. However, if you pasted directly from your code editor, then formatting is certainly a concern.
Here’s a partial shot of what you would see when you first start the lesson:
Notice the blue bar running down the left side of the code editor? This indicates the first indentation. Towards the bottom of the pick, you also see a kind of white bar…this indicates another indent. These indents are 4 spaces, or ‘tabs’ (as offered by the code editor). Without these, we cannot determine if your problem is with an error in your code, or is related to indentation.
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);
def findStrongestTarget():
mostHealth = 0
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in hero.findEnemies():
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
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 hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Much better! Give me a moment to have a look now.
To start with, in your findStrongestTarget function, your ‘for enemy’ loop is outside of the definition…the entire block needs to be indented.
1 Like
Do you need any more help at this level?
1 Like
If you still need help, can you send us your code?
Here is my code:
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);
def findStrongestTarget():
mostHealth = 0
enemies = hero.findEnemies()
enemyIndex = 0
for enemy in hero.findEnemies():
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
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 hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Try to put this inside the function (try to put 4 spaces before each of those lines).
Do you need any more assistance at this level?