Sowing with fire help


#1
# 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 "munchkin" in enemies:
        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 = 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:
        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.
    

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

Tried a few variations but cannot get my griffin riders to attack?


#2

The reason you’re Griffs aren’t attacking is because the chooseStrategy() function never returns attack. What you want to do is to check if there are any at all. (use len())
Hope this helps, (sorry I can’t see if there are any other problems right now because I don’t have time)
:lion: :lion: :lion:


#3

Thanks I saw you mention this before about this level. Changing to if a Len of enemies is > 0 works. Although the level instructions state if the enemies contain ‘ogres’. So technically this is not what it specifies but unless anyone can say anything better I am done with struggling with this one! ta


#4

Yeah loads of people find this hard, you have to be flexible, you can’t take it exactly as it says it. That’s important in coding.
And if you wanted to actually do exactly what it says, you could, like this:

for enemy in enemies:
    if enemy and enemy.type == "munchkin":
        return "attack"
#whereas this:
if "munchkin" in enemies:
    return "attack"
#just won't work, because you haven't put .type in. And "munchkin" is the enemy's type property.

I spent ages on this level, you just need to take time on it.
There are a few of other topics which I looked at when other people had problems, and I got it from there.
:lion: :lion: :lion: