Game Development Reference
In-Depth Information
If Row <> ClickedRow Or Col <> ClickedCol Then
'What percent of the squares need mines?
'Has to be converted to a single precision float
perCent = CSng(minesLeft / squaresleft)
'Roll the dice, get a number from 0 to almost 1
roll = Rnd()
'If we roll less than the percent, we place a mine
'Also, we ensure that we place them all
If (roll < perCent) Or (minesLeft >= squaresleft) Then
'It has a mine!
'Call init on the square - we need the neighbors
Neighbors = NearNeighbors(Row, Col)
Field(Row, Col).Init(Square.HiddenValue.Mine, _
Neighbors)
'We placed a mine, so dec the count remaining
minesLeft -= 1
Else
'It is safe - don't bother the neighbors
Neighbors = New Collection
Field(Row, Col).Init(Square.HiddenValue.Safe, _
Neighbors)
End If
'We either place a mine or not, but we have one less
'Square in the computations
squaresleft -= 1
Else
'It is the initial tile, therefore safe
Neighbors = New Collection
Field(Row, Col).Init(Square.HiddenValue.Safe, Neighbors)
End If
Next Col
Next Row
'Error checking: All mines should be placed by now
If minesLeft > 0 Then
MsgBox(minesLeft.ToString & " Mines leftover!", _
MsgBoxStyle.OkOnly)
End If
End Sub
The new and interesting parts of the code include our first brush with the random
number generator and the underscore ( _ ) continuation character. Random
number generators are not really random. The call to Randomize uses the system
 
Search Nedrilad ::




Custom Search