Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Mixed Unit Tactics NEED HELP

Here is my code dont know why its wrong

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    type = summonTypes[len(hero.built) % len(summonTypes)]
    coin = hero.findNearest(hero.findItems())
    if hero.gold >= hero.costOf(type):
        hero.summon(type)
        hero.findNearestItem()
        hero.move(coin.pos)

def commandTroops():
    friends = hero.findFriends()
    for friend in friends:
        if friend != "palisade":
            enemies=hero.findEnemies()
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)

while True:
    summonTroops()
    commandTroops()

This one may take a bit of working through. To start, let’s work on your summonTroops function. Is there a reason why you are combining summoning with collecting gold? It might be better to have a separate function dedicated to just collection. You can call this new function from your while loop, providing the conditions are right…(hint implied herein :wink:)

1 Like

Don’t forget to add the .type when dealing types.
Try using friend.type != "palisade"

1 Like

This is my code now, still my hero moves a bit and then he stops

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    type = summonTypes[len(hero.built) % len(summonTypes)]
    coin = hero.findNearest(hero.findItems())
    if hero.gold >= hero.costOf(type):
        hero.summon(type)
        hero.findNearestItem()
        hero.move(coin.pos)

def commandTroops():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "palisade":
            enemies=hero.findEnemies()
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)

while True:
    summonTroops()
    commandTroops()

This is my code now, the hero dosent summon the troops but it collects the gold

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)

while True:
    coin = hero.findNearestItem()
    hero.findNearestItem()
    hero.move(coin.pos)


def commandTroops():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "palisade":
            enemies=hero.findEnemies()
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)

while True:
    summonTroops()
    commandTroops()

Your first while True loop is going to run forever…the second one will never start. Instead, rename the first one to ‘def findItems():’ (or something like that), making it a function definition.

Then, you can call it from your actual while true loop, just like you are doing with summonTroops and commandTroops.

1 Like

This is my code now

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer", "soldier", "archer"]
enemies = hero.findEnemies()
def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)

def finditems(): 
    coin = hero.findNearestItem()
    hero.findNearestItem()
    hero.move(coin.pos)


def commandTroops():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "palisade":
            enemies=hero.findEnemies()
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)

while True:
    summonTroops()
    commandTroops(

the code dosent have any errors but my hero dosent move

Just solved it! (20)