验证用户输入时,值超出数组范围

时间:2019-02-28 10:49:11

标签: vb.net

用于命令行步步高项目。

当用户选择一块要移动的东西时,通过此功能检查他们的东西

    Function CheckIsValid(ByVal PiecePosition As Integer, ByVal UserMoveToSelect As Integer) As Boolean

    If CurrentPlayer = Player1 Then

        If UserMoveToSelect = (PiecePosition + dice1 And Gameboard(PiecePosition + dice1) > -2) Or (UserMoveToSelect = PiecePosition + dice2 And Gameboard(PiecePosition + dice2) > -2) Or (UserMoveToSelect = PiecePosition + dice1 + dice2 And Gameboard(PiecePosition + dice1 + dice2) > -2) Then
                CheckIsValid = True
            Else
                CheckIsValid = False
            End If




    ElseIf CurrentPlayer = Player2 Then

        If UserMoveToSelect = (PiecePosition - dice1 And Gameboard(PiecePosition - dice1) < 2) Or (UserMoveToSelect = PiecePosition - dice2 And Gameboard(PiecePosition - dice2) < 2) Or (UserMoveToSelect = PiecePosition - dice1 - dice2 And Gameboard(PiecePosition - dice1 - dice2) < 2) Then
                CheckIsValid = True
            Else
                CheckIsValid = False
            End If




    End If
    Return CheckIsValid
End Function

如果用户块在板子两边的末端附近,那么即使我只是为了验证,他们也会出现超出范围的错误,关于我可以用来实现此问题的任何建议。 ..也许尝试捕获异常?

编辑:这些值是根据Gameboard(23)数组检查的,错误是指棋子位置加上任何骰子值大于23。

0 个答案:

没有答案