Game Development Reference
In-Depth Information
'Get the first and second moves out of the collections.
FirstMove = FirstSquares(i)
SecondMove = SecondSquares(i)
If pass = 1 Then
'Check for follow-up moves first; they pay off better.
If BookSecondMove(FirstMove, SecondMove, Me) > 0 Then Return
Else
'Look for first moves.
If BookFirstMove(FirstMove, SecondMove, Me) > 0 Then Return
End If
Next
Next
End Sub
That chunk of code can be divided into two parts. The first part collects the pairs
of first-move and second-move squares and stores them in the order we want
them checked. It does the corners first and then the edge moves. The edge moves
concentrate on the middle of the edge, hoping for a good breakout pattern in
three directions. We include only two of the four corners, and we do only the top
edge. If we try all four corners, there is a non-trivial chance that our code will hit a
mine near a corner before it tries an edge move. For the sake of instruction, we
want a good chance at seeing the code take multiple book moves before hitting a
mine. All these moves are low risk, but none of them are risk free.
The second part of this code loops through the pairs of moves in order, stopping
as soon as one of them executes. Notice that it looks for second moves in the first
pass before it looks for first moves in the second pass. This is not a coding error.
The second move is the one most likely to net us deterministic safe moves. We
take the risk of the first move only so that we can execute the second move and
make some headway. So we always check for moves we want to take before we
resort to moves we have to take. In any case, since the code takes only a single
move before it chickens out, it has to check for second moves first; otherwise, it
will risk every possible first move in the collection before doing a follow-up
second move. On any run of the code, the code does not know what first moves
have been taken. Even if the AI kept track of first moves that it took, the player
could have taken some first moves that the AI would not know about, so we
always check to make sure.
All we need now is some user-interface code to enable the user to try these risky
moves. Switch to the design view of PlayingField.vb and add a button. Change