[SOLVED] Steel claw gap


#1
# 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()


#2

You are redefining what is defendPoints in your loop, So the result is all your soldier all go on the same defendpoint.

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)]  # <--- problem is here 
        # Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendPoints)

#3

so what should I put


#4

I’ll try to make you understand whats going on you’ll figure out what to do after

let’s say your amounts of defendpoints is 4 and are using those values

defendPoints = defendPoints[friendIndex % len(defendPoints)]

First thing friendIndex is not defined
the value that increment in a for loop is the value i you used.

so right now your code say: defendPoints = defendPoints[Null % len(defendPoints)]

But there is another problem let’s say you have the correct value i in that code

the first loop i = 0

The computer will do this:

defendPoints = defendPoints[i % len(defendPoints)] (the len value is 4)

if we are in the first loop, i = 0

defendPoints = defendPoints[0 % len(defendPoints)] 0 % 4 = 0
defendPoints = defendPoints[0]

here what happen when you do that:
defendPoints = {“x”: 35, “y”: 63}
defendPoints is redefined to {“x”: 35, “y” 63} which is defendPoints[0]

(the len value of defendPoints is now 1)

lets go for the second loop now, i = 1
defendPoints = defendPoints[i % len(defendPoints)] 1 % 1 is still 0
defendPoints = defendPoints[0]
defendPoints = {“x”: 35, “y” 63}

so basically you are losing your data in the variable defendPoints
and friendIndex is not defined

Im sorry if I am not giving you the answer straight ahead I believe if you manage to understand you’ll have a better knowledge


#5

thanks
(2000000000000000000 chars)