Game Development Reference
In-Depth Information
'How many turns have been taken?
Public Function MoveCount() As Integer
Return Turn
End Function
Right now, the buttons can ask the game state if there is a fox or a hound on the
source square. The buttons can ask the game state if there is a checker on the target
square. The buttons can use the arrays inMoves.vb to check for valid moves for the
fox or the hounds. What remains is for the buttons to be able to get the new game
state from the current game state by asking the current game state to make a move.
Then the buttons will be able to set the board's game state with the new one.
We will have the GameState class provide move capability rather than have outside
code change the game state. This defensive measure protects the game fromAI code
bugs as well as user-interface code bugs. We can live with the idea that the AI is not
smart enough, but we cannot accept having an untrustworthy game state. The game
must work, even if the AI goes off the rails. Our highly defensive way of making a
move is to ask the game state to return a clone of itself changed by one move. This
lets us do anything we desire with the new game state, including throwing it away,
which the AI will do often. This also makes our undo method work as expected.
We will implement the cloning part by providing a different way of creating a
new instance of the class. Add the following code to the Class New region. (You
may want to collapse other regions to reduce the clutter in the editor):
'Clone the passed-in gamestate.
Public Sub New(ByVal GS As GameState)
Dim i As Integer
'Copy the positions.
Fox = GS.Fox
Fori=0To3
Hounds(i) = GS.Hounds(i)
Next
'Allocate the squares.
Fori=0To31
Squares(i) = New SquareData
Next
'Copy the turn number.
Turn = GS.Turn
 
Search Nedrilad ::




Custom Search