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

[SOLVED] The two flowers stuck

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findByType("solider")
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
        
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
    


while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

soldiers are not moving? what have I missed? thanks

def commandSoldiers():
    friends = hero.findByType("solider")
    # code follows

soldier not solider
/ I think I answered months ago one of your first questions :slight_smile: /

1 Like

thanks! yea I think you did. embarrassed I missed that one :frowning: !

This is my code:
// If the peasant is damaged, the flowers will shrink!

function summonSoldiers() {
if (hero.gold >= hero.costOf(“soldier”)) {
hero.summon(“soldier”);
}
}

// Define the function: commandSoldiers
function commandSoldiers() {
var friends = hero.findByType(“soldier”);
for (var i = 0; i < friends.length; i++) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.command(enemy, “attack”, enemy);
}
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
var coin = hero.findNearestItem();
hero.move(coin.pos);
}
var peasant = hero.findByType(“peasant”)[0];

while(true) {
summonSoldiers();
commandSoldiers();
pickUpNearestCoin();
}
Whenever I run the code I get and argument error. Can I get some help?
}```

Hi @Ruben_Ony and welcome to the forum! :partying_face:

Can you resend us your code, but this time format your code as it is described here so we will be able to help you?

Andrei

1 Like
// If the peasant is damaged, the flowers will shrink!

function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

// Define the function: commandSoldiers
function commandSoldiers() {
    var friends = hero.findByType("soldier");
    for (var i = 0; i < friends.length; i++) {
        var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(enemy, "attack", enemy);
    }
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
    var coin = hero.findNearestItem();
    hero.move(coin.pos);
}
var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    commandSoldiers();
    pickUpNearestCoin();
    
}

Maybe you need a conditional to check whether the coin exists first here:

function pickUpNearestCoin() {
    var coin = hero.findNearestItem();
    if (coin) {
        hero.move(coin.pos);
    }
}
1 Like

This line looks a bit off…

2 Likes

Gtg, sorry. I will let someone else to help you, @Ruben_Ony.

Andrei

@dedreous
I didn’t see that (; but i’ve replaced it and my code still doesn’t run.

#########

Show your corrected code please.

2 Likes
function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

// Define the function: commandSoldiers
function commandSoldiers() {
    var friends = hero.findByType("soldier");
    for (var i = 0; i < friends.length; i++) {
        var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.command(friend, "attack", enemy);
    }
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
    var coin = hero.findNearestItem();
    if (coin) {
        hero.move(coin.pos);
    }
    
}
var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    commandSoldiers();
    pickUpNearestCoin();
}

You are likely getting a ‘variable not defined error’…‘friend’ is not defined. However, you do have the array ‘friends’, which is the list of all your identified friends. You just need to state which ‘friends’ you are commanding.

@dedreous
How do I do that? Sorry I’m a bit rusty because I’ve been very busy at the moment.

You are iterating thru your friends array, using the ‘for’ loop. Each time the loop cycles, ‘i’ increments by one…‘i’ is the counter (or pointer); it indicates which element of the array is currently being examined. Therefore, friends[0] would point to the first friend in the list, friends[7] would point to the 8th, etc etc.

In your command statement, simply state which of your friends you are commanding.

So I would type

hero.command(friends[0], "attack", enemy);

You are on track, but not quite there…using [0] means you will always be commanding friends[0] and never any of the others. Instead, try using your counter…remember, the loop is constantly changing, as it cycles through the list of friends.

So I increment friends?

no…you are already iterating thru that list:

Sorry I’m stuck. Could you just send me the solution?