Game Development Reference
GameState object to display. Before it displays the new GameState , the board
pushes the current game, if any, onto the undo stack. It then paints the UI with
the new game state. The AI code will also exploit these new capabilities. For many
games, it is a great idea to merge the player input pipeline and the AI input
pipeline as early as possible. Doing so prevents you from having to keep two
different pieces of code that do the same thing synchronized.
Now that we have enabled the undo button, we should give it something useful to
do. Add the following code to the Board class:
Private Sub UndoButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles UndoButton.Click
'Do I have a prior board to show?
If PriorBoards.Count > 0 Then
'Overwrite the current board with the last board
'out of the collection.
ThisGame = CType(PriorBoards(PriorBoards.Count), GameState)
'Remove it from the collection.
'That might have been the last board.
UndoButton.Enabled = (PriorBoards.Count > 0)
'Paint the board.
The undo function is very handy when debugging the AI. Human players likewise
appreciate the ability to recover from accidentally letting go of the mouse button
too soon. That is all of the additions to the board that the buttons need, but they
still need help from the GameState class.
Game State Support for the UI
The buttons need to be able to ask the game state where the fox is and where the
hounds are. Only those squares can be the source of a valid move. It does not
make sense to move an empty square, and we have to allow the fox to move
differently than the hounds. Getting the location of the fox is easy. Change to the
GameState.vb tab in the editor. Add the following code to the Public Methods
region of the GameState class:
'Where is the fox?
Public Function FoxAt() As Integer