Hey everyone! In my free time lately I've been trying to make a classic snake game, as it's always been a goal of mine. Now, as I want this program to be relatively quick, I've decided to emulate strings and their many uses instead of the slow, clunky list variables offered by TI-basic. Here's my code.

Code:

ClrHome
2→D
5→Y:13→X:1→T:Y→B:X→S:2→M
"2→Str1:0→C
Repeat K=45:getKey→K
If max(K={25,26,34,24:Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
randInt(1,10)→P
randInt(1,26)→U
Output(P,U,"+
Repeat K=45 or (X=U and Y=P) or K=105:getKey→K
Output(1,1,"D="+toString(D)+" M="+toString(M
Output(2,1,Str1
Output(3,1,"T="+toString(T
If max(K={25,26,34,24
Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
sub(sub(Str1,1,T-(T≠1))+toString(D)+sub(Str1,T+(T≠length(Str1)),length(Str1)-T+(T=length(Str1))),2(T=1)+(T≠1),length(Str1→Str1
expr(sub(Str1,length(Str1),1→M
Output(Y,X," "
Output(B,S," "
X+(D=2)-(D=3)→X
Y+(D=1)-(D=4)→Y
S+(M=2)-(M=3)→S
B+(M=1)-(M=4)→B
Output(Y,X,"O
Output(B,S,"O
If T=length(Str1):0→T
T+1→T
End
S+(M=3)-(M=2)→S
B+(M=4)-(M=1)→B
Str1+toString(D)→Str1
End

The problem is, when the user tries to move the snake sequentially within two or more game ticks (t), the snake breaks apart and flies to pieces. I have actually reasoned out the bug. The string I'm using, str1, stores all of the elements of the snake, and which direction they are moving. When an arrow key is pressed, it starts at the first element of the string (the head) and changes the direction of each following element. Now, this is where the bug comes in. If you change the direction of the snake and the elements of the string are not finished updating from the previous direction, then it stops updating the previous direction and updates the string from the first element. (line 16). I need a slice of code that will take a string, update it from the beginning, and if another update is passed while its updating, will update it again from the beginning while finishing the last update process. I know this may be very hard to understand, so I'll try and visualize it with some psuedo-code of my desired effect.
//
Repeat "condition"
detect and change direction of head.
update str1(t)
reset t to cycle through elements of string.
end
//
it should output something like this-
(say 222222 is str1)
I press left arrow, indicating that the direction is changed to 3
the program updates str1
str1 now equals 322222 and t is equal to 1
it cycles through the code
str1 now equals 332222 and t is equal to 2

see where I'm heading? this part is done.

now the string is finished updating and it equals 333333.

but say I press the up arrow, indicating a change of direction to 4.
everything proceeds as usual.
str1 equals 433333 and t equals 1.
but what if it continues to cycle, we get to this point-
str1 equals 444333 and t equals 3-
and THEN we want to change the direction again?
say we press the right arrow, changing the direction to 2.
this is where the error comes in.
the string stops updating the rest of the 3's to 4's and starts changing the entire string to 2's.

I need this solved, and believe me, I've tried. for the life of me I cannot figure this puzzle out! Any help is appreciated, I know this may be a struggle to comprehend... Very Happy
You can first start by removing ")" right before the store symbol to help w/ speed. Other than that, I can't help you more.
I don't think the bit saved will improve it that much
Hint: You can avoid storing the positions of the snake in order to make your code 1337.

I'd need to look over the code a little more to understand what you are doing Razz
you're gonna have to say that again mateo... why would I not store the positions of the snake
Jcsq6 wrote:
you're gonna have to say that again mateo... why would I not store the positions of the snake

He may be saying you just need to store the tail and face, so you then move the tail into the snake and the face in the direction you're moving.

Edit:
Like this
Your snake program is really nice, though far from being optimal (for size).And is the second snake program I've tried to optimize!)
The best optimization I could make at-a-glance is this:
Code:
If max(K={25,26,34,24:Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
should be:
Code:
sum(cumSum(K={25,24,26,34:If Ans:Then:Ans->D:1->T:End
On both line 6 and lines 14/15.

Towards the bottom, you have a bunch of lines that look something like:

Code:
S+(M=3)-(M=2)→S
which can be changed to:
Code:
S+sum(deltaList(Ans={2,3->S
And the like for the other similar cases.

Then on line 26 and 27, you have:

Code:
If T=length(Str1):0→T
T+1→T
which is more optimal as:
Code:
1+Tnot(T=length(Str1->T

Lines 18-25 can be reordered to reduce update time, but that isn't necessary.

Don't forget you can drop ending closing parentheses (even when storing to a variable).

Note: all these optimizations are for size and my cause the program to be slower.

I hope you find these useful, and if I figure out what is causing the supposed bug, or I find a way to optimize line 16, I will post.
sorry I posted 2x
LogicalJoe wrote:
Your snake program is really nice, though far from being optimal (for size).And is the second snake program I've tried to optimize!)
The best optimization I could make at-a-glance is this:
Code:
If max(K={25,26,34,24:Then:(K=34)+2(K=26)+3(K=24)+4(K=25)→D:1→T:End
should be:
Code:
sum(cumSum(K={25,24,26,34:If Ans:Then:Ans->D:1->T:End
On both line 6 and lines 14/15.

Towards the bottom, you have a bunch of lines that look something like:

Code:
S+(M=3)-(M=2)→S
which can be changed to:
Code:
S+sum(deltaList(Ans={2,3->S
And the like for the other similar cases.

Then on line 26 and 27, you have:

Code:
If T=length(Str1):0→T
T+1→T
which is more optimal as:
Code:
1+Tnot(T=length(Str1->T

Lines 18-25 can be reordered to reduce update time, but that isn't necessary.

Don't forget you can drop ending closing parentheses (even when storing to a variable).

Note: all these optimizations are for size and my cause the program to be slower.

I hope you find these useful, and if I figure out what is causing the supposed bug, or I find a way to optimize line 16, I will post.


Yes I realized all (well most... I’ve never used cumSum)of these optimizations, but it slows the program down. If loops are fast because if they return false it completely ignores the next line of code. With the explicit code you provided, the calculator has to constantly make calculations, slowing down the program.
slimeenergy wrote:
Jcsq6 wrote:
you're gonna have to say that again mateo... why would I not store the positions of the snake

He may be saying you just need to store the tail and face, so you then move the tail into the snake and the face in the direction you're moving.

Edit:
Like this


But as the good thing with storing it how I am is that it is implied the exact amount of time that the butt needs to wait before updating
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement