I am trying to create an array of bool arrays. I want to have every combination of bool arrays with the exception of {false, false, false, false}. I want the order of this array to hold its sub arrays such that it ascends in the order of least trues to most trues. (Backwards order is fine, but it must still be ordered.)
Each subset of arrays such that they have the same number of trues should be in random order.
I can hardcode it as such:
private bool[][] GetBoolArrays()
{
var fourDoorList = new List<bool[]>();
fourDoorList.Add(new bool[4] { true, true, true, true });
fourDoorList = fourDoorList.OrderBy(c => Random.Range(float.MinValue, float.MaxValue)).ToList();
var threeDoorList = new List<bool[]>();
threeDoorList.Add(new bool[4] { true, true, true, false });
threeDoorList.Add(new bool[4] { true, true, false, true });
threeDoorList.Add(new bool[4] { true, false, true, true });
threeDoorList.Add(new bool[4] { false, true, true, true });
threeDoorList = threeDoorList.OrderBy(c => Random.Range(float.MinValue, float.MaxValue)).ToList();
var twoDoorList = new List<bool[]>();
twoDoorList.Add(new bool[4] { true, true, false, false });
twoDoorList.Add(new bool[4] { true, false, true, false });
twoDoorList.Add(new bool[4] { true, false, false, true });
twoDoorList.Add(new bool[4] { false, true, true, false });
twoDoorList.Add(new bool[4] { false, true, false, true });
twoDoorList.Add(new bool[4] { false, false, true, true });
twoDoorList = twoDoorList.OrderBy(c => Random.Range(float.MinValue, float.MaxValue)).ToList();
var oneDoorList = new List<bool[]>();
oneDoorList.Add(new bool[4] { true, false, false, false });
oneDoorList.Add(new bool[4] { false, true, false, false });
oneDoorList.Add(new bool[4] { false, false, true, false });
oneDoorList.Add(new bool[4] { false, false, false, true });
oneDoorList = oneDoorList.OrderBy(c => Random.Range(float.MinValue, float.MaxValue)).ToList();
var boolArrayList = new List<bool[]>();
boolArrayList.AddRange(fourDoorList);
boolArrayList.AddRange(threeDoorList);
boolArrayList.AddRange(twoDoorList);
boolArrayList.AddRange(oneDoorList);
return boolArrayList.ToArray();
}
But that is so very dirty!
I can create a list like such, but these are unordered in the way I want:
private bool[][] GetBoolArrays()
{
const int subArraySize = 4;
bool[][] combinations = new bool[(int)Mathf.Pow(2, subArraySize) - 1][];
for (int i = 1; i < Mathf.Pow(2, subArraySize); i++)
{
string binary = System.Convert.ToString(i, 2);
while (binary.Length < subArraySize)
{
binary = 0 + binary;
}
bool[] singleCombination = binary.Select(c => c == '1').ToArray();
combinations[i - 1] = singleCombination;
}
return combinations;
}
So to clarify, I am trying to create an array of arrays. Each sub array has 4 bools. The main array has every combination of subarrays except for all false. The subarrays should be ordered by number of trues, but each section with a set number of trues should be randomized.
I apologize if this is a poor explanation of what I am after...it is a bit difficult to explain. I can clarify on anything needed. Any ideas on how I can clean up the hard coded version of this?