Mixed Unit Tactics

its saying try self.findFriends() and line 10friend is not defined i dont know why

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
loop:
    friends=self.findFriends()
    if friend != 'palisade':
        enemies=self.findEnemies()
        for enemy in enemies:
            self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()
1 Like

my hero doesnt even summon troops or collect coins

At that moment, the variable friend doesnā€™t exist. Look at the line above it.

this is my new code my it doesnt say anything is wrong but my hero doesnt even summon troops

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
loop:
    friends=self.findFriends()
    for friend in friends != 'palisade':
        enemies=self.findEnemies()
        for enemy in enemies:
            self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()

if i delete the s on friends it says try self.findFriends()

You have 2 ā€˜mainā€™ loops and one empty function.

def commandTroops():

Has no code inside of it. (Check your indentions.)

i changed it to this and it says hero placeholder needs something to command
def summonTroops():
type=summonTypes[len(self.built) % len(summonTypes)]
if self.gold >= self.costOf(ā€˜archerā€™):
self.summon(ā€˜archerā€™)
elif self.gold >= self.costOf(ā€˜soldierā€™):
self.summon(ā€˜soldierā€™)
def commandTroops():
loop:
friends=self.findFriends()
for friend in friends != ā€˜palisadeā€™:
enemies=self.findEnemies()
for enemy in enemies:
self.command(friend, ā€œattackā€, enemy)
def collectCoins():
coin=self.findNearest(self.findItems())
self.move(coin.pos)
loop:
summonTroops()
commandTroops()
collectCoins()

sorry

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
loop:
    friends=self.findFriends()
    for friend in friends != 'palisade':
    enemies=self.findEnemies()
    for enemy in enemies:
     self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()

When you get that error, do you have any friends on the field?

Your indentations are still wrong.

no i dont and the error is gone but if i change it to this is says summontypes not defind

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
loop:
    friends=self.findFriends()
    for friend in friends != 'palisade':
    enemies=self.findEnemies()
    for enemy in enemies:
     self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()

i dont how how it should be indented

Itā€™s fine to go back and do previous levels to review how things should be indented, then.

im not sure what my code should be

im going to try using my code from steelclaw gap

sorry clicked something there by accident i was going to try my code from steelclaw gap but found it wont help

is this indented correctly but my hero still doesnt summon troops or go get coins

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
loop:
    friends=self.findFriends()
    for friend in friends != 'palisade':
        enemies=self.findEnemies()
    for enemy in enemies:
        self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()

i feel like there might be a bug in summoning troops and collecting coins cause my hero does nothing

i give up i cant figure it out

You have a loop in the def commandTroops() function. Delete it.

Delete also the second for:

for enemy in enemies:

Each soldier is supposed to attack an enemy. You cannot command a soldier to attack all enemies at one.

You also what search for the nearest enemy for a soldier:

enemy=soldier.findNearestEnemy()

Then command each soldier to attack his closest enemy.

I think there are other issues also but, but try to fix at least these

This is my new code on line 2 it says reference error summontypes is not defined

def summonTroops():
    type=summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf('archer'):
        self.summon('archer')
    elif self.gold >= self.costOf('soldier'):
        self.summon('soldier')
def commandTroops():
    friends=self.findFriends()
    for friend in friends != 'palisade':
        enemies=self.findEnemies()
        enemy=soldier.findNearestEnemy()
        self.command(friend, "attack", enemy)
def collectCoins():
    coin=self.findNearest(self.findItems())
    self.move(coin.pos)
loop:
    summonTroops()
    commandTroops()
    collectCoins()