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?
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.
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).
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:
Sometimes your hero suddenly stands still
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)
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
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).
// 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);
}
}
}