Timber Guard code problem?

Here is my code:

loop:
    coin = self.findNearest(self.findItems())
    enemy = self.findNearest(self.findEnemies())
    friends = self.findFriends()
    if coin:
        self.move(coin.pos)
        if self.gold >=20:
            self.summon("soldier")
            
            for friend in friends:
                if friend.type is "soldier":
                    self.command(friend, "move", {'x':58, 'y':43})
                    
        elif enemy:
            if friends:
                for friend in friends:
                    if friend.type is "soldier":
                        self.command(friend, "attack", enemy)
                           

However, for some reason, when I command my soldiers to move, I am only able to command the ones that I summoned perviously. For example, say I summon one soldier. He is not commanded. I collect gold, then summon another soldier. When the command is used again, only the first soldier that I summoned obeys, and so on. What am I doing wrong?

The explanation and solution is at the end. You should also do something when you find no coin!


Let’s play your code through:

Current situation:
You (Whatever hero, doesn't matter)
Helgen (Soldier)
Sofia (Soldier)
Gold : 20

----------------------------------------------------

No coin ->
Do nothing... This really should not happen...

----------------------------------------------------

We found a coin!
We found no enemy!
We got all friends ( == [Helgen, Sofia] )
Move to the coin.
We have more than 20 Gold...
Summon Kira (new soldier)
For every friend in friends ( == [Helgen, Sofia] ) order them to move.

As you can see, your problem is that you don’t update friends after you summon a soldier, and you ever only command your soldiers to move to (58, 43) after you summoned a soldier. Conclusion: The newest soldier will not move until you spawn a new one.

Solve this by putting friends = self.findFriends() just before the for-loop.

1 Like

Please post the correct code so I can see what is different :smile:

@NeedHelpQuick Giving away correct code is frowned upon, as copy pasting isn’t a skill that requieres a lot of thoughts. If you have specific question, please formulate it.

The “gimme da anser” attitude doesn’t trigger much altruism in people. At least not in me.

Sorry I just need help on how to use for loops

Still amazingly vague. What’s the code you’re using, what do you understand, what can’t you make working ? What language ?

Here is some informations about for loops in JavaScript.

sorry posted on the wrong level!

loop:
    # Collect gold.
    golds = self.findItems()
    gold = self.findNearest( golds )
    self.moveXY( gold.pos.x , gold.pos.y)
    # If you have enough gold, summon a soldier.
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")
    # Use a for-loop to command each soldier.
    # For loops have two parts: "for X in Y"
    # Y is the array to loop over.
    # The loop will run once for each item in Y, with X set to the current item.
    for friend in self.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            # If there's an enemy, command her to attack.
            if enemy:
                self.command( friend, "attack", enemy )
            # Otherwise, move her to the right side of the map.
            else:
                pos = {'x':82, 'y':33}
                self.command( friend, "move", pos )

But sometimes it doesn`t work, because solider cannot get the position of {‘x’:82, ‘y’:33} before the enemy appear & attack the peasant.
I think it is random when the ogre appear.

It’s not quite random. If your soldier can’t move there in time, have her move a shorter distance. That should fix the problem.

while(true) {
    
    var coin = hero.findNearestItem();
    hero.moveXY(coin.pos.x, coin.pos.y);
    
    if (hero.gold >= hero.costOf("soldier")){
    hero.summon("soldier");
    var friends = hero.findFriends();
    for(var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        if(friend.type == "soldier") {
            var enemy = friend.findNearestEnemy();
            // If there's an enemy, command her to attack.
            // Otherwise, move her to the right side of the map.
        if (enemy) {
            hero.command(soldier, "attack", enemy);
        }
        else {
            hero.command(friend, "move", {x: 76, y: 47});
        }
        }
        }
}
}

For some reason its saying “soldier” is undefined when I command a soldier to attack.
(This is Javaascript)

It’s because you haven’t checked whether there is a soldier, so it doesn’t know who you’re trying to command because it’s chosen a specific index of an empty array (friends).
Also you indentation is not correct, am I right in thinking that that problem arose when posting?
I believe it should be:

while(true) {
    var coin = hero.findNearestItem();
    hero.moveXY(coin.pos.x, coin.pos.y);
    if (hero.gold >= hero.costOf("soldier")){
        hero.summon("soldier");
    }
    var friends = hero.findFriends();
    for(var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        if(friend.type == "soldier") {
            var enemy = friend.findNearestEnemy();
            // If there is an enemy, command her to attack.
            // Otherwise, move her to the right side of the map.
            if (enemy) {
                hero.command(soldier, "attack", enemy);
            }
            else {
                hero.command(friend, "move", {x: 76, y: 47});
            }
        }
    }
}

Make sure that in future you post your code as you have it in the level otherwise it can lead to people thinking your code has problems which it doesn’t.
Thanks
:lion: :lion: :lion:

Double check to see which variable you are using to command. You can’t command a soldier, because you don’t have a variable named soldier in your code. Your second command will give you the answer.

Also, reconsider what type of move command you are using for collecting your coin. MoveXY prevents you from commanding your friends until you pick up the coin, while move allows you to command your friends every step.

1 Like