Game Development Reference
In-Depth Information
'The naive black count has issues: see Figure 6.6 and the
'last part of the discussion of the evaluation function.
'Rank = TRAPPED - NaiveBlackCount()
Rank = TRAPPED - BetterBlackCount()
End If
End If
End Sub
#End Region
We started by making all the squares black. After adding the checkers, we started
coloring in any green squares. The top row is easy to color green; if no hound sits
on a top square, it is green. The rest of the board is checked from top to bottom.
Note that the code uses MovesUp . The hounds cannot move up, and the fox is never
restricted to moving up. But the coloring algorithm needed to know what
neighbors are above any given square. After making green squares, we can number
any white squares. We keep checking the squares against their neighbors until the
numbers settle. We then see if the fox is trapped and if the fox cannot reach
freedom. The code and the comments show the three ways of computing board
rank that were mentioned in the discussion of the evaluation function. All this
work makes the board easy to display, informative for the player, and far easier for
the AI to consider. The benefits for the AI programmer cannot be overemphasized.
We need to implement the two ways we discussed to count the number of black
squares when computing board rank. The simplest is to just count them, so we
will do that one first. Add the following code to the Internal Stuff region:
Private Function NaiveBlackCount() As Integer
'Just count them.
Dim NBC As Integer = 0
Fori=0To31
If Squares(i).Kind = SquareColor.Black Then NBC = (NBC + 1)
Next
Return NBC
End Function
This function can distract the AI into making sub-optimal moves as was shown
in Figure 6.6. What we really want is to count the number of black squares
available to the fox. We saw that in the end game, the hounds can be fatally
distracted by reducing black squares that the fox cannot get to. Add the following
code to the region for a better way to count black squares:
Private Function BetterBlackCount() As Integer