So I've been playing around with my calculator for the past year or so (ever since I learned I could actually program on it) and I've just finished my first 'real' program (I don't think quadratic solvers and rock-paper-scissors games count).

Like the title says, it's a minesweeper game.
Some issues:
There are some segments that are a bit too slow (like when the mines are being placed, and when it's expanding), almost a third of the program is mainly repeated code, and I keep getting the nagging feeling that my Gotos and Lbls are causing memory leaks...

Suggestions/optimizations please?


Code:

::"MINESWEEPER
:"VERSION 1.3.1
:If 0
:Then
:Lbl 0
:ClrHome
:DelVar ADelVar BDelVar KDelVar LDelVar NDelVar QDelVar RDelVar VDelVar WDelVar XDelVar Y
:DelVar ∟MS
:DelVar [J]
:DelVar Str1
:Output(1,1,"
:Return
:End
:Full
:Float
:4→X
:ClrHome
:1→K
:Repeat 4=X and (K=105 or K=92
:If X=5 and (K=105 or K=93
:ClrHome
:Output(X,1,"{box}
:Output(1,2,"MINESWEEPER"
:Output(2,2,"VERSION 1.3.1
:0→K
:Output(4,2,"1: PLAY!   
:Output(5,2,"2: INSTRUCTIONS
:Output(6,2,"3: ABOUT
:Output(7,2,"               
:Output(8,2,"         
:Repeat K=22 or K=23 or K=25 or K=34 or K=45 or K=92 or K=93 or K=94 or K=105
:getKey→K
:End
:If K=22 or K=23 or K=45
:Goto 0
:If 9=iPart(K/10
:K-88→X
:If K=25
:(X>4)(X-1)+4not(X>4→X
:If K=34
:(X<6)(X+1)+6not(X<6→X
:Output(4,1,"
:Output(5,1,"
:Output(6,1,"
:If (K=105 and X=6) or 94=K
:Then
:Output(4,2,"MAY 12 2010   
:Output(5,2,"BY DEFLECT     
:Output(6,2,"       
:Output(7,2,"DEFLECT.4444 AT
:Output(8,2,"GMAIL.COM
:End
:If (X=5 and K=105) or 93=K
:Then
:ClrHome
:Output(1,1,"PRESS 'Y='
:Output(2,2,"TO SHOW SQUARE
:Output(3,1,"PRESS 'GRAPH'
:Output(4,2,"TO FLAG
:Output(5,1,"PRESS 'TRACE'
:Output(6,2,"TO RESET
:Output(7,1,"PRESS 'DEL'
:Output(8,2,"TO EXIT
:End
:If (X=5 or X=6) and (105=K or 93=K or 94=K
:Then
:Repeat Ans
:getKey
:If Ans=22 or Ans=23 or K=45
:Goto 0
:End
:End
:End
:Lbl A
:ClrHome
:Output(1,2,"MINES1
:Output(2,2,"VERSION
:Output(3,2,"1.3.1
:Output(4,2,"LOADING
:"Output(5,2,{box}{box}{box}{box}{box}{box}{box}{box}{box}
:Output(6,2,"BY
:Output(7,2,"DEFLECT
:Output(8,2,"
:SetUpEditorMS
:6→dim(∟MS
:0→∟MS(2
:0→∟MS(3
:0→∟MS(4
:1→∟MS(5
:1→∟MS(6
:0→L
:{8,10}→dim([J]
:Fill 0,[J]
:For(X,1,randInt(13,16
:If not(fPart(X/2
:Output(5,X/2+1,"{box}
:randInt(1,8→A
:randInt(1,10→B
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If 9>[J](A,B
:Then
:9→[J](A,B
:1+∟MS(2→∟MS(2
:For(Q,{-}1,1)
:For(R,{-}1,1)
:If 9≤[J](A,B
:Then
:If ((A+Q)≥1)((B+R)≥1)((A+Q)≤8)(10≥(B+R
:1+[J](A+Q,B+R→[J](A+Q,B+R
:End
:End
:End
:End
:End
:∟MS(2→∟MS(1
:For(X,1,8
:Output(X,1,"++++++++++
:End
:Output(1,12,"TOTAL
:Output(2,12,"MINES
:Output(3,14,∟MS(2
:Output(4,12,"FLAGS
:Output(6,12,"MOVES
:Output(8,12,":)
:Output(∟MS(5),∟MS(6),"{box}
:While not(L
:0→Y
:∟MS(5→V
:∟MS(6→W
:0→K
:{-}1→N
:If not(fPart([J](V,W
:"+→Str1
:If .3=fPart([J](V,W
:Then
:If not(iPart([J](V,W
:Then
:" →Str1
:Else
:iPart([J](V,W→N
:End
:End
:If .1=fPart([J](V,W
:"≠→Str1
:Repeat K=11 or K=14 or K=15 or K=21 or K=22 or K=23 or K=24 or K=25 or K=26 or K=31 or K=34 or K=45
:Output(V,W,"{box}
:getKey→K
:If Ans
:Then
:If N≠{-}1
:Then
:Output(V,W,N
:Else
:Output(V,W,Str1
:End
:End
:End
:If K=22 or K=23 or K=45
:Goto 0
:If K=14
:Goto A
:If K=24
:Then
:W-1→∟MS(6
:If not(∟MS(6
:10→∟MS(6
:End
:If K=26
:Then
:1+W→∟MS(6
:If 11=∟MS(6
:1→∟MS(6
:End
:If K=25
:Then
:V-1→∟MS(5
:If not(∟MS(5
:8→∟MS(5
:End
:If K=34
:Then
:1+V→∟MS(5
:If 9=∟MS(5
:1→∟MS(5
:End
:If (K=11 or K=21) and not(fPart([J](V,W
:Then
:1+∟MS(4→∟MS(4
:If 9≤[J](V,W
:Then
:1→L
:9→[J](V,W
:End
:If 0<[J](V,W
:Then
:.3+[J](V,W→[J](V,W
:Else
:.2+[J](V,W→[J](V,W
:1+Y→Y
:End
:End
:If (K=15 or K=31) and .1≥fPart([J](V,W
:Then
:1+∟MS(4→∟MS(4
:If not(fPart([J](V,W
:Then
:∟MS(2)-1→∟MS(2
:1+∟MS(3→∟MS(3
:.1+[J](V,W→[J](V,W
:If 9≤[J](V,W
:Then
:∟MS(1)-1→∟MS(1
:Else
:1+∟MS(1→∟MS(1
:End
:Else
:1+∟MS(2→∟MS(2
:∟MS(3)-1→∟MS(3
:{-}.1+[J](V,W→[J](V,W
:If 9≤[J](V,W
:Then
:1+∟MS(1→∟MS(1
:Else
:∟MS(1)-1→∟MS(1
:End
:End
:If not(∟MS(1
:2→L
:End
:Output(5,14," 
:Output(5,14,∟MS(3
:Output(7,14,∟MS(4
:If .3=fPart([J](V,W
:Then
:If not(iPart([J](V,W
:Then
:Output(V,W,"
:Else
:If L≠1
:Then
:Output(V,W,iPart([J](V,W
:Else
:Output(V,W,"*
:End
:End
:End
:If L≠1 and .2=fPart([J](V,W
:Then
:.3→[J](V,W
:Output(8,13,"O
:Output(V,W,"
:For(A,{-}1,1
:For(B,{-}1,1
:If ((V+A)>0 and (V+A)<9) and ((W+B)>0 and (W+B<11
:Then
:If not(fPart([J](V+A,W+B
:Then
:If not(iPart([J](V+A,W+B
:Then
:.2→[J](V+A,W+B
:Y+1→Y
:Else
:.3+[J](V+A,W+B→[J](V+A,W+B
:End
:End
:End
:End
:End
:Repeat not(Y
:If Y=1
:0→Y
:For(Q,(V≤4)+8(V≥5),(V≥5)+8(V≤4),(V≤4)-(V≥5
:For(R,(W≤5)+10(W≥6),(W≥6)+10(W≤5),(W≤5)-(W≥6
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If Ans=14
:Goto A
:If .3=fPart([J](Q,R
:Then
:If not(iPart([J](Q,R
:Then
:Output(Q,R,"
:Else
:Output(Q,R,iPart([J](Q,R
:End
:End
:If .2=fPart([J](Q,R
:Then
:Y-1→Y
:.3→[J](Q,R
:Output(Q,R,"
:For(A,{-}1,1
:For(B,{-}1,1
:If ((Q+A)>0 and (Q+A)<9) and ((R+B)>0 and (R+B)<11
:Then
:If not(fPart([J](Q+A,R+B
:Then
:If not(iPart([J](Q+A,R+B
:Then
:.2→[J](Q+A,R+B
:Y+1→Y
:Else
:.3+[J](Q+A,R+B→[J](Q+A,R+B
:End:End:End
:End:End:End
:End:End:End
:Output(8,13,")
:End:End
:Output(1,12,"{box}{box}{box}{box}{box}
:Output(2,12," YOU
:Output(4,12,"{box}{box}{box}{box}{box}
:Output(5,14," 
:If L=1
:Then
:Output(3,12," LOSE
:Output(8,13,"(
:Output(V,W,"*
:For(A,1,8
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If Ans=14
:Goto A
:For(B,1,10
:If [J](A,B)≥9 and 0=fPart([J](A,B
:Output(A,B,"*
:If [J](A,B)≤9 and .1=fPart([J](A,B
:Output(A,B,"X
:End
:End
:Else
:Output(V,W,"≠
:Output(3,12," WIN!
:End
:15→K
:While K=11 or K=15 or K=21 or K=24 or K=25 or K=26 or K=31 or K=34
:getKey
:If Ans
:Ans→K
:If K=14
:Goto A
:End
:Goto 0
Well, foremost, if you Goto out of a loop (or anything that ends with an End), you'll get memory leaks. I think you've done that in a couple places.
If you have a For( loop without a ), all If's without Then's inside will be slow. Either put Then's on all If's, or close the parenthesis on For( loops that contain If's without Thens.

It looks like Lbl A starts a new game. Since you jump to it from inside a block, it will leak memory (2 bytes for every block you jumped out of I think), which could pose a problem if you play enough games without exiting. You could encase the whole thing in a while 1 loop and make it restart that way.

Nice code, you seem to be familiar with the language and it's peculiarities.

Edit: also, and always happens before or in the BASIC order of operations, so you can get rid of some parenthesis that way.
First pass, minor edits. Whatever jumped out at me.

TIBasic Code wrote:
::"MINESWEEPER
:"VERSION 1.3.1
:If 0
:Then
:Lbl 0
:ClrHome
:DelVar ADelVar BDelVar KDelVar LDelVar NDelVar QDelVar RDelVar VDelVar WDelVar XDelVar YDelVar ∟MSDelVar [J]DelVar Str1
:Output(1,1,"
:Return
:End
:Full
:Float
:4→X
:ClrHome
:1→K
:Repeat 4=X and (K=105 or K=92
:If X=5 and (K=105 or K=93
:ClrHome
:Output(X,1,"{box}
:Output(1,2,"MINESWEEPER"
:Output(2,2,"VERSION 1.3.1
:DelVar KOutput(4,2,"1: PLAY!
:Output(5,2,"2: INSTRUCTIONS
:Output(6,2,"3: ABOUT
:Output(7,2,"
:Output(8,2,"
0:While prod(K-{23,25,34,45,92,93,94,105
:getKey→K
:End
:If not(K-{22,23,45
:Goto 0
:If 9=iPart(.1K
:K-88→X
:If K=25
SadX>4)(X-1)+4not(X>4→X
:If K=34
SadX<6)(X+1)+6not(X<6→X
:Output(4,1,"
:Output(5,1,"
:Output(6,1,"
:If (K=105 and X=6) or 94=K
:Then
:Output(4,2,"MAY 12 2010
:Output(5,2,"BY DEFLECT
:Output(6,2,"
:Output(7,2,"DEFLECT.4444 AT
:Output(8,2,"GMAIL.COM
:End
:If (X=5 and K=105) or 93=K
:Then
:ClrHome
:Output(1,1,"PRESS 'Y='
:Output(2,2,"TO SHOW SQUARE
:Output(3,1,"PRESS 'GRAPH'
:Output(4,2,"TO FLAG
:Output(5,1,"PRESS 'TRACE'
:Output(6,2,"TO RESET
:Output(7,1,"PRESS 'DEL'
:Output(8,2,"TO EXIT
:End
:If (X=5 or X=6) not(prod(K-{105,93,94
:Then
:Repeat Ans
:getKey
:If not(prod(Ans-{22,23,45
:Goto 0
:End
:End
:End
:Lbl A
:ClrHome
:Output(1,2,"MINES1
:Output(2,2,"VERSION
:Output(3,2,"1.3.1
:Output(4,2,"LOADING
:"Output(5,2,{box}{box}{box}{box}{box}{box}{box}{box}{box}
:Output(6,2,"BY
:Output(7,2,"DEFLECT
:Output(8,2,"
:SetUpEditorMS
:6→dim(∟MS
:0→∟MS(2
:0→∟MS(3
:0→∟MS(4
:1→∟MS(5
:1→∟MS(6
:DelVar L{8,10}→dim([J]
:Fill 0,[J]
:For(X,1,randInt(13,16
:If not(fPart(X/2
:Output(5,X/2+1,"{box}
:randInt(1,8→A
:randInt(1,10→B
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If 9>[J](A,B
:Then
:9→[J](A,B
:1+∟MS(2→∟MS(2
:For(Q,{-}1,1)
:For(R,{-}1,1)
:If 9≤[J](A,B
:Then
:If ((A+Q)≥1)((B+R)≥1)((A+Q)≤Cool(10≥(B+R
:1+[J](A+Q,B+R→[J](A+Q,B+R
:End
:End
:End
:End
:End
:∟MS(2→∟MS(1
:For(X,1,8
:Output(X,1,"++++++++++
:End
:Output(1,12,"TOTAL
:Output(2,12,"MINES
:Output(3,14,∟MS(2
:Output(4,12,"FLAGS
:Output(6,12,"MOVES
:Output(8,12,"Smile
:Output(∟MS(5),∟MS(6),"{box}
:While not(L
:DelVar Y∟MS(5→V
:∟MS(6→W
:DelVar K{-}1→N
:If not(fPart([J](V,W
:"+→Str1
:If .3=fPart([J](V,W
:Then
:If not(iPart([J](V,W
:Then
:" →Str1
:Else
:iPart([J](V,W→N
:End
:End
:If .1=fPart([J](V,W
:"≠→Str1
:While prod(K-{11,14,15,21,22,23,24,25,26,31,34,45
:Output(V,W,"{box}
:getKey→K
:If Ans
:Then
:If N≠{-}1
:Then
:Output(V,W,N
:Else
:Output(V,W,Str1
:End
:End
:End
:If K=22 or K=23 or K=45
:Goto 0
:If K=14
:Goto A
:If K=24
:Then
:W-1→∟MS(6
:If not(∟MS(6
:10→∟MS(6
:End
:If K=26
:Then
:1+W→∟MS(6
:If 11=∟MS(6
:1→∟MS(6
:End
:If K=25
:Then
:V-1→∟MS(5
:If not(∟MS(5
:8→∟MS(5
:End
:If K=34
:Then
:1+V→∟MS(5
:If 9=∟MS(5
:1→∟MS(5
:End
:If (K=11 or K=21) and not(fPart([J](V,W
:Then
:1+∟MS(4→∟MS(4
:If 9≤[J](V,W
:Then
:1→L
:9→[J](V,W
:End
:If 0<[J](V,W
:Then
:.3+[J](V,W→[J](V,W
:Else
:.2+[J](V,W→[J](V,W
:1+Y→Y
:End
:End
:If (K=15 or K=31) and .1≥fPart([J](V,W
:Then
:1+∟MS(4→∟MS(4
:If not(fPart([J](V,W
:Then
:∟MS(2)-1→∟MS(2
:1+∟MS(3→∟MS(3
:.1+[J](V,W→[J](V,W
:If 9≤[J](V,W
:Then
:∟MS(1)-1→∟MS(1
:Else
:1+∟MS(1→∟MS(1
:End
:Else
:1+∟MS(2→∟MS(2
:∟MS(3)-1→∟MS(3
:{-}.1+[J](V,W→[J](V,W
:If 9≤[J](V,W
:Then
:1+∟MS(1→∟MS(1
:Else
:∟MS(1)-1→∟MS(1
:End
:End
:If not(∟MS(1
:2→L
:End
:Output(5,14,"
:Output(5,14,∟MS(3
:Output(7,14,∟MS(4
:If .3=fPart([J](V,W
:Then
:If not(iPart([J](V,W
:Then
:Output(V,W,"
:Else
:If L≠1
:Then
:Output(V,W,iPart([J](V,W
:Else
:Output(V,W,"*
:End
:End
:End
:If L≠1 and .2=fPart([J](V,W
:Then
:.3→[J](V,W
:Output(8,13,"O
:Output(V,W,"
:For(A,{-}1,1
:For(B,{-}1,1
:If ((V+A)>0 and (V+A)<9) and ((W+B)>0 and (W+B<11
:Then
:If not(fPart([J](V+A,W+B
:Then
:If not(iPart([J](V+A,W+B
:Then
:.2→[J](V+A,W+B
:Y+1→Y
:Else
:.3+[J](V+A,W+B→[J](V+A,W+B
:End
:End
:End
:End
:End
:Repeat not(Y
:If Y=1
:0→Y
:For(Q,(V≤4)+8(V≥5),(V≥5)+8(V≤4),(V≤4)-(V≥5
:For(R,(W≤5)+10(W≥6),(W≥6)+10(W≤5),(W≤5)-(W≥6
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If Ans=14
:Goto A
:If .3=fPart([J](Q,R
:Then
:If not(iPart([J](Q,R
:Then
:Output(Q,R,"
:Else
:Output(Q,R,iPart([J](Q,R
:End
:End
:If .2=fPart([J](Q,R
:Then
:Y-1→Y
:.3→[J](Q,R
:Output(Q,R,"
:For(A,{-}1,1
:For(B,{-}1,1
:If ((Q+A)>0 and (Q+A)<9) and ((R+B)>0 and (R+B)<11
:Then
:If not(fPart([J](Q+A,R+B
:Then
:If not(iPart([J](Q+A,R+B
:Then
:.2→[J](Q+A,R+B
:Y+1→Y
:Else
:.3+[J](Q+A,R+B→[J](Q+A,R+B
:End:End:End
:End:End:End
:End:End:End
:Output(8,13,")
:End:End
:Output(1,12,"{box}{box}{box}{box}{box}
:Output(2,12," YOU
:Output(4,12,"{box}{box}{box}{box}{box}
:Output(5,14,"
:If L=1
:Then
:Output(3,12," LOSE
:Output(8,13,"(
:Output(V,W,"*
:For(A,1,8
:getKey
:If Ans=22 or Ans=23 or Ans=45
:Goto 0
:If Ans=14
:Goto A
:For(B,1,10
:If [J](A,B)≥9 and 0=fPart([J](A,B
:Output(A,B,"*
:If [J](A,B)≤9 and .1=fPart([J](A,B
:Output(A,B,"X
:End
:End
:Else
:Output(V,W,"≠
:Output(3,12," WIN!
:End
:15→K
:Repeat prod(K-{11,15,21,24,25,26,31,34
:getKey
:If Ans
:Ans→K
:If K=14
:Goto A
:End
:Goto 0
Cool, thanks!

Two questions:
1] What's with the smileys in the code,
2] Doesn't "prod(" multiply elements in a list? How does that work with my getkeys?

okay, so since the end of my code looks like this:

Code:

A bunch of code...

:Lbl A

More code...

:If K=14
:Goto A
:End
:Goto 0


will my memory leaks be gone if I do this?


Code:

A bunch of code...

:while 0
:Lbl A
:End

More code...

:If K=14
:Goto A
:End
:Goto 0


Or is it simpler to do as Will_W said and enclose my entire code into a while 1 loop?
Deflect wrote:
1] What's with the smileys in the code,
That's :) & :( getting automatically converted into Smile & Sad respectively.
Deflect wrote:

2] Doesn't "prod(" multiply elements in a list? How does that work with my getkeys?


Yes. When you subtract "K" from a list (say, {1,2,3}), then if K= 1 or K=2, or K=3, then one of the elements will become 0. When we find prod(, if one of the elements is 0, prod( will yield 0. Therefore, it can be used as a test to see if K matches any of the elements in the list.

Deflect wrote:

okay, so since the end of my code looks like this:

Code:

A bunch of code...

:Lbl A

More code...

:If K=14
:Goto A
:End
:Goto 0


will my memory leaks be gone if I do this?


Code:

A bunch of code...

:while 0
:Lbl A
:End

More code...

:If K=14
:Goto A
:End
:Goto 0


Or is it simpler to do as Will_W said and enclose my entire code into a while 1 loop?


First off, you wouldn't need the final end in the second part of the code.
Second; is there any code following the Lbl A? Because then you should just do


Code:

:A bunch of code...
:Lbl A
:More code...
:If K=14
:Goto A
:Goto 0
comicIDIOT wrote:
Deflect wrote:
1] What's with the smileys in the code,
That's :) & :( getting automatically converted into Smile & Sad respectively.
Pro-tip: If you're posting a chunk of code in a [quote] block instead of a [code] block, check the "Disable Smilies in this post" checkbox. Smile
Quote:
0:While prod(K-{23,25,34,45,92,93,94,105
:getKey→K
:End

I wouldn't recommend that. If the loop is always entered and the variable in the condition is always modified before the loop exits, a Repeat loop without initiation using a zero would work just as well – the reason being that the condition gets tested at the bottom of the loop, unlike that blasted overused example from the TI-83+ Guidebook.

And if you're having trouble finding the converse of "prod(K-", ...well, I am too. Better use "max(K=".
Weregoose wrote:
Quote:
0:While prod(K-{23,25,34,45,92,93,94,105
:getKey→K
:End

I wouldn't recommend that. If the loop is always entered and the variable in the condition is always modified before the loop exits, a Repeat loop without initiation using a zero would work just as well – the reason being that the condition gets tested at the bottom of the loop, unlike that blasted overused example from the TI-83+ Guidebook.

And if you're having trouble finding the converse of "prod(K-", ...well, I am too. Better use "max(K=".
Thanks Weregoose. Smile In other words:


Code:
Repeat max(K={23,25,34,45,92,93,94,105
:getKey→K
:End
Wow - I didn't know you could do those kinds of things with lists...
  
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