Select 案例未按预期工作(代码特定)

Select Case Not working as expected (Code specific)

我有一个显示图形节点数组的 while 循环(使用 returns 要显示图形节点中的一个字符的函数),然后是 'move' 移动 "Creature" 的过程从一个节点到另一个节点。该生物通过按 'W'、'A'、'S' 或 'D' 来决定去哪里,然后取消占用(使用称为 'deoccupy' 的函数)来自生物并将该生物按照生物想要移动的方向放入图形节点。

我试过在几乎所有地方都抛出一些错误,并使用 trycatch 中断不工作的代码,但我没有收到任何错误。我在 movement "Select Case" 语句中添加了一个 case else。

    While True
        Try
            Console.SetCursorPosition(0, 0)
            myMap.ShowMap()
            myMap.MoveCreatures()
        Catch ex As Exception
            Console.Clear()
            Console.WriteLine(ex.Message)
            Console.ReadKey(True)
        End Try
    End While
    Public Sub MoveCreatures()
        For y = 0 To tiles.GetLength(1) - 1
            For x = 0 To tiles.GetLength(0) - 1
                If tiles(x, y).IsOccupied Then
                    tiles(x, y).MoveCreature()
                End If
            Next
        Next
    Public Sub MoveCreature() Implements ITile.MoveCreature
        If Occupied = True Then
            Creature.Action(Me)
        Else
            Throw New Exception("No creature to move here.")
        End If
    End Sub
    Select Case Console.ReadKey(True).KeyChar
        Case "w"
            If currentTile.North IsNot Nothing Then
                currentTile.North.Occupy(currentTile.Deoccupy)
            Else
                Throw New Exception("Can't go in this direction!")
            End If
        Case "a"
            If currentTile.West IsNot Nothing Then
                currentTile.West.Occupy(currentTile.Deoccupy)
            Else
                Throw New Exception("Can't go in this direction!")
            End If
        ...

'S' 和 'D' 的代码相同,但方向有所不同。例如。 'S' 有

currentTile.South

当生物在 'W' 或 'D' 中移动时,它不会重新显示地图,直到我按下另一个键,而当它在 'A' 或 'S' 中移动时,它会立即刷新地图。我希望它在我按 'W'、'A'、'S' 或 'D'.

中的任何一个时刷新地图

P.S。抱歉放了这么多代码。

While True 是一种 C# 解决方法,因为它们无法创建无限循环。在 VB 中,只需使用 Do Loop 代替:

    Do
        Try
            Console.SetCursorPosition(0, 0)
            myMap.ShowMap()
            myMap.MoveCreatures()
        Catch ex As Exception
            Console.Clear()
            Console.WriteLine(ex.Message)
            Console.ReadKey(True)
        End Try
    Loop

估计问题出在方法上

Public Sub MoveCreatures()
    For y = 0 To tiles.GetLength(1) - 1
        For x = 0 To tiles.GetLength(0) - 1
            If tiles(x, y).IsOccupied Then
                tiles(x, y).MoveCreature()
            End If
        Next
    Next

因为您在找到已占用的单元格后不会退出该函数,这取决于您移动的方向 tiles(x, y).IsOccupied 在方法完成并调用 myMap.ShowMap() 之前再次为真。它对我来说也看起来效率很低——你为什么不跟踪生物的当前位置而不是循环遍历整个网格,例如在生物对象中?