[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?