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

[SOLVED] Help with Reaping Fire please! (python)

The code runs okay, but the minefield is not surviving for 30 seconds… any help would be appreciated! As you can see, I’ve tried some things and then commented them out when I discovered they didn’t help.

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

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"

def commandAttack():
    # Command your griffin riders to attack ogres.
    riders = None
    riders = hero.findByType("griffin-rider", hero.findFriends())
    enemies = hero.findEnemies()
    if enemies:
        dist = 9999
        nearest = None
        for enemy in enemies:
            # Attack if it is not a fangrider
            if (not (enemy.type == "fangrider")) and hero.distanceTo(enemy) < dist:
                nearest = enemy
                dist = hero.distanceTo(enemy)
        if nearest and riders:
            for rider in riders:
                hero.command(rider, "attack", nearest)

    
def pickUpCoin():
    # Collect coins
    """
    # Find the "best" coin based on value and distance from hero.
    value = 0
    bestCoin = None
    coins = hero.findItems()
    if coins:
        for coin in coins:
            if coin.value / hero.distanceTo(coin) > value:
                value = coin.value
                bestCoin = coin
    if bestCoin:
        hero.move(bestCoin.pos)
    """
    coin = None
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
    
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    nearest = hero.findNearest(hero.findByType("fangrider", hero.findEnemies()))
    while nearest.health > 0:
        if hero.isReady("chain-lightning"):
            hero.cast("chain-lightning", nearest)
        else:
            hero.attack(nearest)

"""
def commandPeasants():
    peasants = None
    peasants = hero.findByType("peasant", hero.findFriends())
    if not peasants:
        if hero.gold >= hero.costOf("peasant"):
            hero.summon("peasant")
    if peasants:
        for peasant in peasants:
            pc = None
            pc = peasantCoin(peasant.pos.x, peasant.pos.y)
            if pc:
                hero.command(peasant, "move", pc.pos)

def peasantCoin(x1, y1):
    coins = hero.findItems()
    if coins:
        dist = 9999
        bestCoin = None
        for coin in coins:
            between = distanceBetween(coin.pos.x, coin.pos.y, x1, y1)
            if between < dist:
                dist = between
                bestCoin = coin
        if bestCoin:
            return bestCoin
        else:
            return False

def distanceBetween(x1, y1, x2, y2):
    deltaX = abs(x1-x2)
    deltaY = abs(y1-y2)
    return((deltaX**2)+(deltaY**2)**0.5)
"""

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

It works for me, using Tharin. Could you post a screenshot of your equipment and the level screen so I can see the problem.
Thanks
Danny

1 Like

hey could anyone help me on one of my posts for vital powers?

Sure, thanks! Here’s a screenshot of my equipment. I just tried it with Tharin as well, and it failed.

I’ve cleaned up my code, but it’s still not surviving the 30 seconds.
I’ve also upgraded my boots but no luck.
posting updated info…

# hero.summon("peasant")
while True:
    # commandPeasant()
    summonRider()
    commandRiders()
    heroAttack()
    getCoin()

def distanceBetween(x1, y1, x2, y2):
    deltaX = abs(x1-x2)
    deltaY = abs(y1-y2)
    return((deltaX**2)+(deltaY**2)**0.5)

def getCoin():
    value = 0
    bestCoin = None
    coins = hero.findItems()
    for coin in coins:
        if coin.value > value:
            bestCoin = coin
            value = coin.value
    if bestCoin:
        hero.move(bestCoin.pos)

def commandPeasant():
    peasant = None
    pCoin = None
    peasant = hero.findNearest(hero.findByType("peasant", hero.findFriends()))
    if peasant:
        pCoin = peasantCoin(peasant.pos.x, peasant.pos.y)
        if pCoin:
            hero.command(peasant, "move", pCoin.pos)


def commandRiders():
    riders = hero.findByType("griffin-rider", hero.findFriends())
    enemies = None
    enemies = hero.findEnemies()
    for i in range(len(riders)):
        rider = riders[i]
        targets = []
        for enemy in enemies:
            if not enemy.type == "fangrider":
                targets.append(enemy)
        if targets:
            target = hero.findNearest(targets)
            hero.command(rider, "attack", target)

def heroAttack():
    nearest = None
    nearest = hero.findNearest(hero.findByType("fangrider", hero.findEnemies()))
    if nearest and nearest.pos.x <= 36:
        if hero.isReady("chain-lightning"):
            hero.cast("chain-lightning", nearest)
        else:
            hero.attack(nearest)

def peasantCoin(x1, y1):
    coins = hero.findItems()
    if coins:
        dist = 9999
        bestCoin = None
        for coin in coins:
            between = distanceBetween(coin.pos.x, coin.pos.y, x1, y1)
            if between < dist:
                dist = between
                bestCoin = coin
        if bestCoin:
            return bestCoin
        else:
            return False

def summonRider():
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

hero

It still works for me, using your equipment. I don’t get the bonus, but it works.
Could you send me a screenshot of your level screen where you lose.

1 Like

Oh! Sure! Looking at it play out, it looks like the enemy fangriders are attacking my griffin riders on the way in and totally decimating them. Maybe I need to make them dodge? Did anyone else have to do that?

HMMMM OK

I was recently gifted a subscription to CodeCombat.

I was curious, so I went back and played this level again with the same code, same equipment, etc.

GUESS WHAT? IT WORKED. Not suspicious at all…

You could also let your riders fight the fangriders to give them a fighting chance. Instead of your code below, I’d look to see if the enemy.pos.x > 55 to append the targets. This allows your riders to fight the fangriders, but only on the right side of the minefield.

        for enemy in enemies:
            if not enemy.type == "fangrider":
                targets.append(enemy)

The biggest problem with this code is while being faulty it works!

    riders = hero.findByType("griffin-rider", hero.findFriends())
    # riders = hero.findByType("griffin-rider") # all griffins are friends
    # not an error but needless computations
      
    enemies = hero.findEnemies()
    if enemies: # will be always true even when there are no enemies
# code 
    if nearest and riders: # if nearest exists check will be always true
                  # if riders true even when there are no riders 
# code
    coins = hero.findItems()
    if coins: # always true even when there are no coins
# code
    peasants = hero.findByType("peasant", hero.findFriends())
    # not an error: simply hero.findByType("peasant")
    if peasants: # always true even when there are no peasants
# code
return((deltaX**2)+(deltaY**2)**0.5)    
# last round bracket is misplaced                  

see hero.findEnemies() Quiz