none
Using multidimentional arrays RRS feed

  • Question

  • Essentially, what I am wanting to do is have an array store 3 values; an X position, a Y position and a number that relates to a type (this will be a seemingly arbitrary number). The way I want to use it is by testing the X position (to see if its in range), then test the Y position (to see if its in range), then using all 3 values, place a picture box with a color and an identifier, the end result being a grid of colors that can be freely moved around and interacted with.

    This is the basis for a game I want to make, which will allow you to move in a way similar to the original Pokemon games, where then you press left or right, the Y position for every set in the array will go up or down, and the same for up and down with the X position.

    What I want to know is how do I call a value from the array, not by any of my values, but by the order its been 'dimed' (so with "dim num(2) = {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}" i can call the first, second and third set separately), the best way to change a value throughout the set (for pressing up, down, left and right) and if there is any better way of doing this.

    Ideally, I want to be able to call set 1, test the first number, then the second, then use all 3, then move to set 2 and do the same. I plan to use a 'for each' function, but need to know what I use the value on to go through the set, or if I need to use 4 values in each set with the first being the sets identifier.

    Sunday, July 15, 2018 9:16 AM

All replies

  • Hi

    There are many many ways to do the kind of thing you mention - here is an example of one of them.

    Here is some code that may act as a pointer for you. This example uses a 'Class' which holds all the Properties for each Rectangle to be shown. This example only uses 2 rectangles for this demo, and sets them up at run time (could more likely be loaded from a file). The Properties in the Class could be extended to hold whatever is needed.

    Instead of using cumbersome Arrays, here, I use a List(Of Class) which simplifies access to each.

    This example illustrates a simple user interaction(click mouse on a Rectangle to see)

    ' Blank Form1 
    ' run then click mouse on
    ' a Rectangle
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Rects As New List(Of Rect)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New Rect
        With r
          .Location = New Point(20, 60)
          .brush = New SolidBrush(Color.Red)
          .Size = New Size(30, 30)
        End With
        Rects.Add(r)
    
        r = New Rect
        With r
          .Location = New Point(80, 20)
          .brush = New SolidBrush(Color.Blue)
          .Size = New Size(50, 50)
        End With
        Rects.Add(r)
        ' here, list holds 2 Rect
    
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        ' draw rectangles
        For Each r As Rect In Rects
          Dim nr As New Rectangle
          With nr
            .Size = r.Size
            .Location = r.Location
          End With
          e.Graphics.FillRectangle(r.brush, nr)
        Next
      End Sub
    
      Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        ' simple interaction
        For Each r As Rect In Rects
          Dim nr As New Rectangle
          With nr
            .Size = r.Size
            .Location = r.Location
          End With
          If nr.Contains(PointToClient(Cursor.Position)) Then
            r.Location = New Point(r.Location.X + 4, r.Location.Y + 4)
            r.Size = New Size(r.Size.Width + 2, r.Size.Height + 2)
            Invalidate()
          End If
        Next
      End Sub
    
      Class Rect
        Property Location As Point
        Property brush As SolidBrush
        Property Size As Size
      End Class
    End Class


    Regards Les, Livingston, Scotland

    Sunday, July 15, 2018 12:40 PM
  • While this works for the most part, with my project I plan to have a large amount of data being stored, and going through every one of them in such a way would be extremely strenuis. The way I want to try and do this is by seeing if the square would be present on screen (if the X and Y positions are within the range of the window), then create a picture box in that location with a color dependant on a third variable. This would work with this method, however it would require a relatively large amount of code for each square, which I am hoping to avoid by using an array, which, while somewhat messy, takes less space.

    I appreciate your reply, and might be able to use some of it for drawing the squares, however its not the most preferable method.

    Just to clarify, the primary thing I need is a way to use a set from an array (being the numbers contained within { }) by calling a number, preferably that which it was called in, and a way to change some of the numbers through every set (for example, add 5 to the first number in every set). Preferably, I would like to do this with an array, but if another method can be used that requires a small amount of space top store all the variables, I am open to suggestions.

    Monday, July 16, 2018 4:39 AM
  • Hi

    OK, if you feel using arrays is the way to go, then good luck. Arrays are NOT easy to use, particularly multidimensional ones.

    I am confused with regard to 'require a relatively large amount of code for each square' - the code required in complex manipulations using Arrays is likely to be very much larger' than using Lists.

    Indeed, depending on what various operations would be needed on the data, then the Class Properties may well be different to those I show.

    For example, what would your code to find the data items which are currently 'on screen' be?

    Here is my altered code, with a Function returning the number of Rect's currently completely on screen. (IsOnScreen)

    ' Form1 with Label1
    ' run then mouse click 
    ' to bring hidden rect
    ' On screen
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Rects As New List(Of Rect)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New Rect
        With r
          .Location = New Point(20, 60)
          .brush = New SolidBrush(Color.Red)
          .Size = New Size(30, 30)
        End With
        Rects.Add(r)
    
        r = New Rect
        With r
          .Location = New Point(80, 20)
          .brush = New SolidBrush(Color.Blue)
          .Size = New Size(50, 50)
        End With
        Rects.Add(r)
    
        ' OFF screen - use Mouse Click
        ' to bring into view
        r = New Rect
        With r
          .Location = New Point(-60, -60)
          .brush = New SolidBrush(Color.Green)
          .Size = New Size(50, 50)
        End With
        Rects.Add(r)
        Label1.Text = IsOnScreen()
      End Sub
      Function IsOnScreen() As String
        Dim i As Integer = 0
        For Each r As Rect In Rects
          Dim nr As New Rectangle
          With nr
            .Size = r.Size
            .Location = r.Location
          End With
          If ClientRectangle.Contains(nr) Then i += 1
        Next
        Return i.ToString
      End Function
      Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
        For Each r As Rect In Rects
          With r
            .Location = New Point(.Location.X + 5, .Location.Y + 5)
          End With
        Next
        Invalidate()
        Label1.Text = IsOnScreen()
      End Sub
      Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        ' draw rectangles
        For Each r As Rect In Rects
          Dim nr As New Rectangle
          With nr
            .Size = r.Size
            .Location = r.Location
          End With
          e.Graphics.FillRectangle(r.brush, nr)
        Next
      End Sub
    
      Class Rect
        Property Location As Point
        Property brush As SolidBrush
        Property Size As Size
      End Class
    
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, July 16, 2018 1:29 PM
    Monday, July 16, 2018 1:27 PM
  • Admittedly, I should have been clearer in what I mean.  I've been trying to explain what I want to do without reference to what I'm trying to accomplish, so Ill try and explain them both here:

    For the past few months, I've been playing with the idea of making a pokemon-movement kind of game where it is grid based and can only move up, down left and right, that would take up a relatively small amount of space. My solution is by storing each square as three numbers; an X position, a Y position and a type. The X and Y position are straightforward, changing each time you press up, down, left or right, and the type determines the color of the square, and its 'type', for example, a green square would let you walk over it freely, a black square would not let you move over it, blue squares would require a special item to cross, etc.

    Ideally, these three numbers would be stored in an array, where I could call a set (a set being {30, 10, 2} for example, where 30 is the X position, 10 is the Y position and 2 is the type), test if its X and Y positions are within the screens boundaries, and if it is, draw it on screen in its allocated location with its allocated color. This test would happen each time a key is pressed, first the type of square the player is trying to move to is tested, if the player can not move to it, nothing happens. If the player can move to it, the X  or Y position for every single square is changed (so if down is pressed, the Y position of every square is increased), and then the test is done, with the new squares being drawn on screen.

    I imagine an 11x11 grid of squares being on screen at any one time, with the player being represented as a dot on the center square. The player itself never moves, but the 'world' around moves relative to the player, like in pokemon. Naturally, this will require a large amount of squares, creating a large would for the player to move around in, and having each square represented by three numbers makes this a fairly simple task to create, the issue I have being the manipulation of the array.

    I have almost everything worked out, except for two things; changing the X and Y positions of every data set, and testing the three numbers in each data set. These issues come from the need to refer not to the number im using, but to the number allocated to the set (the three numbers in {}).

    The first issue involves going through every set and changing the first or second number, either increasing or decreasing the number by a set amount. to do this, I want to call the set, then select the first or second value, and make it equal to itself + or - a set amount. The issue being how I select the set, and then the value in the set, without referring to the numbers in the set, that is referring to the numbers within {}, without referring to it by the numbers inside.

    The second issue is similar, in that I have to refer to the set of numbers, but it involves testing the first two values (the X and Y positions), then if the test passes, all three numbers are used to draw an image on screen. For this, there are two methods, both of which have the same problem. The first requires going through the XY coordinates which appear on screen (being 0, 0 to, let's say 110, 110), then drawing an image in that location using the third number in the set. The second method being going through every single set and testing the XY coordinates to see if it would be on screen, and if it is, draw it, else move on to the next set. The issue with both of these are that I need to get access to a number in a set without calling it directly, but by calling it from the set its in. For example, with the first method, if Im looking at location 30, 40, I would find the set that had {30, 40, _}, then look at the third number. The issue is, I don't know how to do this. Again in the first issue, I need to go through an change either the first or second number in each and every set. I cant call them through their X or Y position, as there will be many sets with the second number being 140, as they will all have different first numbers, and to go through each possibility would be to ruin the point of my project; I need a way to call each set without calling the numbers inside.

    I understand that this probably sounds very confusing, and my wording is likely not the best, as I've been unable to explain this to anyone before now. At this point, what I need is a method to refer to a set in an array (being the three numbers inside {}), without using the numbers contained inside, if that makes any sense. The reason Im hesitant about using your method is because it would take up a very large amount of space, considering how many squares I want to store, however I appreciate any help you can provide, and can understand if you don't completely understand what I mean, after all, I've spent months trying to figure this out and still only have a concept that might not even be possible in visual basic.

    Monday, July 16, 2018 3:19 PM
  • Hi Tasty,

    You wrote:

    "take up a relatively small amount of space"

    What space? Screen pixels? Memory? Why?


    "and its 'type'"

    Mobility better word? Use a number for calcs. Draw the color based on number.

    "stored in an array, where I could call a set "

    This is one of the things a class defines.

    But with a class, unlike arrays, you can have numbers, strings, arrays, lists, bitmaps ... etc all grouped in one set ie Class Object or Game Character. You make a list of the class and then each item in the list is a class object and it contains all the values in one item with one list index.

    Like an expanded version of Leshay's example.


    "so if down is pressed, the Y position of every square is increased"

    How does one change the Y postition of a square? Should be a constant sqare col 2 row 4. Should move the character from square to square. ie

     mypoke1.y = mypoke1.y + 1

    That is just a data change you dont move a square.


    " imagine an 11x11 grid of squares being on screen at any one time, with the player being represented as a dot on the center square. The player itself never moves, but the 'world' around moves relative to the player, like in pokemon. Naturally, this will require a large amount of squares"

    Nope wrong wrong wrong.

    You only need to show the number of squares that fit onto the screen window you are viewing. Then you just move the window on the world grid. The world grid is infinite in size. Or 9,999,999 or something to match your game.

    So you should stop right there and forget the rest and decide this before proceeding.

    IMHO.

    :)

    PS Les are you old enough to remember Wavy Gravy ?

    Monday, July 16, 2018 4:04 PM
  • First of all, I know all of this is quite impractical, but I'm not exactly doing it to be practical. I more want to do it for the method it has, rather then for how well it works, if that makes sense. The way I like to do things is not so much that they 'work', but so that they work 'efficiently', in that it does what I want it to do in the best possible way, in this case, displaying a small amount of a large, changeable set of data. Part of this is storing that data in as little space as possible, which means both length of code and the file size, even though the file size is very small regardless, again, I like efficiency.

    The reason I want to use an array is because it takes up a small amount of space, while things like classes take up more room and use more code, and I consider that to be less efficient.

    As I anticipated, my terminology is not the greatest. When I say X or Y position, I am referring to the number in the set that is used to determine where the imaginary squares X and Y position would be, that is in the set {10, 30, 2}, 10 is the 'X position' and 30 is the 'Y position'. You are completely correct in that it is a data change, but I decided to refer to the values in the set by what they are used for, rather than what they in fact are.

    As for your last point, if I cant find a 'viable' method of doing it with an array, this is likely what I would end up using, however, I personally don't like this very much, as it is somewhat 'messy' to me. I know this makes me sound like a perfectionist, but I like trying to find the most efficient way of doing things, as impractical as they may be. In this case, I want to find the most efficient way of storing the data for the playable world, that I believe that to be by storing three numbers in an array. Completely illogical and impractical, but neat and 'efficient' in my eyes.

    ps. This is my second post, and both times I have seen references to 'Wavy Gravy', whom I have no affiliation with. I just arbitrarily use 'Tasty Gravy' as a silly sounding name.

    • Edited by TastyGravy Monday, July 16, 2018 4:26 PM
    Monday, July 16, 2018 4:24 PM
  • First of all, I know all of this is quite impractical, but I'm not exactly doing it to be practical. I more want to do it for the method it has, rather then for how well it works, if that makes sense. The way I like to do things is not so much that they 'work', but so that they work 'efficiently', in that it does what I want it to do in the best possible way, in this case, displaying a small amount of a large, changeable set of data. Part of this is storing that data in as little space as possible, which means both length of code and the file size, even though the file size is very small regardless, again, I like efficiency.

    The reason I want to use an array is because it takes up a small amount of space, while things like classes take up more room and use more code, and I consider that to be less efficient.

    As I anticipated, my terminology is not the greatest. When I say X or Y position, I am referring to the number in the set that is used to determine where the imaginary squares X and Y position would be, that is in the set {10, 30, 2}, 10 is the 'X position' and 30 is the 'Y position'. You are completely correct in that it is a data change, but I decided to refer to the values in the set by what they are used for, rather than what they in fact are.

    As for your last point, if I cant find a 'viable' method of doing it with an array, this is likely what I would end up using, however, I personally don't like this very much, as it is somewhat 'messy' to me. I know this makes me sound like a perfectionist, but I like trying to find the most efficient way of doing things, as impractical as they may be. In this case, I want to find the most efficient way of storing the data for the playable world, that I believe that to be by storing three numbers in an array. Completely illogical and impractical, but neat and 'efficient' in my eyes.

    Ok then.

    You should do it your way. I probably don't understand exactly what you mean or you dont. But we always try to point out these things.

    And if your way does not work then there you go you tried.

    :)

    Monday, July 16, 2018 4:31 PM

  • What I want to know is how do I call a value from the array, not by any of my values, but by the order its been 'dimed' (so with "dim num(2) = {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}" i can call the first, second and third set separately), the best way to change a value throughout the set (for pressing up, down, left and right) and if there is any better way of doing this.


    That's what accessing the array with multiple dimensions does (num(2, 0), num(2, 1), etc) - it gives you the array element at a particular index which is the order in which the items are placed in the array (just remember that the index is zero-based; the first item is at index 0, the last is at count -1).  The proper way to declare that array is: Dim num(,) = {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}  To loop through every value, you would use nested For loops with counter variables for the length of each dimension:

    Dim num(,) = {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    For i = 0 To num.GetUpperBound(0)
        For j = 0 To num.GetUpperBound(1)
            'add 5 to every value
            num(i, j) += 5
        Next
    Next

    To work with each subarray, just use the outter loop and then test the individual array elements:

    Dim num(,) = {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    MessageBox.Show(num(1, 2).ToString) '=2
    For i = 0 To num.GetUpperBound(0)
        If num(i, 0) > 0 Then
            If num(i, 1) = 2 Then
                num(i, 2) = 5
            End If
        End If
    Next
    MessageBox.Show(num(1, 2).ToString) '=5

    Now, that said, Leshay is right and this is not an efficient code model for the problem at hand.  The fact that you want to make changes to the values is the first hint that you want a reference-type object to store these related bits of information.  Remember that this is object-oriented code, so encapsulate your related information into objects and take advantage of strongly-typed collection classes.

    You probably want to create a class to encapsulate your data, something like:

    Public Class GridItem
        Public Property X As Integer
        Public Property Y As Integer
        Public Property ItemType As Integer
    End Class

    Or better yet, if you know what all of your possible "item types" are going to be:

    Public Class GridItem
        Public Property X As Integer
        Public Property Y As Integer
        Public Property ItemType As GridItemType
    End Class
    
    Public Enum GridItemType
        None
        WhateverOne
        WhateverTwo
        WhateverThree
    End Enum

    Then you can maintain a List(Of GridItem) that contains all of your elements.  You can access this collection by Item(index) to get an element at a particular position in the list, and loop over all of the elements with For Each item As GridItem In theItemList and modify a particular property value as needed.  With an extension method, you can also keep the same inline initialization that you are already using:

    Public Enum GridItemType
        None
        WhateverOne
        WhateverTwo
        WhateverThree
    End Enum
    
    Public Class GridItem
        Public Property X As Integer
        Public Property Y As Integer
        Public Property ItemType As GridItemType
    End Class
    
    Public Module GridItemExtensions
        <Runtime.CompilerServices.Extension>
        Public Sub Add(targetList As List(Of GridItem), x As Integer, y As Integer, itemType As GridItemType)
            targetList.Add(New GridItem() With {.X = x, .Y = y, .ItemType = itemType})
        End Sub
    End Module


    With that code you can write:

    Dim itemList = New List(Of GridItem) From {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    MessageBox.Show(itemList(1).ItemType.ToString) 'WhateverTwo
    For i = 0 To itemList.Count - 1
        Dim element = itemList(i)
        If element.X > 0 Then
            If element.Y = 2 Then
                element.ItemType = GridItemType.WhateverThree
            End If
        End If
    Next
    MessageBox.Show(itemList(1).ItemType.ToString) 'WhateverThree

    Notice how you can easily access an element at any location and loop over all elements to make comparisons and changes.

    The downside to this approach is that you do not have direct access to an element by X,Y location.  But since you don't seem to require that, it may not be a problem. 

    You could calculate the sequence index from X,Y location values which would still be fairly efficient if you only needed the X,Y location access occasionally, compare to sequence index access:

    Dim itemList = New List(Of GridItem) From {{0, 0, 1}, {0, 1, 3}, {0, 2, 2}, {0, 3, 2}, {1, 0, 1}, {1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    For x = 0 To 1
        For y = 0 To 3
            Dim sequenceElement = itemList(x * 4 + y)
            MessageBox.Show($"X:{sequenceElement.X}, Y:{sequenceElement.Y} = {sequenceElement.ItemType}")
        Next
    Next
    

    If you did want X,Y location access to an element more often than sequence index, then you might consider using a dictionary of dictionaries to store the data instead of a list.  With another extension method on the module you can still maintain your collection initializer:

    <Runtime.CompilerServices.Extension>
    Public Sub Add(targetDict As Dictionary(Of Integer, Dictionary(Of Integer, GridItem)), x As Integer, y As Integer, itemType As GridItemType)
        If Not targetDict.ContainsKey(x) Then targetDict.Add(x, New Dictionary(Of Integer, GridItem))
        targetDict(x)(y) = New GridItem() With {.X = x, .Y = y, .ItemType = itemType}
    End Sub

    Then you could access elements by location:

    Dim indexedList = New Dictionary(Of Integer, Dictionary(Of Integer, GridItem)) From {{0, 0, 1}, {0, 1, 3}, {0, 2, 2}, {0, 3, 2}, {1, 0, 1}, {1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    MessageBox.Show(indexedList(1)(2).ItemType.ToString) '= WhateverTwo
    For x = 0 To indexedList.Count - 1
        For y = 0 To indexedList(x).Count - 1
            If x > 0 Then
                If y = 2 Then
                    indexedList(x)(y).ItemType = GridItemType.WhateverThree
                End If
            End If
        Next
    Next
    MessageBox.Show(indexedList(1)(2).ItemType.ToString) '= WhateverThree

    The downside here is that you no longer have direct access to items by sequence (the order in which they are declared).  However, assuming all elements contain sequential values and you know the maximums, you can calculate the X,Y location from a sequence index (the inverse of calculating sequence from X,Y):

    Dim indexedList = New Dictionary(Of Integer, Dictionary(Of Integer, GridItem)) From {{0, 0, 1}, {0, 1, 3}, {0, 2, 2}, {0, 3, 2}, {1, 0, 1}, {1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
    For itemSequence As Integer = 0 To 7
        Dim indexX As Integer = Math.Floor(itemSequence \ 4) 'where 4 is the total number of y dimensions used (0 to 3)
        Dim indexY As Integer = itemSequence - (indexX * 4)
        Dim sequenceElement = indexedList(indexX)(indexY)
        MessageBox.Show($"X:{sequenceElement.X}, Y:{sequenceElement.Y} = {sequenceElement.ItemType}")
    Next


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Monday, July 16, 2018 4:32 PM
    Moderator

  • PS Les are you old enough to remember Wavy Gravy ?

    Hi

    I am certainly old enough, but that phrase isn't in my diminishing memory bank :)


    Regards Les, Livingston, Scotland

    Monday, July 16, 2018 4:38 PM
  • Part of this is storing that data in as little space as possible, which means both length of code and the file size, even though the file size is very small regardless, again, I like efficiency.

    The reason I want to use an array is because it takes up a small amount of space, while things like classes take up more room and use more code, and I consider that to be less efficient.


    These are false assumptions.

    The amount of code has little relevance because the code you write will be parsed into MSIL and then the MSIL is compiled into binary.  Optimizations and built-in factorings will result in the minimum code to get the job done and are not necessarily proportional to the number of lines of code you wrote.

    An array of integers will not necessarily take up less space than a list of class objects; in fact, a multidimensional array will probably take up more space than an equivalent collection of objects.

    Lets take an example from my previous post and use the memory profiler to see what the difference is for two storage methods.  In this example, the array solution uses 0.04Kb more memory than the class solution:

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim itemArray(,) = {{0, 0, 1}, {0, 1, 3}, {0, 2, 2}, {0, 3, 2}, {1, 0, 1}, {1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
        '+0.12Kb allocated
    End Sub
    
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Dim itemList = New List(Of GridItem) From {{0, 0, 1}, {0, 1, 3}, {0, 2, 2}, {0, 3, 2}, {1, 0, 1}, {1, 1, 3}, {1, 2, 2}, {1, 3, 2}}
        '+0.08Kb allocated
    End Sub
    

    This is easy to see by taking a memory snapshot at the beginning of the program, clicking a button and taking another snapshot, then repeating with the other button.  The results are consistent, the initial allocation or two for each method has a difference of about 0.04Kb in favor of the class solution.

    Everyone participating in this thread so far has a vast amount of experience with VB.Net… you should take what everyone has to say into consideration before simply dismissing it as not applicable to what you are trying to do.



    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 16, 2018 4:59 PM
    Moderator
  • Going into this 'project' of mine, I was already out of my field of knowledge, but this goes into a whole new area of the unknown for me. Some of the things you've mentioned here will most definitely help me in other projects, as well as this one, but much of it are things I am yet to get into, such as using dictionaries for this purpose, as well as using lists and classes, which I am yet to use, but have started doing some research on, so naturally this will take me some time to be able to fully use.

    However, I would like to clear up some things with arrays, being the thing Im currently trying to learn about. First, does;

    For i = 0 To num.GetUpperBound(0)

    go though sets that have the same value in the first number, but different values in the second number, such as {1, 2, 2} and {1, 3, 2}.

    Another question is, using something like;

    For i = 0 To num.GetUpperBound(0)
        If num(i, 0) > 0 Then
            If num(i, 1) = 2 Then
                num(i, 2) = 5
            End If
        End If
    Next

    Is it possible to collect the third numbers value just by using something like;

    Dim x
    x = num(i, j, x)

    This very clearly is not something that woulds work, but my question is how is it done?

    Hopefully over the next few days Ill be able to do some more research and better understand the latter part of your reply, but until then much of my understanding of it is only from inference, but it all seems to work as I would want it to work in such a game.

    Monday, July 16, 2018 5:20 PM
  • On your first question, no, it does not.  It goes through every "subset" that you defined.

    Using your example of  {{1, 1, 3}, {1, 2, 2}, {1, 3, 2}}

    It would loop through {1,1,3} then {1,2,2} then {1,3,2}

    On your second question, you would not include the x, and would specify the position you want for "j", so it would just be:

    x = num(i, 2)

    Where "i" is the index of the first bound in the array (the specific group of three that you want) and "2" is the third item in the group of three (because an array index is zero-bound).

    In the example you questioned, the code is looking for any group of three where x is greater than zero and y is equal to 2, then setting the third value for the group to 5 for every group of three that matches.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 16, 2018 5:29 PM
    Moderator
  • Everyone participating in this thread so far has a vast amount of experience with VB.Net… you should take what everyone has to say into consideration before simply dismissing it as not applicable to what you are trying to do.

    I can understand how I might come off as overly confident or somewhat narcissistic, but I am taking note of everything being said, and I am still very new to the language, but I want to learn as much as I can about the areas I am familiar with, rather than move into unfamiliar areas.

    I've only started programing at the beginning of this year, and I am well aware that all of you most definitely know more about these things than I, and completely respect all of you for it, its just that these are all things I am unfamiliar with, and prefer to stay in the areas which I know about before moving to more 'advanced' stuff.


    Monday, July 16, 2018 5:35 PM
  • Everyone participating in this thread so far has a vast amount of experience with VB.Net… you should take what everyone has to say into consideration before simply dismissing it as not applicable to what you are trying to do.

    I can understand how I might come off as overly confident or somewhat narcissistic, but I am taking note of everything being said, and I am still very new to the language, but I want to learn as much as I can about the areas I am familiar with, rather than move into unfamiliar areas.

    I've only started programing at the beginning of this year, and I am well aware that all of you most definitely know more about these things than I, and completely respect all of you for it, its just that these are all things I am unfamiliar with, and prefer to stay in the areas which I know about before moving to more 'advanced' stuff.


    Tasty,

    Dont worry we see lots of questions and you are not overly anything. You have your way in you mind and want to try it out thats perfectly normal. Dont take what Reed wrote the wrong way etc.


    On the other side we know our way.... duh, did I say I was once like you??? ....

    Classes my friend. Lists and classes. Your array is just a list with concrete goloshas.

    But sure work through some problems first.

    Its just this problem gets real complicated doing it your way. I should say your "old" way.

    I think you should work a simple example as an array, then do it as a class. Just a tester. 3 x3 grid say. 1 pokeman at the center square. Then get something working with the array method 1. Then duplicate it with a list and class method 2.

    You cant learn it all at once.

    It takes more time to write the forum questions that to learn what we mean by lists and classes. Just as easy as learning what your question is on arrays. Prob easier for just the array part.

    :)

    PS I don't really understand exactly what you want to do so keep that in mind too.

    Monday, July 16, 2018 5:55 PM
  • Hi

    Here is some code that 'moves' the world.

    NOTE: no 'player' is included as you say the player remains at the center. Also, these are just random colours and do not for a 'Pathway' through the grid that you allude to.

    This example uses a 1000 x 1000 grid of 10 x 10 cells.

    Further NOTE: I see no correlation to a cell and any X/Y values in the array, so they would be redundant.

    Second further NOTE: mouse click on the grid to see the world move

    ' BLANK Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Grid(0, 0) As Integer
      Dim gridsize As Integer = 1000
      Dim cellsize As Integer = 40
      Dim rand As New Random
      Dim c() As Color = {Color.Red, Color.Black, Color.Blue, Color.Green, Color.Yellow, Color.Purple}

      Dim curX As Integer = 100
      Dim curY As Integer = 100
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ReDim Grid(gridsize, gridsize)
        For i As Integer = 0 To gridsize
          For j As Integer = 0 To gridsize
            Grid(i, j) = rand.Next(0, c.Length)
          Next
        Next
        DoubleBuffered = True
      End Sub

      Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim count As Integer = 0
        For i As Integer = 0 To (ClientRectangle.Width \ cellsize)
          For j As Integer = 0 To (ClientRectangle.Width \ cellsize)

            Dim r As Rectangle = New Rectangle(i * cellsize, j * cellsize, cellsize, cellsize)
            e.Graphics.FillRectangle(New SolidBrush(c(Grid(curX + i, curY + j))), r)
            count += 1
          Next
        Next
        Text = count.ToString
      End Sub
      Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        curX += 1
        curY += 1
        Invalidate()
      End Sub
    End Class



    Regards Les, Livingston, Scotland







    • Edited by leshay Monday, July 16, 2018 6:23 PM added cellpaint count to Form text
    Monday, July 16, 2018 6:08 PM