# This level introduces the % operator, also known as the modulo operator.
# a % b returns the remainder of a divided by b
# This can be used to wrap around to the beginning of an array when an index might be greater than the length
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]
summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# You start with 360 gold to build a mixture of soldiers and archers.
# self.built is an array of the troops you have built, ever.
# Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
# Use % to wrap around defendPoints based on friendIndex
defendPoints = defendPoints[friendIndex % len(defendPoints)
# Command your minion to defend the defendPoint
hero.command(friend, "defend", defendPoints)
while True:
summonTroops()
commandTroops()
This is the first level where they mention %'s. The comments in the code don’t seem to be helping me very much, I’ve also checked for other threads and those haven’t seem to help me either. Could somebody explain both the error I’m getting here :
and explain how to use the %'s? I’m very lost here.
to explain the “%” sign gives you the remainder of a division problem, not the answer. And I can see that you are missing a bracket.
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
# Use % to wrap around defendPoints based on friendIndex
defendPoints = defendPoints[friendIndex % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, "defend", defendPoints)
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]
summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# You start with 360 gold to build a mixture of soldiers and archers.
# self.built is an array of the troops you have built, ever.
# Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
friendIndex = 0
# Use % to wrap around defendPoints based on friendIndex
defendPoints = defendPoints[friendIndex % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, "defend", defendPoints)
while True:
summonTroops()
commandTroops()
Fixed my code around a bit, no longer getting errors. Sadly, all of my units all go into the top left corner and just defend that instead of spreading out to each defend point.
EDIT : Changed absolutely nothing, just pushed it off for a little bit and then when I tried it again, it started to work all of a sudden for no reason
the problem is you redefine the value defendPoints in your loop
You also do not increment friendIndex or using the i value in your for loop.
Use either While loop or For loop not both at same time
for i in range(len(friends)):
friend = friends[i] friendIndex = 0
# Use % to wrap around defendPoints based on friendIndex defendPoints = defendPoints[friendIndex % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, “defend”, defendPoints)
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]
summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# You start with 360 gold to build a mixture of soldiers and archers.
# self.built is an array of the troops you have built, ever.
# Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]
friend = friends[i]
friendIndex = 0
# Use % to wrap around defendPoints based on friendIndex
defendPoints = defendPoints[friendIndex % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, "defend", defendPoints)
while True:
summonTroops()
commandTroops()
I used the same code why are all troops going to the top left