[Solved] Sowing fire, please help

# 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) < 20 and len(enemies) > 1:
        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 % 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.
    for friend in hero.findFriends():
        if friend.type == "griffin-rider":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
    
    pass

def commandRetreat():
    for friend in hero.findFriends():
        if friend.type == "griffin-rider":
            
            hero.say("Retreat!")
            hero.command(friend, "move", {"x": 4, "y": 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()

A lone munchkin is not registered by the griffin riders.

can you send a link pls

CodeCombat - Coding games to learn Python and JavaScript?

1 Like

Sorry im not a subscriber i cant help :frowning:

can you show us a screen shot please of what happens

try doing this def commandAttack(): friends = hero.findFriends() enemy = hero.findNearestEnemy() # Have your griffin riders fend off the attackers. for friend in friends: hero.command(friend, "attack", enemy) pass
instead of this

Hi QinWentian,

The reason that nothing happens when there is only one enemy is the following line:

You have got that the number of enemies needs to be greater than 1. How do you write greater than or equal to?

Jenny

you do this ____ >= ____ @QinWentian I hope this helps

Or just greater than 0.

That would mean that it would have to either greater than 0 or = to but it will always be greater so it kind of causes a bug that is why you do >= 1 instead

More than zero is the same as more than or the same as 1.

Stay on topic @Dragonlouis

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