Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with adding to a multidimensional array.

Discussion in 'Scripting' started by Penkine, Dec 10, 2019.

  1. Penkine

    Penkine

    Joined:
    Jun 5, 2019
    Posts:
    15
    I am making a minesweeper clone and I have an array which holds x and y coordinates along with a int variable acting as a boolean(0=False, 1=True). (Shown below)
    Code (CSharp):
    1. private int[,,] gridArray;
    2. [SerializeField] [Range(10, 50)] private int xGridSize = 10;
    3. [SerializeField] [Range(10, 50)] private int yGridSize = 15;
    4. private int isBomb;
    5. gridArray = new int[xGridSize, yGridSize, isBomb];
    If I wanted to say that the tile on coordinates (2, 5) had a bomb I would have the array say: (2, 5, 1) but I am not sure how I would implement this as I am not sure how to add to parts arrays.

    I am pretty new with C#
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    The easiest way, without complications, is using single dimension array 1D.

    If your filed is 4x5 you use 20,elements in an array.
    4 rows and 5 collums.
    So cell 2,3 will be 2 + 3*4=14 index in array
    Cell 1,1 will be 1 + 1*4 = 5 index in array
    Etc.
     
  3. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    I'd probably do this with a Dictionary. Dictionary can only contain unique "keys", but since every grid cell has a unique coordinate (0,0 1,1 etc) this is not a problem. I'd use the coordinates as the key and a boolean value for the bomb (true means the cells has a bomb:

    Code (CSharp):
    1.             Dictionary<string, bool> bombGridDictionary = new Dictionary<string, bool>();
    2.             int gridWidth = 10;
    3.             int gridHeight = 15;
    4.  
    5.             // initialize grid with no bombs
    6.             for (int x = 0; x < gridWidth; x++)
    7.             {
    8.                 for (int y = 0; y < gridHeight ; y++)
    9.                 {
    10.                     bombGridDictionary.Add($"{x},{y}", false);
    11.                 }
    12.             }
    13.  
    14.             // set a bomb on grid x=0,y=3
    15.             bombGridDictionary["0,3"] = true;
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Using array, and class as data type, you can define different types of field, with same reference.
    Yes, you can also use dictionary, 2D vector (x,y) as key and value as data type.
    Now data type from class, can hold
    Code (CSharp):
    1. Class Cell
    2. {
    3.     bool isBomb ;
    4.     enum markedByPlayer ; // that defining what type of marker is used
    5.     int numberOfNeighbourBombs ; // optional.
    6. }
     
  5. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    210
    Just wanna point out that the array
    Code (CSharp):
    1. new int[xGridSize, yGridSize, isBomb]
    is actually a three-dimensional array filled with integer values (like a rubics cube where each block is assigned a number). If you want to go ahead with your multi-dimensional array approach, you would probably want an array like this
    Code (CSharp):
    1. new bool[xGridSize, yGridSize];
    and if you want the position (2,5) to be a bomb, you would set
    Code (CSharp):
    1. gridArray[2,5] = true;
    .

    Not saying that this is the best approach, but its closest to what you were trying to go for.
     
  6. garylinen

    garylinen

    Joined:
    Dec 19, 2019
    Posts:
    3
    A multidimensional array (int[,]) is a single block of memory (essentially a matrix). Try with jagged array, is an array-of-arrays, so an int[][] is an array of int[], each of which can be of different lengths and occupy their own block in memory.