Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Simple Minesweeper: How to use Randomize/Rnd() .
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 · ·