How do you beat Siege of Stonehold on CodeCombat, I just don't get it?


#1

I just can’t figure out what to do on Siege of Stonehold on Codecombat, I just don’t get it, here is my code. By the way, it’s in Python. I just need to figure out how to make the guy attack the enemies, nothing just seems to work.

loop:
    enemy = self.findNearestEnemy()
    flag = self.findFlag("green")
    if flag:
        self.pickUpFlag(flag)
    elif enemy:
        if self.isReady("cleave"):
            self.cleave(enemy)
    if flag:
        if flag.color is "black":
            if enemy:
                self.moveXY(flag.pos.x, flag.pos.y)
                self.attack(enemy)
            self.pickUpFlag(flag)
            
    if not enemy:
            self.pickUpFlag(flag)

Thank you if you can help me with this problem.


#2

Well, flag is never going to be black, since you set flag only to green flags…

There are other weird things but that depends on what you had in mind. Such as you only attack once then pickup the black flag, so you only ever “attack” once per black flag… (attack is a single swing) oh, and you will run to the blackflag, then attack(once) the enemy nearest to your OLD position, then run back to the flag to pick it up


#3

Now here is my new code but I still can’t beat this level.

loop:
    enemy = self.findNearestEnemy()
    flag = self.findFlag("green")
    flag1 = self.findFlag("black")
    if flag:
        self.pickUpFlag(flag)
    elif enemy:
        if self.isReady("cleave"):
            self.cleave(enemy)
    if flag1:
        if flag1.color is "black":
            if enemy:
                loop:
                    self.attack(enemy)
            self.pickUpFlag(flag1)
            
            if not enemy:
                self.pickUpFlag(flag1)

Merged Doublepost

it’s still not working, whenever I place down the black flag, whenever i place down another flag, nothing happens.


#5

loop has no end, so you loop-forever on attack(enemy)


#6

so what would i do to solve that problem?


#7
if enemy:
    loop:
        self.attack(enemy)

looks really bad. You will attack the one single enemy forever. Best remember to never use a loop inside another loop.

EDIT: This is especially for the loop-command. It often is useful for while or for to be used inside each other (or itself). But loop is guaranteed to never stop.


Also your if not enemy: at the bottom is unnecessary, as you pickup the flag1 anyway with the line right before that (Indentation matters!).


Also you don’t have to check flag1.color is "black", because you specified the color in self.findFlag().


Do I understand your code right:

If there is no green flag and an enemy, cleave that enemy (if possible).

Then, if and only if there is a flag1, attack the nearest enemy.

Then your hero will cleave on his own, but will only ever attack when you place a black flag beforehand, and then also only the enemy which is at that time the nearest.

You might want to think this through again.


#8

ok thanks, ill try that