I need help on Library Tactician

Yea I meant that the soldiers aren’t supposed to die.

P.S : @herokolbert 's way is to make the code look better. It does the samething to what u had before

P.S.S

no the thing is that he needs to check if it exists. It also came with default code so it should work fine.

P.S.S.S : There is the typo that u didn’t fix. At the while loop at the bottom. U don’t need anything except the hero.findByType('soldier') and hero.findByType('archers') (also try to find the difference(the soldier one is right, the archer one is wrong))

answer(only if u REALLY don't know)
if u give up

hero.findByType('archer')

it does not work, but it is closer to the end. It says in line 14 theres a error, idk what it means by that. Let me know if you would like me to send my new code.

Can u send the code and picture?

you got it, one second.

# 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!
console.log()

# Soldiers spread out in a circle and defend.
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPosX = 41
    defendPosY = 40
    defendPos = {"x": defendPosX, "y": defendPosY}
    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.
def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for enemy in enemies:
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            bestTarget = enemy
            soldier = soldiers[i]
            commandSoldier(soldier, i, len(soldiers))
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None


# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    wizard = hero.findNearest(hero.findByType("librarian"))
    # If archerTarget is dead or doesn't exist, find a new one.
    if not archerTarget or archerTarget.health <= 0:
        # Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget()
    friends = hero.findFriends()
    soldiers = hero.findByType("soldier")
    # Create a variable containing your archers.
    archers = hero.findByType('archer')
    enemy = hero.findNearestEnemy()
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))
    # use commandArcher() to command your archers
    friends = hero.findFriends()
    soldiers = hero.findByType("archer")
    wizard = hero.findNearest(hero.findByType("librarian"))
    for i in enumerate(archers):
        archer = archers[i]

A pic or vid of the error/problem?

You need to do commandArcher(archer) at the end

I only see 2 small problems here. For loop at the bottom

for i in enumerate(archers):
    archer = archers[i]

U need a way to command the archers. The soldiers loop should give u the answer. Or look at @moonwatcher348 's post. (The soldiers loop doesn’t need the (soldier, i, len(soldiers)) commandSoldier(soldier) is just fine since u have soldier = soldiers[i] before)


Anyhow. the for i in enumerate(archers): for loop is a more complex for loop.
Enumerate Function
Range Function

Hello, all the thing is wrong is because you are using the same index for both soldier and archer “i”, change it to “j” and it might work. Just do something like this:

for j in range(0, len(archers)):
    archer = archer[j]
    commandArcher(archer)

Another way is to iterate the archer array with “archer” with a normal for-loop, it’s easier this way:

for archer in archers:
    commandArcher(archer)

Both ways work, it’s up to you for the way you choose

The explanation for enumerating is basically using both index and variable defined in the same for-loop, if you are going to use that, you should replace the whole code with the one below. Also, you do not need to define friends = hero.findFriends() twice.

while True:
    # If archerTarget is dead or doesn't exist, find a new one.
    if not archerTarget or archerTarget.health <= 0:
        # Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget()
    friends = hero.findFriends()
    soldiers = hero.findByType("soldier")
    for i, friend in enumerate(friends):
        if friend.type == "soldier":
            commandSoldier(friend, i, len(soldiers))
        if friend.type == "archer":
            commandArcher(friend)
1 Like

Ok, thank you. I will get back to you when Im done tinkering with this.

Ok, Ill let you know if anything happens.

That is the error, that I have with my code.

Put the for j in range(0, len(archers)) inside the whileTrue loop

Also, it is archer = archers[j], not archer = archer[j]
You are defining the archer with the whole array with the “j” element

2 Likes

And remove the 0, it’s useless in this case :slight_smile:

1 Like

I tend to use 0 for accurcy, but removing it still works

1 Like

0, doesn’t add any accuracy though… it’s just there for no reason, it’s like doing 9 + 0 instead of just 9, if anything, it decreases the accuracy because of the small chance of the computer converting it from binary not correctly

1 Like

I ended up taking line 14 out, it got the error out. Here’s the new code

# 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!
console.log()

# Soldiers spread out in a circle and defend.
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPosX = 41
    defendPosY = 40
    defendPos = {"x": defendPosX, "y": defendPosY}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)

# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for enemy in enemies:
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            bestTarget = enemy
            soldier = soldiers[i]
            commandSoldier(soldier, i, len(soldiers))
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None


# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    wizard = hero.findNearest(hero.findByType("librarian"))
    # If archerTarget is dead or doesn't exist, find a new one.
    if not archerTarget or archerTarget.health <= 0:
        # Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget()
    friends = hero.findFriends()
    soldiers = hero.findByType("soldier")
    # Create a variable containing your archers.
    archers = hero.findByType('archer')
    enemy = hero.findNearestEnemy()
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))
    # use commandArcher() to command your archers
    friends = hero.findFriends()
    soldiers = hero.findByType("archer")
    wizard = hero.findNearest(hero.findByType("librarian"))
for j in range(0, len(archers)):
    archer = archers[j]
    commandArcher(archer)
        

Well if you take line 14 out it never commands the soldier… -_-