Timber Guard code problem?


#1

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?


#2

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.


#3

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


#4

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


#5

Sorry I just need help on how to use for loops


#6

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.


#7

sorry posted on the wrong level!


#8
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.


#9

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.