For some reason i am getting an infinite loop and I cant work out why.
Surely the loop should end when the time == 20?
here’s the code and equipment.
# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
coins = hero.findNearest(hero.findItems())
coin = coins
if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)
# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
coins = hero.findNearest(hero.findItems())
coin = coins
if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)
while hero.pos.x > 16:
# Retreat behind the fence
hero.say("I should retreat")
hero.move({"x" : 17 , "y" : 38 })
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)
which while am i changing?
i tried the top one and the hero then cant decide what to do.
if i change the bottom one he just builds the fence before picking up coins.
# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
coins = hero.findNearest(hero.findItems())
coin = coins
if coin.value == 3 or coin.value == 2:
hero.move(coin.pos)
if hero.pos.x > 16:
# Retreat behind the fence
hero.move({"x" : 17 , "y" : 38 })
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)
Ok so the hero now picks coins up but for some reason he wont move behind the barrier before building the fence in the gap?
# You only have 20 seconds until the ogre horde arrives!
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
if hero.pos.x > 16:
# Retreat behind the fence
hero.move({"x" : 15 , "y" : 36 })
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)