Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Sowing Fire Level Help!

I’ve looked at numerous topics, but I can’t seem to understand what I should do and where my code needs help. Can anybody help? Thank you! :frowning_face:

# Goal: build three rows of nine fire-traps.
trapsInColumn = 9
# 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) % trapsInColumn
    if x == 0:
        return"start-next-trap-collumn"
    # Otherwise, return "build-next-trap-in-column"
    else:
        return "build-next-trap-in-column"


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 # ∆ 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:
        enemies = hero.findEnemies()
        for enemy in enemies:
            hero.command(friend, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    friends = hero.findFriends()
    for friend in friends:
        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()

Hi @KalPal, I can see one big error:

It’s just a typo.
I’ll let you try and figure out the rest. If you can’t post your code again.
Danny

Thanks for the correction, but my hero still isn’t building traps. What else needs to be fixed?

# Goal: build three rows of nine fire-traps.
trapsInColumn = 9
# 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) % trapsInColumn
    if x == 0:
        return "start-next-trap-column"
    # Otherwise, return "build-next-trap-in-column"
    else:
        return "build-next-trap-in-column"


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 # ∆ 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:
        enemies = hero.findEnemies()
        for enemy in enemies:
            hero.command(friend, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    friends = hero.findFriends()
    for friend in friends:
        hero.command(friend, "move", {'x':4, 'y':42})
    hero.moveXY(4, 42)

This is partially correct, but you need to also make sure there are some ogres in this statement. Otherwise the code will see that 0 < 20, and will keep running the attack function.

1 Like

So first check if the len(enemies)>0 and thrn if len(enemies)<20.

Here try to find each friend’s nearest enemy and check if it exists and if so, command the friend to attack that enemy.
Do you need any more assistance at this level?

No, thank you all so much for all your help!

1 Like