Game Development Reference
In-Depth Information
This block of code introduces a few notational shortcuts. The first is that you can
assign a value to a variable on the same line that you declare the variable. The next
shortcut is for when you have a single line as the object of an If statement; you
can just put the line after the Then keyword. When you do this, there is no need
for an End If . (If you are new to VB, use this construct sparingly. It is not
advisable to use it inside a nested If statement. The compiler could care less, but
the programmer might get confused.) One of the very nice features of VB is that
it takes care of indenting nested constructs for you. If you think that you have
messed up the indentation, highlight the entire routine (or even the entire file)
and press the Tab key. VB will line everything up based on where the compiler
places the levels.
At this point there is one error. We have called upon the form to initialize the
squares, but that code does not yet exist. The code for this has to walk the field
and randomly place mines. Squares that get a mine need to know who their
neighbors are. Do not let the length of the code fool you into thinking that it is
complicated. Add this routine to the PlayingField class:
'After the first click, place the mines
Public Sub InitializeSquares(ByVal ClickedRow As Integer, ByVal ClickedCol
As Integer)
'There is a lot of code that goes here.
'We will add it in stages.
'We have to track how many mines are yet to be placed
Dim minesLeft As Integer = NumMines
'We track the number of squares to go (one has been clicked)
Dim squaresleft As Integer = (NumRows * NumCols) - 1
'Percent and random numbers are floating point
Dim perCent, roll As Single
'Reseed the random number generator
Call Randomize()
'Our working variables
Dim Row, Col As Integer
Dim Neighbors As Collection
'Walk the grid
For Row = 0 To NumRows - 1
For Col = 0 To NumCols - 1
 
Search Nedrilad ::




Custom Search