Results 1 to 4 of 4

Thread: Minesweeper: Placing Mines in Grid?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    1

    Angry 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

  2. #2
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45
    You should declare the array of squares like this:
    Dim square(7, 7) as Square 'Whatever

    That means from 0 to 7 (eight squares).
    Code:
    For i = 0 to 7
     For j = 0 to 7
      If Int(Rnd * 10) + 1 = 3 Then
       square(i, j) = bomb 'whatever
      End if
     Next j
    Next i
    That puts a random amount of bomb scattered around. To make less bombs, Int(Rnd * 10) + 1 could be changed to Int(Rnd * 30) + 1 or something.

    Hope that helps.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  3. #3
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Not quite sure how you've done the grid exactly, but you should be able to adapt my code resenably easily.

    Also, i think, from the way you coded the placement of the mines, you may not always get 10 mines placed.


    Here is my suggestion (of the top of my head).

    VB Code:
    1. Dim i as Integer
    2. Dim MineGrid(1 to 8,1 to 8) as Integer
    3. Dim NumberOfMines as Integer
    4. ~~~~
    5. Randomize
    6. ~~~~
    7. For i = 1 to NumberOfMines
    8.     MineGrid(Int(Rnd*8)+1,Int(Rnd*8)+1) = 1
    9. Next i
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Slight change of the above:
    VB Code:
    1. Dim r1, r2 as Integer
    2. For i = 1 to NumberOfMines
    3.     r1 = Int(Rnd*8);
    4.     r2 = Int(Rnd*8);
    5.     If(MineGrid(r1+1,r2+1) <> 1) Then
    6.         MineGrid(Int(Rnd*8)+1,Int(Rnd*8)+1) = 1
    7.     Else
    8.         i = i - 1 'give it another try
    9.     End If
    10. Next i

    This will remove the slight chance that the loop trys to put 2 mines on one field.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width