[Solved] Sand snakes (Python)

Hi, I am struggling on how to get started on this level. I have no idea what to do. This is the given code:

# This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.

while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        
            # Set nearest to coin
            
            # Set nearestDistance to distance
            
            
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.

Can someone give me ideas on how to begin?

OK heres the gist.
Loop thru all the coins.
If the coins distance to you is smaller then nearest distance.
Make nearestDistance to the distance, and make nearest to the coin.
So lets say you start.
Coin A is close then 9999.
So you pick it up.
Then,since COIN A is gone, you get coinB.
If its closer then 9999:
Pick it up.
Its kinda like finding the weakest enemy.
You make weakest to none, and make weakest health to 9999
Then, loop thru all enemies, and if its health is less then weakest, make it weakest.
Lets say, there is an ogre, a shaman, and a thrower.
120 is less then 9999 so weakestHealth is 120, and ogre is weakest. Then, you find the shaman in the loop.
60 is less then 120, so weakestHealth is 60, and the shaman is weakest.
Then, you find the thrower.
7 is less then 60, so then weakestHealth is 7, and thrower is weakest.
If the game spawns an enemy that’s health is less then 7, its weakest enemy.
Here is some code for getting the weakest enemy, and it might give you some ideas for something similar for the NEAREST item.

while True:
    weakest = None
    leastHealth = 99999
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy.health < leastHealth:    
            weakest = enemy
            lowestHealth = enemy.health
    # Loop through all enemies.
        
    if weakest:
        hero.attack(weakest)

Then, for the items, just do it with distance, and do items instead of enemies.
I hope this helps.

This is pretty clear, just follow the instructions

Thanks, I will try this tomorrow.
@Lydia_Song, those instructions made me look like this :no_mouth::neutral_face::dizzy_face:‍. They didn’t help me.

the instructions are basically telling you write code that finds out if the coin is the closest.

like imagine coinA, coinB, and coinC
coinA is 5 m away, coinB is 3 m away, and coinC is 13 m away

you have to cycle through every coin and see which is the closest.
nearestDistance = 999
nearest = 0
coinA distance < nearestDistance so coinA distance becomes the new nearest distance and coinA becomes nearest

nearestDistance = 5

coinB distance < nearestDistance so coinB becomes nearest and repeat.

So basically, you’re looping through all the coins to find the nearest coin.
They gave you the nearest as None and nearestDistance at 9999 (cause its just a starting distance and every coin’s distance would be lower than that)

So here it’s telling you, if the distance to the coin is less than the nearestDistance, do nearest = coin. And do nearestDistance = distance.
If there is a nearest, use moxeXY to move to the nearest coin

I’m still struggling on this code. I think I’ve got most of it, but there are two spots I can’t figure it out. Here’s my current code:

# You'll need to find the nearest coins on your own.

while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        
        if item.distance < nearestDistance:
            nearest = item
            nearestDistance = item.distance
            # Set nearest to coin
            
            # Set nearestDistance to distance
            
            
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
    hero.moveXY(item.pos.x, item.pos.y)

I don’t get what to do right before the given code, when it says You'll need to find the nearest coins on your own. And I also don’t understand what to do when it says # If this coin's distance is less than the nearestDistance

Also, in this entire spot:

 if item.distance < nearestDistance:
            nearest = item
            nearestDistance = item.distance

Should I write coin instead of item?

I switched it out with coins, and now my hero moves. She picks up 1 coin, and blows up.

 This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        
        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            # Set nearest to coin
            
            # Set nearestDistance to distance
            
            
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
    hero.moveXY(coin.pos.x, coin.pos.y)

Just put your move command into the while loop. LOL

Like that?

  while True:
        hero.moveXY(coin.pos.x, coin.pos.y)

My hero still blows up. So I’m not sure I fully understand what you’re trying to say.

        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            moooooov
        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            
            hero.moveXY(coin.pos.x, coin.pos.y)

Then I don’t move at all.

Nooooo! nearest.pos.x, nearest.pos.y!!!

1 Like

I am doing exactly what you are telling me, and I don’t move.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        
        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            
            hero.moveXY(nearest.pos.x, nearest.pos.y)

            # Set nearest to coin
            
            # Set nearestDistance to distance
            
            
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.

Okay, lemme use ur code, and see wut happens

That is my code, and I don’t move.

Try this.


while True:
    coins = hero.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        if distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            hero.moveXY(nearest.pos.x, nearest.pos.y)

Oops, formatted it now/

Its bugged. Look at the interpreter/
|Perkele’s Archer| Hero Placeholder had new Programmable problem: plan Line 15: TypeError: Tried to load an unsafe native value into the interperter:function / function(thang, fromEdges) {
var ref;
if (fromEdges == null) {
fromEdges = false;
}
if (!(thang && (thang.isVector || thang.isThang || (!
.isNaN(thang.x + thang.y))))) {
console.log(‘distance from’, this.id, ‘to’, thang != null ? thang.id : void 0, ‘did not work: isVector’, thang != null ? thang.isVector : void 0, ‘isThang’, thang != null ? thang.isThang : void 0, ‘x’, thang != null ? thang.x : void 0, ‘y’, thang != null ? thang.y : void 0, ‘keys’, _.keys(thang));
throw new ArgumentError((_ref = this.contextPhysical) != null ? _ref.error1 : void 0, “distance”, “target”, “object”, thang);
}
return Math.sqrt(this.distanceSquared(thang, fromEdges));
} 0