Can someone please correct my code for the TI-83 BASIC programming language? Whenever you play my fighting game, after a while, it will say Error, memory.


Code:
:ClrHome
:AxesOff
:ZStandard
:ClrDraw
:Text(25,35,"LOADING")
:Shade(‾3,‾1)
:ClrHome
:Output(1,1,"A PROGRAM BY")
:Output(2,2,"TRISTAN SIRRICO")
:Pause
:ClrHome
:ClrDraw
:100→A
:100→B
:0→C
:0→D
:Lbl A
:Text(1,1,"YOUR HEALTH:")
:Text(1,75,A)
:Text(16,1,"OPPONENTS HEALTH:")
:Text(16,75,B)
:Text(32,1,"HIT WHERE?")
:Text(40,1,"HIGH=1 MID=2 LOW=3")
:Lbl B
:While 1
:0→Z
:randInt(1,3)→D
:If A≤0 or B≤0
:Then
:Goto C
:End
:If Z=0
:Then
:getKey→Z
:If Z≠92 and Z≠93 and Z≠94
:Then
:Goto B
:End
:If Z=92 and D=1
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:If Z=92 and D=2
:Then
:randInt(1,20)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=92 and D=3
:Then
:ClrDraw
:randInt(1,20)→E
:B-E→B
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=93 and D=1
:Then
:randInt(1,15)→E
:B-E→B:ClrDraw
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=93 and D=2
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:If Z=93 and D=3
:Then
:randInt(1,15)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=94 and D=1
:Then
:randInt(1,10)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=94 and D=2
:Then
:randInt(1,10)→E
:B-E→B
:ClrDraw
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=94 and D=3
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:Lbl C
:If A≤0 and B>0
:Then
:ClrHome
:Disp "HE WINS"," ","GAME OVER"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
:If B≤0 and A>0
:Then
:ClrHome
:Disp "YOU WIN"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
:If A≤0 and B≤0
:Then
:ClrHome
:Disp "TIE"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
Common problem with that is you are exiting loops without letting the code encounter the proper End commands.

I shall give it a look and see what I can tell needs fixing tho..

Edit

Alright, a quick look shows you going out of a While 1 loop repeatedly with Goto.

This is bad form, and causes your problem you are seeing.

Possible solutions are to either get rid of your while loop, or get rid of the Goto/Lbl commands.
Getting rid of the while would probably be shorter, but what would be the best way to do so?
Somebody move this to the TI-BASIC subforum plzkthxbye
Done.

And actually, continuing with Goto/Lbl is going to be your worst thought, you might want to rethink going with While loops. You can utilize them in much the same way as you can with Lbl and Goto.

The reason I say this is because you have Goto commands coming before End statements. Everytime the calculator comes across a Then or loop command, it logs it within a special part of memory. That eventually fills up your available RAM and causes the problem you are facing.
tifreak8x wrote:
Done.

That eventually fills up your available RAM and causes the problem you are facing.



Thanks, that makes sense. I will rework it.
I recommend against the fake loading screen as it's not actually loading anything and just takes up time.
The problem is all the places that you are performing a Goto inside of an If...(else)...End block.

Corrected and somewhat-optimized code:

Code:
:ClrHome
:AxesOff
:ZStandard
:ClrDraw
:Text(25,35,"LOADING
:Shade(‾3,‾1
:ClrHome
:Output(1,1,"A PROGRAM BY
:Output(2,2,"TRISTAN SIRRICO
:Pause :ClrHome
:100→A:Ans→B
:DelVar CDelVar DWhile A>0 and B>0
:Text(1,1,"YOUR HEALTH:",A
:Text(16,1,"OPPONENTS HEALTH:",B
:Text(32,1,"HIT WHERE?
:Text(40,1,"HIGH=1 MID=2 LOW=3
:DelVar Z:Repeat Z=92 or Z=93 or Z=94
:randInt(1,3→D
:getKey→Z
:End
:ClrDraw
:If Z=92 and D=1
:Text(48,1,"BLOCKED
:If Z=92 and D=2
:Then
:randInt(1,20)→E
:A-E→A
:Text(48,1,"OPPONENT HITS
:End
:If Z=92 and D=3
:Then
:randInt(1,20→E
:B-E→B
:Text(48,1,"YOU HIT
:End
:If Z=93 and D=1
:Then
:randInt(1,15→E
:B-E→B
:Text(48,1,"YOU HIT
:End
:If Z=93 and D=2
:Text(48,1,"BLOCKED
:If Z=93 and D=3
:Then
:randInt(1,15→E
:A-E→A
:Text(48,1,"OPPONENT HITS
:End
:If Z=94 and D=1
:Then
:randInt(1,10→E
:A-E→A
:Text(48,1,"OPPONENT HITS
:End
:If Z=94 and D=2
:Then
:randInt(1,10→E
:B-E→B
:Text(48,1,"YOU HIT
:End
:If Z=94 and D=3
:Text(48,1,"BLOCKED
:End
:ClrHome
:If A≤0 and B>0
:Disp "HE WINS"," ","GAME OVER
:If B≤0 and A>0
:Disp "YOU WIN
:If A≤0 and B≤0
:Disp "TIE"
:Pause
:ClrHome
:ClrDraw
:AxesOn
Thank you, this code works perfectly so far. I now know how to do this for the future.
xXEpicxXXxFailXx wrote:
Thank you, this code works perfectly so far. I now know how to do this for the future.
Whew, I'm glad to hear that; I was a tad concerned I hadn't preserved your functionality as far as which things made you get hit or blocked etc. Smile I hope you'll keep asking us questions and consulting for BASIC tips and optimizations. Did you notice the places I stripped off closing parentheses or quotes, for example? And where 0->VAR became DelVar VAR? Smile
KermMartian wrote:
Did you notice the places I stripped off closing parentheses or quotes, for example? And where 0->VAR became DelVar VAR? Smile

Yeah, you got rid of the quotes to decrease size and delvar is just another way to do it.
Precisely. Once you start to feel more confident with TI-BASIC, you should check out the 1337 BASIC Guide that I wrote together with the eminent Jon "JPez" Pezzino.

http://www.ticalc.org/archives/files/fileinfo/369/36993.html
So, if I'm reading this right, you can't have a Goto inside an If/Then...End block. For example, this would be wrong and would eventually cause ERR:MEMORY:

Code:
:If A=105
:Then
:Goto 2
:End


Would you instead put this?:

Code:
:While A=105
:Goto 2
:End
NormalKid wrote:
So, if I'm reading this right, you can't have a Goto inside an If/Then...End block. For example, this would be wrong and would eventually cause ERR:MEMORY:

Code:
:If A=105
:Then
:Goto 2
:End


Would you instead put this?:

Code:
:While A=105
:Goto 2
:End
No, you wouldn't. A Goto inside any programming control structure that takes End, such as Then, For, While, and Repeat, causes a memory leak. The only way to not create a memory leak would be:


Code:
:If A=105
:Goto 2
Ok I think i know what you're saying, but I don't quite know how to work around it. This is the program I'm stuck on. It's a countdown timer:

Code:
:Input "Minutes:",A
:Input "Seconds:",B
:Lbl 0
:checkTmr(1)+B→Z
:Lbl 1
:checkTmr(1)→Y
:Z-Y→W
:If W=‾1
:Then
:Goto 2
:End
:If W≥10
:Then
:Output(4,6,A
:Output(4,7,":"
:Output(4,8,W
:Goto 1
:End
:If W<10
:Then
:Output(4,6,A
:Output(4,7,":"
:Output(4,8,"0"
:Output(4,9,W
:Goto 1
:End
:Lbl 2
:59→B
:A-1→A
:Goto 0
:End
KermMartian wrote:
The only way to not create a memory leak would be:


Not true. There is another way. If the label is only ever called from a place with the same number of End statements required, you can stop the leak:

[code=TI Basic]While 1
If getKey=21
Then
0→A
Goto LB
End
End
Lbl LB
End:End // Fixes the leak
// Put your code here[/code]
SirCmpwn wrote:
KermMartian wrote:
The only way to not create a memory leak would be:


Not true. There is another way. If the label is only ever called from a place with the same number of End statements required, you can stop the leak:


Code:
While 1
If getKey=21
Then
0→A
Goto LB
End
End
Lbl LB
End:End // Fixes the leak
// Put your code here
That's technically accurate, but I was omitting that for the sake of simplicity, since it takes a fair amount of care to get that right.
Fair enough.
However, omitting it does not fix his code.

Here is the original version, fixed (Untested)

Code:
:ClrHome
:AxesOff
:ZStandard
:ClrDraw
:Text(25,35,"LOADING")
:Shade(‾3,‾1)
:ClrHome
:Output(1,1,"A PROGRAM BY")
:Output(2,2,"TRISTAN SIRRICO")
:Pause
:ClrHome
:ClrDraw
:100→A
:100→B
:0→C
:0→D
:If 1:Then
:If 1:Then
:Lbl A
:End:End
:Text(1,1,"YOUR HEALTH:")
:Text(1,75,A)
:Text(16,1,"OPPONENTS HEALTH:")
:Text(16,75,B)
:Text(32,1,"HIT WHERE?")
:Text(40,1,"HIGH=1 MID=2 LOW=3")
:If 1:Then
:If 1:Then
:Lbl B
:End:End
:While 1
:0→Z
:randInt(1,3)→D
:If A≤0 or B≤0
:Then
:Goto C
:End
:If Z=0
:Then
:getKey→Z
:If Z≠92 and Z≠93 and Z≠94
:Then
:Goto B
:End
:If Z=92 and D=1
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:If Z=92 and D=2
:Then
:randInt(1,20)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=92 and D=3
:Then
:ClrDraw
:randInt(1,20)→E
:B-E→B
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=93 and D=1
:Then
:randInt(1,15)→E
:B-E→B:ClrDraw
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=93 and D=2
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:If Z=93 and D=3
:Then
:randInt(1,15)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=94 and D=1
:Then
:randInt(1,10)→E
:A-E→A
:ClrDraw
:Text(48,1,"OPPONENT HITS")
:Goto A
:End
:If Z=94 and D=2
:Then
:randInt(1,10)→E
:B-E→B
:ClrDraw
:Text(48,1,"YOU HIT")
:Goto A
:End
:If Z=94 and D=3
:Then
:ClrDraw
:Text(48,1,"BLOCKED")
:Goto A
:End
:If 1:Then
:Lbl C
:End
:If A≤0 and B>0
:Then
:ClrHome
:Disp "HE WINS"," ","GAME OVER"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
:If B≤0 and A>0
:Then
:ClrHome
:Disp "YOU WIN"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
:If A≤0 and B≤0
:Then
:ClrHome
:Disp "TIE"
:Pause
:ClrHome
:ClrDraw
:AxesOn
:End
SirCmpwn wrote:
Fair enough.
However, omitting it does not fix his code.

Here is the original version, fixed (Untested)

Code:
(giant piece of code removed)
But I already fixed his code, above, and to be much better and shorter than that.

NormalKid wrote:

Code:
:Input "Minutes:",A
:Input "Seconds:",B
:Lbl 0
:checkTmr(1)+B→Z
:Lbl 1
:checkTmr(1)→Y
:Z-Y→W
:If W=‾1
:Then
:Goto 2
:End
:If W≥10
:Then
:Output(4,6,A
:Output(4,7,":"
:Output(4,8,W
:Goto 1
:End
:If W<10
:Then
:Output(4,6,A
:Output(4,7,":"
:Output(4,8,"0"
:Output(4,9,W
:Goto 1
:End
:Lbl 2
:59→B
:A-1→A
:Goto 0
:End


Let me know if this works:

Code:
:Input "Minutes:",A
:Input "Seconds:",B
:Lbl 0
:B+checkTmr(1→Z
:Lbl 1
:checkTmr(1→Y
:Z-Y→W
:If W=‾1
:Goto 2
:If W≥10
:Then
:Output(4,6,A
:Output(4,7,":"
:Output(4,8,W
:End
:If W≥10
:Goto 1
:If W<10
:Then
:Output(4,6,A
:Output(4,7,":
:Output(4,8,"0
:Output(4,9,W
:End
:Goto 1
:Lbl 2
:59→B
:A-1→A
:Goto 0
I know that he fixed it, I was just demonstrating the concept.
  
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