Безумный Максер Распродажа, Mad Maxer Sells Out

Всем привет, помогите, пожалуйста, понять некоторые вещи в массивах, я до сих пор не понимаю и застриваю в некоторых уровнях по несколько дней. Например, почему этот код не работает?

Hi everyone, can anyone help me please bc I still can not understand a bunch of things in arrays, and usually get stuck on some levels for few days. For instance, why this code is not working?

image

I have decided this solution this way but still could not understand why it didn`t work earlier. I had to change minDist
Я как-то решил этот код, но не понял почему раньше не работал. Пришлось менять минимальную дистанцию

Hey! Arrays are pretty much just a “list” containing objects or variables.

For the first post:

Summary

So i believe what you’re trying to do is to get the coin closest to you but there’s already one mistake:
if coin.value == 3 and distance > minGoldDist:
in other words, if the value is 3 and the distance is GREATER than the minimum gold distance.
so flipping the sign around to < would already solve one of them.
Now however, in case no coin is closer than the preset distance of 1, you wouldn’t get a coin as a result.
What you can do here would be to add an unusually high distance, for example 1000, as the preset minGoldDist value, so every coin “should” be closer than that, if you can see it and it exists.
Next up, right after checking if it is closer than your last check, you already move with

while coinN < len (aCoins):
   #other code
   if closestGold:
      hero.moveXY(coin.pos.x, coin.pos.y); 

so you’d want to get that if block out of this particular while loop, so you check for the closest coin first and THEN move to it, after you’ve determined the one closest to you.

Also just noticed:
You set the coin variable to clostestGold which you started with “none” but you never set something else as it’s value, so it probably should be switched around and in the hero.moveXY
you should probably use closestGold.pos.x and closestGold.pos.y instead of coin.pos.x and coin.pos.y


2nd post:

You’ve also changed the part with
if aCoins[coinN].value == 3 and distance > minGoldDist:
from > sign to < sign (which should be correct now)
and changed it so you set the closestGold value to the closer found coin.
however, you still check if a “closer coin than 22” exists and move there, before you completely go over each coin in the list. So adviceable would be to still move the if closestGold: block below the pass and 2 of these spacings to the left, so it’s part of the while True: loop instead of the loop where you iterate over every coin. (You might also want to increase the minGoldDist value further, unless you don’t want to pick up any coins further away than that from your current position)


Also as a sidenote, you can use triple ` infront and at the end of copy pasted code in here to achieve something like this:

while True:
   closestGold = None
   minGoldDist = 1
   coinN = 0
   #and so on...

so you wouldn’t have to make a screenshot of the code itself and could simply copy paste it.

Hope I’ve been of help and I hope i didn’t explain it too weird.

1 Like

as far as I can understand my mistakes here is that I should have put if closestGold statement matched up with “While True:” block, then it works perfectly with minGoldDist = 9001 but I still can not understand why, why not inside of code where I iterate every coin

when you iterate every coin in order to find the closest one, you first have to check every coin toget the closest one as the list isn’t sorted by distance to the hero, otherwise you’d be able to take the first entry of that list all the time.

If you have it while “iterating” through, you’re pretty much always getting the first in the array, while it might not even be the closest coin.
once you then reached the closest coin, it would iterate all the others, but not move to it.

The order in which you do things is important, as with the second while loop you want to get the closest coin.

For example (distance wise) the array you have could be (distance values entered)

[
   14,
   17,
   32,
    4,
   22,
   19
]

Then you iterate through that array and this would be the result:
14 closer than 9001? Yep, so move to it.
17 closer than 14? Nope, ignore it.
32 closer than 14? Nope, ignore it.
4 closer than 14? Yes! But you just moved exactly 14 steps away from it. so it checked if 18 is closer than 14, so you ignore the actual closest coin from starting position.
22 closer than 4? Nope, ignore it.
19 closer than 4? Nope, ignore it.
(One of the others might be closer than 14 now though, so possible that you pick up two coins or more, depending on where they actually are and how you moved during the iteration)

But if you get the if block matching the while true loop, you’re effectively going through all entries in the array and get the closest one.
After you got the one closest to you, you move to it, as it then would be the one with distance of 4 to the hero, instead of the first entry with distance 14.

Additionally, it might happen that you pick up a coin while moving to another, this would then possibly cause an “error” in which you move to a position that you picked up a coin already if you don’t have it in the while True loop, as the array isn’t “refreshed”.

1 Like

У меня тоже проблема на этом уровне тока на js.
Вот код пишут что бесконечный цикл помогите!!!

while (true) {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = hero.findItems();
        while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        var distans = hero.distanceTo(coin);
        if (coin.value == 3 && distans < minGoldDist) {
                closestGold = coin;
                minGoldDist = distans;
          }
    }
    if (closestGold) {
                hero.moveXY(closestGold.pos.x, closestGold.pos.y);
        coinIndex += 1;
    }
}

:ru: Вы должны переместить это:

coinIndex += 1;

Надеюсь, это поможет.

:uk: You need to move this:
-See above :arrow_up:
I hope this helps.
-Danny