[Solved] Protect And Serve Help!

Hi, I was having trouble with Protect and Serve, and I’m not sure how to finish it.

# Protect the workers and animals!

# Defend these two positions:
defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }
defend[2] = {"x": 96, "y":73}

soldiers = []

friends = hero.findFriends()
for index in range(len(friends)):
    friend = friends[index]
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # Defend the workers:
        hero.command(soldier, "defend", {'x':friend.pos.x, 'y':friend.pos.y})#Error appears here

while True:
    # Use a for-loop to assign each soldier to a corresponding defend[] target
    # Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    for i in range(len(defend)):
        soldier = soldiers[i]
        if soldier:
            hero.command(soldier, "defend", defend[i])
    #Pretty sure that there's something wrong with this code somehow, too.
    pass

I see a couple of issues. First, in your for index loop (as you point out), you have:

        # Defend the workers:
        hero.command(soldier, "defend", {'x':friend.pos.x, 'y':friend.pos.y})#Error appears here

Instead of actively defending, which comes later, you want to add (append) the friend to the ‘defend’ array.

Second, you’ve manually added defend[2] to the code…why?

As you’ve also pointed out, in the while True…you are intending to work through the soldiers array (iterate), but are doing so using an attribute of the defend array. You should use the soldiers array attributes instead.

Finally, if there is an enemy, kill it!

2 Likes

Thanks, I changed my code, but I added the defend[2] to stop the ogres from taking that group of coins for the bonus objective. I took it out and the code worked fine, but when I tried to reinsert it, my code stopped working. Is this how I’m supposed to get the bonus objective?

Update: I told my hero to defend the position instead, and I got the bonus objective. Thank you for your help!

2 Likes

Well done! That was going to be my answer…the hero does it :wink:

3 Likes

Hi, it seems I have a similar problem. Can you tell me what can I do better?

defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }

soldiers = []

friends = hero.findFriends()
for index in range(len(friends)):
    friend = friends[index]
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # Verteidige die Arbeiter:
        defend.append(friend)


while True:
    # Benutze eine for-Schleife, um jedem Soldaten ein entprechendes defend[]-Ziel  zuzuweisen.
    # Verwende command(soldier, "defend", Einheit) oder command(soldier, "defend", Position)
    for i in range(len(soldiers)):    
        soldier = soldiers[i]
        defendziel = defend[i]
        hero.command(soldier, "defend", defendziel.pos)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

Welcome to the forum @Lesyab ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders!

Thank you for formatting your code :slight_smile:

The only thing you need to change is to remove the word “pos” in this line:

Thank you for answering @Aya. Actually, I tried that first:

soldier = soldiers[i]
        defendziel = defend[i]
        hero.command(soldier, "defend", defendziel)

I changed it, following the advice of @KalPal:Update: I told my hero to defend the position instead,

It does not work for me: the level is “incomplete”

Does it work without what Kaleb said? (without the bonus)

no, unfortunately not: “incomplete”

Hmm can you repost your new code along with a screenshot, your gear, and any errors you are getting please

defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }

soldiers = []

friends = hero.findFriends()
for index in range(len(friends)):
    friend = friends[index]
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # Verteidige die Arbeiter:
        defend.append(friend)


while True:
    # Benutze eine for-Schleife, um jedem Soldaten ein entprechendes defend[]-Ziel  zuzuweisen.
    # Verwende command(soldier, "defend", Einheit) oder command(soldier, "defend", Position)     
    for i in range(len(soldiers)):    
        soldier = soldiers[i]
        defendziel = defend[i]
        hero.command(soldier, "defend", defendziel)

Weird, it is working for me. Are you getting any errors?

Thank you for helping. Here is my code.

Sorry. It seems I am not allowed to write in this chat any more:
"An error occurred: We’re sorry, but new users are temporarily limited to 3 replies in the same topic.

Are you still getting this error, it should be fine now i guess

The code still works when i use your gear, that is very weird. What are the incomplete goals?

Oh, yes. Thank you! Yesterday, I got the following e-Mail: "we’ve promoted you up a [trust level] "
:slight_smile:

1 Like

The level didn’t work, did it? What are the incomplete goals? (if it still isn’t working)

yap. Unfortunately, it still doesn’t work.

I have the same problem with the level [Preformatted text](https://codecombat.com/play/level/borrowed-sword)

It seems like it should work, but it does not.

# In diesem Level wird dein Held nicht kämpfen.
# Befehle deinen Schützen auf den Gegner mit der höchsten Gesundheit zu schießen
def opfer(units):
    #enemies = hero.findEnemies()
    enemyIndex = 0
    strongest = None
    besthealth = 0
    for enemyIndex in range(len(units)):
        enemy = units[enemyIndex]
        if enemy.health > besthealth:
            besthealth = enemy.health
            strongest = enemy
        return strongest 

while True:
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    Index = 0
        
    for Index in range(len(friends)):
        friend = friends[Index]
        target = opfer(enemies)
        if friend.type == "archer" and target:
            hero.command(friend, "attack", target)
            

Cay you, please check if it works for you?

Ok, I did Level borrowed-sword does not work

I suggest that you make a new topic for the other level to keep things organized. At the home screen on the top right, you can click on [ + New Topic ] (If you need help in that, feel free to send me a PM)

As for the level protect and serve, can you show me your gear? Like this for example:

Screenshot 2023-02-09 18.53.33

Thanks