HELP Sarven Treasure PYTHON

Hi there!

I’m really newbie in coding, and I’m trying to do my best, learning by myself !
I’m stuck at the level Sarven Treasure !
Here is my code !

return  #Commented out to stop infinite loop.
# Recueillir 150 pièces d'or tout en évitant les ogres avec les téléporteurs
# Si vous gagnez , il devient plus difficile ( et plus gratifiant )
# Si vous perdez , vous devez attendre un jour avant de pouvoir soumettre à nouveau 
# Rappelez-vous, chaque soumission obtient une nouvelle amorce aléatoire
while True:
    coins = hero.findNearestItem()
    coinIndex = 0
    enemy = hero.findNearestEnemy()
    while 1000 > hero.health > 500 and coinIndex < len(coins):
        coin = coin[coinIndex]
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            goldCoin += 1
        elif enemy and hero.distanceTo(enemy) < 8 and hero.isReady("hide"):
            hero.hide()
        if enemy and hero.distanceTo(enemy) < 8:
            hero.attack(enemy)
    while hero.health < 500 and coinIndex < len(coins):
        coin = coin[coinIndex]
        if enemy:
            if hero.isReady("hide"):
                hero.hide()
            else:
                hero.attack(enemy)
        elif coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            goldCoin += 1

I don’t really understand everything I do, to be honnest, but with what i’m learned on the past level, this code should work …

Please could someone help me ?

To explain a bit more: the hero just doesn’t move at all

i don’t think this is a thing your hero can do

Yes my hero can do that but maybe I don’t need to use hero.isready ?

while True:
    coins = hero.findNearestItem()
    coinIndex = 0
    enemy = hero.findNearestEnemy()
    while 1000 > hero.health > 500 and coinIndex < len(coins):
        coin = coin[coinIndex]
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            goldCoin += 1
        if enemy and hero.distanceTo(enemy) < 8:
            hero.attack(enemy)
    while hero.health < 500 and coinIndex < len(coins):
        coin = coin[coinIndex]
        if enemy:
            hero.attack(enemy)
        elif coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            goldCoin += 1

I tried this but it still the same, hero doesnt move

Ok I just saw a lot of mistakes so I’m now trying this, but still… nothing

while True:
    coins = hero.findNearestItem()
    coinIndex = 0
    enemy = hero.findNearestEnemy()
    while 1000 > hero.health > 500 and coinIndex < len(coins):
        coin = coins[coinIndex]
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            coinIndex += 1
        if enemy and hero.distanceTo(enemy) < 8:
            hero.attack(enemy)
    while hero.health < 500 and coinIndex < len(coins):
        coin = coins[coinIndex]
        if enemy:
            hero.attack(enemy)
        elif coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
            coinIndex += 1
while True:
    coins = hero.findItems
    coinIndex = 0
    enemy = hero.findNearestEnemy()
    while 1000 > hero.health > 500 and coinIndex < len(coins):
        coin = coins[coinIndex]
        enemy = hero.findNearestEnemy()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
        coinIndex += 1
        if enemy and hero.distanceTo(enemy) < 8:
            hero.attack(enemy)
    while hero.health < 500 and coinIndex < len(coins):
        coin = coins[coinIndex]
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        elif coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
        coinIndex += 1

Changed again by this
Sorry guys …

I would swap both of these for an if statement

1 Like

I’m not absolutely sure of this, maybe it works, but the better way of writing it is two separate conditions. So while hero.health < 1000 and hero.health > 50 and here the rest of it.

1 Like

I’m trying to make it simplier

while True:
    enemy = hero.findNearestEnemy()
    if hero.health < 1000 and hero.health > 500 and hero.gold < 150:
        coin = hero.findNearestItem()
        enemy = hero.findNearestEnemy()
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
        hero.gold += 1
        if enemy and hero.distanceTo(enemy) < 8:
            hero.attack(enemy)
    if hero.health < 1000 and hero.health > 500 and hero.gold < 150:
        coin = coins[coinIndex]
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        elif coin:
            hero.moveXY(coin.pos.x, coin.pos.y)
        hero.gold += 1
    

I’m not really sure that I need to use an array.
But still with this code it’s not working

Ok now I’m trying to make it even more simplier, so here is my new code:

while True:
    enemy = hero.findNearestEnemy()
    coin = hero.findNearestItem()
    if coin and enemy and hero.distanceTo(enemy) > 8:
        hero.moveXY(coin.pos.x, coin.pos.y)
    if enemy and hero.distanceTo(enemy) < 8:
        hero.attack(enemy)

Now the hero starts by going to pick up all the coins until the distance to enemy is less than 8, then he kills the enemies, but after that he stops moving.

How do I do to make to make the while loop continue ?

It’s okay. That’s how everyone starts, but later on, when you get better at coding, you will be able to make much more complex code. Also, brawls are designed to become harder by time, so you can improve your code bit by bit as you learn new things. Don’t feel disappointed, we are all learning :slight_smile:

1 Like

Thanks for your sweet words ! They give me a lot of power ! I’m learning on codecombat, before doing a training in development, in 6 months !3

2 Likes

@Lourimax hey I can help you if you ever need me to. Just make a new topic and do @herokolbert

Thanks a lot @herokolbert , I’ll do that :slight_smile:

Yeah that’s true, I’m sorry I didnt think about that before

So can you remove the solution? :slightly_smiling_face:

P.S press the checkmark on the post that was the most helpfull/ the post that helped you solve the level so this topic is close and could you add a solved at the title?

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