Noble Sacrifice level: not sure why one solution gets an error, but not the other


#1

Hello!

I was just working through the Noble Sacrifice level and ended up beating the level and encountering strange results along the way.

I entered this code:

loop {
// Collect 80 gold
    while (this.gold < 80) {
        var coin = this.findNearest(this.findItems());
        if (coin) {
            this.move(coin.pos);
        } 
    }
// Build 4 soldiers to use as bait
    for (i = 0; i < 4; i++){
        this.summon("soldier");
    }
// Send your soldiers into position
    var points = [];
    points[0] = { x: 13, y: 73 };
    points[1] = { x: 51, y: 73 };
    points[2] = { x: 51, y: 53 };
    points[3] = { x: 90, y: 52 };
    var friends = this.findFriends();
// Use a for-loop to loop over i from 0 to 4
// Match the friends to the points and command them to move
    for (var i = 0; i < 4; i++) {
        var point = points[i];
        var friend = friends[i];
        this.command(friend, "move", point);
    }
}

and I ended up with a “Hard execution limit of 3000000 exceeded” error, however even with the error it let me pass the level with full success. Wasn’t able to determine why I got the error in the first place or why it let me through after giving me an error.

Since it always seemed to get hung up after the gold is collected but right before summoning the soldiers, In an effort to troubleshoot, I placed this:

else {
            this.say("done!");
        }

After the

        if (coin) {
            this.move(coin.pos);
        } 

To try and see what was going on, leaving this:

// Collect 80 gold
    while (this.gold < 80) {
        var coin = this.findNearest(this.findItems());
        if (coin) {
            this.move(coin.pos);
        } else {
            this.say("done!");
        }
    }

When I added the else, it worked perfectly with no errors!
I’m wondering why?
I wouldn’t think that would really make a difference as to the functioning of the rest of the code.
Here is the full code that worked error free:

loop {
// Collect 80 gold
    while (this.gold < 80) {
        var coin = this.findNearest(this.findItems());
        if (coin) {
            this.move(coin.pos);
        } else {
            this.say("done!");
        }
    }
// Build 4 soldiers to use as bait
    for (i = 0; i < 4; i++){
        this.summon("soldier");
    }
// Send your soldiers into position
    var points = [];
    points[0] = { x: 13, y: 73 };
    points[1] = { x: 51, y: 73 };
    points[2] = { x: 51, y: 53 };
    points[3] = { x: 90, y: 52 };
    var friends = this.findFriends();
// Use a for-loop to loop over i from 0 to 4
// Match the friends to the points and command them to move
    for (var i = 0; i < 4; i++) {
        var point = points[i];
        var friend = friends[i];
        this.command(friend, "move", point);
    }
}

I’m stumped - anyone know why I got an error in the first place, and why adding some speech fixed it?
Many thanks!


#2

It is because if you do not add a wait or if you do not say something, the loop is infinite. Somebody else taught me that.


#3

Ah, thanks - that makes sense now!