# Library Tactician:HELP!

I cant’t solve this puzzle!!

My code is as follows:
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)_
_ self.command(soldier, “defend”, defendPos);_

def findStrongestTarget():
_ mostHealth = 0_
_ bestTarget = None_
_ enemies = self.findEnemies()_
_ enemyIndex = 0_
_ while len(enemies) > enemyIndex:_
_ enemy = enemies[enemyIndex]_
_ if enemy:_
_ enemyIndex += 1_
_ if enemy.health > mostHealth:_
_ mostHealth = enemy.health_
_ bestTarget = enemy_
_ _
_ if bestTarget and bestTarget.health > 15:_
_ return bestTarget_
_ else:_
_ return None_

def commandArcher(archer):
_ nearest = archer.findNearestEnemy()_
_ if archerTarget:_
_ self.command(archer, “attack”, archerTarget)_
_ elif nearest:_
_ self.command(archer, “attack”, nearest)_
_ _
archerTarget = None

loop:
_ 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)_

This is my gear

I’ve spent multiple days searching the forum and it won’t work!!

Please in future use the button to the right of the " button on the textbox taskbar and paste your code, directly from the game, into the gap which says type or paste your code here, for now, to save you time, I’ll do it for you.

``````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)
self.command(soldier, “defend”, defendPos);

def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None

def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
self.command(archer, “attack”, archerTarget)
elif nearest:
self.command(archer, “attack”, nearest)

archerTarget = None

loop:
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)
``````

Sorry I don’t have time to do all of it i’m sure someone else will help but all the brackets around strings should be " "s not whatever you’re using, sorry I can’t help you know. @Chaboi_3000 or @MunkeyShynes will know.

1 Like

Replaced “ and ” with ’ . No other checks or simulating done. You can replace text in codecombat with the shortcut Ctrl-F or Ctrl-H

``````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)
self.command(soldier, 'defend', defendPos);

def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None

def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
self.command(archer, 'attack', archerTarget)
elif nearest:
self.command(archer, 'attack', nearest)

archerTarget = None

loop:
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)
``````
3 Likes

IT STILL DOES NOT WORK!!!One of them always dies.

Here you are increasing the index of the enemy before you check it. Therefore enemies[0] never got checked.

Also, returning bestTarget/return None should be outside of the while loop.

Hi there, my code has a problem, because for some reason, one soldier always dies, then GAME OVER.

``````// 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.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var 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.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (enemy.health > mostHealth) {
mostHealth = enemy.health;
bestTarget = enemy.id;
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if (archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if (nearest) {
hero.command(archer, "attack", nearest);
}
}
var archerTarget = null;
while (true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if (!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archers = hero.findByType("archer");
for (var i = 0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
for (var j = 0; j < archers.length; j++) {
var archer = archers[j];
commandArcher(archer);
}
}

``````

I never understood why sometimes passing a string to hero.attack() works… ( this was in the very first beginner levels). So for me this isn’t logical:

``````function findStrongestTarget() {
// code
bestTarget = enemy.id;
// I will replace it with bestTarget = enemy
//
``````

Try it

THX!!!
It worked!

@GamerKnight “Also, I’m still having problems with Library Tactician. Now it is saying that I have slow code.”
If you still have problems post your current code

Still having problems. here is current 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)
self.command(soldier, 'defend', defendPos);

def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None

def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
self.command(archer, 'attack', archerTarget)
elif nearest:
self.command(archer, 'attack', nearest)

archerTarget = None

def thor(target):
if target:
if hero.canCast("chain-lightning", target):
hero.cast("chain-lightning", target)

while True:
if self.gold > self.costOf('soldier'):
self.summon('soldier')
if not archerTarget or archerTarget.health <= 0:
thor(thanos)
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)

``````

only difference with gear is I have a better watch. Still can us the functions on the watch in the screenshot

did not test the code but first correction iS

``````        if bestTarget and bestTarget.health > 15: # must be outside
return bestTarget                     #  the while loop
else:            # this is really not needed -
return None  # you put bestTarget = None at the beginning
``````

One other thing, the findByType for the archers is currently plural (‘archers’), not singular (‘archer’) like it needs to be.

thanks, but an infinite loop still exists

Make the adjustment that @xython pointed out, along with my first adjustment. Then just remove the extra function you added thor(target). Your hero can’t help from the hill and it is causing an error in your code.