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();
}
# 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()
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.
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)