Mixed unit tactics_


#1
# 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", "soldier", "soldier"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold > hero.costOf(summonTypes[type]):
        hero.summon(summonTypes[type])

def Item():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y) 

def Attack():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
        

while True:
    summonTroops()
    Item()
    Attack()



I don’t seem to be able to summon any troops? Thanks


#2

See your code and compare it with the hints and the examples:

summonTypes = ["soldier", "soldier", "soldier"]```

type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold > hero.costOf(summonTypes[type]):
    hero.summon(summonTypes[type])
You also don't command your troops.

#3

Thanks - a bit of adjusting on ‘type’ and commanding and I got there :slight_smile: