[Solved]Steelclaw Gap [Very lost, help wanted]


#1

This is my current code :

# 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.


#2

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 was missing a bracket


#3

Thanks. Now getting 2 new errors after fixing that. First error is :


The second error is :


#4

This is my current 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]
        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 have no clue why this isn’t working.


#5


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.


#6

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 :confused:


#7

weird must be a bug or something because you did define your index. Well congrats on finishing the level.


#9

Same problem happened to me, can you help me? Why are my soldiers going to the left hand side of the map?


#10

Please post your code using the </> button so we can assist you. Thanks.


#11

I got it! Thanks for your help!


#12

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)