Help with Reaping Fire


#1

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)        


#2

I think you forgot to put something there…


#3

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 ?


#4

I don’t think it is…


#5

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


#6

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


#7

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


#8

Good Job! [20 Characters]


#9

Thank you :slight_smile:

[20 characters]