Is this a gameplay bug in level "peasant protection"? - farmer always gets killed

I’m really getting frustrated by the level “Peasant protection”. The code is correct (I checked the forum for similar issues), but I still end up with the farmer getting killed.

Here’s the code:

while True:
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    if distance < 10:
        # Attack if they get too close to the peasant.
        hero.attack(enemy)
    else:
    # Else, stay close to the peasant! Use else.
        hero.moveXY(40, 37)

The code works as expected. My hero kills two or three enemies, but then one of the attackers manages to kill the peasant, right before my hero kills the attacker.

I tried to fiddle with the distance (anything between 10 and 20) and also with the moveXY coordinates (forum mentions 40,38 regularly, but the in-game hints say it should be 40, 37).

Is there anyone else who has had this??? This is really frustrating, because it is blocking and has nothing to do with good programming (unless I missed something?)

1 Like

Update: the problem looks as if the munchkins are too fast and are always able to get at least one blow in, before the hero attacks them (the peasant dies after three blows).
I tried relatively complicated constructions to move more in the direction of the munchkin to pre-empt the attack, but to no avail.

1 Like

Could you email us your CodeCombat username to team@codecombat.com ? Or you could use direct message and send it me here. I will check your last session and will try to find a problem.

Thanks. I see the problem. Your solution is correct, but your sword is weak and you need hit twice to defeat an ogre.
Do you have “Long Sword”? It’s very useful sword for further levels, because it has “cleave” ability. You can unlock it after Level 8 (forest) - Ogre Encampment.

1 Like

“Ogre Encampment” is placed earlier than “Peasant Protection”. So if you haven’t “skip” levels you should have “Long Sword”.

1 Like

Thank you, Valentin; quite a silly mistake

1 Like

No problem. “silly mistakes” are most often mistakes in developer routine and one of the hardest to find.

1 Like