Need help on Sarven Sentry


#1

Need help on Sarven Sentry. Something is wrong with my code, please help.

Merged Doublepost

My code is:

loop:
    

    flagGreen = self.findFlag("green")
    flagBlack = self.findFlag("black")
    flagViolet = self.findFlag("violet")

    self.findFlag("black")
    if flagBlack:
        pos = flagBlack.pos
        x = pos.x
        y = pos.y
        self.buildXY("fire-trap", x, y)
        self.pickUpFlag(flagBlack)
    self.findFlag("green")
    if flagGreen:
        pos = flagGreen.pos
        x = pos.x
        y = pos.y
        flagGreen = self.buildXY("fence", x, y)
        self.pickUpFlag(flagGreen)
    self.findFlag("violet")
    if flagViolet:
        pos = flagViolet.pos
        x = pos.x
        y = pos.y
        self.moveXY(x, y)
        self.pickUpFlag(flagViolet)

#3

This looks like a serious case of life-destroying indentation-problems. I used to say Indentations save lives, and you’ll need a lot of them (unless the copy-paste failed somehow, then I apologize in advance).
Please look that your complete code is indented correctly before continuing, as Python is pretty picky about it.


Before each if you call self.findFlag(color). As you do not assign the result of this call to anything, they do nothing. You can simply delete them.


You assign the result of self.buildXY('fence', x, y) to flagGreen. self.buildXY doesn’t return anything, so flagGreen is undefined afterwards. You then try to collect it. Simply don’t assign like in the case when you build a fire-trap.


Rest looks good.


#4

I tried that, but it doesn’t work.

Here is what I tried:

loop:

flagGreen = self.findFlag("green")
flagBlack = self.findFlag("black")
flagViolet = self.findFlag("violet")

self.findFlag("black")
if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flag)
self.findFlag("green")
if flagGreen:
    pos = flagGreen.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    flagGreen = self.buildXY("fence", x, y)
    self.pickUpFlag(flag)
self.findFlag("violet")
if flagViolet:
    pos = flagViolet.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    self.moveXY(x, y)
    self.pickUpFlag(flag)

Error is: “Line 31:Pass a flag object to pick up”


#5

You are doing something equivalent to:

if flagViolet:
    flag = self.findFlag("green")
    self.pickUpFlag(flag)

Which means: if I found a violet flag, I am going to pick up the green flag (which probably does not exist).

Why you have some many self.findFlag() all over your program?
You only need the 3 findFlag() at the top. All other can be deleted.
Example:

if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    # no need flag = self.findFlag("green")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flagBlack) #just use flagBlack
```

#6

Oh. Thank You!:grinning:


#7

loop:

flagGreen = self.findFlag("green")
flagBlack = self.findFlag("black")
flagViolet = self.findFlag("violet")

self.findFlag("black")
if flagBlack:
    pos = flagBlack.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("black")
    self.buildXY("fire-trap", x, y)
    self.pickUpFlag(flag)
self.findFlag("green")
if flagGreen:
    pos = flagGreen.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("green")
    flagGreen = self.buildXY("fence", x, y)
    self.pickUpFlag(flag)
self.findFlag("violet")
if flagViolet:
    pos = flagViolet.pos
    x = pos.x
    y = pos.y
    flag = self.findFlag("violet")
    self.moveXY(x, y)
    self.pickUpFlag(flag)

Like this?


#8

It will probably run without errors.

But you still have a lot of self.findFlag(), self.FindFlag(“violet”), etc. that are not really needed

Just keep the 3 self.findFlag at the top and replace every variable flag in your program with
flagBlack, flagGreen or flagViolet depending inside which if you are.