Need help protect-and-serve. Why hero never stop summon archer?


#1

var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };
defend[2] = { x: 74, y: 81 };

var soldiers = [];
var archers = [];
var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == “soldier”) {
soldiers.push(friend);
} else {
// Обороняй рабочих:
defend.push(friend.pos);
}
}

function zeroOne(j){
if(j%2 || j === 0){
return 0;
} else return 1;
}
function count(n){
var a = 0;
if(n<defend.length){
a = n;
} else a = n%a;
return a;
}
for(var j=0; j < soldiers.length; j++){
var soldier = soldiers[j];
hero.command(soldier, “defend”, defend[count(j)]);
}

var o = 0;
if (hero.gold>hero.costOf (“archer”)) {
while(o<8) {
hero.summon(“archer”);
o++;
}
}
var friends = hero.findFriends();

for(var k=0; k < friends.length; k++) {
friend = friends[i];
if(friend.type == “archer”) {
archers.push(friend);
}
}
//}
while(true) {
for(var x = 0; x < archers.length; x++){
var archer = archers[x];
hero.command(archer, “defend”, defend[count(x)]);
}
}
Hello.
Why hero never stop summon archer?
Why is count(j) working good, but count(x) return mistake.


#2

You have no enough gold
So hero wait until gold earned

check indexes

for(var k=0; k < friends.length; k++) {
friend = friends[i];
if(friend.type == "archer") {
archers.push(friend);


#3
for i in range(len(defend)):
        soldier = soldiers[i]
        target = defend[i]
        self.command(soldier, "defend", defend)

what do you use for defend in self.command(soldier, "defend", defend) ?


#4

my entire code:

defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }
defend[2] = { "x": 96, "y": 73}

soldiers = []

friends = self.findFriends()
for friend in friends:
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # Defend the workers:
        defend.append(friend)

loop:
    # Use a for-loop to assign each soldier to a corresponding defend[] target
    # Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    for i in range(len(defend)):
        soldier = soldiers[i]
        target = defend[i]
        self.command(soldier, "defend", defend)


#5

In the command replace defend with target.


#6

I did that before but…


#7

That error message is saying that self.command() is receiving null for the minion argument.
The minion argument is where you put soldier in self.command(soldier, "defend", target).

There seems to be more elements in defend than there are in soldiers.


#9

so, I did a different method and did this:

while True:
    # Use a for-loop to assign each soldier to a corresponding defend[] target
    # Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    for i in range(len(defend)):
        hero.command(soldier, "defend", {"x": 98, "y": 28)
        hero.command(soldier, "defend", {"x": 84, "y": 7})
    pass

but…


#10

What does this mean??


#11

image


#12

T_RBRACE probably means Token: Right Brace, which is this: }
T_RPAR probably means Token: Right Parenthesis, which is this: )
So, that error is saying that the parser expected a } but found a ) instead.
You can see what line the parser found the unexpected token in by looking for the line highlighted in red.

When a left brace/bracket/parenthesis is parsed, the right brace/bracket/parenthesis (matching the type of the left) is expected to close it.

(  {  [  ]  }  )  --  sets matching
vs
(  [  {  )  }  ]  -- sets not matching

#13

I added soldier = soldiers[i] to the loop before hero.command(soldier, "defend", { "x": 98, "y": 28 }) but I got this:

image


#14

So, your minion argument is receiving null. Somehow, your hero.command gets None instead of a soldier to command. How’s None getting stored into soldier?

for i in range(len(defend)):
    soldier = soldiers[i]
    ...

Just focus on these lines.
Compare the length of defend to the length of soldiers.


My post from earlier

Near the top of your program, you have already defined two positions inside the defend array.
Perhaps that is the root cause of this issue.


#15

how would I do this exactly? I am only 10 years old I do not understand what you mean!! :cry:


#16
# there are 5 items in this list:
fiveItems = [10, 20, 30, 40, 50]

# there are 3 items in this list:
threeItems = [71, 72, 73]

for i in range(len(fiveItems)):    # this for loop iterates 5 times (i counts from 0 to 4),
                                   # because there are 5 items in fiveItems
    
    currentItem = threeItems[i]    # what happens when i is 3 or 4?
    
    hero.say(currentItem)
    hero.wait(1)

#17

why does it do that? why not count up to 5?


#18

It’s how loops work in python. range(5) counts from 0 to 4:
0, 1, 2, 3, 4
Notice that there are 5 numbers.

Items in a list in python are zero-based. That means, the first item has an index of 0.

items = ["first", "second", "third", "fourth", "fifth"]
hero.say(items[0]) # hero says "first"
hero.say(items[1]) # hero says "second"
hero.say(items[2]) # hero says "third"
hero.say(items[3]) # hero says "fourth"
hero.say(items[4]) # hero says "fifth"

The bane of soldiers: Help needed
#19

so what does this mean?

for i in range(len(defend))

#20

len(defend) will give you how many items there are in defend.

range(len(defend)) gives you a range of numbers from 0 up to one less than the amount of items there are in defend, counting up by 1 each time.

So, if there are 5 items in defend, then the range will give you 0, 1, 2, 3, 4


#21

so does that mean that the for loop only commands soldiers 1, 2, 3, 4 and 5? (There are 6 soldiers)