diff options
author | TrueDoctor <d-kobert@web.de> | 2018-07-17 21:18:59 +0200 |
---|---|---|
committer | TrueDoctor <d-kobert@web.de> | 2018-07-17 21:18:59 +0200 |
commit | 497a9eded3b785f7f96732c447943cc8bbad284b (patch) | |
tree | 87e40525b6d5173171597a0c8c288fd8d8e7f610 /DiscoBot/Auxiliary/Dice.cs | |
parent | 496c9c32cb8d0d067e675855289904a22b05d9ac (diff) |
Added object oriented dice resolving
Diffstat (limited to 'DiscoBot/Auxiliary/Dice.cs')
-rw-r--r-- | DiscoBot/Auxiliary/Dice.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/DiscoBot/Auxiliary/Dice.cs b/DiscoBot/Auxiliary/Dice.cs index 16a8a77..0cd9656 100644 --- a/DiscoBot/Auxiliary/Dice.cs +++ b/DiscoBot/Auxiliary/Dice.cs @@ -1,5 +1,10 @@ namespace DiscoBot.Auxiliary { + using System; + using System.Linq; + + using Discord.Commands; + public static class Dice // roll it! { private static readonly System.Random Rnd = new System.Random(); @@ -8,5 +13,38 @@ { return Rnd.Next(d) + 1; } + + public static int Roll(string input) + { + var strings = input.ToLower().Split(new[] { 'w', 'd' }, 2, StringSplitOptions.RemoveEmptyEntries).ToList(); + int count = Convert.ToInt32(strings[0]); + int d = Convert.ToInt32(strings[0]); + + if (strings.Count != 2) + { + throw new ArgumentException($"{input}: erfüllt nicht die Formatvogaben( Anzahl d Augenzahl)"); + } + + return Roll(count, d); + } + + public static int Roll(int count, int d) + { + if (d <= 0) + { + return 0; + } + + int sum = 0; + for (int i = 0; i < Math.Abs(count); i++) + { + var roll = Dice.Roll(d); + sum += roll; + } + + sum *= Math.Abs(count) / count; + + return sum; + } } } |