📄 05
字号:
Public Class PuzzleState
Private mState() As Byte
Private mMaxTile As Integer '8 or 15
Private mRowwidth As Integer '3 or 4
'Where in the state array is the open space
Private mSpacePosition As Byte
'A* definitions. f is the cost function, calculated from g (the number of steps from beginning) and h (the estimated cost to goal.)
Private f, g, h As Integer
'link to parent state. This will allow us to maintain the tree and trace the solution.
Private mParent As PuzzleState
Public Sub New(ByVal MaxTile As Integer, ByVal State As String, ByVal Generation As Integer)
'This constructor is generally used for the root, so no parent link is included.
ReDim mState(MaxTile)
mRowwidth = CInt(Math.Sqrt(MaxTile + 1))
For i As Integer = 0 To MaxTile
mState(i) = CByte(State.Substring(i * 2, 2))
Next
For i As Integer = 0 To mState.GetUpperBound(0)
If mState(i) = 0 Then
mSpacePosition = CByte(i)
End If
Next
g = Generation
End Sub
Public Sub CalculateCost()
'This is called in solving, from cmdSolve_Click on main form. Cheaper than calling from constructor, as
'you can avoid calling if you are not adding the node to state space.
If Not gSolver Is Nothing Then
h = gSolver.ManhattanDistance(Me)
f = g + h
End If
End Sub
Public Function GetStateArray() As Byte()
Return mState
End Function
Public Sub New(ByVal State() As Byte, ByVal Generation As Integer)
'Constructor based on state array without parent...
g = Generation
ReDim mState(State.GetUpperBound(0))
Array.Copy(State, mState, State.Length)
For i As Integer = 0 To mState.GetUpperBound(0)
If mState(i) = 0 Then
mSpacePosition = CByte(i)
Exit For
End If
Next
End Sub
Public Sub New(ByVal Generation As Integer, ByVal Parent As PuzzleState, ByVal ParamArray State() As Byte)
'Constructor with parent.
Me.New(State, Generation)
mParent = Parent
End Sub
Public Function GetGeneration() As Integer
Return g
End Function
Public Property Parent() As PuzzleState
Get
Return mParent
End Get
Set(ByVal Value As PuzzleState)
mParent = Value
End Set
End Property
Public Overloads Function Equals(ByVal ps As PuzzleState) As Boolean
'value equality
If ps Is Nothing Then Return False
For i As Integer = 0 To mState.GetUpperBound(0)
If mState(i) <> ps.Tile(i) Then
Return False
End If
Next
Return True
End Function
Public Overloads Function HashCode() As Integer
'because of value equality
Dim Ret As Integer
For i As Integer = 0 To mState.GetUpperBound(0)
Ret = Ret Xor (i * 10 * mState(i))
Next
Return Ret
End Function
Public Property SpacePosition() As Byte
Get
Return mSpacePosition
End Get
Set(ByVal Value As Byte)
mSpacePosition = Value
End Set
End Property
Public Property Tile(ByVal Index As Integer) As Byte
Get
Return mState(Index)
End Get
Set(ByVal Value As Byte)
mState(Index) = Value
End Set
End Property
Public Function Cost() As Integer
'CalculateCost is called in cmdSolve_Click from main form
Return f
End Function
Public Function Upper() As Integer
Return mState.GetUpperBound(0)
End Function
Public Property Generation() As Integer
Get
Return g
End Get
Set(ByVal Value As Integer)
g = Value
End Set
End Property
End Class
Public Class PuzzleComparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
If DirectCast(x, PuzzleState).Cost > DirectCast(y, PuzzleState).Cost Then
Return 1
ElseIf DirectCast(x, PuzzleState).Cost < DirectCast(y, PuzzleState).Cost Then
Return -1
Else
Return 0
End If
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -