# \[Solved\] Sowing fire help!

**URL:** <https://discourse.codecombat.com/t/solved-sowing-fire-help/11019>\
**Category:** Uncategorized\
**Created:** [March 27, 2017, 3:40am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019 "2017-03-27T03:40:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [March 27, 2017, 3:40am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/1 "2017-03-27T03:40:40Z")

</div>

this is my code(I can’t build the fire traps):

```python
# Goal: build three rows of nine fire-traps.

# Returns "retreat", "attack", "start-next-trap-column", or "build-next-trap-in-column"
def chooseStrategy():
    enemies = hero.findEnemies()
    
    # If there are overwhelming ogre forces, return the "retreat" strategy.
    if len(enemies) > 20:
        return "retreat"
    
    if len(enemies)<20:
        return "attack"
    # Use x % 9 is 0 to see if x is divisible by 9.
    # Use len(self.built) to see how many traps you have built.
    # If you have finished a column of 9 traps, return "start-next-trap-column"
    
    
    # Otherwise, return "build-next-trap-in-column"
    else:
        return "build-next-trap-in-column"

trapsInColumn = 9
startX = 40
columnX = startX

# Build the next trap in a column in the correct place.
def buildNextTrapInColumn(columnX,numTraps):
    # Change newY to use % to wrap around and only build trapsInColumn (9) traps per column
    newY = 7 * (numTraps%trapsInColumn) + 10
    if hero.pos.y < newY:
        hero.move({"x": columnX - 5, "y": newY})
    else:
        buildTrap(columnX,newY)

# Start a new column of traps.
def startNextTrapColumn(columnX, numTraps):
    newX = startX - (Math.floor(numTraps / trapsInColumn) * 6)
    if hero.pos.y > 10:
        hero.move({"x": newX - 5, "y": 10})
        return columnX
    else:
        buildTrap(newX,10)
        return newX

def buildTrap(x, y):
    hero.buildXY("fire-trap", x, y)

def commandAttack():
    # Have your griffin riders fend off the attackers.
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        
        if friend.type=="griffin-rider":
            hero.command(friend, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    hero.moveXY(4, 42)
    hero.command(friend, "move", hero.pos)

while True:
    strategy = chooseStrategy()
    if strategy == "attack":
        commandAttack()
    elif strategy == "build-next-trap-in-column":
        buildNextTrapInColumn(columnX, len(hero.built))
    elif strategy == "start-next-trap-column":
        columnX = startNextTrapColumn(columnX, len(hero.built))
    elif strategy == "retreat":
        commandRetreat()

```

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [March 27, 2017, 7:25am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/2 "2017-03-27T07:25:58Z")

</div>

Or this doesn’t work:

```python
def chooseStrategy():
    enemies = hero.findEnemies()
    
    # If there are overwhelming ogre forces, return the "retreat" strategy.
    if len(enemies) > 20:
        return "retreat"
    if len(enemies)<20:
        return "attack"
    # Use x % 9 is 0 to see if x is divisible by 9.
    # Use len(self.built) to see how many traps you have built.
    # If you have finished a column of 9 traps, return "start-next-trap-column"
    if (x%9)==0:
        return "start-next-trap-column"
    
    
    
    else:
        return "build-next-trap-in-column"

trapsInColumn = 9
startX = 40
columnX = startX

# Build the next trap in a column in the correct place.
def buildNextTrapInColumn(columnX,numTraps):
    # Change newY to use % to wrap around and only build trapsInColumn (9) traps per column
    newY = 7 * (numTraps%trapsInColumn) + 10
    if hero.pos.y < newY:
        hero.move({"x": columnX - 5, "y": newY})
    else:
        buildTrap(columnX,newY)

# Start a new column of traps.
def startNextTrapColumn(columnX, numTraps):
    newX = startX - (Math.floor(numTraps / trapsInColumn) * 6)
    if hero.pos.y > 10:
        hero.move({"x": newX - 5, "y": 10})
        return columnX
    else:
        buildTrap(newX,10)
        return newX

def buildTrap(x, y):
    hero.buildXY("fire-trap", x, y)

def commandAttack():
    # Have your griffin riders fend off the attackers.
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        
        if friend.type=="griffin-rider":
            hero.command(friend, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    hero.moveXY(4, 42)
    hero.command(friend, "move", hero.pos)

while True:
    strategy = chooseStrategy()
    if strategy == "attack":
        commandAttack()
    elif strategy == "build-next-trap-in-column":
        buildNextTrapInColumn(columnX, len(hero.built))
    elif strategy == "start-next-trap-column":
        columnX = startNextTrapColumn(columnX, len(hero.built))
    elif strategy == "retreat":
        commandRetreat()

```

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [March 27, 2017, 1:12pm UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/3 "2017-03-27T13:12:10Z")

</div>

> [@Chaboi\_3000](#):
>
> enemy = friend.findNearestEnemy()

You need an `if enemy`

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [March 28, 2017, 8:44am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/4 "2017-03-28T08:44:46Z")

</div>

I can command my griffin riders to attack but there is something wrong here:  
`if (x%9)==0: return "start-next-trap-column"`

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [March 30, 2017, 4:56pm UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/5 "2017-03-30T16:56:05Z")

</div>

> [@Chaboi\_3000](#):
>
> x%9

Was that supposed to be there? Also is `return "start-next-trap-column"` an actual command? Or just a method?

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [March 31, 2017, 6:22am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/6 "2017-03-31T06:22:04Z")

</div>

It is. Scroll down to see more code.

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [April 1, 2017, 7:57am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/7 "2017-04-01T07:57:05Z")

</div>

I found out that I just had to do:

```python
if len(self.built)%trapsInColumn==0:
    return "start-next-trap-column"

```

---

<div class="post-metadata">

**Author:** ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)\
**Post date:** [April 1, 2017, 5:03pm UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/8 "2017-04-01T17:03:36Z")

</div>

Yeah. Just figured that out but you beat me to the response. Nice Job!

---

<div class="post-metadata">

**Author:** ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)\
**Post date:** [April 1, 2017, 8:47pm UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/9 "2017-04-01T20:47:52Z")

</div>

One more thing. I have a question. Do you know in the choose strategy function what to do in the if statements. My character just always commands the griffin riders to attack but

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [April 2, 2017, 5:15am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/10 "2017-04-02T05:15:44Z")

</div>

hmm, you need to check if there is actually an enemy. Otherwise just let the hero build.

---

<div class="post-metadata">

**Author:** ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)\
**Post date:** [April 2, 2017, 6:00am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/11 "2017-04-02T06:00:48Z")

</div>

Thanks a lot! I added an if statement for that and the code worked! I knew something was missing!

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [April 2, 2017, 9:11am UTC](https://discourse.codecombat.com/t/solved-sowing-fire-help/11019/12 "2017-04-02T09:11:06Z")

</div>

I’m so happy it worked!
