People generally like using max(K={24,25,26,34 as the de facto arrow key test.

I've put forward K=34 or 2>abs(K-25, which is three bytes less, but it never seems to stick.

Maybe they'd prefer instead to use not(fPart(1806/(5K-127? I think it has enough of a coolness factor.

It's only one byte smaller than the first one, but it's also faster to execute by 12 seconds over 10,000 iterations.

(It's still slower than the second one by 20 seconds over the same number of passes.)

I won't post the Mathematica code that I whipped up to find this, but it did involve PrimeQ. Hooray PrimeQ!
Very nice, Weregoose! I can't say I've ever used an arrow-key getkey loop; I usually just loop until any key and then apply the standard two-line code to increment or decrement the x and y variables based on the arrow keys pressed, also checking what happened with enter and escape type keys simultaneously.
Do pardon my noob-likeness.

What does an arrow-key getkey loop look like? I've never seen or used one so I don't exactly know how they work. Bad Idea
swivelgames wrote:
Do pardon my noob-likeness.

What does an arrow-key getkey loop look like? I've never seen or used one so I don't exactly know how they work. Bad Idea
the classic arrow movement code being used often is generally

X-(K=24)+(K=26->X
Y-(K=25)+(K=34->X

Or even less optimized

If K=24
X-1->X
If K=26
X+1->X
If K=25
Y-1->Y
If K=34
Y+1->Y
Gotcha. Sorry, it took a minute but I understand how that works. I didn't quite break it down in my head before I kinda just stared at it. Laughing

And weregoose's check would just be slapped on an IF statement before reassigning the coordinate variables?

EDIT
Example:

Code:
:1->X:1->Y
:ClrHome
:Lbl A
:getKey->K
:If K=34 or 2>abs(K-25
:Goto B
:Goto A
:Lbl A
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:Output(Y,X,"X
:Goto A
swivelgames wrote:
Gotcha. Sorry, it took a minute but I understand how that works. I didn't quite break it down in my head before I kinda just stared at it. 0x5

And Weregoose's check would just be slapped on an IF statement before reassigning the coordinate variables?

EDIT
Example:

Code:
:1->X:1->Y
:ClrHome
:Lbl A
:getKey->K
:If K=34 or 2>abs(K-25
:Goto B
:Goto A
:Lbl A
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:Output(Y,X,"X
:Goto A


Maybe something like this, Swivelgames (to avoid that DISGUSTING abuse of labels):


Code:
:1->X:1->Y
:ClrHome
:Repeat K=34
:Repeat K=34 or 2>abs(K-25
:getKey->K:End
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:Output(Y,X,"X
:End
Haha, I never was very good at optimizing source Laughing

Thanks, Kerm! XD
swivelgames wrote:
Haha, I never was very good at optimizing source 0x5

Thanks, Kerm! XD
No worries. Smile I'm sure that Weregoose could still own that code in some strange, eyelash-bending way. When in doubt, avoid Labels/
KermMartian wrote:
I'm sure that Weregoose could still own that code in some strange, eyelash-bending way.

Probably. "eyelash-bending"? Smile

KermMartian wrote:
:1->X:1->Y
:ClrHome
:Repeat K=34 // I think you mean "0"
:Repeat K=34 or 2>abs(K-25
:getKey->K:End
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:Output(Y,X,"X
:End


Btw, Kerm, your code would show a blank screen until the user pressed a button.
rthprog wrote:
KermMartian wrote:
:1->X:1->Y
:ClrHome
:Repeat K=34 // I think you mean "0"
:Repeat K=34 or 2>abs(K-25
:getKey->K:End
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:Output(Y,X,"X
:End


Btw, Kerm, your code would show a blank screen until the user pressed a button.
Good catch.


Code:
:1->X:1->Y
:ClrHome
:Repeat K=45
:Output(Y,X,"X
:Repeat 5.5=abs(K-39.5) or 2>abs(K-25
:getKey->K:End
:Output(Y,X,"
:X-(K=24 and X>1)+(K=26 and X<16->X
:Y-(K=25 and Y>1)+(K=34 and Y<8-Y
:End
Sorry if this seems obvious, but I still haven't figured out which one I should use: using the overall loop to check for getKeys, as in


Code:
ClrHome
1->I
Output(1,1,">
Repeat K=105
getKey->K
If K
Output(I,1,"_
I-(K=25)+(K=34->I
I+4not(I)-4(I=4->I
Output(I,1,">
End

or having a loop for just the getKey, as below?


Code:
ClrHome
1->I
Output(1,1,">
Repeat K=105
Repeat K
getKey->K
End
Output(I,1,"_
I-(K=25)+(K=34->I
I+4not(I)-4(I=4->I
Output(I,1,">
End

I don't mean with this code specifically, but in general (e.g. in a platform game, when testing for keypresses to move the character).
Well, generally the first one is better, because the program will be that much faster to respond once you actually press a key (for example, if you press a key when it's at the "I-(K=25)+(K=34->I " line in the first program, it will have to loop all the way around to If getKey again before it starts processing the key). Also, that first chunk of code has a bug, it should be If K (or If Ans), not If getKey. In the second piece of code, you're running a much tighter loop around the getKey, so it will respond faster when you begin pressing keys. Of course, if you were constantly holding down keys, the second program would be ever so slightly slower, but that's not generally the case. If you have a more complex game where other things are moving and changing while the getKey loop is running, it gets somewhat more complicated, but the same rules of thumb still hold.
use multiple loops if you're looking for speed (which is usually more important than the relatively minor increase in file size).


Code:
:While 1

:While Key = null
:getKey
:End

:While Key=true
:action
:End

:End
Even better than using multiple while loops is to just use a Repeat loop with Ans as the condition. I have not done any calculator programming in a while, but I am fairly certain using Ans is faster than storing to a variable, also. Could anyone confirm this?


Code:
While 1

Repeat Ans
getKey
End

[ACTION]

End
Well, I suppose that could make sense. But if you're just going to use the variable later, is it not faster to store it in the loop instead of calling it twice (one inside the loop, and one after)?
Harq wrote:
Even better than using multiple while loops is to just use a Repeat loop with Ans as the condition.

I still think that 2 loops would be faster, especially in situations like text entry. The idea is that rather than wait for the next keypress, assume that the user will press the next key almost instantly.

swivelgames wrote:
Well, I suppose that could make sense. But if you're just going to use the variable later, is it not faster to store it in the loop instead of calling it twice (one inside the loop, and one after)?


Not really, because then while in the getkey loop, the calculator will be storing a value to K repeatedly, making it ever so slightly less responsive. You'll notice this especially if you have something else going on in the loop.

For example, if I put a time limit on it, the first example (which uses Ans) will be significantly faster/more responsive than the second.


Code:
:DelVar E
:Repeat Ans or E<100
;E+1-->E
:getKey
:End
:Ans --> K


Code:
:DelVar EDelVar K
:Repeat K or E<100
;E+1-->E
:getKey --> K
:End
Harq wrote:
Even better than using multiple while loops is to just use a Repeat loop with Ans as the condition. I have not done any calculator programming in a while, but I am fairly certain using Ans is faster than storing to a variable, also. Could anyone confirm this?


Code:
While 1

Repeat Ans
getKey
End

[ACTION]

End


If you don't need the key value at all, such as for a "press any key to continue" screen

Code:
Repeat getKey
End
Most definitely, elfy. I've used that plenty of times before. Works perfectly Razz

Harq, gotcha! That's one thing I was thinking about but I wasn't sure how greatly that affected it. Makes sense, though Smile
rthprog wrote:
use multiple loops if you're looking for speed (which is usually more important than the relatively minor increase in file size).


Code:
:While 1

:While Key = null
:getKey
:End

:While Key=true
:action
:End

:End


So if I were to make a simple example out of an X moving about the screen, this is what I think you mean:


Code:
:1→A
:1→B
:Output(1,1,"X
:While 1
:Repeat K
:getKey→K
:End
:While K
:Output(A,B,"_
:A-(K=25 and A-1)+(K=34 and A-8→A
:B-(K=24 and B-1)+(K=26 and B-16→B
:Output(A,B,"X
:End
:End


Never thought of that. I'll try it.

EDIT: WOW THAT IS FAST. How did I never learn that?
Deep Thought wrote:


Code:
:1→A
:1→B
:Output(1,1,"X
:While 1
:Repeat K
:getKey→K
:End
:While K
:Output(A,B,"_
:A-(K=25 and A-1)+(K=34 and A-8→A
:B-(K=24 and B-1)+(K=26 and B-16→B
:Output(A,B,"X
:End
:End


Never thought of that. I'll try it.

EDIT: WOW THAT IS FAST. How did I never learn that?


Shouldn't it be this? It doesn't look like there is any way that K would become 0 in that second inner loop.


Code:
:1→A
:1→B
:Output(1,1,"X
:While 1
:Repeat K
:getKey→K
:End
:Output(A,B,"_
:A-(K=25 and A-1)+(K=34 and A-8→A
:B-(K=24 and B-1)+(K=26 and B-16→B
:Output(A,B,"X
:End
  
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 2
» 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