Sowing Fire (Python)

The strat-choosing code and the griffin-riders-attacking code works, but t the start-next-trap-column thing doesn’t. 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) < 21:
        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-colum"
    # Otherwise, return "build-next-trap-in-column"
    else:
        return "build-next-trap-in-column"
trapsInColumn = 9
columnX = 40
# 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.
    riders = hero.findByType("griffin-rider", hero.findFriends())
    for rider in riders:
        enemy = rider.findNearestEnemy()
        if enemy:
            hero.command(rider, "attack", enemy)
def commandRetreat():
    hero.say("Retreat!")
    # You and your griffin riders retreat to safety behind the traps.
    hero.blink({"x": 4, "y": 42})
    riders = hero.findByType("griffin-rider", hero.findFriends())
    for rider in riders:
        hero.command(rider, "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()
1 Like

can you send me a link to the level

1 Like

https://codecombat.com/play/level/sowing-fire?

1 Like

sorry it’s a subscriber level so i can’t

1 Like

There are two problems. Firstly a typo:

(hint, only one letter is wrong).
Secondly, as you’ll see when you run your changed code, startX isn’t defined.

Danny

4 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.