# Level: Shine Getter

Hello all,

Wondering what’s wrong with code on level “Shine Getter”. If players got the hang of figuring out different values of coins (gold, silver, bronze), other levels would make a lot more sense.

Code:

``````    coins = self.findItems()
coinIndex = 0

# Wrap this into a loop that iterates over all coins.
coin = coins[coinIndex]
# Gold coins are worth 3.
if coin.value == 3:
# Only pick up gold coins.
pos=coin.pos
x=pos.x
y=pos.y
self.moveXY(x, y)
``````

All my character does is stand there: he’s not interpreting the last

``````            x=pos.x
y=pos.y
self.moveXY(x, y)```

at all. Any help would be appreciated!``````

I did not do this level yet, and I don’t know this language, but it seems to me that a kind of loop is missing, with something like coinIndex++ somewhere, isn’t it ?

Vettax is right. You check exactly one coin, the one at position 0.
At Shine Getter you should already have encountered iterating over arrays, but for completness:

``````coins = self.findItems()
coinIndex = 0
while coinIndex < len(coins):
if coin.value == 3:
#collect the coin
coinIndex += 1     #Increase coinIndex in every case
``````

Always remember, correct intendations save lives.

2 Likes

Thanks! I was not sure about the len() command, but I originally tried coinIndex = coinIndex + 1. I tried `while coinIndex < 3`, but that didn’t work. Thanks for introducing the len() command to me!

Is there a way to differentiate from enemies as well? (and attack them in priority, etc)

Yes, some levels are designed to show you how to select your enemies (do not attack the yak, for example).
Code to attack by priorities is a little bit more complex, but feasible and needed for multiplayer levels for example.

loop:
coins = self.findItems()
coinIndex = 0

``````# Wrap this into a loop that iterates over all coins.
while len(coins) > coinIndex:
coin = coins[coinIndex]
coinIndex = coinIndex + 1
# Gold coins are worth 3.
if coin.value == 3:
# Only pick up gold coins.
self.moveXY(coin.pos.x, coin.pos.y)
``````

So, this is my code, which works fine. From the past levels I know the while loop works with len() and then I believe I get the idea of the [coinIndex] and the effect of coinIndex + 1… BUT, I am not sure why they are necessary.
The [coinIndex] indicates where in the array the loop is, right? And the coinIndex + 1 advances it forward to loop again. But why is that necessary here? Why can’t it just say, if a coin has a value of 3, then retrieve it.

1 Like

Hi. I’m having a little trouble understanding the code.

I’m not sure why code adding a number to the index, such as coinIndex += 1 in this case, should be placed outside the if-condition. From what I understand, aren’t you only supposed to count the gold coins as part of the coinIndex?

Any helpful response will be much appreciated. Thank you.

If you only count coinIndex + 1 when inside the if-condition you would only increment when the coin in question is a goldie. Which in turn means the while-loop would get stuck as soon as it looks at a copper or silver. The loop would look at the coin thinking “Hm this one isn’t worth 3, better not increment coinIndex” and then because coinIndex is still the same, it would take the SAME coin again, thinking the SAME thing again. And so on. In infinity.

So the +1 has to be outside the “if” for the loop to actually look at all the coins, and also to let the loop fulfill its quitting condition. The condition you set is “while len(coins) > coinIndex:” meaning it will only quit when it has counted up to the total number of coins. And if you only count the goldies it will be impossible to reach this condition.

But I guess the second part is irrelevant because it would be stuck already. Confusing stuff. Hope this works as some sort of explanation.

6 Likes

Hey Guys, i’ve also got an issue here, So i’ve updated my boots and now i can use move(item.pos);
however, my dear hero doesn’t like it and starts moving around, in the end not grabing any coin

i’ve got something that looks like a GIF http://i.imgur.com/nAHVM3x.gif, basically he stands in place trying to find a better coin

here’s my code`

``````    loop {
var coins = this.findItems();
var coinIndex = 0;

// Wrap this into a loop that iterates over all coins.
for (i=0; i < coins.length; i++) {
var coin = coins[i];

// Gold coins are worth 3.
if (coin.value == 3) {
// Only pick up gold coins.
this.move(coin.pos);
}
}
}``````

You tell Tharin to look through all the coins and check the value, taking one step toward the coin if the coin’s value is 3. Try using `moveXY`.

1 Like

Whats wrong with this code.

``````coins = self.findItems()
coinIndex = 0

loop:
coin = coins[coinIndex]
if coin.value == 3:
self.moveXY(coin.pos.x, coin.pos.y)
coinIndex += 1``````

You only define `coins` once, which means that it only sees the first coins your hero sees, if any. Move the `coins = ...`and you should be fine.

Where should it be moved to? And do you mean the code…

`coin = coins[coinIndex]`

Sorry, I should have been more clear. Move the line `coins = self.findItems()` inside the loop.

The sprite is not collecting, did I do something wrong? It says , "Cannot read property ‘length’ of undefined

``````coinIndex = 0
while coinIndex < len(coins):
coin = coins[coinIndex]
coins = self.findItems()
if coin.value == 3:
self.moveXY(coin.pos.x, coin.pos.y)
coinIndex += 1``````

Sorry, I should have been more clear. You’ll notice that in the previous version of your code you had two loops, a while-loop and a `loop:` command. My intention was for you to move the line `coins=self.findItems()` inside the `loop:`, not inside the while-loop.

loop:
coins = self.findItems()
coinIndex = 0

``````# Wrap this into a loop that iterates over all coins.
while coinIndex < len(coins):
if coin.value == 3:
coinIndex += 1
pos = coin.pos
X = pos.x
Y = pos.y
self.moveXY(X, Y)
``````

I’ve been trying for like 20 minutes, keeps telling me that coin is not defined.

To give a value to a variable you must write

``````coin = some_value_here
``````

Your program does not have any such line.

The standard what to loop over all the elements on the list is:

``````while coinIndex < len(coins):
coin=coins[coinIndex]
coinIndex+=1
if coin.value == 3:
``````

The ‘coinIndex+=1’ was also in the wrong place, I put it in the right one.

Don’t you increment the coinIndex at the very end?

You increment it once you know you have finished with that element and it is time to look at the next one

``````while coinIndex < len(coins):
if coin.value == 3:
coinIndex+=1
``````

is wrong because if your coin is not a gold coin you will keep looking at it for ever and ever

``````while enemyIndex< len(enemies):
enemy=enemies[enemyIndex]
if enemy.health>0:
self.attack(enemy)
else:
enemyIndex+=1
``````

is correct, you will only go to the next enemy once you have finished killing the current one.

Shorty, the answer: usually you increment the index at the end of the loop, unless you have a reason to no automatically move to the next element.