- Home /
Finding all possible combinations off a 3x3 grid
I have a 3x3 grid of bools and I'm trying to work out all the combinations I can have around the center cell. I.E.
I tried to work it out on a spreadsheet, but keep getting lost, as I don't want any repeated combinations. I'm trying to think up a way of writing a function that will work out all the combinations and output them to the console so I can copy and paste it into notepad. I know I'll need a list to store the outcomes to reference before writing to debug console, but can't come up with an iteration loop.
Any ideas?
Answer by yogihermawan1993 · Feb 02, 2021 at 02:55 AM
Take a look at this awesome post: Tile Bitmasking
Scroll down to the "8-Bit Bitmasking" section, I think it might help in a way.
I was actually looking at something like this, but given there was 256 combinations and only 47 tiles, I wasn't sure how to get it to work. So this is extremely helpful. Thank you!
Actually, I could use a bit more help. It says in the tut to use a data structure to convert the result (which could be 0-255) to a value for the tile (0-47).
public Texture2D GetAutoTile (int x, int y, rpg_Tile tile)
{
bool top = CheckAutoTile (x, y-1, id);
bool top_left = CheckAutoTile (x-1, y-1, id);
bool top_right = CheckAutoTile (x+1, y-1, id);
bool left = CheckAutoTile (x-1, y, id);
bool right = CheckAutoTile(x+1, y, id);
bool bottom = CheckAutoTile (x, y+1, id);
bool bottom_left = CheckAutoTile (x-1, y+1, id);
bool bottom_right = CheckAutoTile (x+1, y+1, id);
int test =
(left ? 0b00000001:0) +
(bottom_left ? 0b00000010:0) +
(bottom ? 0b00000100:0) +
(bottom_right ? 0b00001000:0) +
(right ? 0b00010000:0) +
(top_right ? 0b00100000:0) +
(top ? 0b01000000:0) +
(top_left ? 0b10000000:0)
;
Debug.Log(test);
The example the tutorial gives is this.
{ 2 = 1, 8 = 2, 10 = 3, 11 = 4, 16 = 5, 18 = 6, 22 = 7, 24 = 8, 26 = 9, 27 = 10, 30 = 11, 31 = 12, 64 = 13, 66 = 14, 72 = 15, 74 = 16, 75 = 17, 80 = 18, 82 = 19, 86 = 20, 88 = 21, 90 = 22, 91 = 23, 94 = 24, 95 = 25, 104 = 26, 106 = 27, 107 = 28, 120 = 29, 122 = 30, 123 = 31, 126 = 32, 127 = 33, 208 = 34, 210 = 35, 214 = 36, 216 = 37, 218 = 38, 219 = 39, 222 = 40, 223 = 41, 248 = 42, 250 = 43, 251 = 44, 254 = 45, 255 = 46, 0 = 47 }
Now my inexperienced solution is just use a switch statement. But is there a more elegant solution?
Answer by Llama_w_2Ls · Feb 02, 2021 at 08:43 AM
Here's a class which uses Heap's recursive algorithm for working out all possible permutations of an array. Permutations are ordered, which means 4, 3 is the same as 3, 4 so it doesn't count as a permutation. I used an array of numbers from 1 to 9, and wherever the middle item (5) is in the middle, the permutation is added to a list of permutations. There are a lot here, since without the middle item condition, the length of the list would be 9! (factorial):
class Heap
{
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
HeapGen heapGen = new HeapGen(a);
foreach (int[] array in heapGen.GeneratedPermutations)
{
foreach (int item in array)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
}
class HeapGen
{
public List<int[]> GeneratedPermutations = new List<int[]>();
public HeapGen(int[] data)
{
HeapPermutation(data, data.Length, data.Length);
SiftThroughArray();
}
// Generating permutations using Heap Algorithm
void HeapPermutation(int[] a, int size, int n)
{
// if size becomes 1 then prints the obtained
// permutation
if (size == 1)
{
List<int> permutation = new List<int>();
foreach (int item in a)
{
//Console.Write(item + " ");
permutation.Add(item);
}
//Console.WriteLine();
GeneratedPermutations.Add(permutation.ToArray());
}
for (int i = 0; i < size; i++)
{
HeapPermutation(a, size - 1, n);
// if size is odd, swap 0th i.e (first) and
// (size-1)th i.e (last) element
if (size % 2 == 1)
{
int temp = a[0];
a[0] = a[size - 1];
a[size - 1] = temp;
}
// If size is even, swap ith and
// (size-1)th i.e (last) element
else
{
int temp = a[i];
a[i] = a[size - 1];
a[size - 1] = temp;
}
}
}
void SiftThroughArray()
{
List<int[]> NormalPermutations = new List<int[]>();
for (int i = 0; i < GeneratedPermutations.Count; i++)
{
int[] array = GeneratedPermutations[i];
// Any array without the middle item in the same place
// should be removed
if (array[4] == 5)
{
NormalPermutations.Add(array);
}
}
GeneratedPermutations = NormalPermutations;
}
}
Now that I think about it... This doesn't really help.
Your answer
Follow this Question
Related Questions
Using GetPixels to break down an image, and only getting the last part. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Swipe incorrectly working 0 Answers