- Home /
Selecting random array
I am trying to create a system where upon starting the game map will be selected and then generated from lets say map pool. All my maps are jaggedArrays. I tried to put jaggedArrays into ArrayList and generate from that but that did not work then I tried putting all my maps into 1 jaggedArray which look like this.
maps[0] = new string[] { null, null, null, null, null, null, null, null, null, "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "eol"}
maps[1] = new string[] { null, null, null, null, null, null, null, null, null, "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "eol"}
"eol" - represents end of line after "eol" map information continues on another row.
(map is 24 by x and rows are limited by "eol" - x is undefined number)
Converting part of jaggedArray into separate array
// vyberMapy transates to chooseMap
public static void vyberMapy()
{
int number = Random.Range(1, 3);
int i = 0;
for (int j = 0; j < maps[number].Length; j++)
{
map1[i][j] = maps[number][j].ToString();
if (maps[number][j].ToString() == "eol")
{
i++;
}
}
}
Conclusion I need to figure way to randomly select one of there maps, via randomly generated number atm. of range between 1-2 and using return function to return chosen map.
Answer by Zodiarc · Mar 08, 2018 at 01:11 PM
I don't really understand what you're trying to achieve, but have you tried
return map[Random.Range(0, map.Length)];
This will return a random "line" of your map array. Note that the max value is exclusive so you don't need to subtract 1 from it to prevent ArrayIndexOutOfRangeExceptions.
So let's say you have the following construct:
ArrayList<string[]> maps = new ArrayList<string[]>();
maps.Add(new string[]{1,2,3,4,5,6,7,8});
maps.Add(new string[]{8,7,6,5,4,3,2,1});
maps.Add(new string[]{5,6,7,8,1,2,3,4});
you could do the followoing:
ArrayList<string[]>() generatedMap = new ArrayList<string[]>();
for(int i = 0; i < <yourNumberOfLines>; i++) {
generatedMap.Add(maps[Random.Range(0,maps.Count)]);
}
Follow this Question
Related Questions
How to make certain elements in an array rarer when using random selection? 4 Answers
Writing Jagged Arrays 1 Answer
When attempting to serialize and save a jagged array it gives me an error message 1 Answer
Changing Questions from static to variable driven.. 0 Answers
Syntax to initialize 2D (Jagged), built-in array at runtime 1 Answer