Hi, I just completed the level Steelclaw Gap. I was able to complete it by getting help from various forum posts, but I don’t understand what the code is doing and how/why it works. Usually when I need to turn to the forums, I am able to understand what I was doing wrong. However, for this level I couldn’t understand what others were saying in their explanations. Here is my code:
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
defPoint = defendPoints[i % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, "defend", defPoint)
while True:
summonTroops()
commandTroops()
I am mainly confused about lines 9 and 18. Could someone try to explain what exactly the code is doing and why it works? Thanks.