Help, Im fairly new to VB and trying to use the ramdomize/rnd() to select 10 random cells to mark with a mine. The total number of cells is 64(an 8x8 control array). Im using a 2d array for placement of the mines.
My code presently marks mines based upon the following code, but I get variable mines set rather than getting a constant number of mines(10).
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
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
blnMinesOn(i, j) = False
intNum = intNum + 1
mines = mines + 1
'use intCntr to display number mines set in lblNumMines:
lblNumMines.Caption = Format(intCntr, "#0")
mines = mines + 1
This sort of works, but open to suggestions to change if need be.
Thanks in advance for your input and direction. Any good resources would be also appreciated.
0 · ·