I followed all the directions, but my hero is not collecting the nearest coin. He’s taking extra time to go to a coin farther away, and then he can’t get all the gold coins before they disappear.
I think it is because the variable minGoldDist starts out so high, and so the first coin is kind of random.
Here is a copy of my code. I have a similar problem in the next level, Mad Maxer Gets Greedy. Help would be greatly appreciated!!
# Coins here disappear after a few seconds!
# Get all the gold coins before they vanish.
loop:
closestGold = None
minGoldDist = 9000
coinIndex = 0
coins = self.findItems()
# Find the closest coin that is gold.
# Remember that gold coins have a value of 3.
while coinIndex < len(coins):
coin = coins[coinIndex]
distance = self.distanceTo(coin)
if distance < minGoldDist:
if coin.value == 3:
closestGold = coin
coinIndex += 1
if closestGold:
self.moveXY(closestGold.pos.x, closestGold.pos.y)
I’m also stumped here. Hero collects some coins, but some disappear before he gets to them, level goals remain incomplete. He doesn’t go to the closest one first, but just some random one.
while True:
closestGold = None
minGoldDist = 9001
coinIndex = 0
coins = hero.findItems()
# Finde die nächste Goldmünze.
# Beachte das Goldmünzen einen Wert (value) von 3 haben
while coinIndex < len(coins):
coin = coins[coinIndex]
if coin:
distance = hero.distanceTo(coin)
if distance < minGoldDist and coin.value == 3:
closestGold = coin
minGoldDistance = distance
pass
coinIndex += 1
pass
if closestGold:
#Begib dich zur nächsten Goldmünzen und bekomme sie!
hero.moveXY(closestGold.pos.x, closestGold.pos.y)
pass
are under the while True loop - because that means the values get reset at each iteration - and each gold coin that happens to turn out first in the array gets set as variable closestGold. But that’s the pre-written code, so it must be correct…?
The pre-written code has to be in the while True loop because the code is iterating and incrementing through an array. If only one coin were to appear then yes, it would be set as closestGold, but you’ll notice that three appear at a time.
You should increase the coinIndex outside of that if-block, otherwise it’ll never “increase” the index if it doesn’t fulfill the conditions and thus never reach coins.length, resulting in an infinite loop.
Also, you probably want to check if the value is 3 in that if block, not if the coin is 3.
Is the main topic here considered global varibles ? so i can study the structure some more,this is my tipping point honestly.i cannot get any further past this. yet that is
With this level and your current code, the challenge is the while loop more than anything. That is creating your infinite loop like @Shurutsue stated. I’ve found the trick with javascript is finding where the curly braces are starting and ending to make sure the group of code is in the right set of braces. To ensure the index is increasing, it needs to be in the correct set of braces. Currently, your isn’t quite correct. Below is the overall picture you want to see with while loops and index for most basic situations. The increasing index coinIndex++ should be the last line of code before you close the while loop.
while (coinIndex < coins.length){
//Other code goes here {
}
coinIndex++
}
To cover some basics on variables (both Global and Local), along with their attributes, check out the link below. The big idea, a variable is Global when available to all the code, while the Local variables are specific to a group of code like a function or loop.