# Library Tactician:HELP!

**URL:** <https://discourse.codecombat.com/t/library-tactician-help/14638>\
**Category:** Adventurer\
**Created:** [May 3, 2018, 3:36pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638 "2018-05-03T15:36:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![GamerKnight](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gamerknight/32/10572_2.png) [@GamerKnight](https://discourse.codecombat.com/u/GamerKnight)\
**Post date:** [May 3, 2018, 3:36pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/1 "2018-05-03T15:36:26Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/f/f968dcd57d6a84b030f2b59dd2be6e45ea1501e0.jpg)

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

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 3, 2018, 5:19pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/2 "2018-05-03T17:19:16Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 3, 2018, 5:42pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/3 "2018-05-03T17:42:44Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/9/91f553e7526f55504f217644f6982a765ac94af9.png)

```python
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)

```

---

<div class="post-metadata">

**Author:** ![GamerKnight](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gamerknight/32/10572_2.png) [@GamerKnight](https://discourse.codecombat.com/u/GamerKnight)\
**Post date:** [May 4, 2018, 6:28pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/4 "2018-05-04T18:28:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [May 6, 2018, 2:55pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/5 "2018-05-06T14:55:13Z")

</div>

> [@xython](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![catkitty123](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@catkitty123](https://discourse.codecombat.com/u/catkitty123)\
**Post date:** [December 1, 2018, 1:58am UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/6 "2018-12-01T01:58:41Z")

</div>

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

```python
// 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);
    }
}

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [December 1, 2018, 8:49pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/7 "2018-12-01T20:49:20Z")

</div>

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:

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

```

Try it 🙂

---

<div class="post-metadata">

**Author:** ![catkitty123](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@catkitty123](https://discourse.codecombat.com/u/catkitty123)\
**Post date:** [December 4, 2018, 10:07pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/8 "2018-12-04T22:07:33Z")

</div>

THX!!!  
It worked! 😍

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [July 24, 2019, 11:07am UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/9 "2019-07-24T11:07:30Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![GamerKnight](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gamerknight/32/10572_2.png) [@GamerKnight](https://discourse.codecombat.com/u/GamerKnight)\
**Post date:** [July 24, 2019, 2:21pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/10 "2019-07-24T14:21:45Z")

</div>

Still having problems. here is current code

```python
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:
    badGuy = findStrongestTarget()
    if self.gold > self.costOf('soldier'):
        self.summon('soldier')
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = badGuy
    thanos = badGuy
    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

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [July 24, 2019, 2:34pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/11 "2019-07-24T14:34:04Z")

</div>

did not test the code but first correction iS

```python
        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 

```

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 30, 2019, 2:05pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/12 "2019-07-30T14:05:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GamerKnight](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gamerknight/32/10572_2.png) [@GamerKnight](https://discourse.codecombat.com/u/GamerKnight)\
**Post date:** [July 30, 2019, 9:12pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/13 "2019-07-30T21:12:02Z")

</div>

thanks, but an infinite loop still exists

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 30, 2019, 10:55pm UTC](https://discourse.codecombat.com/t/library-tactician-help/14638/14 "2019-07-30T22:55:31Z")

</div>

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.
