Help with Reaping Fire

Hi I do not pass this level and I don’t know what is not good in my code,

My Griffin-riders don’t attack and my hero neither…

If somebody see something this will be very kind :slight_smile:

# The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.

def chooseStrategy(friends, enemies):
    # If there is a fangrider on your side of the mines, return "fight-back"
    for enemy in enemies:
        if enemy and enemy.type == "fangrider":
            return "fight-back"
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold > hero.costOf("griffin-rider"):
        return "griffin-rider"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack(friends, enemies):
    if friends and enemies:
        for friend in friends:
            for enemy in enemies:
                hero.command(friend, "attack", enemy)
            # Command your griffin riders to attack ogres.
        #    hero.command(friend, "attack", )
    else:
        pass
    
def pickUpCoin(items):
    # Collect coins
    bestRatio = 0
    bestCoin = None
    for item in items:
        if item.type == "gem" or item.type == "coin":
            radiusX = (hero.pos.x - item.pos.x)**2
            radiusY = (hero.pos.y - item.pos.y)**2
            radius = Math.sqrt(radiusX + radiusY)
            ratio = item.value / radius
            if ratio > bestRatio:
                bestRatio = ratio
                bestCoin = item
    return bestCoin

def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    enemy = hero.findNearest(hero.findEnemies())
    if enemy and hero.distanceTo(enemy) < 30:
        if hero.canCast("fear", enemy):
            hero.cast("fear", enemy)
            if hero.canCast("invisibility", hero):
                hero.cast("invisibility", hero)
        elif hero.canCast("drain-life", enemy):
            hero.cast("drain-life", enemy)
            if hero.canCast("invisibility", hero):
                hero.cast("invisibility", hero)
        elif hero.canCast("invisibility", hero):
                hero.cast("invisibility", hero)
        else:
            while enemy.health > 0:
                hero.attack(enemy)

while True:
#    commandAttack()
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    strategy = chooseStrategy(friends,enemies)
    hero.say(strategy)
    items = hero.findItems()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        hero.summon("griffin-rider")
        commandAttack(friends, enemies)
    elif strategy == "fight-back":
        heroAttack(enemies)
    elif strategy == "collect-coins":
        coin = pickUpCoin(items)
        hero.moveXY(coin.pos.x, coin.pos.y)        

1 Like

I think you forgot to put something there…

2 Likes

Hi and thank you :slight_smile:

Hum I write it like that for the case there is no friends nor enemies but perhaps it is not usefull ?

2 Likes

I don’t think it is…

2 Likes

I removed it but my problem is still here, my griffins don’t attack when enemies approach and landmines explodes

1 Like

Ok, I rewrite all the code and it is working now. I don’t pass the level but the griffins attacks.

It is very difficult to pass this level, I miss 10 seconds to get there.

# The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.

def chooseStrategy():
    enemies = hero.findEnemies()
    enemy = hero.findNearestEnemy()
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold > hero.costOf("griffin-rider") and enemy:
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    elif enemy and enemy.type == "fangrider":
        for enemy in enemies:
            if enemy.type == "fangrider":
                return "fight-back"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    # Command your griffin riders to attack ogres.
    for enemy in enemies:
        for friend in friends:
            if friend.type == "griffin-rider":
                if enemy.type != "fangrider":
                    hero.command(friend, "attack", enemy)

def pickUpCoin(items):
    # Collect coins
    bestRatio = 0
    bestCoin = None
    for item in items:
        if item.type == "gem" or item.type == "coin":
            radiusX = (item.pos.x - hero.pos.x)**2
            radiusY = (item.pos.y - hero.pos.y)**2
            radius = Math.sqrt(radiusX + radiusY)
            ratio = item.value / radius
            if ratio > bestRatio:
                bestRatio = ratio
                bestCoin = item
    hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)

def heroAttack(enemies):
    # Your hero should attack fang riders that cross the minefield.
    for enemy in enemies:
        if enemy.type == "fangrider" and enemy.pos.x < 30:
            while enemy.health > 0:
                hero.attack(enemy)

while True:
    strategy = chooseStrategy()
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    # Call a function, depending on what the current strategy is.
    if hero.findFriends and hero.findEnemies():
        commandAttack()
    if strategy == "collect-coins":
#        hero.say("collect-coins")
        pickUpCoin(hero.findItems())
    elif strategy == "griffin-rider":
#        hero.say("griffin-rider")
        hero.summon("griffin-rider")
        commandAttack()
    elif strategy == "fight-back":
#        hero.say("fight-back")
        heroAttack(hero.findEnemies())

1 Like

Allright… My bad… I pass it with the 60s bonus finally !

I made a mistake on the chooseStrategy “fangrider”… It is working properly now.

Cheers

2 Likes

Good Job! [20 Characters]

2 Likes

Thank you :slight_smile:

[20 characters]

2 Likes

Hi, can someone help me please.

# The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds.

def chooseStrategy():
    enemies = hero.findEnemies()
    enemy = self.findNearest(enemies)
    # If you can summon a griffin-rider, return "griffin-rider"
    if self.gold > self.costOf("griffin-rider") and enemy:
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    elif enemy and enemy.type == "fangrider":
        return " fight-back"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    for enemy in enemies:
        for friend in friends:
            if friend.type == "griffin-rider":
                if enemy.type != "fangrider":
                    hero.command(friend, "attack", enemy)
    pass
    
def pickUpCoin():
    # Collect coins
    bestRatio = 0
    bestCoin = None
    for item in items:
        if item.type == "gem"  or item.type == "coin":
            radiusX = (item.pos.x - hero.pos.x)**2
            radiusY = (item.pos.y - hero.pos.y)**2
            ratio = item.value / radius 
            if ratio > bestRatio:
                bestRatio = ratio
                bestCOin = item
    hero.moveXY(bestCoin.pos.x,bestCoin.pos.y)
    
        
    pass
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    for enemy in enemies:
        if enemy.type == "fangrider" and enemy.pos.x < 30:
            while enemy.health>0:
                hero.attack(enemy)
    pass
    
while True:
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    strategy = chooseStrategy()
    items = hero.findItems()
    if hero.findFriends() and hero.findEnemies():
        commandAttack()
    if strategy  == "collect-coins":
        pickUpCoin(hero.findItems())
    elif strategy == "griffin-rider":
        hero.summon("griffin-rider")
    elif strategy == "fight-back":
        heroAttack(hero.findEnemies())
   
    
    # Call a function, depending on what the current strategy is.
    

Hi, there are two (maybe more) problems with this:

  1. When you’re checking whether too return “fight-back”, you need to check 'If there is a fangrider on your side of the mines"
  2. In your commandAttack() function you’re using a for loop inside a for loop. This isn’t a very good way to command your griffins. In fact I don’t think it works at all. Instead command the friends to attack their nearest enemy, like in earlier levels.

There may be a few other errors, but fix the above ones first and then we’ll see if it works.
Danny

At the risk of adding confusion…another tactic can be sending your hero straight east; from his starting point, he has a clear path on the x axis, straight through the mines. If you decide to explore this idea, be sure to not stop until well clear of the blast radius of the mines; also keep in mind that you might be knocked around, even possibly back in to the mines if too close.

One down side of this is that your peasants will get taken out fairly quickly, so gold collection becomes quite slow. An up side is that the fang riders who cross over the mines get stuck on the west side, so are out of play, until the mines are taken out.

Thank you for that help.

One good (maybe not the most eloquent) is to follow the first paragraph of what @dedreous said then (if you have enough health) shield for the rest of the level’s eternity… because the ogres and munchkins will attack you instead of exploding the mines. This works as the goal is only to protect the mines!! :wink:

2 Likes

That is an excellent idea! I changed my code so that he would ignore any fangrider, and to shield once health was below 1000…it completed at 62 seconds with the mines still intact and there we about 7 fangriders trapped on the west side of the mines!

Otherwise, I summoned paladins, had them heal and protect the hero and ran thru all attack commands available…bash, cleave, cast chain-lightning, attack and of course, shield.