Sand Snakes Javascript

When I run this code my guy walks right into the mine on the right. I’m so lost.

loop {
var coins = this.findItems();
var coinIndex = 0;
var nearest = null;
var nearestDistance = 9999;
while(coinIndex < coins.length) {
var coin = coins[coinIndex];
coinIndex += 1;
var distance = this.distanceTo(coin);
}
if (distance < nearestDistance) {
nearest = coin;
nearestDistance = distance;
}
if (nearest) {
this.moveXY(nearest.pos.x, nearest.pos.y);
}

1 Like

Hello, Nightman, and welcome. to format your code properly, please refer the the FAQ.

I believe you end the while-loop too early. make it encompass everything except the moving commands, and you should be find.

1 Like

Dude! Thank you so much! This is such a mind trip, but I’m enjoying every minute of it. :smile:

Thanks again.

loop {
var flag = this.findFlag(“green”);
if (flag) {
this.pickUpFlag(flag);
}
}

…Okay, so there’s a piece of code. It’s not particularly useful, as it only provides information for flags. And it’s not even formatted properly! Why have you posted this code?

Doesn’t that also defeat the purpose of learning what the level is trying to teach you?

This level is way over my head…

My hero walks to the nearest coin but everything just explodes…

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.
while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    
    // Loop through all the coins to find the nearest one.
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        // If this coin's distance is less than the nearestDistance
        if (coins >= nearestDistance) {
            
        }
            // Set nearest to coin
            nearest = coins;
            // Set nearestDistance to distance
            nearestDistance=distance;
    }
    // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
    if (coins) {
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
}

Your first Mistake:

// If this coin’s distance is less than the nearestDistance
if (coins >= nearestDistance) {

I fixed that. I still just run into the fire trap…
any other mistakes you can see?

never mind.

I figured it out. I forgot to change to “nearest”.pos.

my hero just stands there

// 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.
while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    
    // Loop through all the coins to find the nearest one.
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        // If this coin's distance is less than the nearestDistance
        if (coins.distance < nearestDistance) {
            nearest = coins;
            nearestDistance = distance;
        }
            // Set nearest to coin
            // Set nearestDistance to distance
    }
    // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
        if (nearest) {
            hero.moveXY(nearest.pos.x, nearest.pos.y);
    }
}

If Nearest what? I don’t see how that is being used

I believe it should be if (coin.distance < nearestDistance) because “coin” is an object but “coins” is an array. Like wise, your hero doesn’t move because “nearest returns an array” (remember you set nearest to coins?) Make sure you do coins[0] or set nearest to “coin”

1 Like

I tried, it does not work

You should write coin = coins[0] (To find the nearest coin), then coinIndex++ to move onto the next coin

For example:

var coin = coins[0];
var distance = hero.distanceTo(coin);
if(distance <= nearestDistance){
  hero.moveXY(coin.pos.x, coins.pos.y
}
1 Like

Like this?

Here. Instead of doing all this looping, adding coinIndex and stuff, try this:

while (true){
 var coin = hero.findNearestItem("coin");
 var distance = hero.distanceTo(coin);
 var nearestDistance = 9999;
 if(coin && distance <= nearestDistance){
  hero.moveXY(coin.pos.x, coin.pos.y);
 }
}

I can’t test out the code because I am not on the level yet :frowning: