Game Development Reference
Before we load the Square objects with their ominous data, we have to wait for the
user to click the first tile. In Minesweeper ,thefirstclickis always safe. We placed
error checking in the NewGame code to make sure that at least one square was open
for this very reason. This means that our Square objects have three possible states:
They can be mines, they can be safe, or they can be waiting for the first click. They
have to exist on the form in order for the user to make the first click, but they cannot
have mines loaded until after that first click.
So what we will do next is modify the squares to have three possible states and to
start in the uninitialized state. They will have to detect the first click and ask the
playing field to tell them all what they contain. The squares will need to tell their
neighbors to increment their mine counts. We will use the concept of neighbors a
great deal, so the playing field needs some helper functions to create lists of
neighbors. Finally, we should test that all of this code works. To test, we will add
code that we will later turn into comments.
We start with the Square objects. Under the Inherits line in the Square class, add
the following code:
Public Enum HiddenValue
'Hold the definitions of the button text in one place
Public Const ShowMine As String = "@"
Public Const ShowFlag As String = "F"
Public Const ShowBrokenFlag As String = "X"
'What does this Square object actually hold?
Private contents As HiddenValue
'How many mines are near us?
Private actualNearMines As Integer
The contents variable holds the secret value of the square. An Enum is a way of
creating an enumerated list of independent values. We do not really care what the
values are, we just need for all of them to be different, and we need to be able to tell
them apart. A variable of an enumerated type is restricted to hold only values from
the enumeration. We want our Square objects to be created with their contents
equal to Uninitialized , so we add the following line to the New() routine.
contents = HiddenValue.Uninitialized