I am not receiving any errors in my code, but for some reason the soldiers are just ignoring the witches and attacking the nearest enemy instead. Here’s my code:
hero.commandArcher = function(archer) {
// Soldiers should attack enemies.
var enemy = archer.findNearestEnemy();
if (enemy) {
if (archer.distanceTo(enemy) < 10) {
hero.command(archer, "attack", enemy);
}
}
};
hero.commandSoldier = function(soldier) {
// Soldiers should attack enemies.
var enemies = hero.findEnemies();
var ogre = hero.findByType("ogre");
var thrower = hero.findByType("thrower");
var witch = hero.findByType("witch");
var nearestEnemy = hero.findNearest(witch);
if (enemies) {
if (witch) {
hero.command(soldier, "attack", witch);
}
}
};
while(true) {
var friends = hero.findFriends();
var enemies = hero.findEnemies();
var witch = hero.findByType("witch");
for(var i=0; i < friends.length; i++) {
if (friends.type == "soldier") {
// Use your chooseTarget function to decide what to attack.
hero.commandSoldier(friend);
}
if (friends.type == "archer") {
hero.commandArcher(friend);
}
}
}