Game Development Reference
'This is not a good sign to be here; make the message stand out.
"hit default return.")
'Default is the first move in the list, given that things are bad.
Return CType(SortedMoves(1), GameState)
The first thing to notice is that the code is heavily commented and liberally
sprinkled with debug output, both commented and not. Recursive code should
be written with care and treated as broken until proven working. For all of that,
the routine is simple at its core once the defensive coding is ignored. Looking at
the code from the top down, we see that the goal is to get the fox to a square with
a lower number of steps to freedom. Squares with 0 steps are green squares that
no hound can get to, which means that making it to a 1 is also an assured victory.
The first chunk of code checks to see if the game is already over. There is no need
to move if the outcome has been decided. If the game is in play, then the fox
creates a game state for every valid move that it can make and stores those games
in a sorted list. What follows is a defensive chunk of coding. The passed-in game
state claims not to be a victory for the hounds. Therefore, the fox should have a
move. If the passed-in game state has an incorrect rank, the fox will try to make
moves when it has none. Now we are ready to evaluate the available moves.
We start with the first game in the sorted list. This candidate will have the lowest
rank, which the fox likes. How the fox acts depends on whether it is hemmed in.
When not hemmed in, the AI uses the heuristic of taking the shortest path to
freedom and does not bother looking ahead. In such cases, the first candidate is
the best next move.
Things get interesting when the fox is forced to look ahead. The first part of the
look-ahead checks if the fox only has one move and the caller wanted a move
instead of a result. The look-ahead is optimized away; when only one move is
possible, it is the best move. This situation happens late in the game and can be
seen in Figure 6.10. This board is seen after the hounds take move 40 in an AI