# \[SOLVED\] Stuck in "Library Tactician" (Mountain)

**URL:** <https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451>\
**Category:** Level Help\
**Created:** [May 16, 2017, 9:34pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451 "2017-05-16T21:34:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JuampyXnaker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/juampyxnaker/32/6318_2.png) [@JuampyXnaker](https://discourse.codecombat.com/u/JuampyXnaker)\
**Post date:** [May 16, 2017, 9:34pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/1 "2017-05-16T21:34:41Z")

</div>

Hey guys, I need help with this one, the problem I have is that a unique soldier always dies almost in the end of the battle and I really don’t know what to change…  
So here’s the code, if anyone can help…

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

def summonSoldier():
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")

# 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)
    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.
def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
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 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")
    # Create a variable containing your archers.
    archers = hero.findByType("archer")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    summonSoldier()
    # use commandArcher() to command your archers
    for i, archer in enumerate(archers):
        commandArcher(archer);

```

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.codecombat.com/u/user1)\
**Post date:** [May 16, 2017, 9:42pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/2 "2017-05-16T21:42:06Z")

</div>

Without knowing the level - I notice that the function findStrongestTarget() does not loop over enemies at all. It seems to always return None. Is that intended?

Regards!

---

<div class="post-metadata">

**Author:** ![JuampyXnaker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/juampyxnaker/32/6318_2.png) [@JuampyXnaker](https://discourse.codecombat.com/u/JuampyXnaker)\
**Post date:** [May 18, 2017, 7:59pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/3 "2017-05-18T19:59:49Z")

</div>

OK --\> (code below).  
But I still have the same problem, please somebody help!  
Or Maybe the level needs some rebalancing?

```python
def findStrongestTarget():
    strongest = None
    strongestHealth = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            strongest = enemy
            strongestHealth = enemy.health
        enemy.Index += 1
        return strongest

```

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.codecombat.com/u/user1)\
**Post date:** [May 19, 2017, 4:19am UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/4 "2017-05-19T04:19:28Z")

</div>

Might be just a format issue, but the return statement is executed directly in the while loop.

Plus, in the original function, there was some kind of check, wether your strongest target is worthwhile targeting: “if strongest/bestTarget and strongest.health \> x:”

You might try to use hero.say(strongest) in order to check, wether the unit you are targeting is really worthwhile (i. e. very strong).

---

<div class="post-metadata">

**Author:** ![JuampyXnaker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/juampyxnaker/32/6318_2.png) [@JuampyXnaker](https://discourse.codecombat.com/u/JuampyXnaker)\
**Post date:** [May 19, 2017, 6:48am UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/5 "2017-05-19T06:48:58Z")

</div>

Finally!!! I solved it!!!  
In **def commandSoldier** I was missing this:

```python
if (soldier.health > 60):
     hero.command(soldier, "defend", defendPos)
else:
     hero.command(soldier, "defend", {'x': 42, 'y': 40})

```

---

<div class="post-metadata">

**Author:** ![Electric26](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Electric26](https://discourse.codecombat.com/u/Electric26)\
**Post date:** [January 18, 2018, 10:17pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/6 "2018-01-18T22:17:36Z")

</div>

Thank you! I’ve been stuck on this level for well over a day now, and I wasn’t able to find a single (working) solution anywhere online (even solutions that have supposedly worked for people in the past). I think this level should definitely be tweaked, whether it be the amount of ogres spawned or a better tutorial/comments in the code. As far as I know this solution isn’t the intended way to complete the level. Thanks again!

---

<div class="post-metadata">

**Author:** ![Flamekey\_42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/flamekey_42/32/19618_2.png) [@Flamekey\_42](https://discourse.codecombat.com/u/Flamekey_42)\
**Post date:** [January 28, 2021, 6:21pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/7 "2021-01-28T18:21:56Z")

</div>

I’m still stuck on library tactician here is my code: \<def summonSoldier():  
if hero.gold \> hero.costOf(“soldier”):  
hero.summon(“soldier”)

# 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)  
if (soldier.health \> 60):  
hero.command(soldier, “defend”, defendPos)  
else:  
hero.command(soldier, “defend”, {‘x’: 42, ‘y’: 40})

# Find the strongest target (most health)

# This function returns something! When you call the function, you will get some value back.

def findStrongestTarget():  
strongest = None  
strongestHealth = 0  
enemyIndex = 0  
enemies = hero.findEnemies()  
while enemyIndex \< len(enemies):  
enemy = enemies[enemyIndex]  
if enemy.health \> strongestHealth:  
strongest = enemy  
strongestHealth = enemy.health

```
    enemy.Index += 1
    return strongest

```

# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.

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 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”)  
# Create a variable containing your archers.  
archers = hero.findByType(“archer”)  
for i, soldier in enumerate(soldiers):  
commandSoldier(soldier, i, len(soldiers));  
summonSoldier()  
# use commandArcher() to command your archers  
for i, archer in enumerate(archers):  
commandArcher(archer)\>

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [January 28, 2021, 6:22pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/8 "2021-01-28T18:22:29Z")

</div>

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review [this topic](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601), which shows all essentials of this board! Thanks!

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [January 30, 2021, 10:10pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/9 "2021-01-30T22:10:47Z")

</div>

Can you please format your code

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [January 30, 2021, 10:25pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/10 "2021-01-30T22:25:13Z")

</div>

> [@Flamekey\_42](#):
>
> ` enemy.Index += 1`

Shouldn’t that be enemyIndex +=1?

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [January 30, 2021, 10:25pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/11 "2021-01-30T22:25:38Z")

</div>

do you need this `>`

> [@Flamekey\_42](#):
>
> commandArcher(archer)\>

---

<div class="post-metadata">

**Author:** ![Flamekey\_42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/flamekey_42/32/19618_2.png) [@Flamekey\_42](https://discourse.codecombat.com/u/Flamekey_42)\
**Post date:** [February 3, 2021, 7:37pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/12 "2021-02-03T19:37:51Z")

</div>

Thank u I didn’t know how to format the code then

---

<div class="post-metadata">

**Author:** ![ScienceBoi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/scienceboi/32/11889_2.png) [@ScienceBoi](https://discourse.codecombat.com/u/ScienceBoi)\
**Post date:** [February 28, 2021, 11:58pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/13 "2021-02-28T23:58:06Z")

</div>

So I’m stuck on this and I don’t know if this is a code error or something else but the arrows just keep on missing and I lose, can anyone help me? ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/d/f/dfe8ec7f5ef80f8ba1a6332e44249cf6a9695345.png)

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 28, 2021, 11:59pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/14 "2021-02-28T23:59:53Z")

</div>

Can you please post your code so we can help you.

---

<div class="post-metadata">

**Author:** ![ScienceBoi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/scienceboi/32/11889_2.png) [@ScienceBoi](https://discourse.codecombat.com/u/ScienceBoi)\
**Post date:** [March 1, 2021, 1:29am UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/15 "2021-03-01T01:29:09Z")

</div>

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

def summonSoldier():
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")

# 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)
    if (soldier.health > 60):
        hero.command(soldier, "defend", defendPos)
    else:
        hero.command(soldier, "defend", {'x': 42, 'y': 40})
    

# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
    strongest = None
    strongestHealth = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            strongest = enemy
            strongestHealth = enemy.health
        enemy.Index += 1
        return strongest
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
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 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")
    # Create a variable containing your archers.
    archers = hero.findByType("archer")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
        summonSoldier()
    # use commandArcher() to command your archers
    for i, archer in enumerate(archers):
        commandArcher(archer);

```

---

<div class="post-metadata">

**Author:** ![Rachel699](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rachel699/32/13569_2.png) [@Rachel699](https://discourse.codecombat.com/u/Rachel699)\
**Post date:** [March 1, 2021, 7:19pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/16 "2021-03-01T19:19:25Z")

</div>

Welcome back to the discourse, @ScienceBoi! 🥳

---

<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:** [March 2, 2021, 12:24pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/17 "2021-03-02T12:24:57Z")

</div>

Hi @ScienceBoi. The problem isn’t that you’re missing exactly. The problem is your findStrongestTarget function.  
Firstly this:

> [@ScienceBoi](#):
>
> `enemy.Index += 1`

does not match the variable you created just above it:

> [@ScienceBoi](#):
>
> `enemyIndex = 0`

There should be no .  
Secondly, you’ve put the return strongest inside the while loop, meaning you will return the strongest variable every loop, which is bad, because you want to loop through all the enemies first, then return the strongest. This means you have to put the return strongest outside the while loop.  
Finally, for the commandArcher(archer) function the comments say:

```python
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.

```

You’ve only checked if the archerTarget exists. You also need to check it has more than 15 health:

> [@ScienceBoi](#):
>
> ```python
> def commandArcher(archer):
> nearest = archer.findNearestEnemy()
> if archerTarget:
> hero.command(archer, "attack", archerTarget)
> elif nearest:
> hero.command(archer, "attack", nearest)
> 
> ```

If it doesn’t, then the code will attack the nearest enemy (once you’ve completed the change).  
This will give you success.  
Danny

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [August 26, 2022, 8:43pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/18 "2022-08-26T20:43:26Z")

</div>

I am having trouble in my code. I have read all of the solutions above, and none have worked for me.

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

def summonSoldier():
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")

# 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)
    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.
def findStrongestTarget():
    if (soldier.health > 60):
        hero.command(soldier, "defend", defendPos)
    else:
        hero.command(soldier, "defend", {'x': 42, 'y': 40})
    strongest = None
    strongestHealth = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > strongestHealth:
            strongest = enemy
            strongestHealth = enemy.health
        enemy.Index += 1
        return strongest
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
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 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")
    # Create a variable containing your archers.
    archers = hero.findByType("archer")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    summonSoldier()
    # use commandArcher() to command your archers
    for i, archer in enumerate(archers):
        commandArcher(archer);

```

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [August 26, 2022, 9:08pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/19 "2022-08-26T21:08:57Z")

</div>

> [@Cedar](#):
>
> ```python
> enemy.Index += 1
> 
> ```

you accidentally put a dot

> [@Cedar](#):
>
> ```python
> if archerTarget:
> 
> ```

`if archerTarget and archerTarget.health > 15:`

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [September 2, 2022, 5:36pm UTC](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451/20 "2022-09-02T17:36:41Z")

</div>

it doesn’t work, my soldier start running all over, and a ton of thrower spawn and kill my archers…

[Next page](https://discourse.codecombat.com/t/solved-stuck-in-library-tactician-mountain/11451.md?page=2)
