Modulo Operator Confusion

Hello, I am very confused with the Modulo Operator. Like how do you wrap things with it and how to use it in general. I would really appreciate it if someone could help me with this.

Welcome to the forum @ChaoticReality ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Don’t forget to read the guidelines if you haven’t yet. Have a great time!

So basically the modulo operator does this:
You have 2 numbers, when you divide one of them on the other, the remainder (that is the number left before you start making decimals) will be the result of the modulo.

So imagine you want to divide 13 over 3
there are 4 threes in 13 and an extra one (4*3 = 12, 12 + 1 = 13)
So 13 % 3 = 1 (which is the remainder)

Hope this helps :slight_smile: if you need any more explanation, then feel free to @ me

@Aya This does help but how do you wrap something and how do you translate this into let’s say, summoning troops?

I recall this is based on a level, if you can post your code or at least the level, that would be appreciated

The level is Steelclaw Gap and please don’t post the solution.

For some reason it spawns the first 4 units then it spawns 5 for some reason.

# 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","archer","soldier","archer","soldier","archer","soldier","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
        i = len(hero.built) %  5
        o = len(defendPoints) % 4
        if i == 4:
            # Command your minion to defend the defendPoint
            hero.command(friend, "move",{"x": 35, "y": 63} )
            
            enemy = hero.findNearestEnemy()
            hero.say(i)
            if enemy:
                hero.command(friend, "attack", enemy)
        

while True:
    summonTroops()
    commandTroops()
    hero.moveXY(48, 45)

No worries, I won’t, it is anyways prohibited :slight_smile:

Did you read the hints, the second page? It should help.

yes but I still get it. I did beat the level but it was not a true success I will post the 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","archer","soldier","archer","soldier","archer","soldier","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
        i = len(hero.built) %  5
        o = len(defendPoints) % p
        p = 4
        if i == 4:
            # Command your minion to defend the defendPoint
            if o == 0:
                hero.command(friend, "move",{"x": 49, "y": 63} )
                p -= 1
            if o == 1:
                hero.command(friend, "move",{"x": 49, "y": 26})

def Attack():
    friends = hero.findFriends()
    for i in range(len(friends)):
        friend = friends[i]
        # Use % to wrap around defendPoints based on friendIndex
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)

while True:
    summonTroops()
    commandTroops()
    Attack()
    hero.moveXY(48, 45)
1 Like

So in the for loop, you know that the value of i is always changing. So basically, what they want you to do is to find the remainder of the division of i over the length of the array of the summon types, the one below:

So if you take this from the hint, type = summonTypes[len(hero.built) % len(summonTypes)] it technically says, the type you will summon is the index from the summonTypes list holding the value of the remainder resulting from the division of the value of i over the length of summonTypes, does that make sense? I know it is very confusing.

As for the defendPoints maybe @Deadpool198 can help (please)