Zoo Keeper Help!(Python)


#1

I have been stuck on the level Zoo Keeper for a pretty long time now and any help would be greatly appreciated.
My code:

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}
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", point)

My friends do not attack
Thanks


#2

Does it say anything? Like “fix your code” or “error on line 2” or something?


#3

There are no errors. My Friends just go to the points and don’t attack the ogres. It just tries to keep on going forward but it gets stuck.:pensive:


#4

The last if

if friend:
            self.command(friend, "move", point)

is always true so every attack command will be followed by a “move” command which overwrites the attack command.
attack + move = move only


Zoo Keeper Python Help
#5

No like does it say anything?


#6

No it does not show any errors.


#7

@AdrianCgw Where in my code does it make my if-statement true?


#8

I’m not sure what’s wrong other then what @AdrianCgw mentioned. Is there any more information you can give us?


#9

What other info. can I give @Luke10?Also can you please tell me how to do what @AdrianCgw said.


#10

I not really sure how do do what @AdrianCgw said. I’ll invite @nick


#11

He probably can help. I’m sorry I can’t assist you. :cry:


#12

Try this:

for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy =  friend.findNearestEnemy()
        if friend and enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
            self.command(friend, "attack", enemy)
         elif friend:
            self.command(friend, "move", point)

Shortly, this way of codding is bad:

if something
     attack
if something_else
     move

This is good:

if something 
    attack
elif something_else
    move

Help! Im stuck on zoo keeper
#13

Thanks, I finally beat the level.


#14

Glad to hear it. Sorry I wasn’t much help.


#15

It is okay.:slightly_smiling:


#16

Hello):smiley: i have some question on this level’s code… i passed it, but still have one interest)
So, can i, when my 4 soldiers took already their positions and started attack enemies, command them to take position of another defined soldier (while primary code executing)? Other words, can i direct defined soldier from his own position to other defined position of other soldier? Is it real to modernise this level’s code this way?
It is will be useful when orcs attack from only one side)


#17

In your original code change the if friend part to else, because it just makes them go to the spots instead of attacking


#19

Please do not revive dead topics. This one has been left alone for 6 months. :slight_smile: