views:

27

answers:

1

I'm writing a snake game in TI-Basic, and every time I move I need to see if the head of the snake has hit any point in the tail. The tail is stored as a circular list-based queue, and I can add the beginning and end in constant time.

The only hard part is that I have to do something similar to this on every iteration: (S = Size of the list)

For(I,1,S)
    If X=LX(I) and Y=LY(I)
    Then
        Disp "GAME OVER"
        Return
    End
End

It's a fairly short loop, but it takes forever even on a list of 10 items. I tried the sequence way:

If sum(seq(X=LX(I) and Y=LY(I),I,1,S))
...

The only other optimization I can think of is to not check values for N to N+2 (because the first part of your tail that's possible to hit is at N+3), but that just puts off the problem after 4 points, and having the game unplayable with 14 points is no better than being unplayable after 10 points.

Using assembly isn't an option because I don't have a link cable (or the desire to write assembly).

+3  A: 

Never used TI-Basic...

but how about also storing a 2D array of the game board. Each element in that array indicates if the snake is present. When you move forward, set the value of the array at the head point, and clear the value at the old tail end point. Then to test for collision, you can just do one lookup into the 2D array.

Detmar
That's a really good idea, I'll have to try it tomorrow.
Brendan Long
Actually just wrote it up and it works perfectly. Thanks a lot.
Brendan Long