I want to script my own ruletiles and i need to test out every combination of 3 tiles (air, tileA and tileB)
My approach was to generate a list of integer arrays containing all possibilities. I was able to generate every possible array with numbers 0 and 1 by just randomly generating arrays and not adding double lists but with 3 numbers the possibilities would be 19683 and the approach was pretty dumb anyways.
How can i get a List with integer arrays in size of 9 with every possible combination of the numbers 0 1 and 3?
I don’t quite understand what you are trying to do but usually, just have a list with all the numbers and then do a double for loop:
List<int> values = new() { 0, 1, 3 };
List<string> results = new();
for (int a = 0; a < values.Count; a++)
{
for (int b = a + 1; b < values.Count; b++)
{
results.Add(a.ToString() + b.ToString());
}
}
foreach (string result in results)
{
Debug.Log(result);
}
The output is string because 0 is one of the numbers, but adding zero anywhere makes no difference. You wouldn’t want 0 as your starting numbers, if you want to combine numbers. That’s my assumption of course.
There are 19683 combinations if order is important (because n^r), however there are only 55 if order is not important (because \frac{(r+n-1)!}{r!(n-1)!} = 55) indicating that there are at least 55 unique classes that can be rotated and mirrored around and each of these have varied amounts of unique configurations.
For example try to think of a simple setup where you have a tile with any combination of roads that connect to the edges.
These are all unique (equivalance) classes x unique configurations + empty tile
1+2+4+4+1 = 12
Only the 4 half-road tiles are missing letting us arrive to 2^4=16 combinations (two kinds of half-road over 4 edges).
Therefore, you want the classes, not the combinations, unfortunately there is no easy formula that can get you that, you have do derive a custom combinations formula for yourself, depending on how exactly the tiles interact with each other. I am also pretty certain you don’t need all 9 tiles to fully describe how your tiling set is interacting.
What i was trying to achive was getting all combinations of numbers for a certain size of array.
For example with the numbers 0, 1 and 2 in a size of 2 i would get: {00},{01},{02},{10},{11},{12},{20},{21},{22}.
Or for example for the numbers 0 and 1 and a size of 3 i would get: {000},{001},{010},{100},{011},{101},{111},{110}.
I would need a function that generates me all combinations of 0, 1 and 2 for a size of 8 (i thought i need the size to be 9 but i actually just need to have every combination of neighbors wich is 8)
I think i would need at least all combinations of the 8 neighbors because i also have tiles that connect to 3 sides. I got what i needet with this code
type void GenerateAllCombinations()
{
List<string> all = new List<string>();
string att;
char[] chars = new char[] {'0','1','2'};
for(int i = 0; i != 1000000; i++)
{
att = "";
for(int c = 0; c != 8; c++)
{
att += chars[UnityEngine.Random.Range(0, 3)];
}
if (!all.Contains(att))
{
all.Add(att);
}
}
all.Sort();
Debug.Log(all.Count);
}
but i feel like thats not an elegant solution is there really no other way?
“Not elegant” is an understatement in this case. Am I reading this correctly? You opted for a very large number of iterations in order to maximize the odds of your random number using all values? This needs to send shivers down your spine
A quick question posted to MS Copilot gave us a recursive example which I adapted slightly as follows:
using System;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
Debug.Log("Test.Start");
Char[] characters = { '0', '1', '2' }; // set of characters
Int32 combinationLength = 2; // length of each combination
var results = new List<String>();
GenerateCombinationsRecursive(characters, "", combinationLength, results);
foreach (var result in results)
{
Debug.Log(result);
}
}
private static void GenerateCombinationsRecursive(Char[] characters, String current, Int32 length, List<String> results)
{
if (length == 0)
{
results.Add(current);
return;
}
foreach (var c in characters)
{
GenerateCombinationsRecursive(characters, current + c, length - 1, results);
}
}
}
Oh my! Thank you yes it is an understatement… i just couldnt think of anything else .
So this works now i just need to… understand how this code works so i actually learn something.
Wait until i make a post about showcasing my ruletile system it seems very overcomplicated as well