Completely blocked on Noble Sacrifice level

Hi, i am having trouble with the level “Noble Sacrifice”, been blocked on this one for days, i can’t grasp what i am supposed to do, my code currently look like this:

# Collect 80 gold
while self.gold < 80:
    coin = self.findNearest(self.findItems())
    if coin:
        self.move(coin.pos)

# Build 4 soldiers to use as bait
for i in range(4):
    self.summon("soldier")
# Send your soldiers into position
points = []
points[0] = { "x": 13, "y": 73 }
points[1] = { "x": 51, "y": 73 }
points[2] = { "x": 51, "y": 53 }
points[3] = { "x": 90, "y": 52 }
friends = self.findFriends()
fIndex = 0
friend = friends[fIndex]

for i in range(4):
    self.command(friend, "move", points.pos)
    fIndex += 1
    points += 1
# Use range to make an array to loop over.
# Match the friends to the points and command them to move

I collect the gold alright, summon 4 soldiers alright, then command one of them to move he just go straight ahead and walk into the wall. Then nothing happens. Any hint on what i am doing wrong would be greatly appreciated, thank you very much.

Your problem is here:

friends = self.findFriends()
fIndex = 0
friend = friends[fIndex]                         # this should be inside the 'for' loop below

for i in range(4):
    self.command(friend, "move", points.pos)     # 'points' should be indexed, too
    fIndex += 1
    points += 1                                  # this line is meaningless

Alright, first thank you very much for your very quick answer; unfortunately i must not have understood your indication quite right because i am still confronted to the same problem.

I have tried :

# Collect 80 gold
while self.gold < 80:
    coin = self.findNearest(self.findItems())
    if coin:
        self.move(coin.pos)

# Build 4 soldiers to use as bait
for i in range(4):
    self.summon("soldier")
# Send your soldiers into position
points = []
points[0] = { "x": 13, "y": 73 }
points[1] = { "x": 51, "y": 73 }
points[2] = { "x": 51, "y": 53 }
points[3] = { "x": 90, "y": 52 }


for i in range(4):
    pIndex = 0
    point = points[pIndex]
    friends = self.findFriends()
    fIndex = 0
    friend = friends[fIndex]
    self.command(friend, "move", point.pos)
    fIndex += 1
    pIndex += 1
# Use range to make an array to loop over.
# Match the friends to the points and command them to move

aswell as :

# Collect 80 gold
while self.gold < 80:
    coin = self.findNearest(self.findItems())
    if coin:
        self.move(coin.pos)

# Build 4 soldiers to use as bait
for i in range(4):
    self.summon("soldier")
# Send your soldiers into position



for i in range(4):
    points = []
    points[0] = { "x": 13, "y": 73 }
    points[1] = { "x": 51, "y": 73 }
    points[2] = { "x": 51, "y": 53 }
    points[3] = { "x": 90, "y": 52 }
    pIndex = 0
    point = points[pIndex]
    friends = self.findFriends()
    fIndex = 0
    friend = friends[fIndex]
    self.command(friend, "move", point.pos)
    fIndex += 1
    pIndex += 1
# Use range to make an array to loop over.
# Match the friends to the points and command them to move

Both still only send one soldier straight to the wall, i tried with friend = friends[i] aswell, this time it send the 4 soldiers, but still walk into the wall.

Thanks again for your indications, sorry if i didn’t made the right changes :s

Your problem, in this case, is the self.command(friend,"move",point.pos). You see, point is already a position, and so you can not find the pos of it.

1 Like

Oh, i see! Thank you so much, i tryed with the same code without .pos, one moved to one of the point but other soldiers didn’t move. I did it with friend = friends[i] and point = points[i] and it all worked!!

Thank you so much i’ve been stuck on this for so long i was starting to desperate. =)

Have a wonderfull day/night depending on where you are at, thanks again, bye.

A little bit of revision: to go through a list (array) using indexes, you need:

  • a starting index (*)
  • a loop
  • increase the index within the loop (*)

(*) optional, based on the type of loop (see below)


In python, that would be something like:

index = 0

while index < limit:
# 'while' just checks if a condition is true or false
# so you have to take care of increasing the index

    something = list[index]
    # your code here

    index = index + 1       # if you forget this, it will be an infinite loop!

or:

for index in range(limit):
# 'for' automatically increases the given variable
# staring from zero: index = 0, 1, 2 ... limit

    something = list[index]
    # your code here

Mixing the different loops may not a good idea.


Please :heart: this post if you found it useful. Thanks!

9 Likes

This is my code

points[0] = {"x": 90, "y": 52}
points[1] = {"x": 51, "y": 53}
points[2] = {"x": 51, "y": 73}
points[3] = {"x": 13, "y": 73}
while self.gold < 80:
item = self.findNearest(self.findItems())
x = item.pos.x
    y = item.pos.y
    self.moveXY(x, y)
for i in range(4):
    self.summon("soldier")
loop:
    friends = self.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy = friend.findNearestEnemy()
        if enemy:
            self.shield()
        else:
            pass
            self.command(friend, "move", point)

when one of my soldiers die, another moves to replace it and the yeti doesn’t get lured out.

I suppose that the indentation is OK in the first while loop and your code starts with points = []

Your soldiers move around because you put the commands in an (infinite) loop, so you continuously command them to move to the specified points. This is not a problem as long as all 4 are alive; but as soon as the first one dies, the second will try to go to the first soldier’s place, and so on… so instead of a simple loop, use a for or while loop, that will command your soldiers only once. After that just sit back and watch. Also, you don’t really need to check for enemies on this level…

For more explanation about loops, see my previous comment.

3 Likes

Thank you so much! :smile:

I hearted your posts to show my thanks!

1 Like

I have

points = []
points[0] = {"x": 90, "y": 52}
points[1] = {"x": 51, "y": 53}
points[2] = {"x": 51, "y": 73}
points[3] = {"x": 13, "y": 73}
while self.gold < 80:
    item = self.findNearest(self.findItems())
    x = item.pos.x
    y = item.pos.y
    self.moveXY(x, y)
for i in range(4):
    self.summon("soldier")
loop:
    friends = self.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy = friend.findNearestEnemy()
        if enemy:
            self.shield()
        if friend:
            self.command(friend, "move", point)

but when one soldier dies, then next one on as x moves where the soldier died.

I need help! I don’t know where to go next, I’ve tried updating my code according to your suggestions, but I can’t find out where friends = friends[i] points = points[i] should go.

Collect 80 gold

while self.gold < 80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)

Build 4 soldiers to use as bait

for i in range(4):
self.summon(“soldier”)

Send your soldiers into position

points = []
points[0] = { “x”: 13, “y”: 73 }
points[1] = { “x”: 51, “y”: 73 }
points[2] = { “x”: 51, “y”: 53 }
points[3] = { “x”: 90, “y”: 52 }

for i in range(4):
pIndex = 0
point = points[pIndex]
friends = self.findFriends()
fIndex = 0
friend = friends[fIndex]
self.command(friend, “move”, point)
fIndex += 1
pIndex += 1

Use range to make an array to loop over.

Match the friends to the points and command them to move

The problem is since you defined fIndex and friends within the for loop, fIndex will always become zero every time you loop through the code. First of all, this isn’t necessary, but I recommend you put friends = self.findFriends() outside of the for loop. Second, no need to go through all the hassle of defining fIndex and pIndex, remove those because it won’t work if you keep one reseting it to zero. Maybe you could just use i instead. :wink:

1 Like

so im having trouble with the soldiers .they wont move what is wrong with this part of the program

What language are you using?

i am javascript …did do something incorrect there

Unfortunately, I know very little about JavaScript. I’ll need to let someone answer who does.

A couple of little things, but I’m not sure if it would prevent the soldiers from moving.

In this level, you don’t need the while(true) loop for the last section commanding the soldiers. The for loop will command them all. Also, you don’t need to find the enemy either. The combination of the while loop and finding the enemy triggered an error when I ran it since all of your pals will end up dying.

One last question, do you have the Boss star equipped?

1 Like

yep i did that first .it didnt work :confused: so here is an update

Hi! Could it be that you might never end a previous while(#condition) loop and because of that, never reach the part of the code for commanding? Hows your code looking before the 14th line of code?

im just following instructions as best as i can