Hey, I have a ti-83 plus, and i made this program. Unfortunately, it always lags out and I always get ERR:MEMORY after a few "bombs" have dropped.
I need suggestions to make it more efficient.
Btw, I'm far from a complex programmer, Sad so plz explain any higher than average program suggestions.


Code:

:ClrHome
:Output(1,4,"CALCULATOR"
:Output(2,6,"BOMBER"
:Output(7,4,"PRESS ENTER"
:Pause
:ClrHome
:Output(1,5,"CONTROLS"
:Output(3,1,"LEFT AND RIGHT"
:Output(4,1,"ARROWS MOVE"
:Output(6,1,"EXIT- CLEAR"
:Output(7,1,"DROP BOMBS- 2ND"
:Pause
:ClrHome
:7→X
:0→S
:Lbl 2
:Output(1,12,"     "
:Output(8,14,"   "
:1→W
:Lbl 1
:While 1
:Output(2,X,"(*)"
:Output(8,W," <=>"
:W+1→W
:If W=14
:Goto 2
:getKey→K
:If K=24
:Goto LT
:If K=26
:Goto RT
:If K=21
:Goto BO
:If K=45
:Goto EX
:End
:End
:Lbl LT
:X-1→X
:If X<1:Then
:14→X
:Output(2,1,"                "
:Goto 1
:Else
:Output(2,1,"                "
:End
:End
:Lbl RT
:X+1→X
:If X>14:Then
:1→X
:Output(2,1,"                "
:Goto 1
:Else
:Output(2,1,"                "
:End
:End
:Lbl BO
:X+1→B
:3→Y
:Lbl 3
:Output(Y-1,B," "
:Output(Y,B,"*"
:Output(8,W," <=>"
:Y+1→Y
:W+1→W
:If W=14:Then
:Output(8,14,"   "
:1→W
:Goto 3
:Else
:If Y=8:Then
:Output(Y-1,B," "
:Goto 4
:Else
:Goto 3
:Lbl 4
:If B=W+1:Then
:S+1→S
:Output(1,13,"    "
:Output(1,13,"HIT!"
:Output(8,1,"                "
:Output(1,1,"SCORE"
:Output(1,7,S
:1→W
:Goto 1
:Else
:Output(1,12,"     "
:Output(1,12,"MISS!"
:Goto 1
:End
:End
:Lbl EX
:ClrHome
:Output(1,1,"QUITTED"
I added [code] tags to make it easier to read.
The main problem is that you've introduced a classic memory leak. Each of the following causes some memory to be "leaked" - ie, used without later being freed - when the Goto is executed:


Code:
:If blah:Then
:Goto AA:End



Code:
:If blah:Then
:stuff
:Else
:Goto AA
:End



Code:
:For(X,1,999
:Goto AA
:End


You get the idea. Would you like me to suggest a restructuring of the program to remove the memory leaks, or do you want to try it yourself first?
I'm noticing some Goto calls within If:Then Statements. Try to avoid that. If I recall correctly, calling out of an If:Then Statement wastes memory as the calculator will never reach the :End of that statement (unless you call back into that loop I suppose.

I'm seeing some optimizations (all those Output(Y,X," " can be satisfied with an algorithm I'd imagine, from short to long.) but I no longer have the knowledge I once did so I can't be of much help there at the moment.
Umm, i don't get what code u posted there. Is that an example of a leak or an example of how not to leak?

But yes, i will want to try and edit the code before i get any full reconstructions of it
Yea, i tried taking goto's out, noticing that they caused "lag" or "memory leak"

Could some1 give an example of how to take out a goto in an if then? because i can't figure out how
ahem, please do not double post within a 12-24 hour period...

a memory leak is caused when you use a goto to exit out of a loop, thus causing some memory to not be "released"
What do u mean by a double post?
Qazz, the memory leak definition has already been explained Wink

HoboNerd: A double post is two posts in a row by the same user within a certain amount of time. Within that period (usually before 24 hours), it's more polite to edit your post, than to make a new post. If, after a day, no posts have been made by others, you may "bump" your post.

HoboNerd wrote:
Umm, i don't get what code u posted there. Is that an example of a leak or an example of how not to leak?
All the code segments from KermMartian are examples of Memory Leaks. I'd offer a way to avoid them, but my knowledge is dwindling P:
you posted right after you first post with no one posting inbetween, use the "edit" button
from experience, i 'd suggest removing your Goto statements period. Basic programs run a lot faster without them. Though a little complicated, its always better to use while and repeat loops.
though, dont cast them off forever, just dont over use them
Actually, it's not that complicated. All you need to do is restructure your conditional statements so that you only have something like If Blah:Goto AA. As comicIDIOT correctly judged, all the examples I gave are what not to do.
Kerm, how would you correctly use Goto's in a statement like this?

Code:
:If Y=8:Then
:Output(Y-1,B," "
:Goto 4
:Else
comicIDIOT wrote:
Kerm, how would you correctly use Goto's in a statement like this?

Code:
:If Y=8:Then
:Output(Y-1,B," "
:Goto 4
:Else
Well, for that you could try to break it up into two If statements, which would work fine:


Code:
:If Y=8:Output(Y-1,B," "
:If Y=8:Goto 4
[the stuff after else can go here, because anything Y=8 already jumped away]
qazz42 wrote:
though, dont cast them off forever, just dont over use them

I don't know about that. You can usually tell whether or not a programmer is a beginner, by how much he uses Labels. Most advanced programmers don't ever use them, but would rather use subprograms.

Hobonerd, when you use a Goto statement, there's almost always a better, faster option than your Goto statement. Try thinking of how to make your program so that it can continuously flow through the code, without having to jump from Gotos to Labels.

If that doesn't help much, a good tip I heard a while back was to make many subprograms, that are named what they will do, then to call the subprograms as you would use that code. Example:

Code:

:While 1
:Repeat K=21 or K=24 or K=25 or K=26 or K=34
:getKey→K
:End
:If K=24 or K=25 or K=26 or K=34
:Then
:prgmREMVCHAR
:prgmMOVECHAR
:prgmDISPCHAR
:End
:If K=21
:Then
:prgmUSESWORD
:End
:End

Of course in the long run, a programmer usually doesn't want any subprograms, but it is useful for getting the program to work before you start distributing it to friends and what-not.

The advantages of using subprograms like this rather than Labels, is that subprograms will always return to the program that called the subprogram, exactly where the subprogram was called. So that means when I use prgmDISPCHAR, then it does the code in DISPCHAR, then it will return to the main program just after where the main program called DISPCHAR.

I (and I assume most everyone else here) don't want to optimize your program for you, as you learn more when you do it yourself. See if you can optimize it yourself, without needing someone to tell you what to add and what to take away.
MufinMcFlufin wrote:
qazz42 wrote:
though, dont cast them off forever, just dont over use them

I don't know about that. You can usually tell whether or not a programmer is a beginner, by how much he uses Labels. Most advanced programmers don't ever use them, but would rather use subprograms.
As an advanced programmer, I disagree entirely, if you don't mind. There's a lot of things that Lbls are good for, but you just need to know enough to know when you're using them properly or when it would be better to use a different construction.

@HoboNerd: I'm taking quite a bit of time at the moment to try to optimize your program; it's going to have a lot of new stuff for you to look at when I re-post it.
Make sure to comment it and dissect it P: So we know what's happening!
KermMartian wrote:
MufinMcFlufin wrote:
qazz42 wrote:
though, dont cast them off forever, just dont over use them

I don't know about that. You can usually tell whether or not a programmer is a beginner, by how much he uses Labels. Most advanced programmers don't ever use them, but would rather use subprograms.
As an advanced programmer, I disagree entirely, if you don't mind. There's a lot of things that Lbls are good for, but you just need to know enough to know when you're using them properly or when it would be better to use a different construction.

@HoboNerd: I'm taking quite a bit of time at the moment to try to optimize your program; it's going to have a lot of new stuff for you to look at when I re-post it.


hehe, I remember when I was telling Kerm off for using lbls, I got reprimanded after...

seriously, TI-Mail uses some lbls, BUT I use them efficiently, not crowding my prgm with them...
KermMartian wrote:
MufinMcFlufin wrote:
qazz42 wrote:
though, dont cast them off forever, just dont over use them

I don't know about that. You can usually tell whether or not a programmer is a beginner, by how much he uses Labels. Most advanced programmers don't ever use them, but would rather use subprograms.
As an advanced programmer, I disagree entirely, if you don't mind. There's a lot of things that Lbls are good for, but you just need to know enough to know when you're using them properly or when it would be better to use a different construction.

:P
No offense taken. Yeah, I occasionally use Labels myself, but I don't use them in the way that HoboNerd uses them. I actually use them in my subprograms to quickly return from them without causing memory leaks as would happen with Return in a logic block.

KermMartian wrote:
@HoboNerd: I'm taking quite a bit of time at the moment to try to optimize your program; it's going to have a lot of new stuff for you to look at when I re-post it.

Aww man, no "make them learn" sort of mentality?
  
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