I think you need some more checks for the mines, because I ran it on my calculator and it only generated 7 mines. I ran it a few times and it generated 7,11,12, and 10.

Try this code for validating your function:

```
DelVar [A]{8,16→dim([A]
[A]→[B]
For(I,1,A
randInt(2,7→Y
randInt(2,15→X
If [A](Y,X)=9
A+1→A
9→[A](Y,X
End
//Your function above this line
//Testing below
dim([A]→L₆
DelVar C
For(I,1,L₆(1)
For(J,1,L₆(2)
If [A](I,J)=9:C+1→C
End
End
Disp C
```

Other Optimizations:

```
For(I,1,A
randInt(2,7→Y
randInt(2,15→X
If [A](Y,X)=9
A+1→A
9→[A](Y,X
End
--Can Be--
[A]→[B]
DelVar A
Repeat A=10
randInt(2,7→Y
randInt(2,15→X
If [A](Y,X)≠9:Then
9→[A](Y,X
A+1→A
End
End
```

```
For(I,2,7
Output(I,1,sub("IIIIII",I-1,1
Output(I,16,sub("IIIIII",I-1,1
End
--Can Be--
For(I,2,7
Output(I,1,"I
Output(I,16,"I
End
```

```
Repeat J=0 and K=45 or K=105 and [A](Y,X)=9
--Can Be--
Repeat K=45 or K=105 and [A](Y,X)=9
//Note this optimization is more for functionality, as some people may wish to exit the game early
//This prevents them from having to on-break the program
```

```
X+(K=26 and X<15)-(K=24 and X>2→X
Y+(K=34 and Y<7)-(K=25 and Y>2→Y
--Can Be--
max(2,min(15,X+(K=26)-(K=24→X
max(2,min(7,Y+(K=34)-(K=25→Y
```

```
For(I,1,84
B+1→B
If B>15:Then
A+1→A
2→B
End
If [B](A,B)=2:Output(A,B,"/
If [B](A,B)=0:Output(A,B,"x
If [B](A,B)=1:Then
If [A](A-1,B)=9:E+1→E
If [A](A+1,B)=9:E+1→E
If [A](A,B+1)=9:E+1→E
If [A](A,B-1)=9:E+1→E
If [A](A-1,B-1)=9:E+1→E
If [A](A+1,B-1)=9:E+1→E
If [A](A-1,B+1)=9:E+1→E
If [A](A+1,B+1)=9:E+1→E
If [B](A,B)=1:Output(A,B,E:0→E
End
End
--Can Be--
For(A,2,7
For(B,2,15
If [B](A,B)=2:Output(A,B,"/
If [B](A,B)=0:Output(A,B,"x
If [B](A,B)=1:Then
DelVar E
For(I,A-1,A+1
For(H,B-1,B+1
If [A](I,H)=9:E+1→E
End
End
Output(A,B,E
End
End
End
```

```
0→F
2→A:1→B
For(I,1,84
B+1→B
If B>15:Then
A+1→A
2→B
End
If [A](A,B)=9 and [B](A,B)=2:F+1→F
End
--Can Be--
0→F
For(A,2,7
For(B,2,15
If [A](A,B)=9 and [B](A,B)=2:F+1→F
End
End
```

```
If K=14:Then
2→[B](Y,X
J-1→J
End
If K=13 and [B](Y,X)=2:Then
0→[B](Y,X
J+1→J
End
--Can Be--
If (([B](Y,X)=0)+([B](Y,X)=2))((K=14)+(K=13)):Then
J-1+2([B](Y,X)=2→J
abs([B](Y,X)-2→[B](Y,X
End
```

I was bored so I made an "optimized" version of yours, which could probably be optimized further but I didn't want to put too much time into it:

(Updated Flag button to Del, Other than that arrows still move and enter clicks)

```
DelVar [A]{8,16→dim([A]
[A]→[B]
DelVar A
Repeat A=10
randInt(2,7→Y
randInt(2,15→X
If [A](Y,X)≠9:Then
9→[A](Y,X
A+1→A
End
End
ClrHome
1→X:10→J
For(I,1,12
Output(4,3,sub("MINE*SWEEPER",1,I
X+(X≠5→X
Output(5,6,sub("V 1.1",1,X
End
Repeat K=105
getKey→K
min(8,max(7,Y+(K=34)-(K=25→Y
Output(7,7,"PLAY
Output(8,7,"EXIT
If Y=8:Output(Y-1,6," "
If Y=7:Output(Y+1,6," "
Output(Y,6,">
End
For(I,1,38
Output(randInt(1,8),randInt(1,16),"*
End
ClrHome
If Y=8:Return
4→Y:8→X
Output(1,1," -MINE*SWEEPER- "
Output(8,5,"FLAGS:
Output(8,11,J
For(A,2,7
Output(A,1,"I
Output(A,16,"I
For(B,2,15
Output(A,B,"x
End
End
Output(Y,X,"+
Repeat K=45 or K=1
getKey→K
If sum(K={24,25,26,34,45,105,23:Then
If sum(K={24,25,26,34:Then
Output(Y,X," "
Y→A:X→B
max(2,min(15,X+(K=26)-(K=24→X
max(2,min(7,Y+(K=34)-(K=25→Y
Output(Y,X,"+
If [B](A,B)=2:Output(A,B,"/
If [B](A,B)=0:Output(A,B,"x
If [B](A,B)=1:Then
DelVar E
For(I,A-1,A+1
For(H,B-1,B+1
If [A](I,H)=9:E+1→E
End
End
Output(A,B,E
End
End
If (K=23)(([B](Y,X)=2)+Jnot([B](Y,X):Then
J-1+2([B](Y,X)=2→J
abs([B](Y,X)-2→[B](Y,X
Output(8,11," "
Output(8,11,J
End
If (K=105)not([B](Y,X))not([A](Y,X:1→[B](Y,X
If (K=105)[A](Y,X)=9:1→K
End
End
0→F
For(A,2,7
For(B,2,15
If [A](A,B)=9 and [B](A,B)=2:F+1→F
End
End
ClrHome
If K=1:Disp "YOU HIT A MINE!
Disp "YOU CLEARED
If F=10:Disp "ALL
Disp F,"OF THE MINES
"
```

Commented version:

```
//set up the matrix for the map
DelVar [A]{8,16→dim([A]
[A]→[B]
DelVar A
//fill the map with mines
Repeat A=10
randInt(2,7→Y
randInt(2,15→X
If [A](Y,X)≠9:Then
9→[A](Y,X
A+1→A
End
End
ClrHome
//set the number of flags
1→X:10→J
For(I,1,12
Output(4,3,sub("MINE*SWEEPER",1,I
X+(X≠5→X
Output(5,6,sub("V 1.1",1,X
End
//choose a menu option
Repeat K=105
getKey→K
min(8,max(7,Y+(K=34)-(K=25→Y
Output(7,7,"PLAY
Output(8,7,"EXIT
If Y=8:Output(Y-1,6," "
If Y=7:Output(Y+1,6," "
Output(Y,6,">
End
For(I,1,38
Output(randInt(1,8),randInt(1,16),"*
End
ClrHome
If Y=8:Return
//set the starting position
4→Y:8→X
//draw the map
Output(1,1," -MINE*SWEEPER- "
Output(8,5,"FLAGS:
Output(8,11,J
//fill the screen right away
For(A,2,7
Output(A,1,"I
Output(A,16,"I
For(B,2,15
Output(A,B,"x
End
End
Output(Y,X,"+
//Only test for Clear button or a custom condition
Repeat K=45 or K=1
getKey→K
//only do calculations when one of the buttons that we care about is pushed
//this makes it more responsive by ignoring keys we don't use
If sum(K={24,25,26,34,45,105,23:Then
//break up the functions by key type
//this allows us to skip major portions of the code and only perform the specific action
If sum(K={24,25,26,34:Then
//erase where the player is moving from
Output(Y,X," "
//store their position for drawing
Y→A:X→B
//move the player
max(2,min(15,X+(K=26)-(K=24→X
max(2,min(7,Y+(K=34)-(K=25→Y
//redraw the player
Output(Y,X,"+
//fill in the space they moved from
If [B](A,B)=2:Output(A,B,"/
If [B](A,B)=0:Output(A,B,"x
//if its not a flag and it was clicked, we need to count the mines
If [B](A,B)=1:Then
DelVar E
//count the mines around the square that the player moved from
For(I,A-1,A+1
For(H,B-1,B+1
If [A](I,H)=9:E+1→E
End
End
//display the count at the place the player moved from
Output(A,B,E
End
End
//flag toggle functions
//If there is a flag on the space, OR, if there is nothing in the space and we have flags remaining
If (K=23)(([B](Y,X)=2)+Jnot([B](Y,X):Then
//update the count of how many flags we have remaining
//remove a flag always, and add 2 if we are picking up a flag
J-1+2([B](Y,X)=2→J
//update the state of the space from 0→2 or 2→0
abs([B](Y,X)-2→[B](Y,X
//update the display of how many flags are remaining
Output(8,11," "
Output(8,11,J
End
//if player selects a space, it is empty, and not a mine, change the state to 1
If (K=105)not([B](Y,X))not([A](Y,X:1→[B](Y,X
//if player selects a space and it is a mine, set the flag for our custom exit condition
If (K=105)[A](Y,X)=9:1→K
End
End
//count how many mines the player successfully flagged
0→F
For(A,2,7
For(B,2,15
If [A](A,B)=9 and [B](A,B)=2:F+1→F
End
End
ClrHome
//check our custom exit condition
If K=1:Disp "YOU HIT A MINE!
//tell the player how many of the mines they cleared
Disp "YOU CLEARED
//if they cleared all of them, specify they cleared all 10
If F=10:Disp "ALL
Disp F,"OF THE MINES
"
```