Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Please help me with modulo

Please help. I have no clue what modulo is nor how to use it nor how to wrap over something with it. I just want to know because i’m getting to that point in mountain where i start using it a lot but i need to know how to use it in order to be able to actually use it.

Modulo is used to find out what remains after you divide two integer numbers. For example: 4÷2 = 0, and 7÷3 = 1. You can use it to, for example, summon an archer and a soldier in this order every time. Do you want any more explication on modulo or how it is used?

Andrei

1 Like

How do you use it to summon from a list? And how do you use it to so-called “wrap around” an index?

For example, you want to summon an army in an orderly pattern, let’s say ["soldier", "archer", "soldier", "griffin-rider"]. You can use the module (%) operator. Here’s how.
hero.built returns how many units you have summoned. Because you will be summoning units in a pattern, so the 1st, 3rd, 5th, 7th etc. unit you will summon is a "soldier" type. Using the % operator makes it so that you don’t have to do this:
if hero.built == 1 or hero.built == 3 or hero.built == 5 or hero.built == 7 or hero.built == 9 or hero.built == 11 ...
What if you need to cycle through the pattern 25 times? It’s gonna be way too long. So, using the power of maths and reminders, you’ll only need to check if the reminder of how many units the hero summon (hero.built) divided by how long the cycle is, in this case, 4. If you use the divide operator (÷), you’ll get the quotient, but if you use the modulo operator (%), you’ll get the reminder. In the array, the 1st and 3rd element is "soldier", the 2nd element is "archer", and the 4th element is "griffin-rider". So you’ll only need to check if hero.built % [length of the array, in this case, 4] is equal to 0, 1, 2, and 3 (not 1, 2, 3, 4, since you wouldn’t have a reminder of 4 when you’re dividing a number by 4, so just subtract one from it and it’ll make code sense). If it returns 0 or 2, summon a "soldier". If it returns a 1, summon an "archer", and if it’s a 3, summon a "griffin-rider".
This is probably very in-depth, and you’ll probably get it after reading this.
~ Orb

3 Likes

Thanks! That helps a lot, but can you explain exactly what this means?
image

2 Likes

The Modulo Operator also known as % symbol, it basically returns a remainder of the dividing the left hand operand by rand hand operand. It’s used to get the remainder of a division problem. For Example 7 / 2 = 3 R 1, The 1 is the remainder.

Hope it helps :slightly_smiling_face:
Charlie

1 Like

yeah, i kinda got that. but how do I apply that to the above screenshot?

I think it’s something like summonTypes % len(hero.bult) and remind me what the level is again?

1 Like

The level is Mixed Unit Tactics in mountain.

This is my code so far. Why won’t my hero move at all?

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes[summonTypes % len(hero.built)]
    if hero.gold > hero.costOf(type):
        hero.summon(type)
    else:
        hero.move(hero.findNearestItem().pos)

Did you follow this?
Lydia

i did. did i do something wrong?

You need to switch len(hero.built) to the front. And the back should be len(summonTypes)
Lydia
Edit: Sorry, the back should be len(summonTypes)

The back should be len(summonTypes) not len(hero.built)
~ Orb

What do you mean, front and back? also, I feel like this is wrong.

if hero.gold > hero.costOf(type):
        hero.summon(type)

Okay, so this is what I have so far, and it’s telling me that soldier is not defined (as well as archer). I know what the problem is, I just don’t know how to fix it.

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold > hero.costOf(type):
        hero.summon(type)
    else:
        hero.move(hero.findNearestItem().pos)
while True:
    summonTroops()
    friend = hero.findByType(soldier, archer)
    enemy = hero.findNearestEnemy()
    for friend in friends:
        hero.command(friend, "attack", enemy)

Needs quotation marks.

Ok. So i changed it to this:

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier", "archer", "soldier", "archer"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    #type = summonTypes[???]
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold > hero.costOf(type):
        hero.summon(type)
    else:
        hero.move(hero.findNearestItem().pos)
while True:
    summonTroops()
    friends = hero.findByType("soldier" or "archer")
    enemy = hero.findNearestEnemy()
    for friend in friends:
        hero.command(friend, "attack", enemy)

But my archers won’t move.

If you don’t have any friends other than the soldiers and the archers, it’s easier to use findFriends, but if do have other friends, try using:

for friend in all of your friends:
    if your friend is not a soldier or an archer:
        skip this friend
    else:
        command your friend