Help Sand snakes!

# 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
        if (hero.distanceTo(coin) < nearestDistance):
            # Set nearest to coin
            nearestDistance = coin
            # Set nearestDistance to distance
            nearestDistance = 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.

i dont get this part↑↑↑

1 Like

I think, that you have to put coinIndex to the end of the loop.

1 Like

what do you mean?(20 chars)

1 Like

Sorry, I mean coinIndex+=1 to the end of the loop.

1 Like

I think, you should write

nearest = coin
1 Like

wil try okkkkkkkkkkkkkkkkkk

1 Like

but i dont know what to do at the end of the code

1 Like

After this, you should write hero.moveXY(nearest.pos.x,nearest.pos.y)

2 Likes

thanks it works!!! :grin: :grin: :grin:

2 Likes

You’re welcome. :slightly_smiling_face:

2 Likes

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