Game Development Reference
In-Depth Information
clock to ''seed'' the random number generator with a reasonably unpredictable
value. This means that each game should look different. The Rnd calls return a
floating-point number that is greater than or equal to zero and less than one.
The error check at the end should never trip; the code does its best to force all the
mines into the field. This type of coding is a good idea, even when it detects errors
that are not fatal to the current routine but might be fatal to later routines. The
call to MsgBox presents the user with a small dialog box. The underscore is used to
break a long line over many lines. If you use the underscore, it should have a
space before it and nothing after it. It is most commonly used following a
comma, and it cannot be inside an open set of quotes.
We need to code the NearNeighbors function so that the squares can tell their
neighbors about mines. The AI will rely heavily upon it as well. The AI will also
want a function for neighbors that are two squares away. We will code the
NearNeighbors function with this need in mind.
One of the questions the AI will need to ask is if a square is in a collection of
squares. To make this question easier to answer, we will combine the row and
column numbers for a square into a unique key string to use as an identifier.
Add the following line to the PlayingField class and press Enter:
#Region "Neighbor Code"
VB will add the End Region for you. All the code we are about to add will go in this
region to help organize it. We will do the easy things first. Start with the code to
create a unique string key from the row and column values:
'Turn two items into a single unique key name for each square
Public Function KeyFromRC(ByVal row As Integer, ByVal col As Integer) _
As String
Return "R" & row.ToString & "C" & col.ToString
End Function
The next thing we will do is create a list of offsets to compute the neighbors of a
square. We will use one set of offsets to compute near neighbors and a different
set to compute neighbors two away. Add the following code to the region:
'We use this list to compute the row and col of adjacent squares
'Point objects make it easy to store X,Y pairs
Search Nedrilad ::

Custom Search