[SOLVED] Help with sowing fire

I am not building traps, do you know why. Thanks!

# 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 there are some ogres, return the "attack" strategy.
    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"
    x = len(self.built) % 9
    if x == 0:
        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 = numTraps % 9 # ∆ Change this to use % 9!
    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 = hero.findNearestEnemy()
        if enemy:
            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)
    friends = hero.findFriends()
    for friend in friends:
        hero.command(friend, "move", {"x": 4, "y": 42})

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()

This is a problem I found as well, It’s a very simple solution once you know what it is but before that it seems impossible, It’s just one mistake in the chooseStrategy() which is you need to check if there’s an enemy or enemies as well as checking if the len(enemies) < 20: because if there are no enemies then that still counts as len(enemies) < 20 because 0 enemies is still < 20. I think everything else is right.
Hope this helps!

I did what you said. But when I run the code my hero moves places the firetrap than runs into it.
This is the code I used.

if len(enemies) < 20 and len(enemies) != 0:
        return "attack"

Sorry, there was one other thing I forgot, It’s the newY = numTraps % 9 which is wrong, it should also have 7 * and + 10 before and after it you don’t delete anything that was there before you just add (numTraps % 9) in the middle. Sorry I just forgot about that completely. :roll_eyes:

Thanks! It is working now!