[SOLVED] Sowing Fire Hero won't build traps

Hi …
apparently there’s something wrong with my code as the hero just won’t build any trap but stand there all the time. Could someone be so kind to take a look and point out what went wrong? Many thanks!!

``````# Goal: build three rows of nine fire-traps.
def chooseStrategy():
enemies = hero.findEnemies()

# If there are overwhelming ogre forces, return the "retreat" strategy.
if enemies and len(enemies) > 20:
return "retreat"
# If there are some ogres, return the "attack" strategy.
if enemies 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(hero.built) % trapsInColumn == 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
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 i in range(len(friends)):
friend = friends[i]
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
pass

def commandRetreat():
hero.say("Retreat!")
hero.moveXY(4, 42)
# You and your griffin riders retreat to safety behind the traps.
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
hero.command(friend, "move", hero.pos)

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

``````

Are you using the hammer?

1 Like

yes, I am using “Wooden builder’s hammer” which got these two methods: buildTypes, buildXY(buildType, x, y)
I also tried using other hero like Pender but got the same result.

By re-digging some related posts, I realized that we need to change a little bit on the following method:

(Solution removed)

this is the standard method given by the level, however, the trick is if we do not add a condition related with enemy, the method will always return strategy as “attack” as the following condition will always stay true because the array - “enemies” can be empty (thus true) from the start of the level and while all enemies are dead.

(Solution removed)

So I did changed the method and put an object ‘enemy’ there, so when there is no enemy or all enemies are dead, this condition can stay False and allow the hero to build traps.

(Solution removed)

As you can see I have also changed the ‘hero.built’ to ‘hero.findhazards()’ as people in previous posts mentioned that the hero.built methold will count newly summoned unit(s) as built. (if you do not summon new units that’s fine)

Now I have passed this level:grin:
Thanks Ironhead and all posters related to this topic before

2 Likes

I’m super glad that you figured that out. Well done, but please don’t post solutions on this board. The purpose of this board is to help people learn and simply providing solutions is counterproductive to that goal. Thank you.

3 Likes

Nice job doing it by yourself.

1 Like

oh! I will be careful not to do that in the future, thanks MunkeyShynes!

Thanks TheDemonPrince! Yeah am very glad I did that

@taobo33 is right i want him to build a fire trap but it doesn’t work my hero just stays in place!?

Hi @Erhan_Akun, please could you post your code so we can see the problem.
Thanks
Danny