# Timber Guard code problem?

**URL:** <https://discourse.codecombat.com/t/timber-guard-code-problem/3410>\
**Category:** Uncategorized\
**Created:** [April 5, 2015, 8:37pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410 "2015-04-05T20:37:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [April 5, 2015, 8:37pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/1 "2015-04-05T20:37:56Z")

</div>

Here is my code:

```python
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?

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [April 5, 2015, 11:12pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/2 "2015-04-05T23:12:08Z")

</div>

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

* * *

Let’s play your code through:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![NeedHelpQuick](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@NeedHelpQuick](https://discourse.codecombat.com/u/NeedHelpQuick)\
**Post date:** [May 1, 2015, 9:59pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/3 "2015-05-01T21:59:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VincentMaths](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vincentmaths/32/2418_2.png) [@VincentMaths](https://discourse.codecombat.com/u/VincentMaths)\
**Post date:** [May 1, 2015, 10:29pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/4 "2015-05-01T22:29:24Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![NeedHelpQuick](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@NeedHelpQuick](https://discourse.codecombat.com/u/NeedHelpQuick)\
**Post date:** [May 1, 2015, 10:34pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/5 "2015-05-01T22:34:17Z")

</div>

Sorry I just need help on how to use for loops

---

<div class="post-metadata">

**Author:** ![VincentMaths](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vincentmaths/32/2418_2.png) [@VincentMaths](https://discourse.codecombat.com/u/VincentMaths)\
**Post date:** [May 1, 2015, 10:37pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/6 "2015-05-01T22:37:03Z")

</div>

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](http://www.w3schools.com/js/js_loop_for.asp).

---

<div class="post-metadata">

**Author:** ![spanishimp2](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@spanishimp2](https://discourse.codecombat.com/u/spanishimp2)\
**Post date:** [May 8, 2015, 12:26pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/7 "2015-05-08T12:26:37Z")

</div>

sorry posted on the wrong level!

---

<div class="post-metadata">

**Author:** ![Choung\_young\_jae](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Choung\_young\_jae](https://discourse.codecombat.com/u/Choung_young_jae)\
**Post date:** [July 15, 2015, 11:50am UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/8 "2015-07-15T11:50:23Z")

</div>

```python
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.

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [July 18, 2015, 8:54pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/9 "2015-07-18T20:54:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quackpear534](https://avatars.discourse-cdn.com/v4/letter/q/ecd19e/32.png) [@quackpear534](https://discourse.codecombat.com/u/quackpear534)\
**Post date:** [July 25, 2019, 4:07am UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/10 "2019-07-25T04:07:38Z")

</div>

```python
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)

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [July 25, 2019, 1:56pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/11 "2019-07-25T13:56:01Z")

</div>

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:

```python
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  
🦁 🦁 🦁

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [July 25, 2019, 2:12pm UTC](https://discourse.codecombat.com/t/timber-guard-code-problem/3410/12 "2019-07-25T14:12:55Z")

</div>

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.
