Please in future use the button to the right of the " button on the textbox taskbar and paste your code, directly from the game, into the gap which says type or paste your code here, for now, to save you time, I’ll do it for you.
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {“x”: 41, “y”: 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
self.command(soldier, “defend”, defendPos);
def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
enemyIndex = 0
while len(enemies) > enemyIndex:
enemy = enemies[enemyIndex]
if enemy:
enemyIndex += 1
if enemy.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
self.command(archer, “attack”, archerTarget)
elif nearest:
self.command(archer, “attack”, nearest)
archerTarget = None
loop:
if self.gold > self.costOf(“soldier”):
self.summon(“soldier”)
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType(“soldier”)
archers = self.findByType(“archers”)
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
Sorry I don’t have time to do all of it i’m sure someone else will help but all the brackets around strings should be " "s not whatever you’re using, sorry I can’t help you know. @Chaboi_3000 or @MunkeyShynes will know.
Hi there, my code has a problem, because for some reason, one soldier always dies, then GAME OVER.
Help please?
// 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!
// 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++) {
var enemy = enemies[i];
if (enemy.health > mostHealth) {
mostHealth = enemy.health;
bestTarget = enemy.id;
}
}
// 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);
}
}
var archerTarget = null;
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 j = 0; j < archers.length; j++) {
var archer = archers[j];
commandArcher(archer);
}
}
@GamerKnight “Also, I’m still having problems with Library Tactician. Now it is saying that I have slow code.”
If you still have problems post your current code
if bestTarget and bestTarget.health > 15: # must be outside
return bestTarget # the while loop
else: # this is really not needed -
return None # you put bestTarget = None at the beginning
Make the adjustment that @xython pointed out, along with my first adjustment. Then just remove the extra function you added thor(target). Your hero can’t help from the hill and it is causing an error in your code.