[SOLVED] Help! Reaping Fire [Python]

Code
def chooseStrategy():
    enemies = hero.findEnemies()
    fangriders = hero.findByType("fangrider")
    for fangrider in fangriders:
        if fangrider.pos.x > 34:
            return "fight-back"
    # 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"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    
    # Command your griffin riders to attack ogres.
    friends = hero.findFriends()
    for friend in friends:
        enemyF = friend.findNearestEnemy()
        if friend.type == "griffin-rider":
            hero.command(friend, "attack", enemyF)
    pass
    
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        itemPos = item.pos
        hero.move(itemPos)
    pass
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy.type == "fangrider":
            if hero.isReady("throw"):
                hero.throw(enemy)
            else:
                hero.scattershot(enemy)
                hero.attack(enemy)
    pass
    
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        if hero.gold >= hero.costOf("griffin-rider"):
            
            hero.summon("griffin-rider")
        commandAttack()
    elif strategy == "fight-back"

        heroAttack()
    elif strategy == "collect-coins":
        pickUpCoin()

Equipment

Error

None, but my hero runs into the fire-traps

Link

https://codecombat.com/play/level/reaping-fire?

Sorry for posting so often.
Lydia

3 Likes

YOu could just plot all the coardinates. Hand code them

1 Like

Thank you for the suggestion, but I want to solve the level correctly.
Lydia

2 Likes

Can you send a link to the level, please?

2 Likes

Where is the level again?

2 Likes

Here @PeterPalov and @MrVictor19.
Lydia

3 Likes

Thanks!
For some reason I have β€œincomplete” here:


:frowning:

1 Like

Hmm, some guys were cheating:


I think this have to be fixed.

1 Like

have u solved the lvl @Lydia_Song? and if not can u post your current code?

1 Like

that is what the forum is for asking for help when needed so no need to be sorry for posting so often

3 Likes

This is my current code.

def chooseStrategy():
    enemies = hero.findEnemies()
    fangriders = hero.findByType("fangrider")
    for fangrider in fangriders:
        if fangrider.pos.x > 34:
            return "fight-back"
    # 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"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    
    # Command your griffin riders to attack ogres.
    friends = hero.findFriends()
    for friend in friends:
        enemyF = friend.findNearestEnemy()
        if friend.type == "griffin-rider":
            hero.command(friend, "attack", enemyF)
    pass
    
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        itemPos = item.pos
        hero.move(itemPos)
    pass
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy.type == "fangrider":
            if hero.isReady("throw"):
                hero.throw(enemy)
            else:
                hero.scattershot(enemy)
                hero.attack(enemy)
    pass
    
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        if hero.gold >= hero.costOf("griffin-rider"):
            
            hero.summon("griffin-rider")
        commandAttack()
    elif strategy == "fight-back":

        heroAttack()
    elif strategy == "collect-coins":
        pickUpCoin()

Lydia

1 Like

@Lydia_Song, It looks like there are a couple of issues in your code just because you mixed up the order of them, but one main thing I see is this:

You don’t actually want to use else if (elif) statements for this, you only want to use if statements. If you would like to me to explain why you only want to use if statements, then I can explain that to you. Your code should look more like this:

while True:
    pickUpCoin()
    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()

Another issue I see is this function:

It is up to you, but you might want to try using a warrior to simplify this because you only need a basic attack. You also don’t actually need to all that checking, because i’m pretty sure you did it before, so you should simplify your code to look more like this:

def heroAttack():
    # Your hero should attack fang riders that cross the minefield
    enemy = hero.findNearest(hero.findEnemies())
    if enemy and hero.distanceTo(enemy) < 30 and enemy.pos.x < 38:
        hero.attack(enemy)

Hope this helps!
Grzmot

Now my hero won’t even collect coins. The minefield gets blown up in seconds.
Lydia

I think that this happens because this function has some issues:

Your order of these commands is mixed up and you shouldn’t summon a griffin rider here. I think that you should change your code to look something more like this:

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

Hope this helps!
Grzmot

1 Like

@Lydia_Song I think you should try and summon the griffin-riders before the fang-rider part? And there is also some code you need to refigure.
Here’s what I mean:

Instead of that, put this:

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"
    fangrider = None
    fangrider = hero.findNearest(hero.findByType("fangrider", hero.findEnemies()))
    if fangrider and fangrider.pos.x <= 36:
        return "fight-back"
    # Otherwise, return "collect-coins"
    else:
        return "collect-coins"
2 Likes

Thanks @MrVictor19! I solved the level now!
Lydia

3 Likes

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