# \[SOLVED\] Toil and Trouble soldiers die

**URL:** <https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977>\
**Category:** Level Help\
**Created:** [June 2, 2020, 5:34pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977 "2020-06-02T17:34:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 2, 2020, 5:34pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/1 "2020-06-02T17:34:44Z")

</div>

In the toil and trouble level i have been having problems succeeding. i Have tried two methods and neither work.  
the first is to tell them to attack nearest enemy

```python
# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget():
    archers = hero.findByType("archer")
    for archer in archers:
        
        aenemy = archer.findNearestEnemy()
        hero.command(archer, "attack", aenemy)
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        
        Senemy = soldier.findNearestEnemy()
        hero.command(soldier, "attack", Senemy)
            
while True:
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        chooseTarget()
        pass

```

the second is to have the soldier attack the witches  
this has the desired effect but 2 problems  
1 the ogers kill the soldiers before they get to the witches  
2 it is really slow

```python
# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget():
    archers = hero.findByType("archer")
    for archer in archers:
        
        aenemy = archer.findNearestEnemy()
        hero.command(archer, "attack", aenemy)
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        
        Senemys = soldier.findEnemies()
        for senemy in Senemys:
            if senemy.type == "witch":
                hero.command(soldier, "attack", senemy)
                done = 1
        if done == 0:
            senemy2 = soldier.findNearestEnemy()
            hero.command(soldier, "attack", senemy2)
                

while True:
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        chooseTarget()
        pass

```

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 2, 2020, 5:40pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/2 "2020-06-02T17:40:13Z")

</div>

archers should target ogres, soldiers should target witches as witches can cast heal on ogres

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 2, 2020, 5:53pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/3 "2020-06-02T17:53:24Z")

</div>

the archers should attack the ogres before the throwers?

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 2, 2020, 5:59pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/4 "2020-06-02T17:59:22Z")

</div>

here is the code adapted to your idea  
the soldiers attack witches  
and the archers attack ogres or if no ogres defend a point in the middle

```python
# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
def chooseTarget():
    archers = hero.findByType("archer")
    for archer in archers:
        
        aenemy = archer.findNearestEnemy()
        if aenemy.type == "ogre":
            
            hero.command(archer, "attack", aenemy)
        else:
            hero.command(archer, "defend", {'x':39,'y':39})
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        
        Senemys = soldier.findEnemies()
        for senemy in Senemys:
            if senemy.type == "witch":
                hero.command(soldier, "attack", senemy)
                done = 1
        if done == 0:
            senemy2 = soldier.findNearestEnemy()
            hero.command(soldier, "attack", senemy2)
                

while True:
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        chooseTarget()
        pass

```

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:20pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/5 "2020-06-04T16:20:06Z")

</div>

for the record this code did not work

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:40pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/6 "2020-06-04T16:40:10Z")

</div>

archers attacks nearest enemy

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:41pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/7 "2020-06-04T16:41:55Z")

</div>

i did that the archers kill throwers than try to kill ogres meanwhile the soldiers go for the witches are kill by the ogres and then the witches kill archers

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:42pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/8 "2020-06-04T16:42:21Z")

</div>

paladins should heal soldiers

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:44pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/9 "2020-06-04T16:44:59Z")

</div>

wait a minute there are paladins? i dont see any

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:46pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/10 "2020-06-04T16:46:26Z")

</div>

oops, i thought it was summit’s gate

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:47pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/11 "2020-06-04T16:47:49Z")

</div>

mine worked just fine

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:48pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/12 "2020-06-04T16:48:33Z")

</div>

here’s how i find em

```python
if friend and friend.type is "soldier" and enemy and witch and witch.health > 0:
        hero.command(friend, "attack", witch)
    if friend and friend.type is "archer" and enemy:
        self.command(friend, "attack", enemy)

```

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:50pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/13 "2020-06-04T16:50:19Z")

</div>

i think the problem is that the wiches heal and therefore i cant kill anything without losing friends to the witches poison

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:50pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/14 "2020-06-04T16:50:33Z")

</div>

witches can’t heal themselves

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:51pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/15 "2020-06-04T16:51:09Z")

</div>

they heal each other so even if everyone attacks a witch the other will heal them

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:51pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/16 "2020-06-04T16:51:11Z")

</div>

in my code, 4 soldiers survived and all archers survived

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:51pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/17 "2020-06-04T16:51:33Z")

</div>

then the archers can kill the ogres and then kill

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:52pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/18 "2020-06-04T16:52:04Z")

</div>

did you use the emperors gloves? if the enemyies are close enogh u can cast it

---

<div class="post-metadata">

**Author:** ![EpicCoder999](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/epiccoder999/32/12768_2.png) [@EpicCoder999](https://discourse.codecombat.com/u/EpicCoder999)\
**Post date:** [June 4, 2020, 4:53pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/19 "2020-06-04T16:53:05Z")

</div>

no i didn’t, after the def, you should use for loop

```python
while True:
    friends = self.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        chooseTarget(friend)

```

---

<div class="post-metadata">

**Author:** ![Tv264](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tv264/32/12559_2.png) [@Tv264](https://discourse.codecombat.com/u/Tv264)\
**Post date:** [June 4, 2020, 4:53pm UTC](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977/20 "2020-06-04T16:53:57Z")

</div>

yeah i am using it, it was there at the start

[Next page](https://discourse.codecombat.com/t/solved-toil-and-trouble-soldiers-die/21977.md?page=2)
