Game Development Reference

In-Depth Information

board will have different numbers of moves than squares in the middle of the

board.

When we add moves to the collections, we will add the subscript for every square

that connects to a given square. We will mark each added subscript with a key

corresponding to its value converted to a string so that we can interrogate the

collection using the key to see if it contains the number of a square we are

interested in. (VB collections can hold any type of data, but their keys are always

strings.) The formula for computing neighbors changes between even- and odd-

numbered rows. We will take that into account when we compute the neighbors.

Using Figure 6.4, we can see that the square above and to the left of square 22 is

square 17, which is a difference of 5. But from square 17, the same move takes us

to square 13, a difference of 4. In both cases, the move up and to the right is one

more than the move up and left.

An important point to notice is that we can influence the effectiveness of the AI

by how we arrange the moves in the collections. We will do two things to help.

We will add upward moves first to the neighbors so that the fox tries to go up the

board before trying to go down the board. We will also change how we order

the upward or downward moves so that even and odd rows do left or right moves

in different order. This encourages the fox to zigzag upward instead of going up

and left.

The code to initialize all three arrays may appear daunting, but it beats the

alternative of 200 lines of mind-numbingly regular initializations that differ from

each other only slightly.

Public Sub InitMoves()

Dim row As Integer

Dim col As Integer

'The array subscript is computed off row and col.

Dim ss As Integer

'The final subscript is what we store in the collections.

Dim finalss As Integer

Dim offset As Integer

'Do moves up first so the fox prefers them.

For row = 0 To 7

'Offset will = 0 or 1.

offset = row Mod 2