Stuck: Library Tactician


#1

Hi! This is my first time posting so please forgive me if I do anything wrong :smile:
So I’ve been playing on the level Library Tactician and I’ve been stuck. I have looked at the stimulation and the only thing wrong is that my archers won’t target the strongest target.
Here is my code:

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 about the indents, I can’f find a way to indent so i would appreciate if you help me in that too. But my main problem is that I can’t find anything wrong with the code so I would be really (really really really) thankful if you pointed out the mistake. Thanks! :smile:


#2

Click in the Edit button (the pencil icon below your post), select the code and press Ctrl+K (or click in the </> button). :smile_cat:


#3

Thanks <3 I really appreciate it! :smile:


#4

You have a stray

.

in there.

I tried your code with and without it and it works if you get rid of that.


#5

Also, are you actually calling findStrongestTarget in the commandArcher method, and calling the commandArcher method on each archer? I think there’s some parts at the bottom where you have to set that up, too.


#6

Thanks Nick, I changed my code a bit and now its working fine! :smile:


#7

what is a stray because it is bothering me on the level


#8

Icy magnus do you mind helping me.please because im stuck!thank you


#9

Hello, Dinesh, and welcome.

Phrases like “I need help!” or “This code doesn’t work!” do not help us do anything about your problem. Give us more information. What happens when you run your code? Is there an error? If so, what does it say? Post your code and a description of what happened. Don’t forget to read the FAQ before you post. Format your code properly!


#10

thank you! i finished the level just now but i still have no idea what a stray is in general.
p.s it would be nice if you could call me sanjjith because that is my name


#11

i just changed my username


#12

hey chronistgilver, can you you help me if possible , on the level steelclaw gap.
thanks


#13

Create a new topic for it. This is not the place to discuss Steelclaw Gap.


#14

I am having trouble after the board is about 3/4 of the way complete. All the sudden my archers just quit targeting anyone after targeting them flawlessly up to that point…

# 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.
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);

# 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 = self.findEnemies()
    enemyIndex = 0
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            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

def penderAttack():
    enemy = self.findNearest(self.findByType("ogre"))
    if enemy:
        if self.canCast("drain-life", enemy):
            self.cast("drain-life", enemy)
        elif self.canCast("shockwave", enemy):
            self.cast("shockwave", enemy)
        elif self.canCast("slow", enemy):
            self.cast("slow", enemy)

def summonFriend():
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")

def weakestFriend():
    soldiers = self.findByType("soldier")
    weakestFriend = None
    leastHealth = 100
    for soldier in soldiers:
        if soldier.health < leastHealth:
            weakestFriend = soldier
            soldier += 1
     if weakestFriend:
        if self.canCast("flame-armor", weakestFriend):
            self.cast("flame-armor", weakestFriend)

# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archers):
    enemies = archer.findEnemies()
    enemy = archer.findNearest(enemies)
    if archerTarget:
        self.command(archer, "attack", archerTarget)
    elif enemy:
        self.command(archer, "attack", enemy)

archerTarget = None

loop:
    # 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 = self.findFriends()
    soldiers = self.findByType("soldier")
    archers = self.findByType("archer")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    for i, archer in enumerate(archers):
        commandArcher(archer, i, len(archers));
    commandArcher() 
    weakestFriend()
    penderAttack()
    summonFriend()


#15

Well, commenting out the pender attack function seemed to fix the issue with the archers randomly stopping their attack…