mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-10-30 15:06:06 +00:00
DiceRoller
This commit is contained in:
parent
1c443c0592
commit
1e8109abf0
2 changed files with 118 additions and 8 deletions
102
AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs
Normal file
102
AideDeJeu/AideDeJeu/ViewModels/DiceRollerViewModel.cs
Normal file
|
|
@ -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<int> RollMRick()
|
||||
{
|
||||
var dices = new List<int>();
|
||||
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<int, int> DiceValues(int diceMaxFace)
|
||||
{
|
||||
var dices = new Dictionary<int, int>();
|
||||
for(int i = 1; i < diceMaxFace + 1; i++)
|
||||
{
|
||||
dices[i] = 1;
|
||||
}
|
||||
return dices;
|
||||
}
|
||||
|
||||
public Dictionary<int, int> DicesValues(int diceMaxFace, int diceCount)
|
||||
{
|
||||
if(diceCount == 1)
|
||||
{
|
||||
return DiceValues(diceMaxFace);
|
||||
}
|
||||
else
|
||||
{
|
||||
return AddDicesValues(DiceValues(diceMaxFace), DicesValues(diceMaxFace, diceCount - 1));
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<int, int> AddDicesValues(Dictionary<int, int> dice1, Dictionary<int, int> dice2)
|
||||
{
|
||||
var dices = new Dictionary<int, int>();
|
||||
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<List<int>> AllDices(int diceMaxFace, int diceMaxCount)
|
||||
//{
|
||||
// if(diceMaxCount == 1)
|
||||
// {
|
||||
// return new List<List<int>>() { DiceValues(diceMaxFace).Values.ToList() };
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new List<List<int>>()
|
||||
// {
|
||||
// DiceValues(diceMaxFace).Values.ToList(),
|
||||
// AllDices(diceMaxFace, diceMaxCount - 1)
|
||||
// };
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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<string, Item>();
|
||||
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<string, Item>();
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue