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

can you put your successful code down be cause i keep having problems with mine

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

well also in this discourse we dont post solutions because if you just copy someone elses code then you dont learn

Yes, as @cheddarcheese said, we don’t give solutions because the main goal of this forum is to help, not give answers.
If you tried, and post your code here correctly as it says here, we’ll be more than willing to help you!
Lydia

kay so here is my code what is 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)]
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:
finditems()
summonTroops()
commandTroops()

Can you post your code correctly as it says here ?
Thanks!
Lydia