Level and Method Help - Ogre Gorge Gouger

# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
    # Collect coins
    hero.say("I should pick up a coin")
    while hero.gold < 60:
        nearest = hero.findNearest(hero.findItems())
        hero.move(nearest.pos)
    
while hero.pos.x > 16:
    # Retreat behind the fence
    hero.say("I should retreat")
    hero.move({'x' : 16, 'y' : 38})
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 36)

I’m not really looking for general level help because I won’t get it unless I understand the concepts here.

This is one of the first levels using the .move() method. .move() takes an object literal (a dictionary is the vernacular I’m familiar with) and in the last few levels previous to this one, I’m wondering why code like this:

while hero.pos.x < 40:
    hero.move({'x': 40, 'y': 35})

works fine but my OgreGorgeGouge script is not working. Essentially, it seems to me as though I have the same thing happening in both scripts.

My only guess would be the first statement while hero.pos.x > 16:(entered by default) can easily be a infinite loop. I know .moveXY() stops execution of your program until your hero gets to the positions passed in but I’m not sure I understand how my hero only takes a few steps when I am calling .move().

I know object literals are quite important and I’ve been stumped for a bit on this levels so any and all help is appreciated. Please let me know if there is any other information I can provide.

1 Like

Take a screenshot of your equipment. Thx

1 Like

gear

1 Like

remove the hero.say commands, they just make you slower, also you don’t need to loop until you have 60 coins, just loop while the time is less than 20

2 Likes

actually, when you use the move command, the hero moves, so his x position changes, so it is impossible to be an infinite loop.

your hero only takes one step to the target pos when you call the move command, you can think of it as the hero moving 1 unit in the direction of the position.

3 Likes

the main difference between moveXY and move methods is that when executing the moveXY method, the hero will not do anything else until this is completed. The code doesn’t move to the next line and no other line of code is considered until the hero is at the x,y coordinate of the position. With the move command, the hero takes a single step toward the target coordinates and then the code iterates through to see if there’s anything else it should, or could be doing. In some situations, one is better to use than the other. In other situations, it doesn’t make any significant difference.

2 Likes

you can use move with a while loop that has nothing else than the move command and it will be the same as moveXY, so actually move is better than moveXY, but moveXY only cuts out some code

2 Likes

Thanks a lot guys! I think I’m starting to get the hang of it. Sometimes when I think I know how something works, its better to see how other people would explain it so all of this has put this into perspective for me.

2 Likes