Last active
February 29, 2024 02:17
-
-
Save AndreSacilotto/79cd1411f5abba5fe5f7142498154b38 to your computer and use it in GitHub Desktop.
DiceCombinatory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Roll(string s = "", int depth = 1, int sides = 6) | |
{ | |
if (depth == 0) | |
Console.WriteLine(s.Substring(1)); | |
else | |
for (int i = 1; i <= sides; i++) | |
Roll(s + "," + i, depth - 1); | |
} | |
static int[][] DiceCombinatoryEntry(int times, int diceSides = 6) | |
{ | |
var size = (int)Math.Pow(diceSides, times); | |
var arrayList = new List<int[]>(size); | |
var loopIndex = new int[times]; | |
DiceCombinatory(times, diceSides, arrayList, loopIndex); | |
return arrayList.ToArray(); | |
} | |
static void DiceCombinatory(int times, int diceSides, List<int[]> arrayList, int[] loopIndex) | |
{ | |
if (times == 0) | |
{ | |
var len = loopIndex.Length; | |
var arr = new int[len]; | |
for (int i = 0; i < len; i++) // Clone Array in Reverse | |
arr[i] = loopIndex[len - i - 1]; | |
arrayList.Add(arr); | |
//arrayList[index++] = arr; | |
return; | |
} | |
for (int i = 1; i <= diceSides; i++) | |
{ | |
loopIndex[times - 1] = i; | |
DiceCombinatory(times - 1, diceSides, arrayList, loopIndex); | |
} | |
} | |
static int CountSum(int[][] combinations, int value) | |
{ | |
int count = 0; | |
foreach (var combination in combinations) | |
if (combination.Sum() == value) | |
count++; | |
return count; | |
} | |
#region Main | |
const int SIDES = 6; | |
const int TIMES = 2; | |
var dice = DiceCombinatoryEntry(TIMES, SIDES); | |
var combinatory = dice.Length; | |
//Array.ForEach(dice, x => Console.Write(string.Join(",", x.Select(x => x.ToString())) + "\t")); | |
for (int i = 0; i < dice.Length; i++) | |
{ | |
Console.Write(string.Join(",", dice[i]) + "\t"); | |
if (i % SIDES == SIDES-1) | |
Console.WriteLine(); | |
} | |
var occurrences = CountSum(dice, 7); | |
var chance = occurrences / (double)combinatory; | |
Console.WriteLine($"{occurrences} / {combinatory} = {chance:0.##%}"); | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment