Backwoods Brawl

So I upgraded my classes and now I am having problems getting code to work on backwoods brawl. I would like my hero to kill archers, cleave if four or more enemies and attack when otherwise unoccupied. The issues seems to do with the reference in the self.attack(archer). Any thoughts?

loop:
    enemies = self.findEnemies()
    archers = self.findEnemyMissiles()
    if archers:
        archer = self.findNearest(archers)
        self.attack(archer)
    elif len(enemies) > 4:
        enemy = self.findNearest(enemies)
        if self.isReady("cleave"):
            self.cleave(enemy)
    else:
        enemy = self.findNearest(enemies)
        self.attack(enemy)

Also I would love some thoughts on how to properly use the shield while in engaging in combat.

You can use wrap your code in three backticks to preserve formatting:

```
loop:
    enemies = self.findEnemies()
```

becomes

loop:
    enemies = self.findEnemies()

To more directly answer your question re: archers:

I haven’t upgraded my glasses to this level yet but I believe the findEnemyMissiles method is going to give you a collection of arrows and not archers, e.g. “Great for becoming more like Neo from the Matrix.” I think you’re going to need to use findByType or findEnemies and check the type property.

1 Like

I have some thing like this in my code in order to access the ranged characters:

throwers = self.findByType('thrower')
shamen = self.findByType('shaman')
thrower = self.findNearest(throwers)
shaman = self.findNearest(shamen)

Question, is it shamen or shamans?

Using findMissiles would be for seeing if there is anything flying toward you currently, I guess you could then attempt to dodge it.

I tried this code and it said that I needed to use “if” on the emphasized line below. It said that the “target is null.” I AM using if. What am I doing wrong?

loop {
    var enemy = this.findNearestEnemy();
    if (enemy) {
        this.attack(); //emphasized line
    }
}

I edited your post to show correct indentations for better readability, sadly the emphasization (can you make a noun out of emphasize?) got messed up. I hope my fix is clear enough.


You need to actually tell what you want to attack. Use this.attack(enemy).

1 Like
loop:
    enemy = self.findNearestEnemy()
    if enemy:
       self.attack(enemy)
    if self.isReady("cleave"):
       self.cleave(enemy)
1 Like

Please provide some more information. Does your code work? If not, what happens? What are you expecting to happen? What did you do to resolve the problem?


After this short introduction into “How do I get the best help possible?” I will guess what happens:

  1. Sometimes your hero suddenly stands still
  2. You get a cryptic error message about undefined variables.

This is because after you attacked an enemy, it may be dead, but you try to cleave it. Or there is no enemy at all, and you try to cleave something that doesn’t exist.

This is because your if self.isReady("cleave") is not inside the if enemy:
Which leads me to my new favorite sentence:
Indentations save lives!
If not yours, at least your heros life that is.
The code you probably want is

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        if self.isReady("cleave"):
            self.cleave(enemy)
        else:
            self.attack(enemy)
3 Likes

You create an emphasis when you emphasize something.

1 Like

Hello dwhittaker!

I am also tying to do backwoods brawl and I was trying to use the code that you have in your post here:
throwers = self.findByTypes(‘thrower’)
thrower = self.findNearest(thrower)
…
The thing is that when I try to do this myself it says that undefined is not of function.
Is this a bug or what.
This is a sample of my exact code.

loop:
    archers = self.findByTypes("thrower")
    warriors = self.findByTypes("munchkin")
    archer = self.findNearest(archers)
    warrior = self.findNearest(warriors)
    if archer:
        if warrior:
            if self.distanceTo(warrior) < 3:
                self.attack(warrior)
        self.attack(archer)
    elif warrior:
        if self.distanceTo(warrior) < 7:
            self.attack(warrior)
        else:
            self.powerUp()
    else:
        self.powerUp()

Thanks in advance!
Also this happens to be my first post!
-Jython

[editted to use ``` Please read the FAQ]

1 Like

Check your glasses @jython. Do they have the right methods?

1 Like

1 Like

Is this “undefined is not a function” with or without a flagged line and/or highlight?

(psst: you really should check to see if you can power up before you do so, saves your hero from standing there looking like a fool)

if self.isReady("powerup"):

(I believe, it should tell you if I didn’t spell it right.)

2 Likes

I just looked closer at your code, should be self.findByType not self.findByTypes

2 Likes

Its highlighted.
Also,
thanks for the tip!

1 Like

Thank you.
Its working but now I need to add the ogres too because right now I am just ignoring them. Anyways, thanks a lot!

1 Like

Need help nick i can not fine the glasses i need for backwoods brawl

What glasses do you think you need for Backwoods Brawl?

This level has no glasses restriction, but the one previous does, so to get to Backwoods Brawl, you should already own (and be wearing?) glasses that do a good enough job…

OK, so you want to do a better than just “good enough” then you need at least “Mahogany glasses”. You use “findEnemies” and write your own “findByType” code. Or if you can’t do that yourself then you need at least “Hardened Steel Glasses”. Which do “findByType” for you.

What is the error you are receiving? It help out a lot. Also the place where you are wrong might be when you define “distance” because you define it as hero.distanceTo(target) when it should be hero.distanceTo(thrower).

How much hp do you have?

What is wrong with this code?

// Stay alive for one minute.
// If you win, it gets harder (and more rewarding).
// If you lose, you must wait a day before you can resubmit.
// Remember, each submission gets a new random seed.

// Determines if Warrior should use cleave
// When?
// - If cleave is ready AND
// - If there are more than 2 enemies within cleave range
this.shouldCleave = function() {
if ( this.enemiesWithinRange(10) > 2 && this.isReady(“cleave”) ) {
return true;
} else {
return false;
}
};

// returns the number of enemies within the specified distance
this.enemiesWithinRange = function(distance) {
var enemies = this.findEnemies();
var num = 0;

for (var i = 0; i < enemies.length; i++) {
    if (this.distanceTo(enemies[i]) <= distance) {
        num++;
    }
}

return num;

};

// find the nearest thrower type unit
this.findNearestThrower = function() {
var enemies = this.findEnemies();
var throwers = []; // array of existing throwers

for (var i = 0; i < enemies.length; i++) {
    if (enemies[i].type == 'thrower') {
        throwers.push(enemies[i]);
    }
}

var this.findNearest(throwers);
};

loop {
thrower = this.findNearestThrower();

// attack throwers first
if (thrower) {
    if (this.shouldCleave()) {
        this.cleave(thrower);
    } else {
        this.attack(thrower);
    }

// attack other enemies
} else {
    enemy = this.findNearest(this.findEnemies());
    
    if (enemy) {
        if (this.shouldCleave()) {
            this.cleave(enemy);
        } else {
            this.attack(enemy);
        }
    }
}

}