Library Tactician:HELP!


#1

I cant’t solve this puzzle!!

My code is as follows:
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)_

This is my gear

I’ve spent multiple days searching the forum and it won’t work!!


#2

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.


#3

Replaced “ and ” with ’ . No other checks or simulating done. You can replace text in codecombat with the shortcut Ctrl-F or Ctrl-H

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)

#4

IT STILL DOES NOT WORK!!!One of them always dies.


#5

Here you are increasing the index of the enemy before you check it. Therefore enemies[0] never got checked.

Also, returning bestTarget/return None should be outside of the while loop.