Help with Black Diamond

Can anyone tell me where I am going wrong with this please? My hero picks up the first 3 gems but then runs into the traps.

while True:
    gem = hero.findNearest(hero.findItems())
    if gem:
        clear = hero.isPathClear(hero.pos, gem.pos)
        
        if clear:
            hero.move(gem.pos)
    
        else:
            hero.moveXY(40, 36)

I solved it! I needed to use an object literal in the last line - so that now reads as below.

Can anyone explain why using an object literal worked when the x and y coordinates did not?


        else:
            hero.move({'x' : 40, 'y' : 36})
1 Like

it’s because when you use hero.move, the statement will run for one step before finishing, meaning that a conditional will instantly update after the statement ran.

compared for moveXY which will only finish after the hero has moved all the way back.

This is important because when you use hero.move the hero will find a clear path to a gem while walking back, and will instantly change course. Comparing this to moveXY, the hero will literally just move into the bear traps before changing course even if it sees a new gem.

2 Likes

you didn’t welcome him

Hi @Lulee1! Welcome to the CodeCombat Discourse! This is a safe place to chat and ask for level help. Before you begin your journey, could you please go through our rules that make this discourse an awesome place? It helps us all :grin: We hope you enjoy your stay!!

2 Likes

Thank you - that is really helpful.

1 Like