This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's TI-BASIC subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. TI-Basic => TI-BASIC
Author Message
vic9317


Newbie


Joined: 16 Nov 2008
Posts: 26

Posted: 17 Apr 2009 12:02:10 am    Post subject:

Okay, so here's the deal. I'm trying to make a Minesweeper game. [A] is an 8x8 matrix, with 10 mines (represented as '9') randomly placed.

Here's my current code for calculating the values in a certain position (X,Y) of [A]:

Code:
For(X,1,8
For(Y,1,8
For(R,-1,1
For(C,-1,1
If 9≠[a](X,Y
[A](X,Y)+(9=[A](X+R,Y+C→[A](X,Y
End
End
End
End

Obviously having four For( loops will slow things down considerably (takes around 6-7 cycles to execute). Other than manually hard coding the surrounding positions I need check, is there another method of doing this? Something possibly faster and more efficient way?
Back to top
calc84maniac


Elite


Joined: 22 Jan 2007
Posts: 770

Posted: 17 Apr 2009 12:53:26 am    Post subject:

You can increase the spaces around every mine as you place them.
Back to top
Galandros


Active Member


Joined: 29 Aug 2008
Posts: 565

Posted: 17 Apr 2009 06:52:42 am    Post subject:

I figured out alone a way to reduce 2 for loops into 1.
It is using my favorite way yet of optimizing: modulus.

Implementing it to you should look like:

For(M,8,64
int(M/8
[A](Ans,1+M-8Ans
End
//This is the same as:
For(X,1,8
For(Y,1,8
[A](X,Y
End
End
//maybe not the order looping but still goes to all matrix.

The same thing that I did to X and Y loops, can be done to R and C! :biggrin: Worth a try seeing if 2 loops with extra calculations is faster than 4 loops?
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 17 Apr 2009 11:48:59 am    Post subject:

You don't have to count the number of mines around each spot. You can just increment the count of a spot when you place a mine neighboring it (I think that's what calc84maniac suggested too).

Let's say you have an 8x8 map (stored in [A]), and you want to place 10 mines. Also, suppose we put their locations into L1 in the form R+.1C, so for the Nth mine, R=int(L1(N)) and C=10fPart(L1(N)). Here's some initialization code (not very optimized, but simple to understand):

Code:
:DelVar [A]
:{8,8→dim([A]   ; starts with all zeroes
:10→dim(L1
:For(A,1,10   ;place 10 mines
:Repeat not(max(Ans=L1  ; make sure no value is repeated
:randInt(1,8)+.1randInt(1.8→S
:End
:S→L1(A
:For(R,max(1,int(S)-1),min(8,1+int(S   ;compensate for borders
:For(C,max(1,10fPart(S)-1),min(8,1+10fPart(S
:If R+.1C≠S   ;only care about neighbors
:1+[A](R,C→[A](R,C   ;increase neighbors' counts
:End
:End
:End
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 17 Apr 2009 03:05:13 pm    Post subject:

Incidentally, there's a simple way to account for borders: just make the matrix [A] two elements bigger in either direction (10x10 in this case), and only consider the elements with row and column between 2 and 9. Then for any element [A](R,C) that actually matters, you can always access [A](Rą1,Cą1).
Back to top
vic9317


Newbie


Joined: 16 Nov 2008
Posts: 26

Posted: 18 Apr 2009 11:40:51 am    Post subject:

Galandros wrote:
For(M,8,64
int(M/8
[A](Ans,1+M-8Ans
End
//This is the same as:
For(X,1,8
For(Y,1,8
[A](X,Y
End
End

Galandros, that code is pretty cool! I never would've thought of that. Unfortunately however, because of the extra calculations, it actually runs slower than the original Sad

simplethinker wrote:
You don't have to count the number of mines around each spot. You can just increment the count of a spot when you place a mine neighboring it

That's probably what I'll end up doing; I just tested your code out, and found it takes about half the time of mine. Thanks!
Back to top
simplethinker
snjwffl


Active Member


Joined: 25 Jul 2006
Posts: 700

Posted: 18 Apr 2009 11:48:08 am    Post subject:

vic9317 wrote:
simplethinker wrote:
You don't have to count the number of mines around each spot. You can just increment the count of a spot when you place a mine neighboring it

That's probably what I'll end up doing; I just tested your code out, and found it takes about half the time of mine. Thanks!

No problem Smile You can probably get more speed my expanding the matrix like DarkerLine suggested.
Back to top
Display posts from previous:   
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
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement