Recently I have been teaching a kid at school Ti-Basic. I am now teaching him about optimizing. I made a simple 'game' in 96 bytes but wondering if I could make it smaller. The 'game' moves a '*' around without letting it leave the homescreen boundaries. Yeah I know very simple. Here is what I have:

Code:

:1->A:1->B
:Repeat K=45
:getkey->K
:ClrHome
:Output(B,A,"*
:A+(K=26 and A!=16)-(K=24 and A!=1)->A
:B+(K=34 and B!=8)-(K=25 and B!=1)->B
:End
//!= not equals to

Thanks

Code:

:1->A:1->B
:Repeat K=45
:getkey->K
:ClrHome
:Output(B,A,"*
:A+(Ans=26 and A!=16)-(Ans=24 and A!=1->A
:B+(K=34 and B!=8)-(K=25 and B!=1->B
:End
//!= not equals to

Code:

:1->A:1->B
:Repeat K=45
:ClrHome
:Output(B,A,"*
:getkey->K
:A+(Ans=26 and A!=16)-(Ans=24 and A!=1->A
:B+(K=34 and B!=8)-(K=25 and B!=1->B
:End


Saved two bytes and made a slight speed optimization. I know there is more to be saved but that is what I saw with a quick glance.

Edit: Blue_bear beat me too it, then again I took the time to say what I did.
Oops sorry, I accidentally wrote those closing parentheses... just going off of memory. looking at my code I didn't have those :p
In floating point operations, does "While K-45" save speed over "Repeat K=45"? I know that it saves time and speed in Axe.
I know repeat K=45 is pretty standard.. I have seen Repeat in every program that uses sprite movement. as I understood it, they are the same speed.
Aes_Sedia5 wrote:
I know repeat K=45 is pretty standard.. I have seen Repeat in every program that uses sprite movement. as I understood it, they are the same speed.


In which case Repeat K=45 should be used, as it is more understandable...
What if K is already 45?
Repeat evaluates the conditional at the end of the loop, rather than the beginning. See http://tibasicdev.wikidot.com/repeat. While does it before, so doing "While K-45" could cause the loop to not run. See http://tibasicdev.wikidot.com/while
Repeat loops check their condition at the bottom of the loop instead of at the top like While. Consequently a Repeat loop will always execute at least once, which makes it useful for situations like this where we don't care what K was before the loop.

EDIT: Cemetech needs a Ninja notifier like Omni has Laughing
Yeah the getkey resets K to zero the first time through. I tried while loop, and a for( loop the latter taking the most bytes.

Code:

:for(K,0,45
:getkey->K
:end
Indeed, that's a quite clever way to do it, but sadly not sufficiently optimal for all its slyness. I know I'm beating my own drum too much about this, but I feel I wrote up a fairly thorough guide to optimization as Chapter 10 of my book, and I detailed things like the differences between While and Repeat and the proper places to use each. At any rate, I'd say that the code as presented is a quite nice little program, and about as optimized as you'd want to or be able to get for something like this.
This is a little more optimised:

Code:

:1→A:1→B
:Repeat K=45
:getKey→K
:If Ans                      ;Added this in so as not to be blinking so much.
:ClrHome
:Output(B,A,"*
:max(1,min(16,A+(K=26)-(K=24→A
:max(1,min(8,B+(K=34)-(K=25→B
:End
Mmm, that is indeed better. I had held off on the min-max form for the sake of possible confusion, but you're absolutely right that it's smaller. Out of curiosity, have you checked what the relative speed of that is? In this case I'd expect it to be a negligible difference, I suppose.
I don't have the timings, but I do imagine it is negligible. min() and max() are fairly fast to compute.
hmmm, I have never used max( or min( before. What is the syntax?
zeldaking wrote:
hmmm, I have never used max( or min( before. What is the syntax?
min(X,Y) will find and return the smaller of X and Y; max(X,Y) will find and return the larger of X and Y. If you work with lists instead, min({a,b,c,...z}) or min(L1) will return the smallest value of the list, and symmetrically, max(list) will return the largest value in the list.
  
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