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

HELP Aggressive Mimicry [Solved]

This is my code

# Protect the village from the ogres. 
# Watch for ogres, peasants and ogres disguised as peasants.

# This function checks if the text starts with the word.
def startsWith(text, word):
    # If the word is longer then the text:
    if len(word) > len(text):
        return False
    # Loop through the indexes of word and text.
    for index in range(len(word)):
        # If characters with the same index are different:
        if word[index] != text[index]:
            # Then the word doesn't coincide with the text.
            return False
    # We checked all letters and they are the same.
    return True

ogreNameStart = "Zog"

while True:
    suspectFriend = hero.findNearest(hero.findFriends())
    suspectName = suspectFriend.id
    # Use the function "startsWith" to check
    # if suspectName starts with "Zog":
    if startsWith("Zog", ogreNameStart) is True:
        # Then attack suspectFriend:
        hero.attack(suspectFriend)
    enemy = hero.findNearestEnemy()
    # if there is an enemy, then attack it:
    if enemy:
        hero.attack(enemy)
    # Else return to the red X mark:
    else:
        hero.move({"x": 27, "y": 27})

My hero goes and kills the human at his left

Ry4n…rather then test for “Zog” on line 25, where your other parameter ‘ogreNameStart’ already holds that value, instead test it against the suspect name.

So like this?

    # if suspectName starts with "Zog":
    if startsWith(suspect, ogreNameStart) is True:
        # Then attack suspectFriend:
        hero.attack(suspectFriend)

it says suspect is not defined

Take a look at your line 25 (I might not have the count exact, but it should be close. In that line, you define the suspect…use that variable; it’s the same one you used in our attack statement.

never mind it was suspectName lol

    # if suspectName starts with "Zog":
    if startsWith(suspectName, ogreNameStart) is True:
        # Then attack suspectFriend:
        hero.attack(suspectFriend)

solved it, thanks! (20)

lol…and I goofed in my last reply too :smiley: