# Level Sand Snakes

The path is not safe is we use move, because the hero turn sooner than expected. MoveXY is ok.

1 Like

I would assume most people doing the campaign wouldnâ€™t have bought the compound boots until the Mountain Campaign, so Iâ€™m pretty sure most players would use `moveXY()`.

Although while loops are taught in the desert campaign, so this works:

``````while (best && this.distanceTo(best)) this.move(best.pos);
``````

(Though maybe a slightly greater distance, like `distance < 1` might work better when it comes to time.)

1 Like

Just because most players would be using `moveXY()` does not mean that the level should only work for `moveXY()`. In general, I donâ€™t think a level should fail just because a player has a higher level item.

1 Like

Yeah, I know, but `move()` behaves quite interestingly.

According to the description:

The `move` method sets the heroâ€™s `targetPos` to the given `(x, y)` coordinates and also sets the heroâ€™s `action` to `move` for one â€śstepâ€ť.

Basically at a turn, once the hero picks up a coin (I believe the hero has a collect range of 2 meters), depending on what method the hero uses, the hero will continue to itâ€™s target position or find a new one.

If the hero is using `move()`, the loop immediate continues to its next iteration, and the hero instantly changes their target position, which here will make them collide with the fire traps.

`moveXY()` moves all the way to the target position before the loop can continue, so their next target position will be the next coin in the path.

Which is why if one wants to use `move()`, theyâ€™ll have to use a while loop to check if their distance to their target position is near zero before continuing the loop. (See my post above for an example.)

I donâ€™t see how this is limiting the player.

1 Like

Since the higher level boots also have moveXY on them, hopefully players will know to just switch the method (which is what I had to do). Might be good if in the description something was added about needing to use moveXY or at least about being careful not to cut corners.

2 Likes

`moveXY` didnâ€™t work for me. My hero sits still for a moment, then turns right into the nearest fire trap. This happens without fail. Hereâ€™s my code:

``````# This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.

loop:
coins = self.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999

# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = self.distanceTo(coin)

# If this coin's distance is less than the nearestDistance
# Set nearest to coin
# Set nearestDistance to distance
if distance < nearestDistance:
nearest = coin
nearestDistance = distance
if nearest:
self.moveXY(nearest.pos.x, nearest.pos.y)
``````

Can you show me where I went wrong?

1 Like

So, do you collect any coins at all? Or from the very beginning you head for a mine? I donâ€™t see anything wrong with your code as it stands. Looks to be exactly the same as my working code. What hero? Can you add a screenshot?

1 Like

The code editor says that I am moving to `nearest.pos.x, nearest.pos.yâ€™, but thereâ€™s really no coin where my hero is standing right now.

EDIT: Just saw this, but my hero, when she gets blown up, she is heading for one of the coins that is definitely not the nearest.

1 Like

It looks like your moveXY is inside the while-loop. Going to nearest when the loop isnâ€™t finished yet might get you in some trouble I believe.

1 Like

Nice catch Bibliotek, totally missed that, ChronistGilver, you are trying to get the the current nearest before checking all the coins. Wait to move till youâ€™ve looked at all coins.

1 Like

i coded a shortcut, going to the moon and back :

``````this.summon('soldier');
var flyingMan = this.findFriends()[0];
loop {
if (flyingMan.health <= 0) break;
this.command(flyingMan,'move',this.findItems()[0].pos);
}
this.say('paf, yer flying to the moon xD');
this.moveXY(74,53);``````
1 Like

Perhaps the boss stars should be restricted for this level for the above reason.
Edit: Maybe use the `ignoresItemGold` property to prevent the summoning of troops?

Also, it might be better for the devs to create threads for each new adventurer level as an official place to post feedback and issues during testing.

1 Like

ok same problem here so my question is how do you wait for the while loop to finish before you move

1 Like

just put the move after the loop like so:

``````i = 0;
while(i < 10){
stuff
}

moveXY(10,99);
``````
1 Like

I have this same problem. My adventurer always runs towards the same coin, which is most definitely not the closest by any means. This is the same coin as ChronisGilverâ€™s. I have made sure that the moveXY is not within the while loop. What do I do?

``````loop:
coins = self.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = self.distanceTo(coin)
if distance < nearestDistance:
nearest = coins[coinIndex]
nearestDistance = distance
if nearest:
self.moveXY(nearest.pos.x,nearest.pos.y)
``````

What on earth? Why isnâ€™t my code working? I mean in the forums! I have enclosed it within `'s, which are backticks. Well, anyway, it seems to be indented right in CodeCombat. The moveXY is out of the while loop and all. What's wrong with it? [edit - the backticks are 3 (````) and go on their own lines]

1 Like

You already got â€ścoinâ€ť from â€ścoinsâ€ť why would you get a different oneâ€¦

coin = coins[coinIndex]
coinIndex +=1

nearest = coins[coinIndex]

Use the one you got the distance to not a different one . . . nearest = coin

2 Likes

Hello, I have a strange bug in this level.
Hereâ€™s my code:

``````loop:
coins = self.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999

while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = self.distanceTo(coin)
if distance < nearestDistance:
nearest = coin
nearestDistance = distance

if nearest:
if self.isPathClear(self.pos, nearest.pos):
self.move(nearest.pos)
``````

The problem is the hero (Anya or Tharin, I cheked both of them) doesnâ€™t move exactly on nearest.position , but has some sort of inertia. So sometimes the nearest coin is not the safest one.
The only solution Iâ€™ve found is to add this code in the end:

``````        if self.pos != nearest.pos:
self.moveXY(nearest.pos.x, nearest.pos.y)
``````

So the hero needs twice go to nearest coin position. It works for me, but it doesnâ€™t seem other players use it to solve this level.

1 Like

Hello, Linguini_Incident, and welcome. Please read the FAQ before you post again. This will teach you to â€śpost your code with radiant, harmonious formattingâ€ť, that is, making sure your levels of indentation show up. Iâ€™ve done it for you this time, but please do so yourself in the future.

The comments in the default code specifically say

``````# If there's a nearest coin,...you'll need moveXY so you don't cut corners and hit a trap.
``````

Using `moveXY` by itself should get you along fine.

1 Like

Hello, ChronistGilver, and thank you for help with formatting.
Of course, Iâ€™ve tried moveXY for the first, but the result was the same. After that Iâ€™ve bought a new shoes to try move and jumpTo but nothing changed.
But I think, Iâ€™ve found the problem - I used self.say(self.pos) after moving. When I donâ€™t use it moveXY works fine.

1 Like

Yes, that would be your problem. `self.say()` keeps your momentum, even though youâ€™ve stopped moving, and so you â€śslideâ€ť for a bit. Come to think of it, why wasnâ€™t that in the code you posted earlier? Did you think it wasnâ€™t important?

1 Like