- Home /
List of List of Booleans function giving weird results
Hello there! I'm attempting to create a script that creates a 2D list of Boolean values. This 'grid' will be used for determining whether a certain position can hold an object or not. True for yes, false for no. The one stipulation is that the position cannot be true if the column above it is true. If the position above is false, we use an RNG to determine if it gets marked true or stays false. Here's an example:
[T, F, F, T, F] [F, F, T, F, T] [F, T, F, T, F] ... and so on
I wrote the following script to accomplish this:
private int _gridSize;
private List<List<bool>> _grid;
void Awake()
{
//Set up size of grid and initialize the array
_gridSize = 5;
ResetGrid();
//Pass a random first entry to kick things off
GenerateGridPositions( new List<bool>() { true, false, true, true, false } );
}
void Update()
{
//Save last row of grid
var lastRow = _grid[4];
//Set all positions to false
ResetGrid();
//Generate based on saved row
GenerateGridPositions(lastRow);
//For Debug purposes
for (int i = 0; i < _gridSize; i++)
{
Debug.Log(i + ":" + String.Join(",", _grid[i].Select(x=>x.ToString()).ToArray()));
}
}
private void ResetGrid()
{
_grid = new List<List<bool>>();
for (int i = 0; i < _gridSize; i++)
{
_grid.Add(new List<bool>());
}
}
/// <summary>
/// Sets the positions of the obstacles for the next street. Columns should not have obstacles in
/// two successive rows. Probably should go into the obstacle manager.
/// </summary>
private void GenerateGridPositions(List<bool> lastRow)
{
//Set the first row based on the row passed
for (int i = 0; i < _gridSize; i++)
{
if (!lastRow[i])
{
_grid[0].Add(PositionChance());
}
else
{
_grid[0].Add(false);
}
}
//Set each subsequent row, based on the row positions above it
for (int i = 1; i < _gridSize; i++)
{
for (int j = 0; j < _gridSize; j++)
{
if (!_grid[i-1][j])
{
_grid[i].Add(PositionChance());
}
else
{
_grid[i].Add(false);
}
}
}
}
/// <summary>
/// Determines whether an obstacle is added to the grid. Potentially can be skewed to
/// produce trues more often if there is a difficulty setting.
/// </summary>
private bool PositionChance()
{
var rand = Random.Range(0, 10);
var choice = rand%2 == 0;
return choice;
}
But it does something weird upon attaching VS's debugger to the Unity process and setting breakpoints: I've found that when it attempts to add the next position to a row, it adds that position to all the rows. I'm not quite sure why it does this and I'm completely at a loss. I've also implemented this code in Python just to test it and it works exactly as intended. Does anyone see anything I'm not seeing?
Also of note: this function wouldn't normally be called every frame in Update(). I just have it in there for testing purposes.
Edit 2: Updated to reflect accepted answer.
Answer by Bunny83 · Mar 05, 2015 at 03:30 AM
Your problem ist this piece of code:
_grid = new List<List<bool>>();
_grid.AddRange(Enumerable.Repeat(new List<bool>(), _gridSize).ToList());
This will add the same inner List instance multiple times to your outer List. You only create one outer list and one inner list. So each "row" in your outer list references the same inner list.
You should initialize your grid like this:
_grid = new List<List<bool>>();
for(int i = 0; i < _gridSize; i++)
_grid.Add(new List<bool>());
Perfect! Thank you. So if the first parameter of Repeat is a function or instantiation, then it runs that first and then repeats the reference over and over again? That kinda sucks. Is there a LINQ function that actually does what I was attempting?
Your answer
Follow this Question
Related Questions
How to fix some of location have more than 1 object? 2 Answers
Making a camera list 1 Answer
my random task generator not working 1 Answer
A node in a childnode? 1 Answer
Randomizing numbers on a list. 4 Answers