寻求解决无限循环

时间:2018-09-16 11:50:23

标签: vb.net infinite-loop

我有一段代码可以用Java和Python生成地牢,但似乎在Visual Basic中创建了一个无限循环。我可能忽略了一个小问题。

Dim Touching As Boolean = True
    While Touching = True
        Touching = False
        'Run through all the rooms and check if they overlap
        For i = 0 To numRooms
            Dim aPos As Point = RoomXY(i)
            Dim aDime As Point = RoomWH(i)
            For j = 0 To numRooms
                Dim bPos As Point = RoomXY(j)
                Dim bDime As Point = RoomWH(j)
                If (aPos = bPos) And (aDime = bDime) Then
                    Continue For
                Else

                    'Check for overlapping
                    Dim H_Overlaps As Boolean = (aPos.X <= bPos.X + bDime.X) And (bPos.X <= aPos.X + aDime.X)
                    Dim V_Overlaps As Boolean = (aPos.Y <= bPos.Y + bDime.Y) And (bPos.Y <= aPos.Y + aDime.Y)
                    If H_Overlaps AndAlso V_Overlaps Then
                        Touching = True
                        'Find the minimum amount of movment that stops the squares from touching
                        Dim dx = Math.Min(Math.Abs((aPos.X + aDime.X) - (bPos.X + 2)), Math.Abs(aPos.X - (bPos.X + bDime.X + 2)))
                        Dim dy = Math.Min(Math.Abs((aPos.Y + aDime.Y) - (bPos.Y + 2)), Math.Abs(aPos.Y - (bPos.Y + bDime.Y + 2)))
                        If dx <= dy Then
                            dy = 0
                        Else
                            dx = 0
                        End If
                        If aPos.X >= bPos.X Then
                            RoomXY(i) = New Point(RoomXY(i).X + (dx / 2), RoomXY(i).Y)
                            RoomXY(j) = New Point(RoomXY(j).X - (dx / 2), RoomXY(j).Y)
                        Else
                            RoomXY(i) = New Point(RoomXY(i).X - (dx / 2), RoomXY(i).Y)
                            RoomXY(j) = New Point(RoomXY(j).X + (dx / 2), RoomXY(j).Y)
                        End If
                        If aPos.Y >= bPos.Y Then
                            RoomXY(i) = New Point(RoomXY(i).X, RoomXY(i).Y + (dy / 2))
                            RoomXY(j) = New Point(RoomXY(j).X, RoomXY(j).Y - (dy / 2))
                        Else
                            RoomXY(i) = New Point(RoomXY(i).X, RoomXY(i).Y - (dy / 2))
                            RoomXY(j) = New Point(RoomXY(j).X, RoomXY(j).Y + (dy / 2))
                        End If


                    End If
                End If

            Next

        Next

    End While

为这部分代码指定房间尺寸,检查它们是否重叠,然后将它们移开。我已经尝试了一切,但似乎无法打破无限循环。

0 个答案:

没有答案