From 1e8109abf04e34894a38f6b641cf9af3193f528d Mon Sep 17 00:00:00 2001 From: Yan Maniez Date: Tue, 30 Apr 2019 13:26:42 +0200 Subject: [PATCH] DiceRoller --- .../ViewModels/DiceRollerViewModel.cs | 102 ++++++++++++++++++ AideDeJeu/AideDeJeuUnitTest/UnitTest1.cs | 24 +++-- 2 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs diff --git a/AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs new file mode 100644 index 00000000..57c1e6d1 --- /dev/null +++ b/AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace AideDeJeu.ViewModels +{ + public class DiceRollerViewModel : BaseViewModel + { + private Random _Random = null; + + public DiceRollerViewModel() + { + _Random = new Random(Environment.TickCount); + } + + public List RollMRick() + { + var dices = new List(); + var roll = RollDices(6, 2); + dices.Add(6 + roll); + dices.Add(19 - roll); + roll = RollDices(6, 2); + dices.Add(6 + roll); + dices.Add(19 - roll); + roll = RollDices(6, 2); + dices.Add(6 + roll); + dices.Add(19 - roll); + return dices; + } + public int RollDices(int diceMaxFace, int diceCount = 1, int diceBonusMalus = 0) + { + int dicesResult = diceBonusMalus; + for (int i = 0; i < diceCount; i++) + { + dicesResult += _Random.Next(diceMaxFace) + 1; + } + return dicesResult; + } + + public Dictionary DiceValues(int diceMaxFace) + { + var dices = new Dictionary(); + for(int i = 1; i < diceMaxFace + 1; i++) + { + dices[i] = 1; + } + return dices; + } + + public Dictionary DicesValues(int diceMaxFace, int diceCount) + { + if(diceCount == 1) + { + return DiceValues(diceMaxFace); + } + else + { + return AddDicesValues(DiceValues(diceMaxFace), DicesValues(diceMaxFace, diceCount - 1)); + } + } + + public Dictionary AddDicesValues(Dictionary dice1, Dictionary dice2) + { + var dices = new Dictionary(); + foreach (var kv1 in dice1) + { + foreach (var kv2 in dice2) + { + var key = kv1.Key + kv2.Key; + var value = kv1.Value + kv2.Value; + if (!dices.ContainsKey(key)) + { + dices[key] = value; + } + else + { + dices[key] += value; + } + } + } + return dices; + } + + //public List> AllDices(int diceMaxFace, int diceMaxCount) + //{ + // if(diceMaxCount == 1) + // { + // return new List>() { DiceValues(diceMaxFace).Values.ToList() }; + // } + // else + // { + // return new List>() + // { + // DiceValues(diceMaxFace).Values.ToList(), + // AllDices(diceMaxFace, diceMaxCount - 1) + // }; + // } + //} + + } +} diff --git a/AideDeJeu/AideDeJeuUnitTest/UnitTest1.cs b/AideDeJeu/AideDeJeuUnitTest/UnitTest1.cs index f5b1061a..18d3f5d0 100644 --- a/AideDeJeu/AideDeJeuUnitTest/UnitTest1.cs +++ b/AideDeJeu/AideDeJeuUnitTest/UnitTest1.cs @@ -2,6 +2,7 @@ using AideDeJeu.ViewModels; using AideDeJeuLib; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; namespace AideDeJeuUnitTest @@ -12,16 +13,23 @@ namespace AideDeJeuUnitTest [TestMethod] public async Task TestMethod1() { - var allItems = new Dictionary(); - var store = new StoreViewModel(); - var item = store.ToItem(null, AideDeJeu.Tools.Helpers.GetResourceString($"AideDeJeu.Data.sandbox.md"), allItems); - var md = item.Markdown; - var children = await item.GetChildrenAsync(); - foreach(var iitem in children) + var diceRoller = new DiceRollerViewModel(); + var diceRolls = diceRoller.DicesValues(6, 3); + foreach(var diceRoll in diceRolls) { - md += iitem.Markdown; + Debug.WriteLine($"{diceRoll.Key} => {diceRoll.Value / 3}"); } - Assert.IsNotNull(md); + Assert.IsNotNull(diceRolls); + //var allItems = new Dictionary(); + //var store = new StoreViewModel(); + //var item = store.ToItem(null, AideDeJeu.Tools.Helpers.GetResourceString($"AideDeJeu.Data.sandbox.md"), allItems); + //var md = item.Markdown; + //var children = await item.GetChildrenAsync(); + //foreach(var iitem in children) + //{ + // md += iitem.Markdown; + //} + //Assert.IsNotNull(md); } } }