Infinite loop in searven siege, help please!


#1

I always try to solve all by myself but I am really struggling with this one: I really do not see why the error occurs. Here the code:

loop:
items=self.findItems()
itemsIndex=0
if items:
    while self.gold < 20:
        item=items[itemsIndex]
        if item:
            self.moveXY(item.pos.x, item.pos.y)
            itemsIndex += 1
if self.gold >= 20:
    count=1
    if count == 1:
        self.moveXY(83, 78)
        count =2
    else if count == 2:
        mself.moveXY(84, 51)
        count =3
    else if count ==3:
        self.moveXY(84, 22)
        count = 0

Thanks in advace!


#2

What is the problem? You say infinite loop but you kinda wrapped the whole thing in a loop so it probably should be infinitely looping? :smile:

Shouldn’t itemsIndex=0 be outside of the loop? I mean if it’s inside, wouldn’t it reset the itemsIndex back to 0 every time?


#3

Your indentation is a bit mixed up.
Tip: Better use the Method in the FAQ to post code here, it also highlights the syntax.

Your loop is on the same line as the next one. Technically this means that you don’t order anything, forever.

Instead of

loop:
items=...

do

loop:
    items=...

#4

Your other problem is here:

items=self.findItems()
itemsIndex=0
if items:
    while self.gold < 20:
        item=items[itemsIndex]
        if item:
            self.moveXY(item.pos.x, item.pos.y)
            itemsIndex += 1

You go through the items array once (the items you can initially see at the beginning of the level), then you are done. If you can’t get 20 gold with those items, you’ll start looping forever. But not all the coins spawn at the very beginning of the level.

Probably you want to refresh items = self.findItems() each time through your while-loop, and then use item = findNearest(items) to get the right one to go after.


#5

Thanks for your help and sorry for the late answer, I have been away.

I played further and learnt some new tricks.

Now all seems to work as intended, which is the main point. Running the code I succeeded but when I submitted it I lost hahaha I will try with better equipment


#6

Please do not post correct code, for it gives other players an easy solution without providing education.


#7

OK, Editing right now!