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)
#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
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!
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)
# 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)
# 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)
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.
# 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
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!
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