Help with capture their flag in codecombat

here is my code:

type or paste code here
def placeFlag():
    Myflag0 = hero.findMyFlags[0]
    Myflag1 = hero.findMyFlags[1]
    Myflag2 = hero.findMyFlags[2]
    hero.placeFlag({"x": 134, "y": 107})
    hero.placeFlag({"x": 134, "y": 107})
    hero.placeFlag({"x": 134, "y": 107})

def findEnemyFlags():
    enemyflag0 = hero.findEnemyFlags[0]
    enemyflag1 = hero.findEnemyFlags[1]
    enemyflag2 = hero.findEnemyFlags[2]

def captureFlag():
    hero.moveXY(24, 33)
    enemyflag0 = hero.findEnemyFlags[0]
    enemyflag1 = hero.findEnemyFlags[1]
    enemyflag2 = hero.findEnemyFlags[2]
    if enemyflag0:
        hero.move(enemyflag0.pos)
        distance = hero.distanceTo(enemyflag0)
        if distance < 6:
            enemyflag0 = hero.findEnemyFlags[0]
        if distance < 3:
            hero.captureFlag(enemyflag0)
        hero.moveXY(19, 21)
    if enemyflag1:
        hero.move(enemyflag1.pos)
        distance = hero.distanceTo(enemyflag0)
        if distance < 6:
            enemyflag1 = hero.findEnemyFlags[1]
        if distance < 3:
            hero.captureFlag(enemyflag1)
    hero.moveXY(97, 65)
    if enemyflag2:
        hero.move(enemyflag2.pos)
        distance = hero.distanceTo(enemyflag2)
        if distance < 6:
            enemyflag2 = hero.findEnemyFlags[2]
        if distance < 3:
            hero.captureFlag(enemyflag2)
    hero.moveXY(51, 26)

while True:
    placeFlag()
    findEnemyFlags()
    captureFlag()
P.S.
This is the link: https://codecombat.com/play/level/capture-their-flag?
2 Likes

Hello and welcome to codecombat discourse @FGTEEV ! 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!

Can you explain what the problem is please @FGTEEV?

I haven’t done this level yet but I’ll try to help as best as I can

the problem is that my hero just goes to the middle flag and goes back hero does’nt even capture it even though i told him to.

it’s in glacier (20 chars)

use the link, the link is https://codecombat.com/play/level/capture-their-flag?

Sorry to disappoint, but its 1 am right now, and I can’t find anything in the discourse, so I’m going to call for someone who can help you, @dedreous! @deadpool198!

it’s ok(20 characters)

Hello @FGTEEV, please do not create duplicate topics as it clutters up the Discourse. It doesn’t make your problem solve faster. I see you have already created another topic (Help with capture their flag 2). Can someone shut either topic down?
Lydia

@dedreous @Deadpool198

I am pretty sure that in this level, the methods placeFlag, captureFlag, and findEnemyFlags are already defined, so don’t call those functions these methods. I recommend defining three functions for the three flags, and moving towards each flag in the function, so you call the three functions and finish the level.

this is new code:

def placeFlag():
    Myflag0 = hero.findMyFlags[0]
    Myflag1 = hero.findMyFlags[1]
    Myflag2 = hero.findMyFlags[2]
    hero.placeFlag({"x": 134, "y": 107})
    hero.placeFlag({"x": 134, "y": 107})
    hero.placeFlag({"x": 134, "y": 107})

def findEnemyFlags():
    enemyflag0 = hero.findEnemyFlags[0]
    enemyflag1 = hero.findEnemyFlags[1]
    enemyflag2 = hero.findEnemyFlags[2]

def captureFlag():
    hero.moveXY(24, 33)
    hero.moveXY(19, 21)
    hero.moveXY(97, 65)
    enemyflag0 = hero.findEnemyFlags[0]
    enemyflag1 = hero.findEnemyFlags[1]
    enemyflag2 = hero.findEnemyFlags[2]
    
    if enemyflag0:
        distance = hero.distanceTo(enemyflag0)
        if distance < 6:
            enemyflag0 = hero.findEnemyFlags[0]
        if distance < 3:
            hero.captureFlag(this.getNearestEnemy(this.findEnemyFlags()))
    hero.moveXY(53, 49)
    hero.moveXY(109, 90)
    
    if enemyflag1:
        distance = hero.distanceTo(enemyflag1)
        if distance < 6:
            enemyflag1 = hero.findEnemyFlags[1]
        if distance < 3:
            hero.captureFlag(this.getNearestEnemy(this.findEnemyFlags()))
    hero.moveXY(53, 49)
    hero.moveXY(111, 27)
    
    
    if enemyflag2:
        distance = hero.distanceTo(enemyflag2)
        if distance < 6:
            enemyflag2 = hero.findEnemyFlags[2]
        if distance < 3:
            hero.captureFlag(this.getNearestEnemy(this.findEnemyFlags()))
    hero.moveXY(40, 56)
    

while True:
    placeFlag()
    findEnemyFlags()
    captureFlag()