如何修改该字符串?

时间:2011-04-19 02:52:46

标签: vb.net string

美好的一天,

我不知道如何描述我的问题(这更像是一个挑战) 但我会尽我所能。

我正在开发一个基于文本的游戏,当你的角色移动时,你会得到诸如房间名称和下面的信息,一个描述(示例中的Lorem ipsum),这是一个字符串数组。我想知道如何追加字符。我其实 试图显示由ASCII字符组成的小地图。见第二集团 代码示例。

自:

Room Name Here
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tempus
orci diam. Curabitur hendrerit augue et lorem vulputate semper. Nullam
aliquam eleifend sapien nec bibendum. Donec accumsan leo eu orci
elementum semper in mollis metus. Ut ipsum diam, suscipit vel bibendum
non, congue eu nisi. Donec justo dolor, scelerisque nec fringilla nec,
aliquet sit amet elit. Morbi elementum pharetra odio, nec accumsan
velit lacinia quis.

[Exits: none]   [Doors: none]

分为:

Room Name Here
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis +-----+
tempus orci diam. Curabitur hendrerit augue et lorem vulputate  |  #  |
semper. Nullam aliquam eleifend sapien nec bibendum. Donec      |#-#-#|
accumsan leo eu orci elementum semper in mollis metus. Ut ipsum |  #  |
diam, suscipit vel bibendum non, congue eu nisi. Donec justo    +-----+
dolor, scelerisque nec fringilla nec, aliquet sit amet elit. Morbi
elementum pharetra odio, nec accumsan velit lacinia quis.

[Exits: none]   [Doors: none]

游戏本身是用VB.NET制作的,但伪代码的解决方案也是如此 欢迎。

此外,每条线的长度都是它必须的最大长度。

谢谢你们!

修改 所需的输出必须是字符串数组,因为此数据稍后会发送到使用套接字连接的客户端。

2 个答案:

答案 0 :(得分:1)

听起来你正在尝试将窗口内容构建为一个长字符串?

IMO你最好将控制台窗口分成逻辑区域,即一个用于文本,一个用于房间地图,出口/门区域可以是单独的区域或单个区域。

您可以发送到终端/控制台窗口以重新定位光标的转义序列,或者有些库可以为您执行此类操作,尤其是Unix / Linux上的[n] curses - 寻找类似的东西对于Windows。

答案 1 :(得分:1)

如果我们假设您的行长度为80个字符,请将文本行的最大长度限制为70个字符。如果实际长度小于70,请将空格的差异添加到您希望添加的地图数据的前面。这也取决于使用等宽字体。

Dim LineOfText As String = "This is a test text"
Dim LineOfMap As String = "+------+"
Dim newLine As String

newLine = LineOfText & LineOfMap.PadLeft(80 - LineOfText.Length)

编辑:添加数组,使用Courier New Font

Dim LineOfText(6) As String
Dim LineOfMap(4) As String

Public Sub CreateArray()

    LineOfMap(0) = "+-----+"
    LineOfMap(1) = "|  #  |"
    LineOfMap(2) = "|#-#-#|"
    LineOfMap(3) = "|  #  |"
    LineOfMap(4) = "+-----+"

    LineOfText(0) = "  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tempus"
    LineOfText(1) = "orci diam. Curabitur hendrerit augue et lorem vulputate semper. Nullam"
    LineOfText(2) = "aliquam eleifend sapien nec bibendum. Donec accumsan leo eu orci"
    LineOfText(3) = "elementum semper in mollis metus. Ut ipsum diam, suscipit vel bibendum"
    LineOfText(4) = "non, congue eu nisi. Donec justo dolor, scelerisque nec fringilla nec,"
    LineOfText(5) = "aliquet sit amet elit. Morbi elementum pharetra odio, nec accumsan"
    LineOfText(6) = "velit lacinia quis."

    Dim x As Integer

    For x = 0 To 4
        LineOfText(x) = LineOfText(x) & LineOfMap(x).PadLeft(80 - LineOfText(x).Length) & vbCrLf
    Next
    For x = 0 To 6
        Label1.Text = Label1.Text & LineOfText(x)
    Next
End Sub

编辑:如果行数小于5,您可以创建一个函数并增大数组,以确保为地图留出空间。

Public Function CreateArray(text() As String, map() As String) as String()

    Dim x As Integer
    If text.Length < map.Length Then
        For x = 0 To map.Count - 1
            If x < text.Count - 1 Then
                text(x) = text(x) & map(x).PadLeft(80 - text(x).Length) & vbCrLf
            Else
                Array.Resize(text, text.Count + 1)
                text(x) = map(x).PadLeft(80) & vbCrLf
            End If

        Next
   Else
        For x = 0 To map.Count - 1
            text(x) = text(x) & map(x).PadLeft(80 - text(x).Length) & vbCrLf
        Next
   End If

   return text

End Sub