Having trouble with Siege of Stronghold: please review my code


#1

I am trying to build walls to corral the enemies into columns. I don’t even know if my overall strategy will work, but now I am hung up on the problem at hand.

I am trying to build a loop that will build fences along a vertical axis when I place a green flag, then stop and put down a fire-trap when I put down a violet flag. I feel like it could be fool around with the dynamics of the strategy more once I build my code, but for right now I can’t get my character to stop building fences when I put down a violet flag.

Apparently I’m stuck in a loop, but I don’t know why. my loop starts by looking for a violet flag, then a green flag. Putting down a violet flag should quit the loop, but instead I keep building fences.

Help!

edit: took out my code cuz no cheating!


#2

Your program is stuck in the inner loop. Your code is finding the violet flag in the outer loop, so when your character is busy building fences and you place a violet flag, the statement if violetFlag will remain false because violetFlag hasn’t been updated.

Edit: I assume this was your first code review, congratulations! Code reviews are a common development process for building good software. Submitting your code for review is a good practice to get into as a software developer.


#3

Thanks!

Doesn’t the code return to the beginning of the loop: statement at the end? If it does, it should find the violet flag inside the outer if greenFlag if statement, right?


#4

Execution will only go from the inner loop to the beginning of the outer loop when the break statement inside the inner loop is executed. And currently that break statement is unreachable.

Here’s a simplified version of your code but with lots of the code removed so it’s easier to follow control flow (in other words, the order that the code is executed in).

loop:  # outer loop
    greenFlag = self.findFlag("green")
    violetFlag = self.findFlag("violet")

    if violetFlag:
        # <build fire trap at violet flag then pick up flag>

    elif greenFlag:
        # <move above the green flag>

        loop:  # inner loop
            if violetFlag:
                # <build fire trap at violet flag then pick up both flags>
                break

            elif greenFlag: 
                # <build a fence above green flag>

To reach the inner loop, there must only be a green flag on the map.

loop:  # outer loop
    if violetFlag:
        ...
    elif greenFlag:
        loop:  # inner loop (runs when `if violetFlag` is false)
            ...

Say you placed a green flag on the map and your character starts building fences. While the program is looping inside the inner loop, you place a violet flag on the map because you want your character to build a fire trap.

But that won’t happen because violetFlag still contains the old value that it was assigned in the outer loop. Which means that the condition if violetFlag inside the inner loop will always be false until you update the value for violetFlag by finding it again.

Hope that helps.


#5

Brilliant. Now I can continue to write my code which may or may not work.

Thank you so much!


#6

hello, i used the following code and it might help a bit. Don’t copy though, cause that’s cheating! Here it is:

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!

loop:
enemy = self.findNearest(self.findEnemies())
flag = self.findFlag()
if flag:
self.pickUpFlag(flag)
elif enemy and self.isReady(“cleave”):
self.cleave(enemy)
elif enemy:
self.attack(enemy)


#7

It helped beat mine. By the way I’m, using the sword which can cleave