# Stuck: Library Tactician

**URL:** https://discourse.codecombat.com/t/stuck-library-tactician/4868
**Category:** Level Help
**Created:** [September 7, 2015, 2:21pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868 "2015-09-07T14:21:42Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![IcyMagus](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@IcyMagus](https://discourse.codecombat.com/u/IcyMagus)
#### Post date: [September 7, 2015, 2:21pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/1 "2015-09-07T14:21:43Z")

</div>

Hi! This is my first time posting so please forgive me if I do anything wrong 😄  
So I’ve been playing on the level Library Tactician and I’ve been stuck. I have looked at the stimulation and the only thing wrong is that my archers won’t target the strongest target.  
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)
    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 about the indents, I can’f find a way to indent so i would appreciate if you help me in that too. But my main problem is that I can’t find anything wrong with the code so I would be really (really really really) thankful if you pointed out the mistake. Thanks! 😄

---

<div class="post-metadata">

### Author: ![UltCombo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ultcombo/32/3822_2.png) [@UltCombo](https://discourse.codecombat.com/u/UltCombo)
#### Post date: [September 7, 2015, 4:49pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/2 "2015-09-07T16:49:09Z")

</div>

> [@IcyMagus](#):
>
> Sorry about the indents, I can’f find a way to indent so i would appreciate if you help me in that too.

Click in the Edit button (the pencil icon below your post), select the code and press Ctrl+K (or click in the `</>` button). 😸

---

<div class="post-metadata">

### Author: ![IcyMagus](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@IcyMagus](https://discourse.codecombat.com/u/IcyMagus)
#### Post date: [September 8, 2015, 3:46am UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/3 "2015-09-08T03:46:50Z")

</div>

Thanks \<3 I really appreciate it! 😄

---

<div class="post-metadata">

### Author: ![James63](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@James63](https://discourse.codecombat.com/u/James63)
#### Post date: [September 8, 2015, 1:35pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/4 "2015-09-08T13:35:47Z")

</div>

You have a stray

> .

in there.

I tried your code with and without it and it works if you get rid of that.

---

<div class="post-metadata">

### Author: ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)
#### Post date: [September 9, 2015, 4:54am UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/5 "2015-09-09T04:54:54Z")

</div>

Also, are you actually calling findStrongestTarget in the commandArcher method, and calling the commandArcher method on each archer? I think there’s some parts at the bottom where you have to set that up, too.

---

<div class="post-metadata">

### Author: ![IcyMagus](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@IcyMagus](https://discourse.codecombat.com/u/IcyMagus)
#### Post date: [September 9, 2015, 5:14am UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/6 "2015-09-09T05:14:24Z")

</div>

Thanks Nick, I changed my code a bit and now its working fine! 😄

---

<div class="post-metadata">

### Author: ![sir-sonic-Destroyer](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sir-sonic-Destroyer](https://discourse.codecombat.com/u/sir-sonic-Destroyer)
#### Post date: [November 8, 2015, 4:01pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/7 "2015-11-08T16:01:50Z")

</div>

what is a stray because it is bothering me on the level

---

<div class="post-metadata">

### Author: ![sir-sonic-Destroyer](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sir-sonic-Destroyer](https://discourse.codecombat.com/u/sir-sonic-Destroyer)
#### Post date: [November 8, 2015, 4:03pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/8 "2015-11-08T16:03:53Z")

</div>

Icy magnus do you mind helping me.please because im stuck!thank you

---

<div class="post-metadata">

### Author: ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)
#### Post date: [November 8, 2015, 4:29pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/9 "2015-11-08T16:29:05Z")

</div>

Hello, Dinesh, and welcome.

Phrases like “I need help!” or “This code doesn’t work!” do not help us do anything about your problem. Give us more information. What happens when you run your code? Is there an error? If so, what does it say? Post your code and a description of what happened. Don’t forget to read the FAQ before you post. Format your code properly!

---

<div class="post-metadata">

### Author: ![sir-sonic-Destroyer](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sir-sonic-Destroyer](https://discourse.codecombat.com/u/sir-sonic-Destroyer)
#### Post date: [November 8, 2015, 4:41pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/10 "2015-11-08T16:41:07Z")

</div>

thank you! i finished the level just now but i still have no idea what a stray is in general.  
p.s it would be nice if you could call me sanjjith because that is my name

---

<div class="post-metadata">

### Author: ![sir-sonic-Destroyer](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sir-sonic-Destroyer](https://discourse.codecombat.com/u/sir-sonic-Destroyer)
#### Post date: [November 8, 2015, 4:45pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/11 "2015-11-08T16:45:03Z")

</div>

i just changed my username

---

<div class="post-metadata">

### Author: ![sir-sonic-Destroyer](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sir-sonic-Destroyer](https://discourse.codecombat.com/u/sir-sonic-Destroyer)
#### Post date: [November 8, 2015, 5:09pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/12 "2015-11-08T17:09:30Z")

</div>

hey chronistgilver, can you you help me if possible , on the level steelclaw gap.  
thanks

---

<div class="post-metadata">

### Author: ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)
#### Post date: [November 8, 2015, 9:13pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/13 "2015-11-08T21:13:45Z")

</div>

Create a new topic for it. This is not the place to discuss Steelclaw Gap.

---

<div class="post-metadata">

### Author: ![harper4](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@harper4](https://discourse.codecombat.com/u/harper4)
#### Post date: [December 24, 2015, 4:05pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/14 "2015-12-24T16:05:39Z")

</div>

I am having trouble after the board is about 3/4 of the way complete. All the sudden my archers just quit targeting anyone after targeting them flawlessly up to that point…

```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.
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);

# 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 = self.findEnemies()
    enemyIndex = 0
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            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

def penderAttack():
    enemy = self.findNearest(self.findByType("ogre"))
    if enemy:
        if self.canCast("drain-life", enemy):
            self.cast("drain-life", enemy)
        elif self.canCast("shockwave", enemy):
            self.cast("shockwave", enemy)
        elif self.canCast("slow", enemy):
            self.cast("slow", enemy)

def summonFriend():
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")

def weakestFriend():
    soldiers = self.findByType("soldier")
    weakestFriend = None
    leastHealth = 100
    for soldier in soldiers:
        if soldier.health < leastHealth:
            weakestFriend = soldier
            soldier += 1
     if weakestFriend:
        if self.canCast("flame-armor", weakestFriend):
            self.cast("flame-armor", weakestFriend)

# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archers):
    enemies = archer.findEnemies()
    enemy = archer.findNearest(enemies)
    if archerTarget:
        self.command(archer, "attack", archerTarget)
    elif enemy:
        self.command(archer, "attack", enemy)

archerTarget = None

loop:
    # 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 = self.findFriends()
    soldiers = self.findByType("soldier")
    archers = self.findByType("archer")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    for i, archer in enumerate(archers):
        commandArcher(archer, i, len(archers));
    commandArcher() 
    weakestFriend()
    penderAttack()
    summonFriend()

```

---

<div class="post-metadata">

### Author: ![harper4](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@harper4](https://discourse.codecombat.com/u/harper4)
#### Post date: [December 28, 2015, 3:06pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/15 "2015-12-28T15:06:11Z")

</div>

Well, commenting out the pender attack function seemed to fix the issue with the archers randomly stopping their attack…

---

<div class="post-metadata">

### Author: ![sheene](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sheene](https://discourse.codecombat.com/u/sheene)
#### Post date: [April 17, 2020, 4:03am UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/16 "2020-04-17T04:03:11Z")

</div>

yo hice todo bien pero, nearest = archer.findNearestEnemy() no funciona

nearest = archer.findNearestEnemy()  
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  
bestTarget = None  
enemies = hero.findEnemies()  
index=0  
while index \< len(enemies):  
ene=enemies[index]  
if ene.health\>mostHealth:  
bestTarget = ene  
mostHealt = ene.health  
index +=1

```
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 not archerTarget or archerTarget.health \<= 0:  
archerTarget = findStrongestTarget()

```
friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archer=hero.findByType("archer")
for i in range(len(soldiers)):
    soldier = soldiers[i]
    commandSoldier(soldier, i, len(soldiers));
commandArcher(archer)
```

---

<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: [April 17, 2020, 2:27pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/17 "2020-04-17T14:27:51Z")

</div>

Hi, I’m not sure whether the rest of your code works or not, but the source of your error is in these two lines:

```python
archer = hero.findByType("archer")
commandArcher(archer)

```

Go to the documentation for hero.findByType(“archer”) (it’s on the item bar in the level under your glasses), what does it return? An array or an object? (a singular thing like enemy when you write enemy = hero.findNearestEnemy().  
Now think about this line:

> [@sheene](#):
>
> ```python
> nearest = archer.findNearestEnemy()
> 
> ```

If the variable archer is indeed an array how is this going to work?  
I hope this helps  
Danny

---

<div class="post-metadata">

### Author: ![sheene](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sheene](https://discourse.codecombat.com/u/sheene)
#### Post date: [April 17, 2020, 5:23pm UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/18 "2020-04-17T17:23:39Z")

</div>

entoses que ba aqui  
while True:  
# If archerTarget is defeated 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")

```

_emphasized text_ # Create a variable containing your archers.  
archers = hero.findByType(“archer”)  
for i in range(len(soldiers)):  
soldier = soldiers[i]  
commandSoldier(soldier, i, len(soldiers));  
# use commandArcher() to command your archers  
commandArcher(archers)

---

<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: [April 18, 2020, 9:08am UTC](https://discourse.codecombat.com/t/stuck-library-tactician/4868/19 "2020-04-18T09:08:25Z")

</div>

Sorry, maybe I didn’t phrase that right. The function commandArcher() takes a single parameter, and that parameter needs to be a single thing, not an array.  
What you need to do is copy this code:

> [@sheene](#):
>
> ```python
> for i in range(len(soldiers)):
> soldier = soldiers[i]
> commandSoldier(soldier, i, len(soldiers));
> 
> ```

And do the exact same for the archers.  
Danny
