Question How to use multidimensional arrays

Mynotoar

Active member
Joined
Sep 27, 2012
Messages
28
Location
England
Programming Experience
Beginner
Hey again guys!


I've been working on an Ice Puzzle: you're a character (e.g. '*') in a grid (2D array) and you can move using the arrow keys - when you move, you keep going in that direction until you hit a surface. The aim is to pick up a key, which progresses you to the next level. There are three elements that need to be placed on the grid as different characters: the player (*), the block (█), the key (~).


I've got everything mostly working, and I'm at the point where I can change around the layout of the board from the initialised variables outside the Sub. The problem is, it's messy, and it's not intuitive for even me to work with, let alone someone else who wanted to tinker with the code. I'll give an example (I don't want to post the full code simply because it's so long.)


VB.NET:
Dim PlayerCoord() As Integer = {7, 4} 'Player Coordinates, read DOWN and then ACROSS.
Dim BlockDown() As Integer = {7, 6, 2, 1} 'Block Coordinates DOWN
Dim BlockAcross() As Integer = {3, 4, 7, 3} 'Block Coordinates ACROSS
Dim KeyDown As Integer = 7 'Key Coordinates DOWN
Dim KeyAcross As Integer = 2 'Key Coordinates ACROSS


As you can see, it's a tad convoluted. I assign all the values using this Sub.


VB.NET:
Sub PlaceObjectElements()
        For i = 0 To BlockAcross.Count - 1
            PlayGrid(BlockDown(i), BlockAcross(i)) = BlockCursor
        Next
        PlayGrid(KeyDown, KeyAcross) = KeyCursor
        PlayGrid(PlayerCoord(0), PlayerCoord(1)) = PlayerCursor
End Sub


What I want to be able to do for the BlockCoordinates is have coordinates within the array. So I imagine it might look something like this:

Dim BlockCoords(,) As Integer = {{1,2},{2,2}}

The problem is, I don't know how initialising multidimensional arrays works - like in that example, what do 1,2 and 2,2 refer to? How could I specifically assign a down and across value on the grid from those? Essentially, I want to be able to declare all the coordinates in one line so that I can save the level files elsewhere and call them as and when. I'm not even entirely sure if I want to be using 2D arrays or nested arrays. Basically, I just need some clarification on how these things work ^^'.

Hope y'all can help, much love.
 
Wait, never mind! I've figured out how to do it after all.

The declarations:
VB.NET:
Dim PlayerCoord() As Integer = {7, 4}
    'Player Coordinate, read DOWN and then ACROSS.
    Dim BlockCoords(,) As Integer = {{7, 3}, {1, 4}, {3, 4}}
    'Block Coordinates, each pair of braces is a new block.
    Dim KeyCoord() As Integer = {7, 2}
    'Key Coordinate, read DOWN and then ACROSS.

The placement:
VB.NET:
Sub PlaceObjectElements()
        For i = 0 To BlockCoords.GetLength(1)
            PlayGrid(BlockCoords(i, 0), BlockCoords(i, 1)) = BlockCursor
        Next
        PlayGrid(KeyCoord(0), KeyCoord(1)) = KeyCursor
        PlayGrid(PlayerCoord(0), PlayerCoord(1)) = PlayerCursor
    End Sub

So for the blocks, it runs a loop for every element defined as a block (using GetLength which I don't fully understand but, it works, so...) and tells it to use the index value (whatever 'i' is) for which coordinate it picks, and then a 0 or 1 based on whether it's reading the first value (down value) or the second value (across value.) So at BlockCoords(i,0), if i is 0, it will pick the first set of coordinates, {7,4}, and read the 7 as the down value.

And I've got it all running so that I can define all of this in a separate text file if I wanted to have it that way, and build levels that way :).
 
Further revisions!

It should look like this:

VB.NET:
Sub PlaceObjectElements() 'Places player, key and blocks.
        For i = 0 To BlockCoords.GetLength(0) - 1
            PlayGrid(BlockCoords(i, 0), BlockCoords(i, 1)) = BlockCursor
        Next
        PlayGrid(KeyCoord(0), KeyCoord(1)) = KeyCursor
        PlayGrid(PlayerCoord(0), PlayerCoord(1)) = PlayerCursor
    End Sub
 
Back
Top