Reaping Fire Python - Almost Got It! (but a little help needed)

I am struggling to complete reaping fire with this code in Python. The main code is below and beneath it I will identify the problem areas.


def chooseStrategy():
    enemies = hero.findEnemies()
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold >= hero.costOf("griffin-rider"):
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    if hero.findByType("fangrider") > 0 and fangrider[0].pos.x < 38:
        return "fight-back"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    griffins = hero.findFriends()    
    for rider in griffins:
        enemies = rider.findEnemies()
        for enemy in enemies:
            fangrider = hero.findByType("fangrider")
            if enemy != fangrider:
                hero.command(rider, "attack", enemy)
    pass
    
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    pass
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    fangs = hero.findByType("fangrider")
    for fang in fangs:
        fangPos = fag.pos.x
        if fangPos < 36: 
            while fang.health > 0:
                hero.attack(fang)
    pass
    
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        hero.summon("griffin-rider")
    if strategy == "fight-back":
        heroAttack()
    if strategy == "collect-coins":
        pickUpCoin()

The first problem lies below. In def commandAttack() my griffin-riders do attack enemies, however I cannot get them to not attack the fang-riders, I wish for them to only attack the ground mobs.

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

The second problem is with the code for def heroAttack() as seen below. I cannot get my hero to attack the fang-riders, he simply carries on collecting coins and summoning griffin-riders.

def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    fangs = hero.findByType("fangrider")
    for fang in fangs:
        fangPos = fag.pos.x
        if fangPos < 36: 
            while fang.health > 0:
                hero.attack(fang)

Any help regarding these solutions would be welcomed!

1 Like

Well, in order to check the type of the enemy, your code needs an API reference (like .type, .team, .pos, etc.) You are simply checking if the enemy is not a fangrider, but you need to check whether or not the enemy’s type is fangrider. So in this case, you could use if enemy.type != "fangrider" and remove fangrider = hero.findByType("fangrider").

What is fag.pos.x? :wink: Sometimes the biggest problems are the smallest.