I’m having the same trouble on this level. Based upon what I’ve read here, the code should be working.
# Collect more coins than your doppelganger.
# You only have a few seconds to collect coins. Choose your path wisely!
loop:
bestCoin = None
maxRating = 0
coinIndex = 0
coins = self.findItems()
# Try calculating "value / distance" to decide which coins to get.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
if coin.value / self.distanceTo(coin) > maxRating:
maxRating = coin.value / self.distanceTo(coin)
bestCoin = coin
if self.isPathClear(self.pos, bestCoin.pos):
self.moveXY(bestCoin.pos.x, bestCoin.pos.y)
I’ve tried changing the position of coin.value and self.distanceTo(coin) but it doesn’t change the outcome. My guy appears to be heading for the lowest value coin. Sometimes he will head for the silver coins but I have yet to see him head for the gold first. Not sure what to do at this point.
If you are moving to bestCoin while you are still in the while-loop, you will be moving to the least value coin that still has a greater ratio than 0, that is, the worst coins possible. Move the part where you move to the coin outside the while-loop. That should fix your problem.
hi im having difficulties with this stage, i used some help from the discussion above and it still doesnt work, when theres no coins the hero goes into the wall saying “i cant get there” and is taking too long to move to new coins that appear thus the dopplgenger bypasses me in gold count…
if (bestCoin) {
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y);
}
and another question is - in the hints it says to use the hero.move() and not hero.moveXY(x,y), then why doesnt it work like hero.move(bestCoin)?
sry for jumping an old post, seems better then opening new threads i think
Here try to also check if the path from hero.pos to bestCoin.pos is clear, then move to the bestCoin’s position.
Do you need any more assistance at this level?
There is a known ‘glitch’ where the hero will see items/enemies that are not on the map, if the equipped glasses are ‘infinity’ types. In this case, it appears that your guy is finding coins on the other side of the dividing wall and tries to go pick them up…as soon as the other guy collects that particular coin, your guy is freed and will continue with the nearer coins on his side.
There are two ways (that I came up with) to counter this. The simplest is to equip a non-infinity pair of glasses…I tried using the Fine Telephoto glasses and it worked fine.
The other, cheaper, method is to set a distance qualifier in your if statement:
// you currently have:
if (value / distance > maxRating)
// try instead:
if (value / distance > maxRating && coin.pos.x < 38)
I tried changing the glasses and it worked! (if anyone reading this in the future: it didnt work first time, had to re-enter the stage then it worked) tyvm ^^
p.s I cant seem to edit my post with the code for some reason (to remove full splution)