[SOLVED]Two Flowers

I can’t figure it out its so frustrating!!!
here is my code:

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        soldiers = hero.findByType("soldier")
        for soldier in soldiers:
            if enemy:
                hero.command(soldier, "attack", enemy)

# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)

peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

please help

Try to do this

friends = hero.findByType("peasant", hero.findFriends())
  for friends in friends:
       enemy = friend.findNearestEnemy()
      if enemy:
         hero.command(friend, "attack" enemy)
        

This way it attacks the closest enemy to the peasant

so where should I put the code?

do it in the command friends thing

huh I don’t know where is the command friends thing

Do it for command soldiers.

it still doesn’t work

here is my entire code:

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        soldiers = hero.findByType("soldier")
        for soldier in soldiers:
            if enemy:
                hero.command(soldier, "attack", enemy)
                friends = hero.findByType("peasant", hero.findFriends())
    for friends in friends:
        enemy = friend.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)

# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)

peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

Get rid of this

And indent this properly

it still doesn’t work

show me your new code now and what happens

here is my new code:

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    friends = hero.findByType("peasant", hero.findFriends())
    for friends in friends:
        enemy = hero.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)

# Define the function: pickUpNearestCoin
friends = hero.findByType("peasant", hero.findFriends())
for friends in friends:
    enemy = friend.findNearestEnemy()
if enemy:
    hero.command(friend, "attack", enemy)
peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    # commandSoldiers()
    commandSoldiers()
    # pickUpNearestCoin()
    pickUpNearestCoin()

and it say’s try hero.findFriends

1 Like

Ill help you soon, just let me start up CoCo ok?

6 Likes

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

1 Like

Thanks for welcoming me! I’ve been here before tho :slight_smile:

4 Likes

ok then what do I do?

2 Likes

um how do I do that step?

1 Like

delete the command Soldiers funtion and instead put this:

def commandSoldiers():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy and soldier.type == "soldier":
            hero.command(soldier, "attack", enemy)
3 Likes

ok then what do I do?

1 Like