[SOLVED] Timber Guard Python help

Hi,

I’m stuck on this level, it all seems to work fine but the soldiers I send out don’t all go to the same point, they often stop walking half way. The first soldier I send out moves to the XY coordinates but the rest sort of stop en route, is it something to do with them spotting an enemy?

Here’s my code:

while True:
# Collect gold.
coins = hero.findNearestItem()
hero.moveXY(coins.pos.x,coins.pos.y)
# If you have enough gold, summon a soldier.
if hero.gold > hero.costOf(“soldier”):
hero.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 hero.findFriends():
if friend.type == “soldier”:
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(friend, “attack”, enemy)
else:
pos = {‘x’: 68, ‘y’: 42}
hero.command(friend, “move”, pos)

Thanks in advance

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly

Apologies! Hopefully it’s formatted correctly this time:

while True:
    # Collect gold.
    coins = hero.findNearestItem()
    hero.moveXY(coins.pos.x,coins.pos.y)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.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 hero.findFriends():
            if friend.type == "soldier":
                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(friend, "attack", enemy)
                else:
                    pos = {'x': 68, 'y': 42}
                    hero.command(friend, "move", pos)

Try using the move method instead of moveXY. When using the moveXY method, once it identifies the nearest item the code doesn’t iterate through until the moveXY is complete. When using the move method, the hero takes a single step towards the item and then the code looks to see if there’s anything else it could be doing before taking another step. You need to command your troops and can’t do that if you’re moveXY is completing. MoveXY is good in some situations and move is better in others. Make sure that you are equipped with boots supporting the move method.

Thanks for the response. I added your suggested change but the same still happened - I also tried commanding them to go further to the right of the screen:

while True:
    # Collect gold.
    coins = hero.findNearestItem()
    hero.move(coins.pos,coins.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.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 hero.findFriends():
            if friend.type == "soldier":
                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(friend, "attack", enemy)
                else:
                    pos = {'x': 75, 'y': 42}
                    hero.command(friend, "move", pos)

image

The screenshot is just to show the reinforcements not moving, they walk a little bit and then stop

click on your boots in the middle of the game screen to see the proper use of the move method. You should always put an if conditional statement before using a method to find items. If there is ever a split second where there are no items, the code will fail.

Also, what glasses are you using?

I’m using crude telephoto glasses and I’ve got boots of jumping. I’ve added an if statement at the start for if item but I don’t see what I’ve done wrong with the move method. I’ve realised that the first soldier goes off fine but the subsequent ones get confused when the enemy they are moving towards dies, they then freeze and don’t move again for 5 or so seconds.

This is a structure problem. Look where the for loop is located. The only time you will command your friends is when you summon another soldier. The for loop needs to be inside the while True loop, but it is erroneously nested in another if conditional.

Ah I see, thanks for the help. Makes sense now!

When I use my code that I made with the hints it says cannot transform: set can someone explain how to fix my code.
MY CODE:
while True:
# Collect gold.
gold = hero.findNearestItem()

if gold:
    hero.moveXY(gold.pos.x, gold.pos.y)
# If you have enough gold, summon a soldier.
if {hero.gold > hero.costOf("soldier")}:
    hero.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 hero.findFriends():
    if friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        defendPos = {"x": 91, "y": 40}
        # If there's an enemy, command her to attack.
        # Otherwise, move her to the right side of the map.
        if enemy:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", defendPos)

Howdy and welcome to the forum!

I see two problems. The first is causing your error:

if {hero.gold > hero.costOf("soldier")}:

Is there a reason why you are enclosing the logic inside curly brackets?

Second, the ‘move’ method you are using. Think of ‘moveXY’ as a fire and forget method…once it fires, no other code will be executed until the x,y destination is reached. The ‘move’ method is a step at a time…it fires and the hero takes a step; if there is any code following the move command, it will be executed in turn.

i found another problem with the code. (for me anyway idk if you finished already) but at the top of the code

if gold:

it isn’t identified.

1 Like

@Levitius have you completed this level? If not, try to listen to what advice gave @dedreous and @098765432123?

well if so i am still stuck

i cant seem to get the code to work @AnSeDra

Can you create a new topic with your code?

@Levitius if you need any more help at this level check out this topic:
https://discourse.codecombat.com/t/timber-guard-help-please/22520/14