Game Development Reference
In-Depth Information
Fox and Hounds
Open your Fox and Hounds project from Chapter 6 or from the CD-ROM. Edit
the file AI.vb and add the following region to the module between the last #End
Region and the End Module lines.
#Region "Book of Moves"
Private Function ConsultFoxBook(ByVal GS As GameState) As GameState
'We only have one move in our topic, only at the beginning.
'We get there on move 8, since the moves start at 0.
If GS.MoveCount > 8 Then
Return Nothing
End If
Dim bestMove As Byte = 64 'Higher than any square on the board.
Dim ss As Byte
'The fox will move up and left to get to square 8.
For Each ss In Moves.Neighbors(GS.FoxAt)
'Don't consider a blocked square.
If Not GS.HasChecker(ss) Then
'The smaller the ss the better.
If ss < bestMove Then bestMove = ss
End If
Return GS.ProposeFoxTo(bestMove)
End Function
#End Region
The code is very simple and takes advantage of the way the squares are numbered.
Square 8 is the first square of the third row since we start at 0 at the top left. The
smallest numbered square is always above and if possible to the left. It takes the
fox to square 8, but not if we forget to call the new code. Add the following lines
near the top of the Fox2() function in the same file just below the initial check for
a game either side has won and before where the fox gets its potential moves.
'Check the topic of moves.
Dim Candidate As GameState = ConsultFoxBook(GS)
'If the topic gave a move, use it.
If Candidate IsNot Nothing Then Return Candidate
You will need to comment out the declaration for the variable Candidate further
down in the routine. Do this by adding a single quote character in front of the
Search Nedrilad ::

Custom Search