Timber Turncoat help

My code is fine, it fineshes the level (as long as those death frisbee thingies don’t hit a peasant) but it glitches out and my hero does this wierd turning back and fourth thing, as if he stein one direction and then another.

c = 0
while True:
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
          

i tried putting “if item and c < 5” in the code but then the hero loses the level, even thought this should make him arrive to fight earlier. What is wrong here?

also all the if c > or if c < stuff is to make certain parts of the code only run at certain times

c = 0
while True:
    array = hero.findFriends()
    
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item and len(array) < 16:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
            

fixed the code but id still like to make it make more sense, what i did here is count the peasants (11) and make 5 soldiers (total 16, which is why array has to be less than 16) which i shouldn’t have to do because the variable c should work the same yet it doesnt.

also the code will mess up and make the hero collect gold if a peasant is killed but if that happens i lost anyways

c = 0
array = hero.findFriends()
while True:
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x" : 80, "y" : 40})
    item = hero.findNearestItem()
    if item and len(array) < 16:
        hero.move(item.pos)
    if hero.gold >= hero.costOf("soldier") and c < 5:
        hero.summon("soldier")
        c += 1
    elif c > 4:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        else:
            hero.move({"x" : 80, "y" : 40})
    if len(array) < 16:
        array = hero.findFriends()   

i also just locked the code so if len(array) ever equals 16 the game will never reset it to 15 or lower (like if a soldier gets killed)

also one more thing i try to get the hero to stand in the way of the death to peasant frisbee things but he just moves on no matter what

The second to last code you posted pretty much solves the level, you just need to keep the hero on the left side of the map. If you move the hero to the right then headhunters and a shaman appear, whereas if you just stay on the left collecting gold, only scouts turn up, and one by one over the level so that the soldiers can easily kill them.
Danny

A shaman appears both ways

also,
the last peice of code I posted now works for some reason. I solved it days ago but that was just replaying the same code over and over again. It now works without pressing the ‘submit’ button.

I don’t know what is wrong with my code, but it’s just not working! Here is my code:

while(true) {
    // Collect gold.
    let item = hero.findNearestItem();
    if (item) {
        hero.move(item.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.
    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.
            // Careful! If your soldiers are defeated, a warlock will appear!
            // Otherwise, move her to the right side of the map.
    if (enemy) {
        hero.command(friend, "attack", enemy);
    }        
    else {
        hero.command(friend, "move", {x:84,y:45});
    }
    if (friend.health < 100) {
        hero.command(friend, "move", hero.pos);
    }
        }
    }
}

i think your soldiers are just reaching too far; try changing the position they move to to like {x:70,y:45} or something around there

I did that, but it is still not working :sob:

If you have boss star 2 you can change the code to summon and command soldiers instead; otherwise its just a matter of luck so keep submitting it until you win :man_shrugging:

I don’t have a boss star 2

The seeds are really inconsistent, sometimes you can summon 7-8 soldiers before a single scout spawns and sometimes 3 spawn at once when you only have like 2-3 soldiers, so I would just keep submitting your code until you get a reasonable seed.