Sowing fire python

pls help i am stuck on sowing fire my code is here:

# 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"
    if x % 9 and len(self.built):
        return "start-next-trap-collumn"
    # 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 = 9 * numTraps + 10 # ∆ 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():
    friend = hero.findFriends()
    # Have your griffin riders fend off the attackers.
    hero.command(friend, "attack", hero.findNearestEnemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    hero.command(friend, "move", {"x": 4, "y": 42})
    hero.moveXY(4, 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()

anything helps thanks

Hi @cheddarcheese, I think the issues in your code are here:

This should be changed to:

if len(enemies) > 0:
        return "attack"

The next issue in your code is here:

This should be changed to:

if len(self.built) % 9 == 0:

The next issue is with your retreat function and your attack function. (They have the same problem, so I explained the issue for one of them and you can do the same thing for the other one)

This function isn’t actually going to make all of your friends retreat, you need to iterate through them like this:

for friend in friends():

Try this out and see if it fixes the problem. Hope this helps!
Grzmot

1 Like

thanks @grzmot! :smile:

Did you complete the level?
Grzmot

no the ogres run into the fire-traps

Could you please post your most recent code now?
Grzmot

# Goal: build three rows of nine fire-traps.
hero.summon("soldier")
hero.summon("soldier")
hero.buildXY("fire-trap", 33, 6)

# 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) < 0:
        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 len(self.built) % 9 == 0:
        return "start-next-trap-collumn"
    # 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 = 9 * numTraps + 10 # ∆ 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    # Have your griffin riders fend off the attackers.
    for friend in friends:
        hero.command(friend, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    hero.command(friend, "move", {"x": 4, "y": 42})
    hero.moveXY(4, 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()

You can take the first 3 line of your code out, you don’t need to summon soldiers or place traps.
Grzmot

i did that it still does not work

my hero does not build anything now or do anything

This should say:

if len(enemies) > 0:

Small mistake, you just need to turn the sign around
Grzmot

oh it helps its just that my hero has a big x under it but has no error

This should say:

newY = 7 * (numTraps % 9) + 10
1 Like

there is still a big x under my hero

You need to turn the sign around for this one.
Grzmot

it says “try hero.findFriends”

the error says try hero.find friends

For your retreat function, your iteration should look something like this:

for friend in hero.findFriends():

Grzmot

now it says need an object

For the retreat function?
Grzmot