Timber turncoat - can't solve

Hi,

I can’t get through this level, my codes work, but all my summoned soldiers will be killed by the enemy and then the peasants, please help, thanks

while True:

gold= hero.findNearestItem()
hero.moveXY(gold.pos.x, gold.pos.y)

if hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    

for friend in hero.findFriends():
    if friend.type == "soldier":
        enemy = friend.findNearestEnemy()
       
        if enemy:
            hero.command(friend, "attack", enemy)
            if friend.health < friend.maxHealth:
                hero.command(friend, "move", {'x':44, 'y':44})

            
        else:
            hero.command(friend, "move", {'x':60, 'y':43})

Make a while true statement using if gold:
hero.move(gold.pos)

look at the statement if friend.health < hero.maxHealth you can change to if friend.health > 100

hero.command(friend, “move”, {‘x’:25, ‘y’:45})
hero.command(friend, “move”, {‘x’:75, ‘y’:45});

hey mate, thanks for the update :slight_smile:

  • this is already a ‘while true’ loop;
  • if friend.health > 100?
  • 2 commands under if friend.health > 100?
  • I’ve done these changes, but still not working.(soldier died and Shaman came out)

Your current code tells your soldiers to attack regardless of their health.

        if enemy:
            hero.command(friend, "attack", enemy)
            if friend.health < friend.maxHealth: 
                hero.command(friend, "move", {'x':44, 'y':44})

Consider checking your health before commanding the attack so those with less health can move away. If their health is above a certain range (wouldn’t use maxHealth because one hit and every soldier runs away), command them to fight or move to a good defense point until their health goes below, then retreat. To help determine what is a safe range, the soldier’s start with 200 health and each hit from a scout takes 12 health.

Another tip, you can click on any of the characters to see their health bar to help gauge your ranges.

hey mate

I understand what you’re talking, what you said was what I tried for the first time in this level, but the reality is even if I command my soldier to run away right after the the first hit, my soldier still don’t have enough time to run alive, because the enemies, they just attack the same soldier at the same time.

I was wondering does it have to do with the hero? I’m using “Captain Anya Westen”

Thanks

or is there a way to upgrade the boss star, because I feel that my soldiers are too weak, all the enemies come to the same coordinate and attack the same soldier, even if it’s 12HP/sec, my soldier couldn’t take it, and usually got killed while retreating

A boss star 1 will work on this level and hero has no change to this one. It’s all about commanding the soldiers. When I ran your earlier code, I noticed the soldiers don’t get to the enemy before the enemy takes out a peasant. Move your soldiers farther right to intercept the enemies first.

I also noticed my comment about the soldiers always attacking was incorrect. On a separate conversation, we discussed how the hero.command() will only run one time per loop and it will always be the last code accepted. You need to arrange your if-elif-else checks so that only one hero.command() will run based on the checks you put in place.

What is your code now?

thanks for getting back on this, I’m on other tasks now, but this is the codes for that level:
I had to put “friend.health < 200” here, but even so, my soldier still got killed.

while True:
gold= hero.findNearestItem()
if gold:
hero.moveXY(gold.pos.x, gold.pos.y)

if hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    

    
for friend in hero.findFriends():
    if friend.type == "soldier":
        enemy = friend.findNearestEnemy()

        if enemy:
            hero.command(friend, "attack", enemy)
            
        else:
            rightPos = {"x": 80, "y": 45}
            hero.command(friend, "move", rightPos)
            
        if friend.health < 200:
            LeftPos = {"x": 22, "y": 45}
            hero.command(friend, "move", LeftPos)

As soon as your soldier takes one hit, they run away… meaning they don’t attack the enemy and kill it. They need to stand their ground for a while to kill the enemy. Try changing your health from < 200 to <100. Also, instead of having the health check last, let’s put it first, with an else statement. In the else statement include the enemy check. Your three commands are correct, you just need to get them in the correct alignment so you only command the ally once based on the checks

Health low- Yes NO
Enemy - Yes NO
if no to both just wait

but there is a dilemma, if I just let the soldier stand there attacking the enemy for more than twice, there is literally no chance that my soldier can retreat alive, I’ve tried health <150 and health < 100, it doesn’t work, and the death of the first soldier just brings the “Shaman” who makes the soldiers even more poweful. But I’ll try it again.

Thanks

typo, “Shaman” makes the enemies even more powerful

Ok, so I misspoke when I said the hero has no change in this level. They do to some extent by how fast or slow they are getting coins. The boss star would get you more coins to help out, or get the speed ring with fast boots. Or pick a faster hero, if they are available to you.

1 Like

hey, this is an interesting point of view, I’ll try pender then, he’s faster, will keep you pudated

it succeeded with pender with the spell “haste”, that’s an amazing performance! thanks!

You are welcome. It has been a while since I’ve played CodeCombat and I forget the gaming component can play a bigger part of this than you realize. I also have a subscription so I rarely use the free characters and forget their limitations.

yeah, I’m subscribed user as well, but just never spend the gems on anything that isn’t very necessary, I have over 60K gems in the game, but I only spend it when I’m asked to in the game, a little bit cheapskate I know, lol, I need spend more time with the equipment. But that makes me feels a lot better walking through this level, it’s been there for a couple of days, was a bit frustrated. Thanks mate!