// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {
x: 41,
y: 40
};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
var hushbaum = hero.findByType("libraian");
if (soldier.health < 50) {
hero.command(soldier, "move", hushbaum.pos);
}
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].health > mostHealth) {
mostHealth = enemies[i].health;
bestTarget = enemies[i];
}
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if (archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if (nearest) {
hero.command(archer, "attack", nearest);
}
}
while (true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if (!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
for (var i = 0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
commandArcher();
}```
On line 35 it says âerror: best Target is not definedâ
What am I doing wrong?
Delete these lines - leave the function as it was written before:
var hushbaum = hero.findByType("libraian");
if (soldier.health < 50) {
hero.command(soldier, "move", hushbaum.pos);
}
Delete one of the } after âbestTarget = enemies[i]â, and put it in after the âreturn nullâ instead. This will make those lines of code are included in the function.
At the bottom, write a for loop to go through archers, and run your commandArcher function for each of them.
Try these things, if you need more help then post your new code again.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
}
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].health > mostHealth) {
mostHealth = enemies[i].health;
bestTarget = enemies[i];
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
The error still comes. Sorry I am posting a few days later.
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {
x: 41,
y: 40
};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
var mostHealth = 0;
var bestTarget = null;
}
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].health > mostHealth) {
mostHealth = enemies[i].health;
bestTarget = enemies[i];
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if (archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if (nearest) {
hero.command(archer, "attack", nearest);
}
}
while (true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if (!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archer = hero.findByType("archer");
for (var i = 0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
for (var i = 0; i < archer.length; i++) {
var archer = archer[i];
commandArcher(archer, i, archer.length);
}
}
P.S how do you get rid of the red font?
Thanks @Deadpool198 for that change.
I tried that and the error didnât come, but I still didnât pass the level.
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {
x: 41,
y: 40
};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
var mostHealth = 0;
var bestTarget = null;
function findStrongestTarget() {
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].health > mostHealth) {
mostHealth = enemies[i].health;
bestTarget = enemies[i];
}
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if (archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if (nearest) {
hero.command(archer, "attack", nearest);
}
}
while (true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if (!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archer = hero.findByType("archer");
for (var i = 0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
for (var i = 0; i < archer.length; i++) {
archer = archer[i];
commandArcher(archer, i, archer.length);
}
}
You do want both the variables, but just with different names! Have a look at
var soldiers = hero.findByType("soldier");
and then: var soldier = soldiers[i];
For the findStrongestTarget function, you have 3 { brackets, and then
}
}
}
which will end the function. You need to keep the next 4 lines inside the function! You donât have to indent tidily with JavaScript, but it does make it easier to see chunks of code.
I tried that but It still says incomplete and I donât pass @jka2706 .
// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
var archerTarget = null;
// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
var angle = Math.PI * 2 * soldierIndex / numSoldiers;
var defendPos = {
x: 41,
y: 40
};
defendPos.x += 10 * Math.cos(angle);
defendPos.y += 10 * Math.sin(angle);
hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
var mostHealth = 0;
var bestTarget = null;
function findStrongestTarget() {
var enemies = hero.findEnemies();
// Figure out which enemy has the most health, and set bestTarget to be that enemy.
for (var i = 0; i < enemies.length; i++) {
if (enemies[i].health > mostHealth) {
mostHealth = enemies[i].health;
bestTarget = enemies[i];
}
}
// Only focus archers' fire if there is a big ogre.
if (bestTarget && bestTarget.health > 15) {
return bestTarget;
} else {
return null;
}
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
var nearest = archer.findNearestEnemy();
if (archerTarget) {
hero.command(archer, "attack", archerTarget);
} else if (nearest) {
hero.command(archer, "attack", nearest);
}
}
while (true) {
// If archerTarget is defeated or doesn't exist, find a new one.
if (!archerTarget || archerTarget.health <= 0) {
// Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget();
}
var friends = hero.findFriends();
var soldiers = hero.findByType("soldier");
// Create a variable containing your archers.
var archers = hero.findByType("archer");
for (var i = 0; i < soldiers.length; i++) {
var soldier = soldiers[i];
commandSoldier(soldier, i, soldiers.length);
}
// use commandArcher() to command your archers
for (var i = 0; i < archers.length; i++) {
var archer = archers[i];
commandArcher(archer, i, archer.length);
}
}
Youâve got this slightly confused. You want the:
var mostHealth = 0;
var bestTarget = null;
Inside the function. You donât need to change anything else, just move them inside the findStrongestTarget() function.
The reason for this is that functions canât access variables outside their code, so you have to write the variables inside the function.
Danny
In answer to your question about how does the function work, then it depends a bit on how much you know about sine, cosine and radians. If these are all new things to you, you might need to go and read up on them (search for trigonometry).
ButâŚseeing as you asked. A radian is just a different way of measuring angles - there are 2Ď radians in a circle (so one radian is about 57 degrees). Python works in radians, so the angle variable is just working out what how far round the circle each soldier will be if there are numSoldiers number of soldiers. So if there were 4 soldiers, they would have quarter of the circle each and theyâd be spaced 1.57 radians (= 90 degrees) round. If there were 12 theyâd have a twelth of the circle each, so theyâd be spaced 0.52 radians (= 30 degrees) round.
The defendPos position is the centre of the circle. For each soldier, a bit gets added on or taken away from the defendPos x and y to give that soldierâs x and y position. This means they all end up spaced around the circle. The bit that gets added on or taken away is the sine or cosine of the angle - these are formulae that describe the ratio between the length of the sides of a right angle triangle with different angles in the other corners. This picture maybe helps:
The blue line stays the same length for each soldier, but as the angles are different, the amount that gets added on or deducted from defendPos is different (ie the green and red lines are different lengths). This means the soldiers all get neatly spaced out.
I want this to be magic, but actually itâs maths .
Yay It worked!
Was the level âsteelclaw gapâ supposed to open?
To continue the main path do I do âThe spy among usâ or âuneasy truceâ?
Thanks to everybody who helped me completed this level.
Thanks for the Math explanation, but I am still in 5th grade.
This topic is closed.