[SOLVED] Two flowers (Python)


#1

This 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():
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
**# Define the function: pickUpNearestCoin**
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)
    peasant = hero.findByType("peasant")[0]

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

My hero will summon soldiers, but the soldiers just protect me and Hector dies.
Can’t figure out what is wrong


#2

I am JavaScript but try telling your soldiers to find the enemy, not your hero.

enemy = friend.findNearestEnemy()
or
enemy = friend.findNearest(friend.findEnemies())

Also maybe add an “if enemy” in there

Also quick question, what is the purpose of this line:

peasant = hero.findByType(“peasant”)[0]

?Are you identifying Hector for some reason?

Sorry last question, I noticed you have a line separating your definition of “summonSoldiers()” but no line separating “commandSoldiers()” and “pickUpNearestCoin”. I am not familiar with Python yet but could that be a contributing factor?

Also for future reference it is good practice to post your code with the </> button

ie)

# 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():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “attack”, enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType(“peasant”)[0]

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

#3

What kind of strange double quotes are you using? Your browser is maybe not set to English as default language. If properly indented your code must work if your double quotes are right and you

hero.moveXY(item.pos.x, item.pos.y) # comment this line and replace it with
hero.move(item.pos)
#/ use the **</>** symbol above to format properly /

You must also replace boots if they don’t support move - moveXY has a hidden loop inside it and can add troubles when you command troops. In your case I think that they follow you hero as pets until you all die.

#cmmnd can be also defend  ( attack needs enemy)
#and pos can be peasant.pos or hero.pos or soldier.pos
#Try all possibilities and see what works best
hero.command(soldier, cmmnd, pos)

#4

You have to see if the enemy exists. If you want it to protect Hector, then change the code to

this = hero.findByType(peasant)[o]
if this:
    hero.command(friend, "defend", this)

#5

Thanks to you all, but I still have a problem. This is my code again

# 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():
    friend = hero.findFriends()
    enemy = friend.findNearest(friend.findEnemies())
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    this = hero.findByType("peasant")[0]
    if this:
        hero.command(friend, "defend", this)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

Now it says that friend has no method findEnemies


#6

So then use friend.findNearestEnemy().


#7

Changed some stuff. Here.

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    if hero.findByType("peasant"):
        hero.command(hero.findFriends, "defend", hero.findByType("peasant"))
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

Now I get an error message: Command's argument minion should have type unit, but got function. Hero Placeholder needs something to command.


#8
peasants = hero.findByType("peasant")
for thing in peasants:
    if this:
        hero.command(this, "defend", thing)

or

peasant = hero.findByType("peasant")
if peasant and this:
    hero.command(this, "defend", peasant)
    

#9

where do I insert this?


#10

In your commandSoldiers()


#11

I did what you said, but I still get an error.

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        hero.command(soldier, "attack", enemy)
    peasant = hero.findByType("peasant")
    if peasant and soldier:
        hero.command(soldier, "defend", peasant)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

It says defend takes no arguments. As in the part in commandSoldiers.


#12

Now I have this:

# 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():
    friend = hero.findFriends()
    enemy = hero.findNearestEnemy()
    soldiers = hero.findByType("soldier")
    peasant = hero.findByType("peasant")
    x = peasant.pos.x
    y = peasant.pos.y
    for soldier in soldiers:
        hero.command(soldier, "defend", {x, y})
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

My hero just stands there. How do I fix that?


#13

Thanks to you all!
I got it!
:smile:


#14

Please don’t post solutions on the board, thanks 123hi123!

It took me a little bit because I don’t know python but I got it as well lol (not in time to help you though!) I didn’t try defend because I am not familiar with the language but I am happy you got it!

If you want to post spoilers people usually put them between a

[spoiler] text here [/spoiler]

Like this! <- To get this =) (click to reveal)

Also can you add [SOLVED] to your topic header?