Omnimaga
Calculator Community => TI Calculators => TI-BASIC => Topic started by: Sorunome on July 24, 2013, 11:53:09 am
-
Hey, I was just curious, what is the fastest/most optimized way to move a character around the home screen?
what I currently have is this:
ClrHome
1->A
1->B
Repeat C=45
Output(B,A,"*")
Repeat C
getkey->C
End
Output(B,A," ")
max(min(A-(C=24)+(C=26),16),1)->A
max(min(B-(C=25)+(C=34),8),1)->B
End
-
You can replace variables with Ans in a few places.
ClrHome
1→A
Ans→B
Repeat C=45
Output(B,A,"*")
Repeat Ans
getKey→C
End
Output(B,A," ")
max(min(A-(Ans=24)+(Ans=26),16),1)→A
max(min(B-(C=25)+(C=34),8),1)→B
End
-
You can also remove some of the closing parentheses but that won't affect speed at all, sadly.
-
You can replace variables with Ans in a few places.
why didn't i think of ans x.xYou can also remove some of the closing parentheses but that won't affect speed at all, sadly.
i know that, but idk why i put 'em in in this prog....
-
Personally, I would organise it as:
ClrHome
1→A
Ans→B
Repeat C=45
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
End
But that is just size optimsation with probably a negligible speed boost. That is pretty close to my typical routine :P
-
Oh, and you can also reorganize it so that the getkey loop is last. That way, you can replace the Repeat C=45 with Repeat Ans=45.
ClrHome
DelVarC1→A
Ans→B
Repeat Ans=45
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
Output(B,A,"*
Repeat Ans
getKey→C
End
End
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
EDIT:
And let's not forget the ultimate tool for the guy who desperately wants to use Ans, and Ans only... LISTS! I'm not sure if a list in Ans is faster than variables accessed normally... Probably the list is slower... anyone want to test this code? My calc is bricked...
ClrHome
{1,1,0
Repeat 45=Ans(3
Output(Ans(1),Ans(2),"*
Repeat Ans(3
{Ans(1),Ans(2),getKey
End
Output(Ans(1),Ans(2),"
{max(1,min(16,Ans(1)-(Ans(3)=24)+(Ans(3)=26))),max(1,min(8,Ans(2)-(Ans(3)=25)+(Ans(3)=34))),Ans(3
End
Oh gahwd, this is messy. It's definitely not faster lol.
-
um, why is there a DelVar C, you don't need that.
Also, that is some pretty nice optimizing :D
-
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
-
I still have never really figured out how Repeat works... I do this:
ClrHome
DelVar K
1->X
1->Z
While K!=45
getkey->K
If K!=0:Then
ClrHome
X+(K=26 and X<16)-(K=24 and X>0)=->X
Z+(K=34 and Z>0)-(K=25 and Z<8)->Z
Output(X,Z,"X
End
End
ClrHome
It is less efficient and probably slower... D: Can someone help me understand how to use Repeat?
-
Repeat executes the inner body at least once, because it checks whether its condition is true after running each iteration of the body. While doesn't make this guarantee.
-
Repeat executes the inner body at least once, because it checks whether its condition is true after running each iteration of the body. While doesn't make this guarantee...
... as it checks the condition first. Also Repeat executes until the condition turns true and While until it turns false. ;)
-
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
But in TI-Basic a loop is alwys entered at least once (I thought), so even if C is 45 in the beginning it'll enter the loop and'll be overwritten by getKey->C
Also, just tested it and i'm right, it enteres the repeat loop once in the beginning
-
Yes, as was said before, Repeat works like this:
Lbl 1
<code code code>
If K≠45
Goto 1
While works like this:
Lbl 1
If K=45
Then
<code code code>
Goto 1
End
I hope that helps a little. I personally prefer Repeat loops over While loops in most cases.
-
um, why is there a DelVar C, you don't need that.
But that requires a DelVar of C in case C somehow had a value of 26 or 34... x.x
If C=26 or C=34 before launching the program, then it'll start off in the wrong place, having already moved. lol, doesn't really matter, but just in case.
But in TI-Basic a loop is alwys entered at least once (I thought), so even if C is 45 in the beginning it'll enter the loop and'll be overwritten by getKey->C
Also, just tested it and i'm right, it enteres the repeat loop once in the beginning
No, I didn't mean that it will quit early if C=45. I meant C=26 or C=34. Do 26->C or 34->C before executing my code (without the DelVar). I'm almost positive that the * will NOT be in the upper left corner, it will be shifted right or down.
...and after testing, 26->C doesn't do this, as my code changes A with Ans, which is initialized as 1. But do a 34->C before executing my code. The * will start out at 2,1 rather than 1,1. That's what I was saying, not that it would quit.
-
No, again same thing (just tried it out), as xeda pointed out with lables.
You enter the inner loop at least once, the keypress is stored an there you go, C is again 0 until you hit a key.
-
But C is evaluated before the getkey.
Repeat Ans=45
Output(B,A,"
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B //C is evaluated here! If C is 25 or 34, the thing will move
Output(B,A,"*
Repeat Ans
getKey→C //and here C is being reset
End
End
That means the first time it may happen that the thing moves without a key being pressed because C was not cleared before. It's unlikely because quitting the loop sets C to 45, but if there was a different part of the program that uses C, it may happen (or the program was quit with on before).
-
oh, right, i forgot that optimization.....i was still at the one where the inner loop was before evaluating.
Thanks for clarifying MGOS
EDIT: wouldn't it be quicker to do Ans->C then instead of DelVar C?
-
Oh, so we were talking about completely different codes lol.
I was interested in whether Ans would be faster than DelVar or not.
For(A,1,500
DelVar CEnd
vs
For(A,1,500
Ans->C
End
Anyone wanna time this? I would do it, but my calc is bricked...
-
The first code takes about 3,2secs whereas the second takes 5,7 secs on my calc, so it would be a great optimisation to use "Delvar C" I think
-
Coudln't we also optimize it to Delvar A,B,C then?
-
I think so Sorunome
-
Coudln't we also optimize it to Delvar A,B,C then?
I think so Sorunome
Nope. ERR:Syntax.
-
You could do
DelVar ADelVar BDelVar C
That's the same size (since you don't need new lines in between), but be aware that the homescreen coordinates start at 1,1 in TI-Basic instead of 0,0.
another
Delvar AA+1->A
would be slower again slower than just 1->A
so it wouldn't be an optimization.
-
Oh, I forgot that part x.x
-
You could do
DelVar ADelVar BDelVar C
That's the same size (since you don't need new lines in between)
Nope, "DelVar " is a two-byte token and "," is a one-byte token.
Also, I understand that you want to optimize speed-wise what's inside the loop to get the fastest movement, but the initialization (what is before the loop) should be optimized for size, shouldn't it ?
-
Actually, you CAN do the DelVars. The min() and max() reset it, remember? All we need to do is move the erasing Output to the end of the loop, so that it doesn't give an error for out of bounds coordinates.
Like so:
DelVar ADelVarBDelVarCClrHome //I don't think commas work for DelVars.
Repeat Ans=45
max(1,min(16,A-(Ans=24)+(Ans=26→A
max(1,min(8,B-(C=25)+(C=34→B
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A,"
End
-
Actually, you CAN do the DelVars.
Yeah, my "Nope" was not a "Nope, it doesn't work", but a "Nope, it doesn't take the same size" since the DelVar always take one more byte than the period.
-
Oh, I was responding to MGOS about the DelVarA vs the 1->A.
-
And further, you can remove the DelVar C because the maximum the numbers can become is 1, and the minimum is -1, but is overwritten in the bound check.
-
What again?
You need the DelVar C otherwise the thing can move right/down when first looping.
-
Ah, but with the most recent code, with your optimization of using DelVar ADelVar B instead of 1->A, A and B are now 0. It would've caused some problems with that first erasing output, but I moved that to the bottom. Now the first line in the loop is the min(max(stuff)), which will move it to 1. And even if C=34 or something, max(1,0+1) = 1.
DelVar ADelVar BClrHome
Repeat Ans=45
max(1,min(16,A-(Ans=24)+(Ans=26→A // If Ans=24, max(1,min(16,0-1))=1. If Ans=26, max(1,min(16,0+1))=1. If otherwise, max(1,min(16,0))=1 still. Yay.
max(1,min(8,B-(C=25)+(C=34→B //Same applies for C. The max(1,X) guarantees it, and putting A and B as 0 with DelVar makes it so that the maximum it can be from the beginning is 1, which is fine. :)
Output(B,A,"*
Repeat Ans
getKey→C
End
Output(B,A," //This would've caused some problems - it would've given out of bounds. But I moved it to the end.
End
:) Test it!
-
oooooooh, i get it now, that is pretty clever! :D