[SOLVED] -Sand Snakes

Hello, I have been stuck on this level for a while and finally decided to ask for help. I don’t understand what i’m doing wrong.

Here’s my code:

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 self.distanceTo(coin) < nearestDistance:
            # Set nearest to coin
            item = hero.findNearestItem()
            # 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.
            self.moveXY(nearest.pos.x, nearest.pos.y)


Here’s a picture of my code as well.

#1. You are making a variable distance, but you are not using it.
#2 You are using .self method but you use hero method before.
#3 You are telling your hero to move to Nearest and Nearest value will never be something other than “None” because you did not define it in your while loop.

#4 You are using extra line of code that can be removed there, as coins is already defined at beginning

1 Like

Most of your code is fine, just a few mistakes. Firstly, after the comment, set nearest to coin, you’ve said, item = hero.findNearestItem. The point of this level is that findNearestItem doesn’t work. You are looping over an array and comparing your distance to each coin in that array, until you find the one closest to you. You have also said item when the code above defines it as coin. What you need to put is nearest = coin thereby setting the current coin as the nearest coin. Secondly, after the comment if this coin's distance is less than the nearestDistance you have restated if self.distanceTo(coin). You have already defined distance in the line above, so to keep you code neat, you might as well just say if distance. Finally, you’ve put self.moveXY(nearest.pos.x, nearest.pos.y) in the while True loop. This will not work. You need to put the move command outside the while coinIndex < len(coins) loop, so that the loop can find the nearest coin. Then you move to it. I hope this helps!

3 Likes

I fixed my code but now he just runs in place.

Here’s my code:

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 self.distanceTo(coin) < nearestDistance:
            # Set nearest to coin
            nearest = 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.
            while coinIndex < len(coins):
                hero.moveXY(nearest.pos.x, nearest.pos.y)

To fix that, remove your while coinIndex < len(coins) loop at the bottom, and instead put outside the first while coinIndex < len(coins):

if nearest:
    hero.moveXY(nearest.pos.x, nearest.pos.y)

This will allow your loop to identifiy the nearest coin, and then after it has found the nearest one, you move to it.

1 Like

Okay, I fixed it and my hero is finally moving but he is not defining the coins he is defining an item. I’m not sure what i’m not doing right.

Here’s my code:

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 self.distanceTo(coin) < nearestDistance:
        # Set nearest to coin
        nearest = 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.
        if nearest:
            hero.moveXY(nearest.pos.x, nearest.pos.y)

You need to put your

if nearest:
    hero.moveXY(nearest.pos.x, nearest.pos.y)

out side of the while coinIndex < len(coins): loop

2 Likes

ohhh okay, sorry I didn’t understand. I passed the level! Thanks for helping me out! :stuck_out_tongue_closed_eyes:

Glad I could help :grin:

I put solved
(20 chars)

how can you do that?

You have to be trust level three.

Thanks, couldn’t work out how to do that.

# 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.
coins = 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.distanceTo < nearestDistance:
            # Set nearest to coin
            nearest = 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.
    if nearest:
        hero.moveXY(nearest.pos.x, nearest.pos.y)

I am not moving

Hello again.
You’re problem’s here:

what is coin.distanceTo? Why don’t you use the nifty variable you just made?

la encontre

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 distance<nearestDistance:
        # Set nearest to coin
        nearest = 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.
if nearest:
    hero.moveXY(nearest.pos.x, nearest.pos.y)

Hi @sheene, welcome to the codecombat discourse!
Are you having problems with this level? Your code looks good to me.
If you are having problems please post a screenshot of your level screen and your equipment.
Thanks
Danny

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 distance<nearestDistance:
# Set nearest to coin
nearest = 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.

if nearest:
hero.moveXY(nearest.pos.x, nearest.pos.y)
I’m having problems with this level pls help
My hero just stands still

Hi @MA_05HS_Sherwood_Mil, welcome to the CodeCombat discourse! :tada:
thank you for trying to format your code, but please could you put the ``` at the start and end of your code so it formats it all. Then it will be easier for me to help you.
Thanks
Danny