# \[SOLVED\] Help With Library Tactician

**URL:** https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410
**Category:** Level Help
**Created:** [November 15, 2020, 5:57pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410 "2020-11-15T17:57:41Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:35pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/21 "2020-11-15T18:35:53Z")

</div>

it say’s archer is not defined

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:37pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/22 "2020-11-15T18:37:16Z")

</div>

no add for archer in archers:

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:41pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/23 "2020-11-15T18:41:17Z")

</div>

the code looks fine but the infinite loop message is here again

```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)
    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.
    bestTarget = mostHealth
    if mostHealth > bestTarget:
        bestTarget = mostHealth
    else:
        return None
    # 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 in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers));
    # use commandArcher() to command your archers
    for archer in archers:
        commandArcher(archer)

```

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:42pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/24 "2020-11-15T18:42:09Z")

</div>

Try subbmitting it. it takes long but it should work

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:44pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/25 "2020-11-15T18:44:42Z")

</div>

@Eric_Tang i tried submitting it but the same 2 soldiers died there in the bottom left corner

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:46pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/26 "2020-11-15T18:46:48Z")

</div>

> [@WaWa\_Yang](#):
>
> ```python
> if mostHealth > bestTarget:
> bestTarget = mostHealth
> 
> ```

the fist line change mostHealth to enemy the scond line change mostHealth to enemy

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:48pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/27 "2020-11-15T18:48:20Z")

</div>

so both mostHealth have to be enemy?

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:49pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/28 "2020-11-15T18:49:02Z")

</div>

yes the both have to be enemy

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:49pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/29 "2020-11-15T18:49:55Z")

</div>

oof they all just move away from Hushbaum and they all die

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:50pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/30 "2020-11-15T18:50:09Z")

</div>

(including Hushbaum)

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:54pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/31 "2020-11-15T18:54:31Z")

</div>

I made a error actually change the first line to enemy.health

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:55pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/32 "2020-11-15T18:55:57Z")

</div>

@Eric_Tang it say’s “enemy is not defined”

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:56pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/33 "2020-11-15T18:56:19Z")

</div>

on wich line does it say that

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 6:56pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/34 "2020-11-15T18:56:56Z")

</div>

and can you post your most recent code

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:57pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/35 "2020-11-15T18:57:37Z")

</div>

the line you told me …

> [@Eric\_Tang](#):
>
> enemy.health

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:58pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/36 "2020-11-15T18:58:05Z")

</div>

and heres the most recent code:

```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)
    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.
    bestTarget = mostHealth
    if enemy.health > bestTarget:
        bestTarget = enemy
    else:
        return None
    # 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 in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers));
    # use commandArcher() to command your archers
    for archer in archers:
        commandArcher(archer)

```

---

<div class="post-metadata">

### Author: ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)
#### Post date: [November 15, 2020, 6:59pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/37 "2020-11-15T18:59:12Z")

</div>

i dont know why but after

> [@WaWa\_Yang](#):
>
> `if enemy.health > bestTarget:`

i added

> [@WaWa\_Yang](#):
>
> ```python
> else:
> return None
> 
> ```

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 7:01pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/38 "2020-11-15T19:01:31Z")

</div>

> [@WaWa\_Yang](#):
>
> ```python
> bestTarget = mostHealth
> if enemy.health > bestTarget:
> bestTarget = enemy
> 
> ```

add the bestTarget = mostHealth line after the if enemy.health \> bestTarget:  
after you did that add a for enemy in enemies: line in front of the if enemy.health \> bestTarget line

---

<div class="post-metadata">

### Author: ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)
#### Post date: [November 15, 2020, 7:03pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/39 "2020-11-15T19:03:08Z")

</div>

Yeah you can delete the else part

---

<div class="post-metadata">

### Author: ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)
#### Post date: [November 15, 2020, 8:34pm UTC](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410/40 "2020-11-15T20:34:22Z")

</div>

Delete this:

> [@WaWa\_Yang](#):
>
> `bestTarget = mostHealth`

Replace this

> [@WaWa\_Yang](#):
>
> ```python
> if enemy.health > bestTarget:
> bestTarget = enemy
> else:
> return None
> 
> ```

with this:

```python
for enemy in enemies:
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            bestTarget = enemy

```

Lydia

[Previous page](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410.md?page=1)

[Next page](https://discourse.codecombat.com/t/solved-help-with-library-tactician/25410.md?page=3)
