Steelclaw a bit confused in level Steelclaw Gap

The example provided in level Steelclaw Gap uses one thing like a soldier or archer to work with the modulo operator, but the task to complete involves two things: x and y coordinates. Maybe I’m overthinking this, but I’m thrown for a loop (no pun intended).

If I understand this correctly, I have to work with 2 arrays in tandem, but they are of different lengths. To keep them in sync as one wraps to the beginning I use the % operator. The finished example has one value for each array–length:

var type = summonTypes[this.built.length % summonTypes.length];

However, the code I need to finish at bottom has to include coordinates. This is my broken logic:

// 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


var defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}];

var summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"];

// You start with 360 gold to build a mixture of soldiers and archers.
// this.built is an array of the troops you have built, alive or dead
// Here we use "this.built.length % summonTypes.length" to wrap around the summonTypes array
this.summonTroops = function() {
    var type = summonTypes[this.built.length % summonTypes.length];
    if(this.gold >= this.costOf(type)) {
        this.summon(type);
    }
};


this.commandTroops = function() {
    var friends = this.findFriends();
    for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
// The rabbit hole starts here
        // Use % to wrap around defendPoints based on friendIndex
        var point = defendPoints[friendIndex % defendPoints.length];
        // Command your minion to defend the defendPoint
        for (var defendPoint = 0; defendPoint < defendPoints.length; defendPoint++){
            this.command(friend, "defend", defendPoint);
        }
        
    }
};

loop {
    this.summonTroops();
    this.commandTroops();
}
1 Like

point is a minion’s defend point (with x and y coordinates), so just command your minions to defend their point. No need for that inner for loop.

1 Like

Thanks trotod, I deleted the unnecessary line and it worked!

1 Like

can someone help me in this level. im stuck .my code is when you start it . i do python

1 Like

If your code is the default code, then you have not done any work on this level. At least try it before you come to us for help.

1 Like

i have im stuck when i cant command my soldier to defend defendpoints but it says " must defend x.y or targe" can i show you my code so far

1 Like
# 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, alive or dead
# Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
"len(self.built) % len(summonTypes)"
def summonTroops():
    type = summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf(type):
        self.summon(type)


def commandTroops():
    friends = self.findFriends()
    for friendIndex, friend in enumerate(friends):
        # Use % to wrap around defendPoints based on friendIndex
        "len(defendPoints) % len(friendIndex)"
        # Command your minion to defend the defendPoint
        "i am stuck here"
loop:
      summonTroops()
      commandTroops()
1 Like
"len(self.built)%len(summonTypes)"
"len(defendPoints)%len(friendIndex)"

is my code.
the part where it says
i am stuck is where im stuck

1 Like

this is what i tried :

self.command(friend, "defend", defendpoints)
1 Like

Please put your code in the Format as shown by the FAQ. Indents are important in most if not all programming languages, and your problem could be in the indenting. In addition, it’s just plain easier to read.

1 Like

My code is:

# Command your minion to defend the defendPoint
    defendPoint=defendPoints[len(defendPoints) % friendIndex] 
    self.command(friend, "defend", defendPoint)

I’m just having a hard time understanding this % thing, what can I change here?

1 Like

Oops, sorry.

I believe you have len(defendPoints) and friendIndex switched. That might be the problem.

1 Like

That worked, but then my hero just stands in the middle and tries to keep building guys with an x underneath him

1 Like

Give us your entire code. Perhaps the problem is somewhere there.

1 Like
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"]

def summonTroops():
    type = summonTypes[len(self.built) % len(summonTypes)]
    if self.gold >= self.costOf(type):
        self.summon(type)


def commandTroops():
    friends = self.findFriends()
    for friendIndex, friend in enumerate(friends):
        # Use % to wrap around defendPoints based on friendIndex
        pass
        # Command your minion to defend the defendPoint
        defendPoint=defendPoints[friendIndex % len(defendPoints) 
        self.command(friend, "defend", defendPoint)
loop:
    summonTroops()
    commandTroops()
1 Like

can you explain to me what the % thing does?

1 Like

The % operator, or modulo, returns the remainder of the division of two numbers. For example, 2 % 4 = 0, while 6 % 4 = 2.

1 Like

how does that work with the defined varibles?
like:

friendIndex=friendIndex[len(enemy) % enemyIndex]

that isn’t exactly code, but how does it work with stuff like that

1 Like

Basically, it distributes all troops as equally as possible among the things in the list. Like this:

friends = ["Joan","Brian","Bob", "Random Name"] # List of my friends
enemies = ["Trogdor","Vyrryx","Oniko","Uld'Mak"] # List of my enemies
for friendIndex, friend in enumerate(friends):
    # Take a friend
    enemy = enemies[friendIndex % len(enemies)] # Find the remainder of the friend's 
    #position in the list divided by the remainder of enemies. 
    #That's the enemy the friend is going to attack. 
    self.command(friend, "attack",enemy)
1 Like

ah, i see. it chooses something for you

1 Like