Useful Competitors


#1

Can anybody please explain why my code doesn’t work for this level. Also, by the way whatever I do, I am unable to get my indentations to turn blue? Any help please?

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if not enemy.type is "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        itemx = item.Pos x
        itemy = item.Pos y
        if item.type != "poison":
            hero.moveXY(itemx, itemy)

#2

One issue I see is definitely here:

    itemx = item.Pos x       # should be: item.pos.x
    itemy = item.Pos y       # should be: item.pos.y

Cheers


#3

Thank you, it worked. So for future reference, locating an item without it’s specific coordinates I would use code with the format of “name.pos.x , name.pos.y”?


#4

@jackattack825 If I understand you correctly. You are asking if one doesn’t receive actual coordinates of an item in the form x = 30 and y = 45 can you use “name.pos.x” to find its x coordinate?

Yes.

As long as that game piece has a “pos” attribute. For instance the Hero has a “pos” enemies have a “pos”, doors may have a “pos”. The game should throw an error if the game piece doesn’t have a “pos” and you type “name.pos.x”.

Friends, Enemies, and items should generally have a “pos” And everything that has a “pos” has an “x” and “y” component.


#6

I ran your code and it worked perfectly fine. Have you let it run all the way through? If there is a peon it goes and gets the poison and dies but if not you attack then go get coins.


#8

Help! My code is didn’t work. Maybe it’s a bug?
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != “peon”:
hero.attack(enemy)
item = hero.findNearestItem()
if item:
# Собери предмет только если тип (type) не равен “poison”.
if item.type != “poison”:
pos = item.pos
x = pos.x
y = pos.y
hero.moveXY(x, y)
pass


#9

First please format your code correctly by placing triple backticks around it and adding python to the top one.


#10

I have the same problem, I can’t get past Useful Competitors level, and I have tried both Python and JavaScript.

My Python looks like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if not enemy.type is "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        itemx = item.pos.x
        itemy = item.pos.y
        if item.type != "poison":
            hero.moveXY(itemx, itemy)

I can defeat all the enemies but I’m left with one gem every time.

I used the same code as s1mpsonn in the beginning, and that didn’t work either.


#11

The Python was formatted correctly, but for some reason when I press Reply it “unformats”


#12

See the FAQ for code formatting tips. I’ve fixed it for you this time.

Your code is working for me, maybe you got a bad seed. Try resubmitting the code (click “Submit”) to see if it helps.


#13

Thanks Alex!

I now have a sword and fix the code. That’s what worked in my python (sorry if I posted the answer, feel free delete it)

[redacted, please don’t post solutions]


#14

oh man, it ruins all indentation again!


#15

Check the FAQ for code formatting tips. And yeah, please don’t post solutions. :slight_smile:


#16

I used the same code, and it doesn’t work for me. Help!


#17

Here is my code.
while True:
enemy = hero.findNearestEnemy()
if enemy:
if not enemy.type is “peon”:
hero.attack(enemy)
item = hero.findNearestItem()
if item:
itemx = item.pos.x
itemy = item.pos.y
if item.type != “poison”:
hero.moveXY(itemx, itemy)


#18

Please like @UltCombo said format your code according to the FAQ.
https://discourse.codecombat.com/faq


#19

I just can’t get my program to work. Tell me the answer


#20

Dude this topic is dead. Notice on the top it reads just before your post 5 MONTHS LATER!!!


#22

[don’t post solutions]


#23

i tried doing it using javascript but it won’t work

while(true) {
enemy = hero.findNearestEnemy()
if (enemy) {
if (not enemy.type == “peon”): {
hero.attack(enemy)
item = hero.findNearestItem()
if item:
itemx = item.Pos.x
itemy = item.Pos.y
if item.type != “poison”:
hero.moveXY(itemx, itemy)
}
}

}

this is what poped up

this

any help?