Librarian tactician I need help


#1

hi guys I need to talk.


#2

plz
I need to talk because i need help


#3

I am stuck on librarian tactician
here is my 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!

# 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()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    self.buildXY("archer", self.pos.x, self.pos.y)
    self.buildXY("archer", self.pos.x, self.pos.y)
    # 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):
    if archerTarget:
        self.command(archer, "attack", archerTarget)
    elif nearest:
        self.command(archer, "attack", nearest)

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")
    
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));

    # use commandArcher() to command your archers

i have no idea what to do but i think if i build 2 archers it helps


#4

The example code in the level:

for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));

    # use commandArcher() to command your archers

shows you how to command your soldiers.

After the comments try to write the same type of code to command your archers.

Soldiers list:                 soldiers
Soldiers command function:     commandSoldier(soldier,i, len(soldiers))
Archers list:                  archers
Archers command function:     commandArcher(archer)

Do not just copy the code, try to understand why it works like this. Google and find a python tutorial. Google is your friend


#5

Something is wrong when I run my code.

Here is my 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!

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()
# Figure out which enemy has the most health, and set bestTarget to be that 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 =
if archerTarget:
self.command(archer, “attack”, archerTarget)
elif nearest:
self.command(archer, “attack”, nearest)

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")

for i, soldier in enumerate(soldiers):
    commandSoldier(soldier, i, len(soldiers));

commandArcher()

Where it says to define nearest I used this:

nearest = archer.findNearestEnemy()

But that did not work. How do I correct that error?


#6

Look at your code where you command the archers.

Now look at the code where you call the function to command the archers.

Look at the lines above commandArcher(), notice anything … different?


#7

I don’t get what you mean by “notice anything … different?”


#8

This your commandArcher function definition:

def commandArcher(archer):

It says that it expects a value, when it is called. More specifically it expects one archer to command.

Your code need to go through a list of archers, and for each archer in the list, call commandArcher with the value of the archer to command.
As Serg mentioned, just about in the code, it show exactly how to do this with the commandSoldiers:

for i, soldier in enumerate(soldiers):
    commandSoldier(soldier, i, len(soldiers));

commandArcher()

The difference is that the code for commandSoldiers goes through the soldiers.

Please go back and revisit the CodeCombat earlier levels again. Or google and search how to use array in python.