> # Protect the cage.
> # Put a soldier at each X.
> points = []
> points[0] = {"x": 33, "y": 42}
> points[1] = {"x": 47, "y": 42}
> points[2] = {"x": 33, "y": 26}
> points[3] = {"x": 47, "y": 26}
> # 1. Collect 80 gold.
> coin = self.findNearest(self.findItems())
> if self.gold <=80:
> if coin:
> self.move(coin.pos)
> # 2. Build 4 soldiers.
> for i in range(4):
> self.summon("soldier")
> # 3. Send your soldiers into position.
> loop:
> friends = self.findFriends()
> for j in range(len(friends)):
> point = points[j]
> friend = friends[j]
> enemy = friend.findNearestEnemy()
> if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
> # Command friend to attack.
> self.command(friend, "attack", enemy)
> pass
> else:
> self.command(friend, "move", point.pos)
> # Command friend to move to point.
> pass
The title says “Stuck” but you never say how.
To receive quality help we need 3 things from you:
- Your code (You gave it, wonderful)
- What you expect to happen (short description)
- What happens instead (long description, including in which situation this happens)
As an auxiliary:
- Screenshots of the situation where the error happens
Always remember our slogan: Help us help you!
Hi everyone,
I’m working in python my code gathers the gold, summons the soldiers sends them to their points but only the first one fights off the ogres. I’ve tried incresasing J but it doesn’t take it. It tells me there is an indent problem on the J = J + 1 line but when I solve the ident problem only the first soldier moves. what I need is for all four soldiers to go their posts and fight off attacks
Here is my code:
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}
while self.gold <=80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
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 = self.findNearest(self.findEnemies())
if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
self.command(friend, "attack", enemy)
pass
else:
self.command(friend, "move", point)
pass
i = i + 1
j = j + 1
First please read the FAQ and then you’ll know how to put your code in a message so that it shows up properly. (3 `)
- Your indenting is all over the place. You indent to show what lines are part of a group:
if blah == bleck:
all the
indented stuff
is part of the if
no longer part of the if
since we're not indented
same with “loop:” and “for …:” if you break the indenting, the block/group of code is over!
SO…
for j in range(len(friends)):
point = points[j]
the “for j” loop does nothing len(friends) times.
You need to indent everything that goes inside the “for j” loop so that it knows what you want it to do “len(friends)” times.
-
you don’t need to “j=j+1” the “for” loop does that for you.
-
there is no “i” anymore, as it were, so “i=i+1” doesn’t do anything. (and the for loop that ‘i’ was part of does that anyway (see #2)
4a) while it does “hurt” anything, the “pass” statements are filler statements, and do nothing. Once you have other statements in the “if/else” you can remove them.
4b) Again isn’t "hurt"ing anything but. Move the contents of the while loop back an indent so that every thing is nice and the same through out the whole program (4 spaces per indent level, instead of 8 then 4).
while ...
coin ...
if ...
...
for i ...
summon...
loop:
friends ...
for j ...
point ...
...
...
while self.gold <=80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
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 = self.findNearest(self.findEnemies())
if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
self.command(friend, "attack", enemy)
else:
self.command(friend, "move", point)
```
Thanks for the advice, I've corrected everything and it doesn't show any code problems but still only the first soldier is the only one attacking
the bottom part of the code hasn’t lined up too well here’s a screenshot
Try using friend.findNearestEnemy
instead of self
's version.
Thanks nick, it works perfectly now. Sorry about the late reply been a bit busy at work
I did, nothing changed. Why?
Nvm got it. It was a little mistake.
while self.gold <=80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
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 and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
self.command(friend, "attack", enemy)
else:
self.command(friend, "move", point)
this is what I got out of it, but it says error on line 11.
In your friend.findNearestEnemy
function, You have forgotten to put a pair of parentheses after it.
Do you still have the lines that define points
?
You’re adding items to a list that doesn’t exist. Try adding a line at the beginning that says, “points = []
”. This will create an empty list called “points” that you can add list items to.
Ok, now everything is working except the part that makes them move. Here is what I have, but it doesn’t work.
points = []
points[1] = ({"x": 33, "y": 42})
points[2] = ({"x": 47, "y": 42})
points[3] = ({"x": 33, "y": 26})
points[4] = ({"x": 47, "y": 26})
while self.gold <=80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
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 and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
self.command(friend, "attack", enemy)
if friend:
self.command(friend, "move", points)
Points
is a list, not an item. Try moving to point
instead.
Don’t put parentheses around your point dictionaries (the {}
s). That puts them inside a tuple.
Also, arrays start at index 0, not 1. You should have points 0…3 instead of 1…4.