Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Backwood Forest > Siege of Stonehold - help needed

Been trying to use flags, attacking nearest, and then moving to the healer if health < maxhealth/2, but the while seems to run once and then stop running. Don’t really know what to do.
Any help will be greatly appreciated.

Mind of you post your full code so we can see what’s wrong with the code?

Code is:
while True:
flag = hero.findFlag()
if flag == “green”:
enemy = hero.findNearestEnemy()
if enemy:
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.pickUpFlag(flag)
elif flag == “black”:
hero.moveXY(flag.pos.x, flag.pos.y)
elif flag == “violet”:
enemy = hero.findNearestEnemy()
if enemy:
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.pickUpFlag(flag)

Indentations for some reason did not translate in copy paste, but they are appropriate (no faults of this kind). Something in my logic sucks.

Try doing making three separate variables for green, black, and violet. Then checking if they exist. FYI you can format code. Read this:

Again,
Since I just cut/paste, for some reason my formatting did not hold in the paste. I asked you to trust that it is there. This is not the source of my problems.
Also, I tried using the black flag as healer position, and green & violet to show enemy, but again it reacts for first 2 flags and then shuts down, instead of repeating the loop. How do I know? - becausse it does not try and attack when swept with enemies hacking at it.

While your indentation may be correct, it makes it easier for others to help you when it is formatted correctly by copying/pasting into our own editor to work with. I received unexpected errors when copying your code that I had to troubleshoot before even working on your code. Adding 3 back ticks ` (just left of the 1) before your code and after will format it easily. The button above with </> will also create these for you and then paste the code between them

The two key pieces that your are missing is

  1. checking for a flag before looking at the
  2. flag.color == "green" # missing .color

One other question, why do you move to the enemy before attacking? The attack method has that built into the code. For certain levels, I do suggest this approach, but advise to use the move() instead of moveXY() if you have the boots available so your hero reevaluates the move and runs other code after every step

Thanks brooksy for your advice.
I used a non discriptive falg, without attributing color here, just to look for any flag without reference to any specific color. In previouse codes it actually work OK for me, and let me use flags as I see fit. BUT, since I’m here to learn coding, it’s a good idea to start using “.color” as reference (e.g., instead of using “moveXY” to the healer, just using a specific color flag and not pick it up, or creating a flag triangle where the green and violet are picked up and hero looks for the next color…-My main problem was the whole “while” loop seems not to complete somehow. It stops doing anything after one/two loops…

How were you moving to the flag without defining the color? There are two ways to get the color, either look for a specific color or check against the flag.color.

while True:
    green_flag = hero.findFlag("green")
    if green_flag:
        hero.moveXY(green_flag.pos.x, green_flag.pos.y)
    #or
    flag = hero.findFlag()
    if flag and flag.color == "green": # need to check for flag and specify .color to check
        hero.moveXY(flag.pos.x, flag.pos.y)

Without knowing how you are indenting, I can’t get your code to work correctly at all. Since you don’t check for a flag, your code may lock up when trying to verify the color.

#these lines will always return a false 
if flag == "green": # need to check the attribute .color of the flag
# test code to check if flag is working
while True:
    flag = hero.findFlag()
    if flag == "green": # returns false and ignores flag
        hero.say("found it") # added hero.say to see if this if statement returns True 
        hero.moveXY(flag.pos.x, flag.pos.y)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

Thanks Jacob,
I actually used non-discript flags to be able to find ANY flag whatsoever. It worked for me in previous stages just fine. Again, this does not preclude using conditionals to NOt pick the black flag, and use it for healing (healer position), However, my “while” loop seems to not respond at spme point and I can’t really tell why. Without running it via “submit” you will find it hard to undersand what I mean. It just stops in the middle with no apparent errors, and waits to die, not responding to finding enemies or attacking them.

For some reason - this time i was able to preserve the indentations:

Help your friends beat the minions that Thoktar sends against you.

You’ll need great equipment and strategy to win.

Flags might help, but it’s up to you–be creative!

There is a doctor behind the fence. Move to the X to get healed!

while True:
flag = hero.findFlag()
if flag == “green”:
enemy = hero.findNearestEnemy()
if enemy:
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.pickUpFlag(flag)
elif flag == “black”:
hero.moveXY(flag.pos.x, flag.pos.y)
elif flag == “violet”:
enemy = hero.findNearestEnemy()
if enemy:
hero.moveXY(enemy.pos.x, enemy.pos.y)
hero.attack(enemy)
hero.pickUpFlag(flag)

EOF

Hi Simpletuna,
I’m afraid your code still isn’t formatted, please do format it for the reasons Brooksy said above, and that it just looks a lot nicer.
In case you don’t know how to do it:
Click on this button:
Screen Shot 2020-01-01 at 18.03.35
You will see this:
Screen Shot 2020-01-01 at 18.04.02
Then delete the text there, and paste your code directly from the CodeCombat level window with the indentations.
Thanks,
Danny

# Help your friends beat the minions that Thoktar sends against you.
# You'll need great equipment and strategy to win.
# Flags might help, but it's up to you–be creative!
# There is a doctor behind the fence. Move to the X to get healed!

while True:
    flag = hero.findFlag()
    if flag == ("green") or ("violet"):
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            hero.pickUpFlag(flag)
            if hero.health < 0.5*hero.maxHealth:
                flag = hero.findFlag("black")
                hero.moveXY(flag.pos.x, flag.pos.y)

To use the flags live, you need to press Submit. Otherwise, you can’t place the flags during a run and they will only show up if you placed them during a Submit.

You are still missing the .color check and your code is not running like you think it is. Check the documentation below. The value of the flag is not green, the color of the flag is green. That is why you need to add the.color attribute to the flag variable.
flag.color

Also, you can’t add an or between two parameters which is giving you a false positive.

if flag == ("green") or ("violet"): 
if flag.color == "green" or flag.color == "violet":
#this is another way to do it
if flag.color in ["green","violet"]:

One last thing, you should name your flag variables differently so they don’t get confused. Right now, your green, violet and black flags are all the variable flag. This may get you in trouble when your code if your hero.health < 0.5*hero.maxHealth and you don’t have a black flag, it will find the green or purple flag and run the hero.moveXY() code.

Thanks for the advice. Especially of the forbidden logicals between paramteres (I wonder if I name them differently, if it IS posible…)
Anycase, if you submitted my code, you would have noticed that it stops working after few moves, which never happened to me before (used the non-discriptive falg for all flags and such, but got over stages swimingly so far).

Tried the following to no avail. appreciate your input:

while True:
    flaggreen = hero.findFlag("green")
    if flaggreen:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            hero.pickUpFlag()

    flagviolet = hero.findFlag("violet")
    if flagviolet:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
            hero.pickUpFlag()

    if hero.health < 0.5*hero.maxHealth:
        flagblack = hero.findFlag("black")
        hero.moveXY(flagblack.pos.x, flagblack.pos.y)