Sarven Treasure/General Code problem


#1

Lately when attempting levels I am having a hard time/unable to target and attack monsters.

using the code:

    # Collect 150 gold while evading ogres with teleporters.
    # If you win, it gets harder (and more rewarding).
    # If you lose, you must wait a day before you can resubmit.
    # Remember, each submission gets a new random seed.
    enemies = self.findEnemies()
    enemyIndex = 0

    loop:
        while enemy <len(enemyIndex):
            enemy = self.findNearest(enemies)
            if enemy:
                self.say("1234")

My hero never actually says 1234

If I add:

while not enemy:
        self.say("5678")

He will sit and spam 5678 and not attack the ogre.

enemies = self.findEnemies()
enemyIndex = 0

loop:
    while enemy <len(enemies):
        enemy = self.findNearest(enemies)
    while enemy:
        self.say("1234")
    while not enemy:
        self.say("5678")

will also sit and spam 5678

I have also tried:

enemies = self.findEnemies()
enemyIndex = 0

loop:
    while enemy <len(enemies):
        enemy = self.findNearest(self.findEnemies())
    while enemy:
        self.say("1234")
    while not enemy:
        self.say("5678")

It also spams 5678.

Edit: This is the gear I am using Picture


#2

you dont need to kill them you can just quickly get the coins with speed ring and softened leather boots
you get farther through the levels


#3

I get that I can speed through the levels, but I’m trying to figure out the way to beat them while also learning.


#4

A few problems with your code:

while enemy <len(enemyIndex):
  • You are comparing enemy to a number. (len(varName) returns a number. enemy would be an object, and comparing an object to a number doesn’t make sense.
  • At the time you are checking while enemy <len(enemyIndex): enemy hasn’t been defined. So the while statement never evaluates. However, because you define it later, it doesn’t give an error.
  • You are using len() on a non-list variable type. len(["a", "b", "c"]) is 3, len(0) is None.

Hopefully these get you on the right track.


#5

So, you get the list of enemies only one time, at the first second of the game, when there is no enemies on the map.

Put this

enemies = self.findEnemies()
enemyIndex = 0

Inside the loop


#6

So I end up actually being to slow if I kill the enemies, is there a way to prioritize the 2 and 3 gold coins over the 1 value and if no 2/3 then collect 1’s?


#7

yup and i recommend speed ring and softened leather boots


#8

I’m about 1200 short of getting the speed ring sadly.

My code right now is:

loop:
    coins = self.findItems()
    coinIndex = 0
    enemies = self.findEnemies()
    enemyIndex = 0
    
    enemy = self.findNearest(enemies)
    if enemy:
        while enemy.health > 0:
            self.attack(enemy)
        
    # Wrap this into a loop that iterates over all coins.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
    # Gold coins are worth 1 or more.
        if coin.value >=1:
        # Move to coin.
            self.moveXY(coin.pos.x, coin.pos.y)
        pass
        coinIndex += 1

I end up only getting 56 coins after 59.9 seconds right now though.

And if I don’t attack, I die.


#9

what level are you on
and what gear


#10

Sarven Treasure in the Sarven Desert, my max hp is 1159


#11

get a better chestplate if you can and what level in sarvern treasure its a replayable level
I could not finish it until I got the speed ring and softened boots with runesword but mostly the speed helped


#12

Oh, I’m still stuck only level 1


#13

I think its impossible to win without speed ring and softened leather boots
i have tried to win without those and I failed just always runs out of time
but when i used the speed ring with the boots it worked


#14

So I have another issue now that seems to be happening;

marks = [{"x": 84, "y": 78},{"x": 84, "y": 51},{"x": 83, "y": 22}]
coin = self.findNearest(self.findItems())


while True:
    enemy = self.findNearest(self.findEnemies())
    if enemy and self.distanceTo(enemy) < 12:
        while enemy.health >= 0:
            self.attack(enemy)
    nearest = self.findNearest(self.findItems())
    self.move(nearest.pos)
    if self.gold >= 20:
        mark = self.findNearest(marks)
        self.moveXY(mark.x, mark.y)

This tells me that nearest is undefined however this code was copied and pasted from another level where the self.move(nearest.pos) worked.


#15

If there are no items near you then nearest will be undefined. Do a

if(nearest)

before moving


#16

Thanks a ton everyone, I finally beat it - without the ring of speed at that.