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.
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
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
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…
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)
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.
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?
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?