Sowing Fire Python Help! (Solved)

I have the strangest problem in this level. Everything works as it should, but a munchkin gets away from my griffin riders, and detonates the traps!

Here is my code:

# 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) > 0 and 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 len(self.built) % 9 == 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 % 9) + 10 # ∆ Change this to use % 9! # I did!
    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.
    riders = hero.findByType("griffin-rider")
    for rider in riders:
        enemy = rider.findNearestEnemy()
        if enemy:
            hero.command(rider, "attack", enemy)
    pass

def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    riders = hero.findByType("griffin-rider")
    for rider in riders:
        hero.command(rider, "move", Vector(4, 42))

#def summonGriffins():
#    if hero.gold >= hero.costOf("griffin-rider"):
#        hero.summon("griffin-rider")

while True:
    strategy = chooseStrategy()
    #summonGriffins()
    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()

Here is a screenshot:
image
My equipment:

The problem could possibly be here, you should use hero.moveXY({"x": newX - 5, "y": 10})
You forgot about the ‘XY’ :wink:

I don’t see anything wrong yet. (My eyes just took a look) May be a while before good coders come

Umm, that would throw an error, since moveXY takes 2 arguments: x and y, not a single object. Also, move is better for this.

You just need a faster hero :slight_smile:

this isn’t about the hero, it’s about the griffins. They can’t catch the munchkin fast enough

i see wat you mean @moonwatcher348

True, then use a wizard and summon skeletons.

if i summon then the hero skips a mine and leaves a gap

Hmmm, idk then, I haven’t completed it yet either… maybe… try summoning more griffin riders?

Ah, I know, set the x position to like 10 for the closest row…

oops, all I had to do was submit and it randomized the seed, and I won. Wow I didn’t think it would be that simple XD… thanks for helping anyway!

1 Like

but to get it every time do
if enemy and friend.distanceTo(enemy)<30:
attack
else:
defend (somewhere far from mines)