# \[SOLVED\] Hunter and Prey Help

**URL:** <https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047>\
**Category:** Level Help\
**Created:** [September 27, 2018, 1:55pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047 "2018-09-27T13:55:04Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 14, 2020, 3:03pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/41 "2020-07-14T15:03:36Z")

</div>

sorry that seems like a riddle what

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 14, 2020, 3:04pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/43 "2020-07-14T15:04:39Z")

</div>

Define enemy as a person, not a distance. Put the distance line inside the `if enemy` line

---

<div class="post-metadata">

**Author:** ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)\
**Post date:** [July 15, 2020, 2:28am UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/44 "2020-07-15T02:28:29Z")

</div>

[en\_US.composer.my\_button\_text]

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:37pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/45 "2020-07-15T14:37:10Z")

</div>

so now ive got this as my code so far

```python
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    friendType = hero.findByType("friendType", hero.findFriends())
    if enemy == hero.findNearestEnemy() > 25:
        hero.command(archer, "target", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friends)
            pass

```

but now this line

```python
    if enemy == hero.findNearestEnemy() > 25:

```

has an error saying there is no enemy  
@Anna

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 15, 2020, 2:39pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/46 "2020-07-15T14:39:10Z")

</div>

first define the variable enemy before you do anything with an if. then say something like

`if hero.distanceTo(enemy) < 25:`

for the distance line

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:40pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/47 "2020-07-15T14:40:02Z")

</div>

how do i do that again? define a variable

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 15, 2020, 2:40pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/48 "2020-07-15T14:40:53Z")

</div>

to define an enemy type `enemy = hero.findNearestEnemy()`

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:41pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/49 "2020-07-15T14:41:21Z")

</div>

i already have that (20)

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 15, 2020, 2:42pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/50 "2020-07-15T14:42:29Z")

</div>

In the function `commandArcher(archer)`?

It has to be separate from your if’s because that will confuse the hero/archer.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:43pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/51 "2020-07-15T14:43:12Z")

</div>

> [@098765432123](#):
>
> `friendType = hero.findByType("friendType", hero.findFriends())`

Instead of this, put that enemy is archer’s nearest enemy.

> [@098765432123](#):
>
> ` == hero.findNearestEnemy()`

And you now do not need this part of the code, do you?

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:46pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/52 "2020-07-15T14:46:47Z")

</div>

so let me get this right you want me to put this

```python
enemy = archer.findNearestEnemy()

```

instead of this?

```python
friendType = hero.findByType("friendType", hero.findFriends())

```

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:47pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/53 "2020-07-15T14:47:32Z")

</div>

and im pretty sure i need

```python
== hero.findNearestEnemy()

```

i can try without it

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:47pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/54 "2020-07-15T14:47:37Z")

</div>

Yes, that is what I mean.

Andrei

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:48pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/55 "2020-07-15T14:48:05Z")

</div>

Try without it and see if it workes. 😉

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:48pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/56 "2020-07-15T14:48:22Z")

</div>

well in that case there is an error saying archer has no method

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:48pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/57 "2020-07-15T14:48:50Z")

</div>

Can I see your code?

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:49pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/58 "2020-07-15T14:49:07Z")

</div>

```python
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearestEnemy()
    if enemy:
        hero.command(archer, "target", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friends)
            pass

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:49pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/59 "2020-07-15T14:49:56Z")

</div>

> [@098765432123](#):
>
> `"target"`

Why do you use the target command that does not exists when you want to command to attack?

Andrei

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [July 15, 2020, 2:50pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/60 "2020-07-15T14:50:33Z")

</div>

where is that in the code?

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 15, 2020, 2:51pm UTC](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047/61 "2020-07-15T14:51:29Z")

</div>

> [@098765432123](#):
>
> `hero.command(archer, "target", enemy)`

Right here is where you should make the change.

Andrei

[Previous page](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047.md?page=2)

[Next page](https://discourse.codecombat.com/t/solved-hunter-and-prey-help/16047.md?page=4)
