Storing map information with variable amount of elements

How you would store data structure for map information in your game if it was like this

Sectors (Fixed amount)
L Screens (May be variable, most cases 6)
L Points (Most cases variable, from 6 to 25, depending on Sector & Screen pair)

I tryed to compile a sample Excel grid to show this from better perspective.


My first approach was to use simple array like:
int[,] map = new int[numSectors, numScreens, numPoints];

But this lead me to situation where I don’t know for sure how many Points are there in particular Screen, since map.GetLength(2) always will return numPoints while there could be “empty” elements for this particular part of array.

As per my example above I cannot reference to map[0, 0, 7] (because there’s only 6 elements), but I can easily access map[0, 1, 7] (because there are 25 elements).

Any advice here?

Since you have variable amounts all over the place, seems you’re looking for a list, within a list, within a list.

I would use classes.

public class Map
{
  IList<Sector> Sectors { get; } = new List<Sector>();
}

public class Sector
{
  IList<Screen> Screens { get; } = new List<Screen>(6);
}

public class Screen
{
  IList<Point> Points { get; } = new List<Point>(25);
}

public class Point
{
  // not clear what information a point holds
}

public void SomeFunction()
{
  var Map = // ... initialize the map here
  foreach (var sector in Map.Sectors)
    foreach (var screen in sector.Screens)
      foreach (var point in screen.Points)
        // .. do something with point

 // alternatively
  for (int i = 0; i < Map.Sectors.Count)
    for (int j = 0; j < Map.Sectors[i].Screens.Count)
      for (int k = 0; k < Map.Sectors[i].Screens[j].Points.Count)
        // do something with Map.Sectors[i].Screens[j].Points[k]
}

Got it, thanks! Never saw such solution in previous projects. Is this OK to use Lists like this? I mean, is this treated as a “normal” usage of Lists?

Yes. This is one way to implement spatial partitioning. If you want to read more about spatial partitioning in general and see some example images, check Wikipedia’s Quadtree page.