Completely blocked on Noble Sacrifice level


#1

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.


#2

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

#3

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


#4

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.


#5

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.


#6

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!


#7

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.


#8

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.


#9

Thank you so much! :smile:

I hearted your posts to show my thanks!


#11

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.


#12

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


#13

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: