Noble sacrifice help JS


#1

// Collect 80 gold
var item = this.findNearest(this.findItems());
if (item && this.gold <= 80) {
this.moveXY(item.pos.x, item.pos.y);
}
// Build 4 soldiers to use as bait
if (this.gold > 4*this.costOf(“soldier”)) {
this.summon(“soldier”);
this.summon(“soldier”);
this.summon(“soldier”);
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();
var friendIndex = 0;
var friend = friends[friendIndex];
var point = points[i];

// 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 = 3; i > -1; i–) {
this.command(friend, “move”, point);
friendIndex ++;
}
I used this code but I could not pass the level. It says hero placeholder needs something to command on the command line. Please Help!!!


#2

That error message usually means you are trying to command something that doesn’t exist. Did you remember to check for friend before you commanded? The command code may have executed before you had any friends to command.


#3

I get it now! Thanks


#4

Now, there isn’t an error now, but my hero doesn’t collect enough gold.
Here is my code:

// Collect 80 gold
var coin = this.findNearest(this.findItems());
if (this.gold <= 4 * this.costOf(“soldier”)) {
this.moveXY(coin.pos.x, coin.pos.y);
}
// Build 4 soldiers to use as bait
if (this.gold > 4*this.costOf(“soldier”)) {
this.summon(“soldier”);
this.summon(“soldier”);
this.summon(“soldier”);
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();
var fIndex = 0;
var friend = friends[fIndex];
var point = points[i];
// Use a for-loop to loop over i from 0 to 4
// Match the friends to the points and command them to move
if (friend) {
for (var i = 3; i > -1; i–) {
this.command(friend, “move”, point);
fIndex ++;
}
}


#5

Is your coin collection in a loop?
My Javascript is rusty but (I hope) it should be:


while (this.gold <= 4 * this.costOf("soldier")) {
    var coin = this.findNearest(this.findItems());
    this.moveXY(coin.pos.x, coin.pos.y);
}

A while loop keeps on doing something as long as the condition is true, then stops when it is false. Before it was an if statement and would only happen once.


#6

this is my code now. It says that the code exceeded the hard execution limit of 3,000,000
can you tell me the problem?
’’’
// Collect 80 gold
var money = this.gold;
while (money <= 80) {
if (coin) {
var coin = this.findNearest(this.findItems());
this.moveXY (coin.pos.x, coin.pos.y);
}
}
// Build 4 soldiers to use as bait
if (this.gold > 4 * this.costOf(“soldier”)) {
this.summon(“soldier”);
this.summon(“soldier”);
this.summon(“soldier”);
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();
var fIndex = 0;
var friend = friends[fIndex];
var point = points[i];
// Use a for-loop to loop over i from 0 to 4
// Match the friends to the points and command them to move
if (friend) {
for (var i = 3; i > -1; i–) {
this.command(friend, “move”, point);
fIndex ++;
}
}
’’’


#7

There are several problems with your code.

First you grab gold until you reach 80 or more, thats ok so far, but you search only for a coin when you already found a coin. Which is not right as you now might see. Move the coin find line above the condition.

Then you buy 4 soldiers only when and only when you have MORE than 80 gold (four times twenty). In the first part of your code you grab an amount of at LEAST 80 coins, which may under certain circumstances be exactly 80, which will not satisfy your more than 80 gold condition.

In your command part of your code you select a move point by writing point = points[i] but the i variable will only be set after that code, in your for loop.

I can only assume it, but you want to give each soldier an unique point. So your code in this part would be much easier if you iterate from 0 to 3 (or from 3 to 0), then selecting the point by i, then the friend by i and then commanding this friend to this point.

Greetings


#8

Sorry, I forgot that while loops reset faster than loop.

Loop activates once per frame while will go as fast as it can, so if there is no coin it executes over 3,000,000. I made the same mistake myself once before.

You should use loop or put say “There is no coin” if there isn’t a coin.


#9

Thanks for the help everybody!! I am working on it.


#10

Okay, I submitted your code and got it to work in JS…
Several problems mostly stemming from the fact we are using 2 loops instead of 1 loop (which makes thing more complex)

  1. Money isn’t set to self.gold in the while loop, so the loop never ends
  2. As marrk said you check for coin before you define what they are
  3. As marrk said your friends and points are not in the for loop so you will only command 1 soldier to move right. I just commanded friends[fIndex] to move to points[fIndex] inside your loop, but his solution is the one Code Combat encourages.