Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Level : Protect and Serve (JavaScipt)

Due to the lack of hints and countless trial and errors to redo or modify the code i cannot pass this level. The best code i have only makes the soldiers protect the peasants and not the livestock and coins. I would like some help on the matter, if there are any tutorial videos on this subject please send me a link. THX.

@Valentino_Artawan do you still need help? Also can you copy and paste you code.

// Protect the workers and animals!

// Defend these two positions:
var defend = ;
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };
defend[2] = { x: 98, y: 67 };
defend[3] = { x: 74, y: 76 };
defend[4] = { x: 58, y: 76 };
defend[5] = { x: 21, y: 67 };
defend[6] = { x: 22, y: 35 };
defend[7] = { x: 54, y: 14 };
var soldiers = ;

var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == “soldier”) {
soldiers.push(friend);
} else {
// Defend the workers:
defend.push(friend);
}
}

while(true) {
// Use a for-loop to assign each soldier to a corresponding defend target
// Use command(soldier, “defend”, thang) or command(soldier, “defend”, position)
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
var defense = defend[i];
if(friend.type == “soldier”) {
hero.command(friend, “defend”, defend);
}
}
}

I tried to input this. in theory it should pan out (i added those extra defend array numbers) but when i ran it the code didn’t do anything (red x below character)

Please tell me how to solve this problem. I’ve been stuck here for a week
THX

@Valentino_Artawan I’m going to break the problem down and try to go step by step to understand better.

Original unedited

// Protect the workers and animals!

// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };

var soldiers = [];

var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
    var friend = friends[i];
    if(friend.type == "soldier") {
        soldiers.push(friend);
    } else {
        // Defend the workers:
        defend.push(friend);
    }
}

while(true) {
    // Use a for-loop to assign each soldier to a corresponding defend[] target
    // Use command(soldier, "defend", thang) or command(soldier, "defend", position)

}

hints:
// This level introduces the “defend” command: hero.command(soldier, “defend”, target) the defend target can be another unit, or position. Command soldiers to defend the working peasants! There are equal amounts of peasants and soldiers. Use an array index to iterate over two arrays at once.

The first part of the problem we want to send the soldiers only to these two positions to defend.

// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };

The first part doesn’t require any change.

The second part below:.

var soldiers = []; // empty array we need to add all soldiers to

var friends = hero.findFriends(); // All the friends the hero can see stored in an array
for(var i=0; i < friends.length; i++) { // Loops through each friend  in friends and checks if/else statement
    var friend = friends[i]; // To check each friend in the array of friends
    if(friend.type == "soldier") { 
        soldiers.push(friend); // Adds the friend to the soldiers array
    } else {
        // Defend the workers:
        defend.push(friend); // This adds the friend to the defend array so the soldier will go here  and defend if same index
    }
}

Third part below

while(true) {
    // Use a for-loop to assign each soldier to a corresponding defend[] target

   for (var i = 0; i < soldiers.length; i += 1) {
      var soldier = soldiers[i];
      var something = defend[i]; // This is because it says they are the same amount

    // Use command(soldier, "defend", thang) or command(soldier, "defend", position)

      hero.command(soldier, "defend", ???);
   }
hero.moveXY(100, ??); // Have hero move through coins so the enemy cannot get them
hero.moveXY(??, 77); // top left corner
hero.moveXY(19, ??); // bottom left corner
hero.moveXY(95, ??); // bottom right
}

For the defend the friends it uses the friend position because the friend can move around and the soldiers will follow within certain radius. The friend collecting coins at the bottom for example instead of XY. Since we already made soldiers array we can use it as well as the defend array. Also because we made the arrays for soldiers && defend in the first and second parts it is better to use these in while statement at the end.

So what you’re saying is, the (push) code is used to move an object onto an array?
i think i understand it now, i’ll input the code now and i’ll tell you the results
Thanks for the help

It works! thanks for the help

1 Like

Correct the .push method adds to the end of the array.