I need help on Library Tactician

# 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
    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.
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
    # 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:
    hero.findFriends()
    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("archers")
    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):
        archerTarget = FindStrongestTarget
        archer = archers[i]
        commandArcher(archer, i, len(archers))
type or paste code here

Switch this for a for i in range for loop.


You donā€™t need this.(After the for i in range(archers):)


You need the for i in range type of for loop. Or else, it return a different enemy for every archer.(I think)


Right after the for loop, you need something that gets one of the enemies from the enemies.

Hint

You already have some in your code. Look at the for loop for the soldiers and what comes after it.


You can del the

at the top of the while loop.
Your defining wizard twice. But I donā€™t think your gonna be using that(might as well del that).


Like i said before,

Your code:

at that line, commandArcher(archer) is just fine.(Since you have archer = archers[i] it already returns one out of all of the archers(And then signals the others to do the same. Pretty much like a repeat for the others))


And last but not least. At this line:

Your code:

You have a typo.

archers = hero.findByType('archers')

look at the find soldiers one

Do you spot any difference?


Stuff i have to ask you @Lukyme:

P.S : whats the console.log() thing at the top?

P.S.S : If you have any more problems with your code. Pls tell us

It says that I need to have a object and then it is incomplete.

    hero.command(soldier, "defend", defendPos)

This is giving me a arguement error, and it wont let me fix it.

I also find that enumerate is a better method than range.

Can you send the current code?

Im pretty sure thereā€™s a difference in those two. I tried the enumerate method and it giveā€™s an error on the other hand. I switched it back to range method and it worked fine.

# 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
    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.
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('archers')
    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]
        commandArcher(archer, i, len(archers))

Iā€™m only around during the weekends, Incase you donā€™t hear from me.

Ok, umm I see that some of the things I told you to do are not done. OFC Iā€™m lazy :eyes:(I should really change that personality of mine huh :rofl:?) and donā€™t want to point them out. @Lukyme you really need me to point them out I can. Just ask :smile:.

P.S : Can anyone explain the difference between enumerate and range?

I would like you to point them out for me if you can, I would greatly appreciate it if you do.
P.S Not lazy, I just donā€™t know. I may be smart, but obviously not as smart as you :rofl:

Umm, if you followed all my instructions on this post it should be fine :thinking:. Check that post and if you have any other problems post the updated code (Your code doesnā€™t contain changes :eyes:)

Iā€™m not that smart. If you code everyday and practice a lot you should be able to surpass me in coding :smile: .

Put the archerTarget definition into the commandArcher() function

The only problem I have is with Line 12.

    hero.command(soldier, "defend", defendPos)

The soldier is dying slower, what I mean is that It dies after the usual point of when it dies.

change {"x": 41, "y": 40} into Vector(41, 40)

I donā€™t think he should use vectors. This level is in mountains.

2 Likes

ok then change it to this:

defendPosX = 41
defendPosY = 40

then add from there and do:

defendPos = {"x": defendPosX, "y": defendPosY}

wait @Lukyme is there an actual code error or does it work

Umm, then there is a slight problem with the archers :eyes: they are not supposed to die i think. Iā€™ll review the code in some time. Just try to fix the defined thing with archers. I think I mentioned somewhere above.

I tried that and it didnā€™t work, instead what happened was I made it longer, but I ended up dying either way. I donā€™t know why It is happening. It also gave me a code error on line 14 when I put what you told me to put.

It isnā€™t the archers who are dying, its the soldiers who are dying. I donā€™t know why but I tried @herokolbert technique and it didnā€™t seem to work.