Hi everyone, I need a little help of understanding this code. It seems that this code works this way.

``````# Собери больше монет, чем двойник.
# У тебя есть лишь несколько секунд на сбор монет. Выбирай путь мудро!
while True:
bestCoin = None
maxRating = 0
coinIndex = 0
coins = hero.findItems()
# Попробуй рассчитать соотношение "ценность / расстояние", чтобы выбрать лучшую монету.
while coinIndex < len (coins):
coin = coins[coinIndex]
value = coin.value
distance = hero.distanceTo(coin)
coinIndex +=1
if value/distance > maxRating:
bestCoin = coin
maxRating = value/distance
if bestCoin:
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
coinIndex +=1

``````

But… When I try to put

``````if bestCoin:
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
coinIndex +=1
``````

inside of `if value/distance > maxRating:` statement, it doesnt work. I am trying to understand why it works this way, not this way:

``````# Собери больше монет, чем двойник.
# У тебя есть лишь несколько секунд на сбор монет. Выбирай путь мудро!
while True:
bestCoin = None
maxRating = 0
coinIndex = 0
coins = hero.findItems()
# Попробуй рассчитать соотношение "ценность / расстояние", чтобы выбрать лучшую монету.
while coinIndex < len (coins):
coin = coins[coinIndex]
value = coin.value
distance = hero.distanceTo(coin)
coinIndex +=1
if value/distance > maxRating:
bestCoin = coin
maxRating = value/distance
if bestCoin:
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
coinIndex +=1

``````

So there is a little line that is unnecessary. You only want the `coinIndex += 1` once in the while loop. Then have the `if bestCoin:` outside of the while loop

The idea for this code is to find the best coin based on the value/distance. The first section of code will check every coin before your hero is told to move to the best coin making sure you are only moving to the best coin. The second one will move to the coin with the best value after every coin. And since you are using moveXY(), once the moveXY command starts, it will move to the coin until you pick it up.

Example: if you have 4 coins with values 4, 5, 3, 6

Your first code will run through them all and only move to bestCoin = 6

The second code will run and the first coin sets the original maxRating so it moves to the first one and then every coin that has a higher rating too.
You will move to bestCoin = 4, then move to bestCoin = 5, but not 3, and finally move to bestCoin = 6

To help see this in action, you can add hero.say("not best " + bestCoin) and then after the while loop
hero.say("best " + bestCoin)

hero.say to watch code
``````    while coinIndex < len (coins):
coin = coins[coinIndex]
value = coin.value
distance = hero.distanceTo(coin)
coinIndex +=1 #only need once
if value/distance > maxRating:
bestCoin = coin
maxRating = value/distance
if bestCoin: # This will move after every coin it the new coin is higher rating
hero.say("not best " + bestCoin)
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
coinIndex +=1 # not needed
if bestCoin:
hero.say("real best " + bestCoin)
``````

**just know that this will mess up your timing and you won’t pass, but it let’s you see how the two different codes work. There is a more advanced way to check this if you are interested.

2 Likes