[SOLVED] Help! Restless Dead [Python]

I need help with Restless Dead.

while True:
    enemy = hero.findNearestEnemy()
    soldiers = hero.findFriends()
    soldierIndex = 0
    soldier = soldiers[soldierIndex]
    item = hero.findNearestItem()
    
    if enemy:
        
        if hero.isReady("throw"):
            hero.throw(enemy)
        else:
            hero.attack(enemy)
    elif item:
        hero.moveXY(item.pos.x, item.pos.y)
    elif hero.gold > 0:
        hero.moveXY(19, 40)
    elif hero.gold > hero.costOf("soldier"):
        hero.moveXY(19, 40)
        hero.summon("soldier")
    elif soldier:
        soldierIndex += 1
        hero.command(soldier, "attack", enemy)

This is my code, it defeats the yeti and collects the coins. And it attacks the skeletons, but for some reason, it doesn’t summon the soldiers.
Somebody please help me!!

def commandSoldiers():
    for soldier in hero.findByType("soldier"):
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)
            
            
            
while True:
    enemy = hero.findNearestEnemy()
    soldiers = hero.findFriends()
    soldierIndex = 0
    soldier = soldiers[soldierIndex]
    item = hero.findNearestItem()
    
    if enemy:
        
        if hero.isReady("throw"):
            hero.throw(enemy)
        else:
            hero.attack(enemy)
    elif item:
        hero.moveXY(item.pos.x, item.pos.y)
    
    elif hero.gold > hero.costOf("soldier"):
        
        hero.summon("soldier")
        for soldier in hero.findFriends():
            enemy = soldier.findNearestEnemy()
            if enemy:
                hero.command(soldier, "attack", enemy)
        
        commandSoldiers()
    else:
        hero.moveXY(19, 40)

This is my revised code, it does all the stuff that I said in the previous post, plus it summons soldiers now, but the soldiers won’t attack the skeletons. Please help! :worried:

I see a few problems.
First you forgot the increment of soldierIndex

Second you already defined commandSoldiers() at the top whats the point of repeating it again in the loop

Lastly since you already defined commandSoldiers() and it includes hero.findByType than theres no point to put it again in the loop.

Hope it helps
Charlie

I did what you said(code below), but my soldiers still won’t attack.

def commandSoldiers():
    for soldier in hero.findByType("soldier"):
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)
            
            
            
while True:
    enemy = hero.findNearestEnemy()
    soldiers = hero.findFriends()
    soldierIndex = 0
    soldier = soldiers[soldierIndex]
    item = hero.findNearestItem()
    
    if enemy:
        
        if hero.isReady("throw"):
            hero.throw(enemy)
        else:
            hero.attack(enemy)
    elif item:
        hero.moveXY(item.pos.x, item.pos.y)
    
    elif hero.gold > hero.costOf("soldier"):
        
        hero.summon("soldier")
        
        soldierIndex += 1
        commandSoldiers()
        
    else:
        hero.moveXY(19, 40)

Maybe it can work

def commandSoldiers():
soldiers = hero.findFriends()
    if soldiers:
        for soldier in soldiers:
             if soldier and soldier.type == "soldier":
               enemy = soldier.findNearestEnemy()
               if enemy:
                 hero.command(soldier, "attack", enemy)

And about soldiers

 elif hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
        friends  hero.findFriends()
        if friends:
           for friend in friends:
               if friend and friend.type == "soldier":
                   commandSoldiers()
else:
       hero.moveXY(19, 40)

Delete the parts below because you already defined commandSoldiers()

I suggest you do something like this:

def commandSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    for soldier in hero.findByType("soldier"):
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

Yes, if you use “for”, you can delete"Index".

I did what y’all said

def commandSoldiers():
    soldiers = hero.findFriends()
    if soldiers:
        for soldier in soldiers:
            if soldier and soldier.type == "soldier":
                enemy = soldier.findNearestEnemy()
                if enemy:
                    hero.command(soldier, "attack", enemy)
                    
                    
                    
while True:
    enemy = hero.findNearestEnemy()
    
    item = hero.findNearestItem()
    
    if enemy:
        
        if hero.isReady("throw"):
            hero.throw(enemy)
        else:
            hero.attack(enemy)
    elif item:
        hero.moveXY(item.pos.x, item.pos.y)
    
    elif hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
        friends = hero.findFriends()
        if friends:
            for friend in friends:
                if friend and friend.type == "soldier":
                    commandSoldiers()
        else:
            hero.moveXY(19, 40)
    else:
        hero.moveXY(19, 40)

This is my code now, but it still didn’t work. :worried:

Delete this part because you already defined commandSoldiers() and the code below are included inside

Why are you adding this again if you already have it, so you can delete this part.

After this part try calling the commandSoldiers()

(I’m not sure if its going to work because I reset my progress for JavaScript and lost all my equipment so I can’t test the code to see if it works, I’ll try to assist without running the code)

You might wanna blur that

If it is the solution, can you please remove it because it is against the rules of this forum?

Andrei

OK. Done.(20 characters)

@AnSeDra since your regular can’t you edit or delete posts?

No, I can not. Only leaders can do that.

Andrei

And if it is the solution I meant to delete all of the code, not just the blur.

Andrei

Oh ok make sense by the way where is the rules of the forum

Here they are:

Andrei

But why? In this topic there are lots of programs.

And under my post it doesnt say “solution”

I meant if you can copy and paste the code to the level and it completes, then it is a solution and it should be removed. If not, try to only give pieces of the correct code and only after you tried to explain a copule of times, ok @PeterPalov?

Andrei