This is my second game and I think it's much cleaner than my first one, so let me know what you think of my version of Snake! And, if you have any suggestions for another classic game I could try to make I would love to hear them! (please be reasonable...)

Code:
```ClrHome Lbl T If B>|LHIGH(1    B->|LHIGH(1 Output(4,8,"Highscore: " Output(4,20,|LHIGH(1 Wait 1 ClrHome DelVar KDelVar ADelVar BDelVar DDelVar E 1->dim(L1 1->dim(L2 randInt(1,10)+100(randInt(1,26->C While K!=23    If C=D or not(D    Then       B+1->B       Output(10,1,B       Repeat not(max(L1=L2          randInt(1,10)+100(randInt(1,26->D          Fill(D,L2       End       Output(remainder(D,100),remainder(round(D/100,0),100),"O"    End    getKey->K    If K    Then       If K=25 and A!=1          ~1->A       If K=34 and A!=~1          1->A       If K=24 and A!=100          ~100->A       If K=26 and A!=~100          100->A    End    For(E,dim(L1),1,~1       L1(E->L1(E+1    End    B->dim(L1    C->L1(1    dim(L1->dim(L2    If C+A>2699 or remainder(C+A,100)>10 or C+A<100 or remainder(C+A,100)<1    Then       Goto T    Else       C+A->C    End    Output(remainder(L1(dim(L1)),100),remainder(round(L1(dim(L1))/100,0),100)," "    Output(remainder(C,100),remainder(round(C/100,0),100),"X"    Fill(C,L2    If max(L1=L2) and L1(dim(L1))!=L2(1) and A!=0       Goto T End ClrHome ```
I could never make a working snake in basic and here this is! Very cool!
Thanks bikmin! It took me a while to figure out how to do the trail behind the snake.
I would try it but I only own the monochrome calculator I'm not quite sure if this is still thing in CE but I assume they kept how TI-BASIC works in non colored version, so here we go.

If you're interested in size optimization, for multiple getKey handling, instead of doing this:

Code:
```If K    Then       If K=25 and A!=1          ~1->A       If K=34 and A!=~1          1->A       If K=24 and A!=100          ~100->A       If K=26 and A!=~100          100->A    End```

You can do a slight space-saving optimization by using the fact that logics will return 1 if true and 0 if false.
Like this.

Code:
```If K (K=34 and A!=~1)-(K=25 and A!=1)-100(K=24 and A!=100)+100(K=26 and A!=~100)->A ```

(Technically "If K" can be skipped from above, but I don't remember if having that "If K" saves time so.)

Minor optimization that I can find are:

Code:
`If A!=0`

Doing this

Code:
`If A`

will achieve the same thing, since If A will only return false if A is 0.

Code:
```If C+A>2699 or remainder(C+A,100)>10 or C+A<100 or remainder(C+A,100)<1    Then       Goto T    Else       C+A->C    End```

You can simply do

Code:
```If C+A>2699 or remainder(C+A,100)>10 or C+A<100 or remainder(C+A,100)<1 Goto T C+A->C```

Since If without Then can take one line, it will achieve the same effect of running C+A->C when the condition is false.

EDIT: Oh, and also, just like the end brackets, you can skip the end quotations for letters if it's at the very end of the line. So instead of having "X" you can get away with "X since the line ends with it.
yeongJIN_COOL wrote:

If you're interested in size optimization, for multiple getKey handling, instead of doing this:

Code:
```If K    Then       If K=25 and A!=1          ~1->A       If K=34 and A!=~1          1->A       If K=24 and A!=100          ~100->A       If K=26 and A!=~100          100->A    End```

You can do a slight space-saving optimization by using the fact that logics will return 1 if true and 0 if false.
Like this.

Code:
```If K (K=34 and A!=~1)-(K=25 and A!=1)-100(K=24 and A!=100)+100(K=26 and A!=~100)->A ```

That wouldn't work the same that way. What if K=34 and A!=~1? Then A will be set to 0, which should never happen!
A would be 1!

If K=34 and A!=~1, then

(K=34 and A!=~1) is true so it's 1.
minus
(K=25 and A!=1) would be false so 0.
minus
100(K=24 and A!=100) would be false so 100*0 = 0
plus
100(K=26 and A!=~100) would be false so 100*0 = 0

Thus
1-0-0+0 = 1
Sorry, I meant to say if K=34 and A did =~1. Then all would be false and would set A to 0. If the user presses a key in the opposite direction that they are going, it shouldn't change anything.
Oh, sorry. I didn't realize that you wanted to keep the A's original value if none of them matches.
Then it would be something like

Code:
```(K=34 and A!=~1)-(K=25 and A!=1)-100(K=24 and A!=100)+100(K=26 and A!=~100) If Ans Ans->A```

EDIT: What this does is that it keeps the values in Ans, and test the Ans if it's not 0.
Since nothing should happen to A when none of the conditions are met, we only store the new number to A when the conditions check is not 0 (since it can only be -1, 1, -100, or 100)

EDIT2: If you still want that "If K", you can just wrap all this in If K-Then-End. It will still be smaller than testing 4 Ifs.
Ooh, I didn't think of that. Thanks! Additionally, jumping out of loops or if-then-end statements with a Lbl and Goto is strongly discouraged because it leaks memory.

Basically, the interpreter sets aside a small bit of memory to remember if you are in a loop or if-then-end block, and gobbles this memory back up when it reaches the end statement.

If you never reach the end statement (say, by jumping out of it), this memory will not be released until the program ends.

This doesn't particularly affect your program, but it's a bad habit to get into.
Wow, this is a great game!
But a few thoughts:

I decided to sit down with that one line of code everyone's been optimising
Code:
```(K=34 and A!=~1)-(K=25 and A!=1)-100(K=24 and A!=100)+100(K=26 and A!=~100) If Ans Ans->A```
and came up with this:
Code:
```For(E,1,4 {100,1,~100,~1 If A!=Ans(E) and K=23+Z+7(E=4 ~Ans(E->A End```
I know it's not optimal, and a bit slower (I guess), but it's smaller and but I liked it.

Code:
```For(E,dim(L1),1,~1 L1(E->L1(E+1 End```
(Ugh, code boxes)
Can also be turned into this:

Code:
`seq(L1(E-(E!=1)),E,1,B->L1`
This will only work If B<=1+dim(L1 (Which, by the look of your code, B can only increase by 1 every update, so this will work).
Then the code after it

Code:
```B->dim(L1 C->L1(1 dim(L1->dim(L2```
can be
Code:
```dim(Ans->dim(L2 C->L1(1```
Because Ans will have L1 in it and we already took B into account.

And don't forget that you can drop closing parentheses:

Code:
```remainder(C+A,100)<1 should be 1>remainder(C+A,100```

I really like the fact that positions are indicated with a single variable rather then 2.
I hope these help and that you can learn from them, keep up the great work! 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.

»
» All times are GMT - 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