Jump to content



Photo
- - - - -

Minesweeper Board Generation: Can I make it faster?

fx-9750GII Casio Basic

  • Please log in to reply
3 replies to this topic

#1 Jay Bergé

Jay Bergé

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:S.E. Ontario

  • Calculators:
    fx-9750GII

Posted 30 March 2025 - 04:23 PM

Hello! I made a Minesweeper program for my calculator, and I am looking to see if I can make my board generating code more efficient.
 
I generate the boards using two matrices: one to place the mines and the other to generate the numbered tiles. To make the calculations a bit easier, I extend the matrices by bookending one row and column (making the matrices 9x20 instead of the gameboard size 7x18) and not displaying the outer cells. Used to think this would be efficient, but this board size takes about six seconds to generate. Adding ten rows and columns seems to increase the time threefold. I do not like that. Below is the code I am using, with only the elements used for making the board included.
 
ClrMat A
ClrMat B
{9,20} -> Dim Mat A
{9,20} -> Dim Mat B

0 -> N
While N<25                              /// Arbitrary amount of mines, I just like the number 25
 

RanInt#(2,8) -> G
RanInt#(2,19) -> K
If Mat A[G,K]=0 And G+K≠4
Then
1 -> Mat A[G,K]
N+1 -> N
IfEnd

WhileEnd


For 2 -> G To 8
For 2 -> K To 19

If Mat A[G,K]=1
Then 10 -> Mat B[G,K]                    /// A value of 10 indicates a mine
Else Mat A[G-1,K-1]+Mat A[G-1,K]+Mat A[G-1,K+1]+Mat A[G,K-1]+Mat A[G,K+1]+Mat A[G+1,K-1]+Mat A[G+1,K]+Mat A[G+1,K+1] -> Mat B[G,K]   /// Adding the number of mines in the surrounding cells; I hate how this looks
IfEnd

Next
Next


Mat B

My apologies in advance if I made any mistakes posting this.


Edited by Jay Bergé, 02 April 2025 - 11:41 PM.


#2 Jay Bergé

Jay Bergé

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:S.E. Ontario

  • Calculators:
    fx-9750GII

Posted 30 March 2025 - 04:45 PM

Meant to add this: the added condition that G+K cannot equal 4 when placing mines is just to guarantee that the top-left tile is safe. Good Minesweeper editions tend to leave the first click safe, and this was my attempt at doing that



#3 Hlib2

Hlib2

    Casio Freak

  • Members
  • PipPipPipPip
  • 145 posts
  • Gender:Male
  • Location:Ukraine
  • Interests:industrial electronics,
    graphing calculators

  • Calculators:
    fx-9860GII-2
    graph-100+
    fx-991DE_X
    ti-89_Titanium
    ti-voyage200
    ti-84+SE

Posted 04 April 2025 - 07:11 AM

The placement of mines should begin only after the first selection of any cell. Then the program should open several cells next to the first move, and only then you can start the game. Otherwise, the second move will be dangerous and unpredictable, and the game turns into primitive guessing instead of analysis.
In a correct algorithm, the initial field preparation time does not depend on the size of the matrix. It depends only on the number of mines.
Here is an example of generating mines and filling the matrix on the first move.
The field size is 20 (width) × 10 (heigt), the number of mines is N=25. The calculation time for the fx-9750GII(SH3) is t=2.6_sec.
20->W : 10->H // widht, height
{H W}->Dim Mat A : Fill(0, Mat A)
Mat A->Mat B : 3->R : 5->C // Mat A[3,5] for example, first step
1->Mat B[R,C] // R=3 and C=5 are not for mines
25->N : 0->S // number of mines and its counter
Do :
RanInt#(1,10)->A : RanInt#(1,20)->B
If Mat B[A,B]≠1 And Mat A[A,B]≠-1
Then Isz S : -1->Mat A[A,B] // put mine
For B-(B≠1)->K To B+(B≠W)
For A-(A≠1)->L To A+(A≠H)
Mat A[L,K]->D : Isz D : D->Mat A[L,K]
Next : Next : IfEnd
LpWhile S≠N : Mat A
// Mat A[Row,Col]=-1 it is a mine
// Mat A[R,C]=0...8 is number of mines around a cell
// Mat B[R,C]=0 is unopened cell without flag
// Mat B[R,C]>0 is a cell with the number of step
// Mat B[R,C]=-1 flag "?" is set to unopened cell
When the number of mines around an open cell is equal to the number of unopened cells around it, the program should automatically open and show the detected mines.
I think, 15 (width) × 10 (height) is the most suitable field size for playing on a calculator.
Here is an example of the design of the screen for the game.
GridOff : AxesOff : LabelOff : BG-None
ViewWindow 1,127,128,-63,-1,64
"###############"->Str 1 // 15 # symbols in String
// # is unopened cell
For 3->K To 57 Step 6
Text K,3,Str 1 : Next
Vertical 1 : Vertical 93
F-Line 2,-1,92,-1 : F-Line 2,-63,92,-63
Text 27,9,"▲" //image of mine, bold type up arrow from char/symbol menu
Text 27,21,"1 space" // clears left side of wide symbol
Text 27,21+1,"?" // temporary flag for an unopened cell
// 21+1 correction for the right placement
Text 27,33,"1 space"
Text 27,33+1,"2" // number of mines around an opened cell
Text 27,45,"1space"
Text 27,45+1,"□" // opened cell with no mines around
Text 27,57,"↓" // fatal step, down arrow
Text 3,99,"MINES" : Text 9,103,"15/1"
Text 21,99,"SCORE" : Text 27,107,"18"
Text 39,103,"END" : Text 45,107,"↓"
Also, the FX-9750GII allows you to make a game with dynamic interactive controls.

Edited by Hlib2, 04 April 2025 - 07:42 AM.


#4 Jay Bergé

Jay Bergé

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:S.E. Ontario

  • Calculators:
    fx-9750GII

Posted 04 April 2025 - 04:12 PM

Also, the FX-9750GII allows you to make a game with dynamic interactive controls.

Any resources for this? I would love to see if I can improve my current control system.

 

Thank you for the improved code as well! I will try my best to make sense of it.





Also tagged with one or more of these keywords: fx-9750GII, Casio Basic

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users