user8222775
user8222775

Reputation:

Find neighbors in matrix TI-84

I'm making a barebones game of Minesweeper in TI-Basic for my TI-84 Plus. I'm stuck on adding numbers around mines. I have the number 9 representing the bombs since TI-Basic doesn't allow other data types in matrices. For example I have the matrix

0 0 0 0 0 0 0 9 0
0 9 0 0 0 0 0 0 0
0 0 0 0 0 9 0 0 0
0 0 9 0 9 0 0 0 0
0 0 0 0 9 0 0 0 0
9 0 0 0 0 0 0 0 0
0 9 0 0 0 0 0 0 0
0 0 0 0 0 9 0 0 0
0 0 0 0 0 0 0 0 9

I'd like to change this to

1 1 1 0 0 0 1 9 1
1 9 1 0 1 1 2 1 1
1 2 2 0 2 9 1 0 0
0 1 9 3 9 3 1 0 0
1 2 0 3 9 2 0 0 0
9 2 1 1 1 1 0 0 0
2 9 1 0 1 1 1 0 0
1 1 1 0 1 9 1 1 1
0 0 0 0 1 1 1 1 9

Sorry if I missed any numbers, I did that manually.

Any ideas how I could do this in TI-Basic

Upvotes: 0

Views: 57

Answers (2)

user8222775
user8222775

Reputation:

Ok I know this is really old, but I came up with a solution that I never shared. So if anyone is ever looking over this, here's what I did. First, I set up a matrix with the dimensions of the minefield. Then, I added an extra couple dimensions to the matrix, and filled it with -99. Here's how it would work on my original

0 0 0 0 0 0 0 9 0
0 9 0 0 0 0 0 0 0
0 0 0 0 0 9 0 0 0
0 0 9 0 9 0 0 0 0
0 0 0 0 9 0 0 0 0     ->  
9 0 0 0 0 0 0 0 0
0 9 0 0 0 0 0 0 0
0 0 0 0 0 9 0 0 0
0 0 0 0 0 0 0 0 9
-99 -99 -99 -99 -99 -99 -99 -99  -99 -99 -99
-99  0   0   0   0   0   0   0   9   0   -99
-99  0   9   0   0   0   0   0   0   0   -99
-99  0   0   0   0   0   9   0   0   0   -99
-99  0   0   9   0   9   0   0   0   0   -99
-99  0   0   0   0   9   0   0   0   0   -99    
-99  9   0   0   0   0   0   0   0   0   -99
-99  0   9   0   0   0   0   0   0   0   -99
-99  0   0   0   0   0   9   0   0   0   -99
-99  0   0   0   0   0   0   0   0   9   -99
-99 -99 -99 -99 -99 -99 -99 -99  -99 -99 -99

Then, iterate through each cell in the inner 9x9 matrix (the ones that aren't negative). Then, go through a loop searching for mines around that cell.

For(I,2,10)
For(J,2,10)
For(K,-1,1)
If [M](I+K,J+L)>8 //A neighbor can't be greater than eight unless it's a mine.
Then
For(L,-1,1)   
[M](I+K,J+L)+1->[M](I+K,J+L)+1
End
End
End
End
End

This loop goes through all neighbors and if one is a mine, it adds one to that cell. The negative numbers are ignored entirely this way, and you don't have to hard-code for corners and sides.

Code is available on my github: ti84sweeper

Upvotes: 1

Michelle Sprugdug
Michelle Sprugdug

Reputation: 1

There's a very simple way of doing it, but it probably wouldn't be very fast. First though, use a double for loop to loop through every cell in the matrix :

For(a, 1, 9

For(b, 1, 9

End

End

Then, when you're checking the neighbors you'll have to account for corners and edges that don't have eight spaces around them.

For(a, 1, 9

For(b, 1, 9

If a>1 and a <9 and b>1 and b<9

Then

End

End

End

Now, we can check how many spaces around us are 9's. We'll use a counter "c" to count how many times we find a 9. It's tedious, but it's the simplest way

0->c

For(a, 1, 9

For(b, 1, 9

If a>1 and a <9 and b>1 and b<9

Then

c + ([a](a-1, b-1)=9) + ([a](a-1, b)=9) + ([a](a-1, b+1)=9) + ([a](a, b-1)=9) + ([a](a, 
b+1)=9) + ([a](a+1, b-1)=9) + ([a](a+1, b)=9) + ([a](a+1, b+1)=9)->c

End

c->[a](a, b)

End

End

I left out accounting for the corners and the edges, you'll just have to add another if statementHope this helped!

Upvotes: 0

Related Questions