0

I have some code where I have a "map" stored in a string and then I put this into an array. This array has seperated the string into characters for each index position. I now want to put these characters into a two dimensional array as shown below is my attempt:

Sub DisplayMap()
    Dim MapTog As String
    MapTog = ("--------------------------------------------------------------------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-------------------                                        ---------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("--------------------------------------------------------------------------------")
    Dim MapSep() As Char = MapTog.ToCharArray
    For Count = 0 To 24
        For Count2 = 0 To 79
            Map(Count)(Count2) = MapSep(Count2)
            Console.Write(MapSep(Count2))
        Next Count2
    Next Count
    Console.SetCursorPosition(0, 0)

End Sub

I think I have a problem to do with my For loops, but I cannot figure it out. Any help is much appreciated, thanks! :)

1 Answer 1

0

Here's one approach:

Module Module1

    Public Map As New List(Of List(Of Char))

    Public Sub Main()
        CreateMap()
        DisplayMap()

        Console.ReadLine()
    End Sub

    Public Sub DisplayMap()
        Console.Clear()
        ' this demonstrates how you can access each char separately:
        For y As Integer = 0 To Map.Count - 1
            For x As Integer = 0 To Map(y).Count - 1
                Console.SetCursorPosition(x, y)
                Console.Write(Map(y)(x))
            Next
        Next

        ' ... or you could do ...

        Console.Clear()
        ' this demonstrates how you can convert each row to a string
        For y As Integer = 0 To Map.Count - 1
            Console.SetCursorPosition(0, y)
            Console.Write(New String(Map(y).ToArray))
        Next
    End Sub

    Public Sub CreateMap()
        Dim MapTog As String = _
             "--------------------------------------------------------------------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-------------------                                        ---------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "--------------------------------------------------------------------------------"

        Map.Clear()
        For Each line As String In MapTog.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
            Map.Add(New List(Of Char)(line.ToCharArray))
        Next
    End Sub

End Module
4
  • Thanks for the help, I'm very new to coding so this is great. However I seem to have the problem "The value must be greater than or equal to zero and less than the console's buffer size in that dimension. Parameter name: left Actual value was 80." in the displaymap() sub at Console.SetCursorPos(x, y) Commented Jun 7, 2013 at 15:55
  • It's saying that your map WIDTH is bigger than your console screen. Either decrease your map width (remove some chars from each line), or increase the size of your console screen (click the top left icon of the console window and select properties, then change buffer and width to 80).
    – Idle_Mind
    Commented Jun 7, 2013 at 16:08
  • I used Console.BufferWidth = 80 and if then said the buffer was too small as it needed 81, however the width should only be 80 characters long. Whatever buffer I enter it always is 1 extra. This may have something to do with that MapTog is roughly 1500 characters long Commented Jun 7, 2013 at 16:19
  • The SetCursor() function is 0 (zero) based. Note that I loop with : For x As Integer = 0 To Map(y).Count - 1 So the very leftmost column is at 0 (zero) and the rightmost column would be 79.
    – Idle_Mind
    Commented Jun 7, 2013 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.