Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Reaping Fire (Python). Help!

Hi! My griffin-rider doesn’t move. I think that the mistake is in commandAttack(), but I don’t get what is wrong. Could you please give me an advice?

# 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.findNearest(enemies)
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    elif enemy and enemy.type == "fangrider" and hero.distanceTo(enemy) < 30:
        return "fight-back"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    for griffin in hero.findByType("griffin-rider"):
        if griffin:
            enemy = griffin.findNearestEnemy()
            if enemy: 
                hero.command(griffin, "attack", enemy)

def pickUpCoin():
    # Collect coins
    while True:
        item = hero.findNearestItem()
        if item:
            hero.move(item.pos)
            pass
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    target = hero.findNearest(hero.findByType("fangrider"))
    if target:
        if hero.distanceTo(target) < 20:
            hero.move(targetPos)
        if hero.canCast("chain-lightning", target):
            hero.cast("chain-lightning", target)
        else:
            hero.attack(target)
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        commandAttack()
    if strategy == "fight-back":
        heroAttack()
    if strategy == "collect-coins":
        pickUpCoin()

Actually, the While True: in the pickUpCoin() function is the problem. It created a loop that won’t let the other code run. Remove the one line and pull back the indentation on the rest and it will let the other functions run.

def pickUpCoin():
    # Collect coins
    while True: # delete and shift the indentation on the lines below
        item = hero.findNearestItem()
        if item:
            hero.move(item.pos)
            pass
1 Like

Hi! Thanks! It fixed my problem. But now, I’d like to improve my code by avoiding the enemy.type “fangrider” because, some times my “griffin-riders” attack them, the mines get unprotected and the other enemies arrive and everything explodes. I don’t want to use the if enemy.type != "fangrider" because I think that it makes my riders to wait until other enemy gets closer, so I just want them to avoid that type and pass to the next one.

def commandAttack():
    # Command your griffin riders to attack ogres.
    for griffin in hero.findByType("griffin-rider"):
        if griffin:
            enemy = griffin.findNearestEnemy()
            if enemy: 
                hero.command(griffin, "attack", enemy)
            

Have you tried using if enemy.type != "fangrider" yet?

Hi, yes, you were right. I got confused about that. It already works and I passed the level :man_facepalming:

Great. I’ll put a “[SOLVED]” on the title so people will know this topic is solved.

Thanks, I think that I already put it. :+1: