[SOLVED] Mixed Unit Tactics - Python

I rewrote my code, and it is the same problem.

# 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"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    soldier = summonTypes["soldier"]
    archer = summonTypes["archer"]
    len(hero.built) % len(summonTypes)

def collectCoins():
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)

def commandTroops():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", enemy)
        hero.command(soldier, "attack", enemy)
    else:
        hero.command(soldier, "defend", hero.pos)
        hero.command(archer, "defend", hero.pos)

while True:
    summonTroops()
    collectCoins()
    commandTroops()
1 Like

Ok, I’m afraid to say your code still has the same problems it had before.
Let me explain this in more detail.
Firstly the summonTroops() function:

You need to remember, this function is for summoning only. Nothing else needs to happen here (like defining soldier and archer).
You had it right when you put this in:

type = summonTypes[]

Now you need to combine that line with this one:

len(hero.built) % len(summonTypes)

You need to put that line in the [] of the first line, like it says here:

Where the question marks are.
Once you’ve defined “type”, you need to summon it. (use it like “soldier” or “archer”, but without quotes because the value the type variable is carrying is a string already.

if hero.gold >= ......
   hero.summon.....

Now onto commandTroops():
Start this function from scratch.
As I recommended before, why don’t you use a for loop? (it really won’t work without one.)
You should probably look at previous mountain levels to explain more about these topics, because you seem a little confused about it.

# you could do something like this:
1) make a friends array
2) make a for loop to loop through that array
3) because you seem to want your archers and soldiers to do the same
thing you could just command the friend to do whatever you want them both to do.

I hope this helps
Danny

2 Likes

Thank you, thank you, thank you! I did a

for friend in friends:

loop and it worked!

3 Likes

i now need help i am stuck and this is what i have

for friend in friends:
        # 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"]
    
    def summonTroops():
        # Use % to wrap around the summonTypes array based on len(hero.built)
        #type = summonTypes[???]
        soldier = summonTypes["soldier"]
        archer = summonTypes["archer"]
        len(hero.built) % len(summonTypes)
    
    def collectCoins():
        coin = hero.findNearestItem()
        if coin:
            hero.move(coin.pos)
    
    def commandTroops():
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)
        else:
            hero.command(soldier, "defend", hero.pos)
            hero.command(archer, "defend", hero.pos)
    while True:
        summonTroops()
        collectCoins()
        commandTroops()

1 Like

@dedreous

hero.findFriends()
for friend in friends:
       # 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"]
   
   def summonTroops():
       # Use % to wrap around the summonTypes array based on len(hero.built)
       #type = summonTypes[???]
       soldier = summonTypes["soldier"]
       archer = summonTypes["archer"]
       len(hero.built) % len(summonTypes)
   
   def collectCoins():
       coin = hero.findNearestItem()
       if coin:
           hero.move(coin.pos)
   
   def commandTroops():
       enemy = hero.findNearestEnemy()
       if enemy:
           hero.command(soldier, "attack", enemy)
           hero.command(soldier, "attack", enemy)
       else:
           hero.command(soldier, "defend", hero.pos)
           hero.command(archer, "defend", hero.pos)
   while True:
       summonTroops()
       collectCoins()
       commandTroops()


1 Like
friends = hero.findFriends()
for friend in friends:
        # 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"]
    
    def summonTroops():
        # Use % to wrap around the summonTypes array based on len(hero.built)
        #type = summonTypes[???]
        soldier = summonTypes["soldier"]
        archer = summonTypes["archer"]
        len(hero.built) % len(summonTypes)
    
    def collectCoins():
        coin = hero.findNearestItem()
        if coin:
            hero.move(coin.pos)
    
    def commandTroops():
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.summon(soldier, "attack", enemy)
            hero.summon(soldier, "attack", enemy)
        else:
            hero.summon(soldier, "defend", hero.pos)
            hero.summon(archer, "defend", hero.pos)
    while True:
        summonTroops()
        collectCoins()
        commandTroops()

here is the error i am getting
image

1 Like

Hi @ZAX155,
I think you’re code needs a bit of… restructuring.
Let’s look at this on a large scale, because you’re problem is on a large scale (the error message is the least of your worries I’m afraid)

The overall structure of your code at the moment is:

for friend in friends:
    summonTypes
    def summonTroops():
    def collectCoins():
    def commandTroops():
    while True:
        # call functions

Why are you defining the functions and running the while True loop inside a for loop? You don’t need to run all that code for each friend, you only want to run the command code for each friend.
What I’m trying to say is: when you command your friends- use a for loop! Every time, it’s simply the best option. (you will have to put it in the commandTroops function)

Also please could you limit how many posts you make, you could edit the original post to change your code rather than making multiple long posts.
Thanks, I hope this helps
Danny

1 Like