Library Tactician [Passed in Python]


#1

Hi,all
Through 2 days of time, constant adjustment and optimization, and finally through the clearance, the following code for your reference!

===========================================================
def commandSoldier(soldier, soldierIndex, numSoldiers):

angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 8 * Math.cos(angle)
defendPos.y += 8 * Math.sin(angle)
hero.command(soldier, "defend", defendPos);
if soldier.health <=soldier.maxHealth/2:
    defendPos = {"x": 38, "y": 39}
    hero.command(soldier, "move", defendPos)
    hero.command(soldier, "defend", defendPos)

def findStrongestTarget():
hero.buildXY(“archer”, hero .pos.x-2, hero .pos.y)
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()

# # i = 0
# # i < len(enemies)

# # enemy = enemies[i]
for enemy in enemies:
    if enemy.health > mostHealth:
        
        mostHealth = enemy.health
        # bestTarget = enemies[i]
        bestTarget = enemy
    # hero.say(bestTarget)
# i = 0
# i < len(enemies)
# i += 1
# enemy = enemies[i]
# for enemy in enemies:
#     if enemy.health > mostHealth:
#         bestTarget = enemy
#         mostHealth = enemy.health
#         bestTarget = enemies[i]
#     hero.say(bestTarget)
if bestTarget and bestTarget.health >0:
    return bestTarget
    
else:
    return None

def commandArcher(archer):
nearest = archer.findNearestEnemy()

if archerTarget:
    hero.say(archerTarget)
    hero.command(archer, "attack", archerTarget)
elif nearest:
    hero.command(archer, "attack", nearest)
if archer.health < archer.maxHealth/3:
    defendPos = {"x": 40, "y": 40}
    hero.command(archer, "move", defendPos)
    hero.command(archer, "defend", defendPos)

def commandArcher(archer):

nearest = archer.findNearestEnemy()

archers = archer.findByType(“archer”)

# hero.say(“message”)

for archer in archers:

if archerTarget:

hero.command(archer, “attack”, archerTarget)

elif nearest:

hero.command(archer, “attack”, nearest)

archerTarget = None

loop:

if not archerTarget or archerTarget.health <= 0:
    archerTarget = findStrongestTarget()


friends = hero.findFriends()
soldiers = hero.findByType("soldier")
archers = hero.findByType("archer")
for i, soldier in enumerate(soldiers):
    commandSoldier(soldier, i, len(soldiers));

for j, archer in enumerate(archers):
    commandSoldier(archer,j,len(archers))

commandArcher(archer)
# hero.buildXY("decoy", hero .pos.x-1, hero .pos.y+2)
# hero.buildXY("archer", hero .pos.x-2, hero .pos.y)
# hero.buildXY("decoy", hero .pos.x-1, hero .pos.y+2)
# hero.buildXY("decoy", hero .pos.x-1, hero .pos.y-2)

#2

Please avoid posting solutions, that just takes away the point of the game.

Also, that is not how you are supposed to solve this level. You are supposed to maintain the formation otherwise more enemies will spawn. See the related threads (e.g. this one) if you need further help.