public static List <FarkleAction> GenerateActions(Roll roll) { List <FarkleAction> actions; if (GenCache.TryGetValue(roll, out actions)) { return(new List <FarkleAction>(actions)); } actions = new List <FarkleAction>(); var dice = roll.Count(); for (int subdice = 1, diceToRoll = dice - subdice; subdice <= dice; subdice++, diceToRoll--) { var combos = Dice.RollCombinations(subdice, roll); foreach (var subroll in combos) { int score; if (!ValidScoreCache.TryGetValue(subroll, out score)) { Roll subrollcopy = new Roll(subroll); score = ValidScore(subrollcopy); ValidScoreCache.Add(subroll, score); } if (score > 0) { actions.Add(new FarkleAction(score, diceToRoll == 0 ? 6 : diceToRoll)); } } } GenCache.Add(roll, actions); return(new List <FarkleAction>(actions)); }
public static List <Roll> RollCombinations(int dice, Roll roll) { Tuple <int, Roll> key = new Tuple <int, Roll>(dice, roll); List <Roll> combos; if (RollComboCache.TryGetValue(key, out combos)) { return(combos); } if (dice == roll.Count()) { combos = new List <Roll> { new Roll(roll.OrderBy(x => x)) }; RollComboCache.Add(key, combos); return(combos); } combos = new List <Roll>(); for (int a = 0; a < roll.Count; a++) { if (dice == 1) { combos.Add(new Roll { roll[a] }); continue; } for (int b = a + 1; b < roll.Count; b++) { if (dice == 2) { combos.Add(new Roll { roll[a], roll[b] }); continue; } for (int c = b + 1; c < roll.Count; c++) { if (dice == 3) { combos.Add(new Roll { roll[a], roll[b], roll[c] }); continue; } for (int d = c + 1; d < roll.Count; d++) { if (dice == 4) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d] }); continue; } for (int e = d + 1; e < roll.Count; e++) { if (dice == 5) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d], roll[e] }); continue; } for (int f = e + 1; f < roll.Count; f++) { combos.Add(new Roll { roll[a], roll[b], roll[c], roll[d], roll[e], roll[f] }); } } } } } } combos = combos.Distinct().ToList(); RollComboCache.Add(key, combos); return(combos); }