[SOLVED] Hunters and Prey python bugs and please help me

  1. there is a bug
    my reindeer are killed, but i still pass

  2. this is my code, why it said so?

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coins = self.findItems()
    coinIndex = 0
    coin = coins[coinIndex]
    if coin:
        self.move(coin.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        # Loop over all your soldiers and order them to attack.
        
        soldiers = hero.findFriends()
        soldierIndex = 0
        while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]
            soldierIndex += 1
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            
            
            hero.command(soldier, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher(archer):
    enemy = hero.findNearest(hero.findEnemies())
    if enemy and archer.distanceTo(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
    
    
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

Even though it is not the right error you are asking your soldier to attack enemy even if there is no enemy.

there should alway be a condition if enemy
(before attacking them, because when you ask the computer to do something with an object or a value that is null it will always return an error)

but i have ‘’ if enemy ‘’…

and my code can work in CLOUDRIP BRAWL…

actually, every soldier gonna attack enemy so at some points there will be no enemy and they are still asked to attack the enemy.

every soldier should be asked to check if there is an enemy. But right now it is defined before your Loop command

thank you.
i change my code, but hero still doesn’t work…

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coins = self.findItems()
    coinIndex = 0
    coin = coins[coinIndex]
    if coin:
        self.move(coin.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    
       
        
    soldiers = hero.findFriends()
    soldierIndex = 0
    while soldierIndex < len(soldiers):
        soldier = soldiers[soldierIndex]
        soldierIndex += 1
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            
        enemy = hero.findNearest(hero.findEnemies())   
        hero.command(soldier, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher(archer):
    enemy = hero.findNearest(hero.findEnemies())
    if enemy and archer.distanceTo(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
    
    
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    enemy = hero.findNearest(hero.findEnemies())
    
    if enemy:
        for friend in friends:
            if friend.type == "soldier":
                commandSoldier(friend)
              
            elif friend.type == "archer":
            
                commandArcher(friend)
            pass

and it still show this

Just found what doesn’t work

def commandSoldier(soldier):
    # Soldiers should attack enemies.
    
       
        
    soldiers = hero.findFriends() # <- in this there is all friends inside the array soldier
    soldierIndex = 0
    while soldierIndex < len(soldiers):
        soldier = soldiers[soldierIndex]
        soldierIndex += 1
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            
        enemy = hero.findNearest(hero.findEnemies())   
        hero.command(soldier, "attack", enemy)
    pass

in your function commandSoldier(soldier) you have already the object soldier
but you recall hero.findfriends() after which include every friend units in it including the reindeer.

You could just remove the whole friend calling in this function and it should work

1 Like

oh! yes!
it works!
thank u!!!

Change the topic to [Solved]