Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Help! Restless Dead

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!!

3 Likes
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:

3 Likes

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

4 Likes

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)
2 Likes

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)
2 Likes

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)
4 Likes

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

3 Likes

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:

3 Likes

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)

3 Likes

You might wanna blur that

2 Likes

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

Andrei

2 Likes

OK. Done.(20 characters)

3 Likes

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

1 Like

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

Andrei

1 Like

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

Andrei

1 Like

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

2 Likes

Here they are:

Andrei

2 Likes

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

2 Likes

And under my post it doesnt say “solution”

2 Likes

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

3 Likes