borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (How can re-index the dynamic controls array after remove an control item? )Next Topic (please help with quiz creation!!!) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Minesweeper help with rnd()/randomization to set mines
Poster Message
mpetersen
Level: Guest


icon Minesweeper help with rnd()/randomization to set mines  Archived to Disk

I am doing a project for software engineering and we are writing a code for a simple minesweeper game.  The game has an 8x8 grid and we wish to place "10" mines in the grid(a control array of 1 to 64).

My problem is that I am using the randomize, rnd() functions but I cannot get a constant 10 mines places randomly.  I get a different number at the start of ea game.
Help??  Seems should be a simple solution but I just cant get it to work??

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 in advance for any new ideas!!  Im new to VB and this is sort of learning on the fly!!!


[Edited by admin on 10-04-2002 at 09:24 AM GMT]

06-04-2002 at 08:41 PM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Minesweeper help with rnd()/randomization to set mines  Archived to Disk

As a simple tip, without going into too much detail:

Rnd is always a decimal less than 1 (I believe at least, it may go to 1), CInt(Rnd()*5+1) Will be a random number from 1 to 5.

if conOdds = 10/64 then conOdds wil allways =.1564

Think of it this way, If you pick ten random numbers from 1-64 they could all be greater than 32, or all below 32. Your code checks to see if it's below .1564, which as you can possibly tell, could display no mines to 64 mines.

I'd suggest creating the array, then marking ten of them as a mine randomily. that is, have all fields as no mines, then select a random x,y coordinate.

An example being:

Do
    x = Rnd * 8 + 1
    y = Rnd * 8 + 1

    If Field(x, y) = False Then
        Field(x, y) = True 'Is mine
        mines = mines + 1
    End If
Loop Until mines = 10


____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

06-04-2002 at 10:09 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VB General : Minesweeper help with rnd()/randomization to set mines
Previous Topic (How can re-index the dynamic controls array after remove an control item? )Next Topic (please help with quiz creation!!!) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder