“指定的转换无效” vb.net异常

时间:2019-11-12 16:05:42

标签: vb.net list class exception minimax

我正在尝试为国际象棋游戏制定MiniMax算法,但是目前每次运行时,我得到指定的强制转换是无效错误。 错误指向行TestBed.configureTestingModule({ declarations: [DefinitionComponent] })

这是课程的其余部分:

Nodes(rootNum).Children.Add(Mini(rootNum, 1, Nodes(rootNum)))

这是树结构:

Private Sub AIMove2()
        Dim Nodes() As Node = New Node(15) {}
        Dim maxcount As Integer
        Dim maxmove As New List(Of Integer)
        Dim validmove As New List(Of Integer)
        maxmove.Add(0)
        maxmove.Add(0)
        maxmove.Add(-1000)
        For rootNum = 0 To 15
            Nodes(rootNum).Children = New List(Of Node)
            Nodes(rootNum).piece = rootNum + 1
            Nodes(rootNum).depth = 0
            NodeCount += 1
            For x = 0 To 7
                For y = 0 To 7
                    If checkValidMove(x, y, piecestats, rootNum + 1) = True Then
                        validmove.Add(x)
                        validmove.Add(y)
                        validmove.Add(0)
                        For enemy = 17 To 32
                            If piecestats(enemy).xPos = x And piecestats(enemy).yPos = y Then
                                validmove.Insert(2, piecestats(enemy).value)
                            End If
                        Next
                        If maxmove(2) < validmove(2) Then
                            maxmove.Insert(0, validmove(0))
                            maxmove.Insert(1, validmove(1))
                            maxmove.Insert(2, validmove(2))
                            maxcount = rootNum
                        End If
                        validmove.Clear()
                    End If
                Next
            Next
            Nodes(rootNum).move = {maxmove(0), maxmove(1)}
            Nodes(rootNum).value = maxmove(2)
            Nodes(rootNum).Children.Add(Mini(rootNum, 1, Nodes(rootNum)))
        Next
        For rootNum = 0 To 15
            Console.WriteLine("V: " & Traversal(Nodes(rootNum), 0))
        Next
    End Sub
    Private Function Max(ByVal rootNum As Integer, depth As Integer, noder As Node)
        Dim maxcount As Integer
        Dim maxmove As New List(Of Integer)
        Dim validmove As New List(Of Integer)
        Dim childNodes() As Node = New Node(15) {}
        maxmove.Add(0)
        maxmove.Add(0)
        maxmove.Add(-1000)
        For childNum = 0 To 15
            childNodes(childNum).piece = childNum + 1
            childNodes(childNum).depth = 0
            NodeCount += 1
            For x = 0 To 7
                For y = 0 To 7
                    If checkValidMove(x, y, piecestats, childNum + 1) = True Then
                        validmove.Add(x)
                        validmove.Add(y)
                        validmove.Add(0)
                        For enemy = 17 To 32
                            If piecestats(enemy).xPos = x And piecestats(enemy).yPos = y Then
                                validmove.Insert(2, piecestats(enemy).value)
                            End If
                        Next
                        If maxmove(2) < validmove(2) Then
                            maxmove.Insert(0, validmove(0))
                            maxmove.Insert(1, validmove(1))
                            maxmove.Insert(2, validmove(2))
                            maxcount = childNum
                        End If
                        validmove.Clear()
                    End If
                Next
            Next

            childNodes(childNum).move = {maxmove(0), maxmove(1)}
            childNodes(childNum).value = maxmove(2)
            noder.Children.Add(childNodes(childNum))
            If depth < 1 Then
                noder.Children.Add(Mini(childNum, depth + 1, noder))
                Return noder.Children
            Else
                Return noder.Children
            End If
        Next

    End Function
    Private Function Mini(ByVal rootNum As Integer, depth As Integer, noder As Node)
        Dim mincount As Integer
        Dim minimove As New List(Of Integer)
        Dim validmove As New List(Of Integer)
        Dim childNodes() As Node = New Node(15) {}
        minimove.Add(0)
        minimove.Add(0)
        minimove.Add(1000)
        For childNum = 0 To 15
            childNodes(childNum).piece = childNum + 1
            childNodes(childNum).depth = 0
            NodeCount += 1
            For x = 0 To 7
                For y = 0 To 7
                    If checkValidMove(x, y, piecestats, childNum + 17) = True Then
                        validmove.Add(x)
                        validmove.Add(y)
                        validmove.Add(0)
                        For enemy = 1 To 16
                            If piecestats(enemy).xPos = x And piecestats(enemy).yPos = y Then
                                validmove.Insert(2, piecestats(enemy).value)
                            End If
                        Next
                        If minimove(2) > validmove(2) Then
                            minimove.Insert(0, validmove(0))
                            minimove.Insert(1, validmove(1))
                            minimove.Insert(2, validmove(2))
                            mincount = childNum
                        End If
                        validmove.Clear()
                    End If
                Next
            Next

            childNodes(childNum).move = {minimove(0), minimove(1)}
            childNodes(childNum).value = minimove(2)
            noder.Children.Add(childNodes(childNum))
            If depth < 1 Then
                noder.Children.Add(Max(childNum, depth + 1, noder))
                Return noder.Children
            Else
                Return noder.Children
            End If
        Next
    End Function

0 个答案:

没有答案