0

so for my A* algorithm I want to create a settings file that contains the data information for the loops creating the grid.

Currently my cells just need to know their field cost but this might change later on. I want to keep the information flexible.

A simple way would be creating a byte array

grid

but I can only store the field cost in there. I thought about taking JSON for this

{
  "1": [ // my first row
    {
      "x": 1, // cell one in row one
      "cost": 1
    },
    {
      "x": 2,
      "cost": null
    },
    {
      "x": 3,
      "cost": 3
    },
    {
      "x": 4,
      "cost": null
    },
    {
      "x": 5,
      "cost": 2
    }
  ],
  "2": [ // my second row
    {
      "x": 1, // cell one in row two
      "cost": 3
    },
    {
      "x": 2,
      "cost": 2
    },
    {
      "x": 3,
      "cost": null
    },
    {
      "x": 4,
      "cost": 1
    },
    {
      "x": 5,
      "cost": 2
    }
  ]
}

By using JSON I can store more data to cells but this structure seems to be way more complex than a simple byte array.

Are there any better solutions for this?

Question3r
  • 2,166
  • 19
  • 100
  • 200
  • Why not a class array? – Mark Baijens May 04 '18 at 16:53
  • 1
    [Serialization](https://stackoverflow.com/questions/5877808/what-is-serializable-and-when-should-i-use-it) is the act of taking an object and persisting its state somewhere, there is built in support for this. – Alex K. May 04 '18 at 16:54

1 Answers1

0

Don't use JSON just read for a text file.

String input = File.ReadAllText( @"c:\myfile.txt" );

int i = 0, j = 0;
int[,] result = new int[10, 10];
foreach (var row in input.Split('\n'))
{
    j = 0;
    foreach (var col in row.Trim().Split(' '))
    {
        result[i, j] = int.Parse(col.Trim());
            j++;
        }
        i++;
    }
A.Mills
  • 357
  • 3
  • 16
  • How does this enable providing more than one piece of information per cell? – Rufus L May 04 '18 at 17:41
  • Easy, each value you assign maps to a dictionary. So if i read in 0 its an open tile, if i read in 1 its a closed tile, 2 = door etc etc. The dictionary can be defined hard coded in your code. – A.Mills May 07 '18 at 13:28