Level: Peasant Protection


#63

well this is what i got it works actually it could help u

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 10) {
        loop {
            if (this.isReady("cleave")) {
                this.cleave(this.findNearestEnemy());
            }
            else {
                this.attack(enemy);
                this.shield();
            }
        }
        // Attack if they get too close to the peasant.
            
    }
    // Else, stay close to the peasant! Use else.
    
    
}

i hope it works


#64

oops i did the wrong program, nvm i thought i did the right one, :frowning:


#65

Hello DustNite and welcome to the forum,

You can display your code nicely by adding thre backticks (```) before and after your code. I did this for you this time.
Please also indent your code properly. It increases readability by a lot and also shows a little flaw in your logic this time. I indentet the code for you this time.

Both of these points are mentioned in the FAQ, which you should read before posting.


Please test your code before posting it as solution. You should only do so if you passed a level with the code you posted.

The problem your code has is that you have two loops that will run forever, one in the other. What will happen is that once an enemy comes closer than 10, you will enter the inner loop and never escape again, as you can’t exit a loop [1]. This means you will always try to cleave the nearest enemy (not too bad), but always attack the first enemy (which is usually dead pretty fast). And your distance is never updated.

Other problems your code has:

  1. You use enemy withough checking if there is an enemy.
  2. You never stay close to the peasant.

Getting everything together, this results in the following code:

loop {
    var enemy = this.findNearestEnemy();
    if (enemy && this.distanceTo(enemy) < 10) { //See how I combined the checks?
        if (this.isReady("cleave")) {
            this.cleave(enemy); //You already know the nearest enemy, no need to find it again.
        }
        else {
            this.attack(enemy);
        }
    }
    else { //== if( !(enemy) || !(this.distanceTo(enemy) < 10 )
        //move close to the peasant (I don't know the correct position right now.
    }
}


Footnotes

[1] Yes I know you can break; out of a loop, but it hasn’t been teached by now. And breaking is generally a bad habit, otherwise we could go back and program directly in Assembler. Once people know how to properly write a program without break;, we can introduce it. Otherwise we get what is called Spaghetticode. And nobody wants to debug Spaghetticode.


Pesant Protection help
#66

Guys I found a easy way to beat it:

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 15) {
        if (enemy) {
            this.attack(enemy);
            this.moveXY(40, 33)

it is a easy way


#67

how do i show my code


#68

is this for peasent protection because if it is its not working and i copied it word for word


#69

You have to copy it over here (Ctrl+C->Ctrl+V) and enclose it in three backticks (```) before and after the Code, as described in the FAQ.


#70

oh i know i for got to close the loop and the if statement i will give u the new one with the closing loop and if statement:

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 15) {
        if (enemy) {
            this.attack(enemy);
            this.moveXY(40, 33);
        }
        // Attack if they get too close to the peasant.
        
    }
    // Else, stay close to the peasant! Use else.

Merged Doublepost

it should work now i gave the whole code


#72

Alright thanks ill do that tommorow because i have no more free time


#73

just getting a badge


#74

I’ve tried to do all sorts of things, copy people, do my own code, nothing worked! This is my code, and can you please see what my problems are?

loop:
enemy = self.findNearestEnemy()
distance = self.distanceTo(enemy)
if distance < 10: self.attack(enemy)
if self.distanceTo(“Victor”) < 5: self.cleave(enemy)
if distance < 10: self.attack(enemy)


#75

Please put ``` (three backticks on an otherwise empty line before and after your code so that it formats the way you have it in the code window. (also read the posting faq, just 'cause) :smile:

On that level you are supposed to:

loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy)
    if distance < 10:
        # Attack if they get too close to the peasant.
                
    # Else, stay close to the peasant! Use else.

your attack commands will steadily move you away from the peasant.
You need code for that “# Else, stay close” comment

    else:
        self.moveXY(x, y)

use your mouse to get the x,y coordinates for the big X by the peasant.

OK, now a question. Why do you cleave (an enemy) if you are close to “Victor” wouldn’t it better to cleave if you are near an enemy? (no matter how close you might be to Victor, you will walk to the enemy to cleave it)


#76

The name “Victor” in this case is not named for the peasant. You have to be close to him:

moveXY( 40, 33);

So u would be close to Victor


#77

Hi, all
I try many variants. But they killed Victor/ What wong?

while true do
    enemy = self:findNearest(self:findEnemies())
    farmer = self:findNearest(self:findFriends())
    self:moveXY(40,37)
    if enemy then
        if self:distanceTo(enemy) < 10  then
             self:attack(enemy)
            end
    else 
        self:moveXY(40,37)
   end
   self:say("Delete this when your loop isn't infinite.")   
end

#78

Hi Bambr and welcome to the forum,
you can present your complete code in a nice way by enclosing it in three backticks (```) before and after the code. This is also described in the FAQ, which you should read anyway before posting.
I fixed it for you this time.


Your problem is that when there is an enemy that is further away than 10 m, you do nothing.
you should check

if enemy and self:distanceTo(enemy) <10 then
    self:attack(enemy)
else
    self:moveXY(40, 37)
end

#79

loop{
var enemy= this.self.findNearestEnemy();.
var distance= this.self.distanceTo(enemy)
if (distance < 15) {
if (enemy) {
this.attack(enemy);
this.moveXY(40, 33)


#80

http://discourse.codecombat.com/t/level-peasant-protection/1329/77http://discourse.codecombat.com/t/level-peasant-protection/1329/77


#82

You did neither write what the problem with this code is nor did you present it properly, despite explained several times how to in this thread alone.
Also this code will not run, as their are obviously some closing brackets missing and their is a point behind the second line.
Please edit this post to present your code properly and describe your code.


This is a (not working, but I see what you tried) link to bambrs post just three posts ago. If you want to reference it, it is much better to quote something or use the reply-button in the lower right corner of each post.
Other than that, I can’t see what you want to try to achieve with this.

Also if you notice that you messed up something, but already hit “reply” (which happens even to me), you can edit your post from the tools-bar right next to the reply-button.


#83

Nick. PLEASE HELP ME! im learning python and i want to stop alltogether.
The original code it gave me (enemy = self.findNearestEnemy()) is in front of the loop and messes it up, and he wont attack anything. im getting soo mad and i only started 1 day ago.
Thanks,
EJ


#84

Nevermind. Now i feel like an idiot, but the only problem was, i had the upgrade above the wooden glasses, so

self.findNearestEnemy

didnt exist.
Thank you anyway, for helping other people.