Minesweeper: Placing Mines in Grid?
I am writing a simple minesweeper game for a class project. It is a beginner levle with an 8x8 grid with 64 cells. The grid is done with a control array of 1 to 64 and Ive indexed two 2d arrays 1. for placement of mines and 2. for placement of flags.
My problem is that Im using randomization/rnd() and I am unable to place 10 mines ea time I reset the game. The number various due to the structure of rnd()?? Im stuck on this and am looking for either a new solution or revision of my current code to fix the problem.
Im new to VB so am learning this on the fly. So make the explanations as simple as possible. I have worked with java more but am by far not an expert.
Find below my current code:
Below is my code:
>condOdds is set in the global as
>Dim conOdds = 10/64 'ten mines in 64 squares(control array).
>
>Private Sub setmines()
>
> Dim i As Integer ' control outer for..next loop
>
> Dim j As Integer 'control inner for..next loop
>
> Dim intNum As Integer 'use in optional codemakes mines
> ' visible when debug
>
>
> Dim mines As Integer
>
>
> 'call randomize to initialize random number generator
> 'set global variable intCntr(declared in general section) to 0
>
> 'initialize intNum to 1. use set optional debug inf. about loc. mine
>
> Randomize
> intNum = 1
> intCntr = 0
>
>mines = 0
>
> 'use nested for..next loop to perform test on ea
> 'cell in minefield:
> 'compare return value of vbfunction Rnd to constant
> 'conOdds. if return value < constant = lay mine
> 'by setting blnMinesOn property to True. and add
> '1 to intCntr(counts mines).
> '"else"; if value returned > than constant
> 'set blnMinesOn property to False.
>
>
> For i = 1 To conSize
> For j = 1 To conSize
>
>
> If Rnd < conOdds Then
>
> blnMinesOn(i, j) = True
> 'keep below as use to debug the rand placement of
>mines
> lblMines(intNum).Caption = "MINE"
> intCntr = intCntr + 1
>'need
>to check this be sure is ok
> Else
> blnMinesOn(i, j) = False
>
> End If
> intNum = intNum + 1
> mines = mines + 1
>
>
>
> Next j
>
> Next i
>
>
>
>'use intCntr to display number mines set in lblNumMines:
>
>lblNumMines.Caption = Format(intCntr, "#0")
> mines = mines + 1
>
>End Sub
Thanks so much for your input and direction.
Mpetersen